fix: non-blocking grain timing — use mxlGetNsUntilIndex instead of mxlSleepUntil
mxlSleepUntil blocks the entire thread including LWS poll, causing the control server to become unresponsive and grains to be missed. Instead, check mxlGetNsUntilIndex and skip process() if the grain isn't due yet (>2ms away). The node loop spins with poll(1) keeping LWS responsive, and only attempts a grain read when it's nearly due.
This commit is contained in:
@@ -28,7 +28,6 @@ void PassthroughNode::on_add_reader(const std::string& port_id, mxlFlowReader re
|
||||
auto now = mxlGetTime();
|
||||
auto current_index = mxlTimestampToIndex(&grain_rate_, now);
|
||||
read_index_ = current_index - READ_DELAY_GRAINS;
|
||||
delivery_deadline_ = mxlIndexToTimestamp(&grain_rate_, read_index_ + 1);
|
||||
aligned_ = true;
|
||||
|
||||
spdlog::info("Passthrough: reader added, grain_rate={}/{}, read_index={}, delay={} grains",
|
||||
@@ -55,12 +54,9 @@ void PassthroughNode::process() {
|
||||
return;
|
||||
}
|
||||
|
||||
auto now = mxlGetTime();
|
||||
if (delivery_deadline_ > now) {
|
||||
auto sleep_ns = delivery_deadline_ - now;
|
||||
if (sleep_ns < 500000000ULL) {
|
||||
mxlSleepUntil(delivery_deadline_);
|
||||
}
|
||||
auto ns_until = mxlGetNsUntilIndex(read_index_, &grain_rate_);
|
||||
if (ns_until > 2000000ULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
mxlGrainInfo grain_info{};
|
||||
@@ -70,10 +66,9 @@ void PassthroughNode::process() {
|
||||
if (status != MXL_STATUS_OK) {
|
||||
if (status == MXL_ERR_OUT_OF_RANGE_TOO_LATE || status == MXL_ERR_OUT_OF_RANGE_TOO_EARLY) {
|
||||
auto old_index = read_index_;
|
||||
now = mxlGetTime();
|
||||
auto now = mxlGetTime();
|
||||
auto current_index = mxlTimestampToIndex(&grain_rate_, now);
|
||||
read_index_ = current_index - READ_DELAY_GRAINS;
|
||||
delivery_deadline_ = mxlIndexToTimestamp(&grain_rate_, read_index_ + 1);
|
||||
spdlog::warn("Passthrough: index {} out of range ({}), realigned to {}",
|
||||
old_index, static_cast<int>(status), read_index_);
|
||||
}
|
||||
@@ -87,7 +82,6 @@ void PassthroughNode::process() {
|
||||
spdlog::warn("Passthrough: failed to open output grain at index {}: {}",
|
||||
grain_info.index, static_cast<int>(status));
|
||||
read_index_++;
|
||||
delivery_deadline_ = mxlIndexToTimestamp(&grain_rate_, read_index_ + 1);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -99,7 +93,6 @@ void PassthroughNode::process() {
|
||||
mxlFlowWriterCommitGrain(*writer_, &out_grain);
|
||||
|
||||
read_index_++;
|
||||
delivery_deadline_ = mxlIndexToTimestamp(&grain_rate_, read_index_ + 1);
|
||||
grains_processed_++;
|
||||
|
||||
if (grains_processed_ <= 5 || grains_processed_ % 50 == 0) {
|
||||
|
||||
@@ -39,7 +39,6 @@ private:
|
||||
std::optional<mxlFlowWriter> writer_;
|
||||
mxlRational grain_rate_{50, 1};
|
||||
uint64_t read_index_ = 0;
|
||||
uint64_t delivery_deadline_ = 0;
|
||||
uint64_t grains_processed_ = 0;
|
||||
bool aligned_ = false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user