-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
initialize timer with clock #272
Changes from 1 commit
252bdaf
e8f2e34
0c97207
28e6ba1
e1a400e
de691c4
83d96cb
433d0c0
9e7737c
0eda193
1c73377
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,8 @@ extern "C" | |
|
||
typedef struct rcl_timer_impl_t | ||
{ | ||
// The clock providing time. | ||
rcl_clock_t * clock; | ||
// The user supplied callback. | ||
atomic_uintptr_t callback; | ||
// This is a duration in nanoseconds. | ||
|
@@ -50,26 +52,29 @@ rcl_get_zero_initialized_timer() | |
rcl_ret_t | ||
rcl_timer_init( | ||
rcl_timer_t * timer, | ||
rcl_clock_t * clock, | ||
int64_t period, | ||
const rcl_timer_callback_t callback, | ||
rcl_allocator_t allocator) | ||
{ | ||
RCL_CHECK_ALLOCATOR_WITH_MSG(&allocator, "invalid allocator", return RCL_RET_INVALID_ARGUMENT); | ||
RCL_CHECK_ARGUMENT_FOR_NULL(timer, RCL_RET_INVALID_ARGUMENT, allocator); | ||
RCL_CHECK_ARGUMENT_FOR_NULL(clock, RCL_RET_INVALID_ARGUMENT, allocator); | ||
RCUTILS_LOG_DEBUG_NAMED(ROS_PACKAGE_NAME, "Initializing timer with period: %" PRIu64 "ns", period) | ||
if (timer->impl) { | ||
RCL_SET_ERROR_MSG("timer already initailized, or memory was uninitialized", allocator); | ||
return RCL_RET_ALREADY_INIT; | ||
} | ||
rcl_time_point_value_t now_steady; | ||
rcl_ret_t now_ret = rcutils_steady_time_now(&now_steady); | ||
rcl_time_point_value_t now; | ||
rcl_ret_t now_ret = clock->get_now(clock->data, &now); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The commit e8f2e34 uses your proposed function. It requires to get a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I raised the concern about the existing API in #273 in order to keep it separate from this patch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (now_ret != RCL_RET_OK) { | ||
return now_ret; // rcl error state should already be set. | ||
} | ||
rcl_timer_impl_t impl; | ||
impl.clock = clock; | ||
atomic_init(&impl.callback, (uintptr_t)callback); | ||
atomic_init(&impl.period, period); | ||
atomic_init(&impl.last_call_time, now_steady); | ||
atomic_init(&impl.last_call_time, now); | ||
atomic_init(&impl.canceled, false); | ||
impl.allocator = allocator; | ||
timer->impl = (rcl_timer_impl_t *)allocator.allocate(sizeof(rcl_timer_impl_t), allocator.state); | ||
|
@@ -105,18 +110,18 @@ rcl_timer_call(rcl_timer_t * timer) | |
RCL_SET_ERROR_MSG("timer is canceled", *allocator); | ||
return RCL_RET_TIMER_CANCELED; | ||
} | ||
rcl_time_point_value_t now_steady; | ||
rcl_ret_t now_ret = rcutils_steady_time_now(&now_steady); | ||
rcl_time_point_value_t now; | ||
rcl_ret_t now_ret = timer->impl->clock->get_now(timer->impl->clock->data, &now); | ||
if (now_ret != RCL_RET_OK) { | ||
return now_ret; // rcl error state should already be set. | ||
} | ||
rcl_time_point_value_t previous_ns = | ||
rcl_atomic_exchange_uint64_t(&timer->impl->last_call_time, now_steady); | ||
rcl_atomic_exchange_uint64_t(&timer->impl->last_call_time, now); | ||
rcl_timer_callback_t typed_callback = | ||
(rcl_timer_callback_t)rcl_atomic_load_uintptr_t(&timer->impl->callback); | ||
|
||
if (typed_callback != NULL) { | ||
int64_t since_last_call = now_steady - previous_ns; | ||
int64_t since_last_call = now - previous_ns; | ||
typed_callback(timer, since_last_call); | ||
} | ||
return RCL_RET_OK; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,13 +50,19 @@ class TestTimerFixture : public ::testing::Test | |
|
||
TEST_F(TestTimerFixture, test_two_timers) { | ||
rcl_ret_t ret; | ||
|
||
rcl_clock_t clock; | ||
rcl_allocator_t allocator = rcl_get_default_allocator(); | ||
ret = rcl_clock_init(RCL_STEADY_TIME, &clock, &allocator); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommend finishing tests with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely - done in 0c97207. |
||
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
rcl_timer_t timer = rcl_get_zero_initialized_timer(); | ||
rcl_timer_t timer2 = rcl_get_zero_initialized_timer(); | ||
|
||
ret = rcl_timer_init(&timer, RCL_MS_TO_NS(5), nullptr, rcl_get_default_allocator()); | ||
ret = rcl_timer_init(&timer, &clock, RCL_MS_TO_NS(5), nullptr, rcl_get_default_allocator()); | ||
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
ret = rcl_timer_init(&timer2, RCL_MS_TO_NS(20), nullptr, rcl_get_default_allocator()); | ||
ret = rcl_timer_init(&timer2, &clock, RCL_MS_TO_NS(20), nullptr, rcl_get_default_allocator()); | ||
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); | ||
|
@@ -95,13 +101,19 @@ TEST_F(TestTimerFixture, test_two_timers) { | |
|
||
TEST_F(TestTimerFixture, test_two_timers_ready_before_timeout) { | ||
rcl_ret_t ret; | ||
|
||
rcl_clock_t clock; | ||
rcl_allocator_t allocator = rcl_get_default_allocator(); | ||
ret = rcl_clock_init(RCL_STEADY_TIME, &clock, &allocator); | ||
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
rcl_timer_t timer = rcl_get_zero_initialized_timer(); | ||
rcl_timer_t timer2 = rcl_get_zero_initialized_timer(); | ||
|
||
ret = rcl_timer_init(&timer, RCL_MS_TO_NS(5), nullptr, rcl_get_default_allocator()); | ||
ret = rcl_timer_init(&timer, &clock, RCL_MS_TO_NS(5), nullptr, rcl_get_default_allocator()); | ||
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
ret = rcl_timer_init(&timer2, RCL_MS_TO_NS(10), nullptr, rcl_get_default_allocator()); | ||
ret = rcl_timer_init(&timer2, &clock, RCL_MS_TO_NS(10), nullptr, rcl_get_default_allocator()); | ||
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); | ||
|
@@ -140,9 +152,15 @@ TEST_F(TestTimerFixture, test_two_timers_ready_before_timeout) { | |
|
||
TEST_F(TestTimerFixture, test_timer_not_ready) { | ||
rcl_ret_t ret; | ||
|
||
rcl_clock_t clock; | ||
rcl_allocator_t allocator = rcl_get_default_allocator(); | ||
ret = rcl_clock_init(RCL_STEADY_TIME, &clock, &allocator); | ||
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
rcl_timer_t timer = rcl_get_zero_initialized_timer(); | ||
|
||
ret = rcl_timer_init(&timer, RCL_MS_TO_NS(5), nullptr, rcl_get_default_allocator()); | ||
ret = rcl_timer_init(&timer, &clock, RCL_MS_TO_NS(5), nullptr, rcl_get_default_allocator()); | ||
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); | ||
|
@@ -175,9 +193,15 @@ TEST_F(TestTimerFixture, test_timer_not_ready) { | |
|
||
TEST_F(TestTimerFixture, test_canceled_timer) { | ||
rcl_ret_t ret; | ||
|
||
rcl_clock_t clock; | ||
rcl_allocator_t allocator = rcl_get_default_allocator(); | ||
ret = rcl_clock_init(RCL_STEADY_TIME, &clock, &allocator); | ||
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
rcl_timer_t timer = rcl_get_zero_initialized_timer(); | ||
|
||
ret = rcl_timer_init(&timer, 500, nullptr, rcl_get_default_allocator()); | ||
ret = rcl_timer_init(&timer, &clock, 500, nullptr, rcl_get_default_allocator()); | ||
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
ret = rcl_timer_cancel(&timer); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,8 +103,13 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), negative_timeout) { | |
ret = rcl_wait_set_add_guard_condition(&wait_set, &guard_cond); | ||
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
rcl_clock_t clock; | ||
rcl_allocator_t allocator = rcl_get_default_allocator(); | ||
ret = rcl_clock_init(RCL_STEADY_TIME, &clock, &allocator); | ||
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
rcl_timer_t timer = rcl_get_zero_initialized_timer(); | ||
ret = rcl_timer_init(&timer, RCL_MS_TO_NS(10), nullptr, rcl_get_default_allocator()); | ||
ret = rcl_timer_init(&timer, &clock, RCL_MS_TO_NS(10), nullptr, rcl_get_default_allocator()); | ||
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
ret = rcl_wait_set_add_timer(&wait_set, &timer); | ||
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
@@ -205,8 +210,14 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), canceled_timer) { | |
ret = rcl_wait_set_add_guard_condition(&wait_set, &guard_cond); | ||
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
rcl_clock_t clock; | ||
rcl_allocator_t allocator = rcl_get_default_allocator(); | ||
ret = rcl_clock_init(RCL_STEADY_TIME, &clock, &allocator); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in 1c73377. |
||
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
||
rcl_timer_t canceled_timer = rcl_get_zero_initialized_timer(); | ||
ret = rcl_timer_init(&canceled_timer, RCL_MS_TO_NS(1), nullptr, rcl_get_default_allocator()); | ||
ret = rcl_timer_init( | ||
&canceled_timer, &clock, RCL_MS_TO_NS(1), nullptr, rcl_get_default_allocator()); | ||
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
ret = rcl_timer_cancel(&canceled_timer); | ||
EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string_safe(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docblock for this function needs to be updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely, packages on-top of
rclcpp
also haven't been updated yet. Will put it back in review when that is the case and the docblocks are updated too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the docblock in e1a400e.