Skip to content

Commit

Permalink
If timer canceled, rcl_timer_get_time_until_next_call returns TIMER_C…
Browse files Browse the repository at this point in the history
…ANCELED (ros2#963)

* Return TIMER_CANCELED rcl_timer_get_time_until_next_call

Signed-off-by: Mauro Passerino <mpasserino@irobot.com>
  • Loading branch information
mauropasse committed Mar 11, 2022
1 parent 5433946 commit 8d2baa6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions rcl/include/rcl/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ rcl_timer_is_ready(const rcl_timer_t * timer, bool * is_ready);
* \return #RCL_RET_OK if the timer until next call was successfully calculated, or
* \return #RCL_RET_INVALID_ARGUMENT if any arguments are invalid, or
* \return #RCL_RET_TIMER_INVALID if the timer is invalid, or
* \return #RCL_RET_TIMER_CANCELED if the timer is canceled, or
* \return #RCL_RET_ERROR an unspecified error occur.
*/
RCL_PUBLIC
Expand Down
10 changes: 8 additions & 2 deletions rcl/src/rcl/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,13 @@ rcl_timer_is_ready(const rcl_timer_t * timer, bool * is_ready)
RCL_CHECK_ARGUMENT_FOR_NULL(is_ready, RCL_RET_INVALID_ARGUMENT);
int64_t time_until_next_call;
rcl_ret_t ret = rcl_timer_get_time_until_next_call(timer, &time_until_next_call);
if (ret != RCL_RET_OK) {
if (ret == RCL_RET_TIMER_CANCELED) {
*is_ready = false;
return RCL_RET_OK;
} else if (ret != RCL_RET_OK) {
return ret; // rcl error state should already be set.
}
*is_ready = (time_until_next_call <= 0) && !rcutils_atomic_load_bool(&timer->impl->canceled);
*is_ready = (time_until_next_call <= 0);
return RCL_RET_OK;
}

Expand All @@ -309,6 +312,9 @@ rcl_timer_get_time_until_next_call(const rcl_timer_t * timer, int64_t * time_unt
{
RCL_CHECK_ARGUMENT_FOR_NULL(timer, RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ARGUMENT_FOR_NULL(time_until_next_call, RCL_RET_INVALID_ARGUMENT);
if (rcutils_atomic_load_bool(&timer->impl->canceled)) {
return RCL_RET_TIMER_CANCELED;
}
rcl_time_point_value_t now;
rcl_ret_t ret = rcl_clock_get_now(timer->impl->clock, &now);
if (ret != RCL_RET_OK) {
Expand Down
4 changes: 4 additions & 0 deletions rcl/test/rcl/test_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@ TEST_F(TestTimerFixture, test_canceled_timer) {
ret = rcl_timer_cancel(&timer);
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;

int64_t time_until_next_call = 0;
ret = rcl_timer_get_time_until_next_call(&timer, &time_until_next_call);
EXPECT_EQ(RCL_RET_TIMER_CANCELED, ret) << rcl_get_error_string().str;

rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set();
ret = rcl_wait_set_init(&wait_set, 0, 0, 1, 0, 0, 0, context_ptr, rcl_get_default_allocator());
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
Expand Down

0 comments on commit 8d2baa6

Please sign in to comment.