Skip to content

Commit cbc4a1e

Browse files
Ensure correct sleep in 'rmw_wait' (backport #344) (#346)
Signed-off-by: David Laseca Perez <davidlaseca@eprosima.com> Co-authored-by: David Laseca Perez <davidlaseca@eprosima.com>
1 parent e64d79f commit cbc4a1e

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

‎rmw_microxrcedds_c/src/rmw_wait.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ rmw_wait(
7070
if (NULL == wait_timeout || rmw_time_equal(*wait_timeout, (rmw_time_t)RMW_DURATION_INFINITE)) {
7171
timeout.i32 = UXR_TIMEOUT_INF;
7272
} else {
73-
timeout.i64 = rmw_time_total_nsec(*wait_timeout) / 1000000ULL;
73+
// Round non-zero values up to nearest millisecond to sleep at least 1 ms and
74+
// never wake up early (e.g. 0 ns -> 0 ms, 1 ns or 999999 ns -> 1 ms)
75+
timeout.i64 = (rmw_time_total_nsec(*wait_timeout) + 999999ULL) / 1000000ULL;
7476
timeout.i32 = (timeout.i64 > INT32_MAX) ? INT32_MAX : timeout.i64;
7577
}
7678

@@ -139,7 +141,7 @@ rmw_wait(
139141
// wait, guard conditions are polled between slices and the deadline is not overshot.
140142
item = session_memory.allocateditems;
141143
int64_t now = uxr_millis();
142-
while (!data_available && !guard_condition_triggered && now < deadline) {
144+
while (!data_available && !guard_condition_triggered && now <= deadline) {
143145
rmw_context_impl_t * custom_context = (rmw_context_impl_t *)item->data;
144146
const int64_t remaining = deadline - now;
145147
const int32_t slice = (remaining < RMW_UXRCE_MAX_SESSION_WAIT_SLICE_MS) ?

0 commit comments

Comments
 (0)