Skip to content

Commit 7495ea6

Browse files
author
Janosch Machowinski
committed
fix: Make passing of timer data thread safe
Signed-off-by: Janosch Machowinski <[email protected]> # Conflicts: # rclcpp/include/rclcpp/experimental/timers_manager.hpp # rclcpp/include/rclcpp/timer.hpp # rclcpp/src/rclcpp/executors/static_single_threaded_executor.cpp # rclcpp/src/rclcpp/experimental/timers_manager.cpp
1 parent 4e5544e commit 7495ea6

File tree

12 files changed

+104
-49
lines changed

12 files changed

+104
-49
lines changed

rclcpp/include/rclcpp/executor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ class Executor
478478
*/
479479
RCLCPP_PUBLIC
480480
static void
481-
execute_timer(rclcpp::TimerBase::SharedPtr timer);
481+
execute_timer(rclcpp::TimerBase::SharedPtr timer, const std::shared_ptr<void> & dataPtr);
482482

483483
/// Run service server executable.
484484
/**

rclcpp/include/rclcpp/experimental/executors/events_executor/events_executor_event_types.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#ifndef RCLCPP__EXPERIMENTAL__EXECUTORS__EVENTS_EXECUTOR__EVENTS_EXECUTOR_EVENT_TYPES_HPP_
1616
#define RCLCPP__EXPERIMENTAL__EXECUTORS__EVENTS_EXECUTOR__EVENTS_EXECUTOR_EVENT_TYPES_HPP_
1717

18+
#include <memory>
19+
1820
namespace rclcpp
1921
{
2022
namespace experimental
@@ -34,6 +36,7 @@ enum ExecutorEventType
3436
struct ExecutorEvent
3537
{
3638
const void * entity_key;
39+
std::shared_ptr<void> data;
3740
int waitable_data;
3841
ExecutorEventType type;
3942
size_t num_events;

rclcpp/include/rclcpp/experimental/timers_manager.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ class TimersManager
8686
RCLCPP_PUBLIC
8787
TimersManager(
8888
std::shared_ptr<rclcpp::Context> context,
89-
std::function<void(const rclcpp::TimerBase *)> on_ready_callback = nullptr);
89+
std::function<void(const rclcpp::TimerBase *,
90+
const std::shared_ptr<void> &)> on_ready_callback = nullptr);
9091

9192
/**
9293
* @brief Destruct the TimersManager object making sure to stop thread and release memory.
@@ -164,9 +165,10 @@ class TimersManager
164165
* the TimersManager on_ready_callback was passed during construction.
165166
*
166167
* @param timer_id the timer ID of the timer to execute
168+
* @param data internal data of the timer
167169
*/
168170
RCLCPP_PUBLIC
169-
void execute_ready_timer(const rclcpp::TimerBase * timer_id);
171+
void execute_ready_timer(const rclcpp::TimerBase * timer_id, const std::shared_ptr<void> & data);
170172

171173
/**
172174
* @brief Get the amount of time before the next timer triggers.
@@ -527,7 +529,8 @@ class TimersManager
527529
void execute_ready_timers_unsafe();
528530

529531
// Callback to be called when timer is ready
530-
std::function<void(const rclcpp::TimerBase *)> on_ready_callback_ = nullptr;
532+
std::function<void(const rclcpp::TimerBase *,
533+
const std::shared_ptr<void> &)> on_ready_callback_ = nullptr;
531534

532535
// Thread used to run the timers execution task
533536
std::thread timers_thread_;

rclcpp/include/rclcpp/strategies/allocator_memory_strategy.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ class AllocatorMemoryStrategy : public memory_strategy::MemoryStrategy
368368
++it;
369369
continue;
370370
}
371-
if (!timer->call()) {
371+
auto data = timer->call();
372+
if (!data) {
372373
// timer was cancelled, skip it.
373374
++it;
374375
continue;
@@ -377,6 +378,7 @@ class AllocatorMemoryStrategy : public memory_strategy::MemoryStrategy
377378
any_exec.timer = timer;
378379
any_exec.callback_group = group;
379380
any_exec.node_base = get_node_by_group(group, weak_groups_to_nodes);
381+
any_exec.data = data;
380382
timer_handles_.erase(it);
381383
return;
382384
}

rclcpp/include/rclcpp/timer.hpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <atomic>
1919
#include <chrono>
2020
#include <functional>
21+
#include <optional>
2122
#include <memory>
2223
#include <sstream>
2324
#include <thread>
@@ -43,6 +44,12 @@
4344
namespace rclcpp
4445
{
4546

47+
struct TimerInfo
48+
{
49+
Time expected_call_time;
50+
Time actual_call_time;
51+
};
52+
4653
class TimerBase
4754
{
4855
public:
@@ -96,16 +103,20 @@ class TimerBase
96103
* The multithreaded executor takes advantage of this to avoid scheduling
97104
* the callback multiple times.
98105
*
99-
* \return `true` if the callback should be executed, `false` if the timer was canceled.
106+
* \return a valid shared_ptr if the callback should be executed,
107+
* an invalid shared_ptr (nullptr) if the timer was canceled.
100108
*/
101109
RCLCPP_PUBLIC
102-
virtual bool
110+
virtual std::shared_ptr<void>
103111
call() = 0;
104112

105113
/// Call the callback function when the timer signal is emitted.
114+
/**
115+
* \param[in] data the pointer returned by the call function
116+
*/
106117
RCLCPP_PUBLIC
107118
virtual void
108-
execute_callback() = 0;
119+
execute_callback(const std::shared_ptr<void> & data) = 0;
109120

110121
RCLCPP_PUBLIC
111122
std::shared_ptr<const rcl_timer_t>
@@ -193,12 +204,6 @@ class TimerBase
193204
set_on_reset_callback(rcl_event_callback_t callback, const void * user_data);
194205
};
195206

196-
struct TimerInfo
197-
{
198-
Time expected_call_time;
199-
Time actual_call_time;
200-
};
201-
202207
using VoidCallbackType = std::function<void ()>;
203208
using TimerCallbackType = std::function<void (TimerBase &)>;
204209
using TimerInfoCallbackType = std::function<void (const TimerInfo &)>;
@@ -257,27 +262,28 @@ class GenericTimer : public TimerBase
257262
* \sa rclcpp::TimerBase::call
258263
* \throws std::runtime_error if it failed to notify timer that callback will occurr
259264
*/
260-
bool
265+
std::shared_ptr<void>
261266
call() override
262267
{
268+
rcl_timer_call_info_t timer_call_info_;
263269
rcl_ret_t ret = rcl_timer_call_with_info(timer_handle_.get(), &timer_call_info_);
264270
if (ret == RCL_RET_TIMER_CANCELED) {
265-
return false;
271+
return nullptr;
266272
}
267273
if (ret != RCL_RET_OK) {
268274
throw std::runtime_error("Failed to notify timer that callback occurred");
269275
}
270-
return true;
276+
return std::make_shared<rcl_timer_call_info_t>(timer_call_info_);
271277
}
272278

273279
/**
274280
* \sa rclcpp::TimerBase::execute_callback
275281
*/
276282
void
277-
execute_callback() override
283+
execute_callback(const std::shared_ptr<void> & data) override
278284
{
279285
TRACEPOINT(callback_start, reinterpret_cast<const void *>(&callback_), false);
280-
execute_callback_delegate<>();
286+
execute_callback_delegate<>(*static_cast<rcl_timer_call_info_t *>(data.get()));
281287
TRACEPOINT(callback_end, reinterpret_cast<const void *>(&callback_));
282288
}
283289

@@ -289,7 +295,7 @@ class GenericTimer : public TimerBase
289295
>::type * = nullptr
290296
>
291297
void
292-
execute_callback_delegate()
298+
execute_callback_delegate(const rcl_timer_call_info_t &)
293299
{
294300
callback_();
295301
}
@@ -301,7 +307,7 @@ class GenericTimer : public TimerBase
301307
>::type * = nullptr
302308
>
303309
void
304-
execute_callback_delegate()
310+
execute_callback_delegate(const rcl_timer_call_info_t &)
305311
{
306312
callback_(*this);
307313
}
@@ -314,7 +320,7 @@ class GenericTimer : public TimerBase
314320
>::type * = nullptr
315321
>
316322
void
317-
execute_callback_delegate()
323+
execute_callback_delegate(const rcl_timer_call_info_t & timer_call_info_)
318324
{
319325
const TimerInfo info{Time{timer_call_info_.expected_call_time, clock_->get_clock_type()},
320326
Time{timer_call_info_.actual_call_time, clock_->get_clock_type()}};
@@ -333,14 +339,14 @@ class GenericTimer : public TimerBase
333339
RCLCPP_DISABLE_COPY(GenericTimer)
334340

335341
FunctorT callback_;
336-
rcl_timer_call_info_t timer_call_info_;
337342
};
338343

339344
template<
340345
typename FunctorT,
341346
typename std::enable_if<
342347
rclcpp::function_traits::same_arguments<FunctorT, VoidCallbackType>::value ||
343-
rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value
348+
rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value ||
349+
rclcpp::function_traits::same_arguments<FunctorT, TimerInfoCallbackType>::value
344350
>::type * = nullptr
345351
>
346352
class WallTimer : public GenericTimer<FunctorT>

rclcpp/src/rclcpp/executor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ Executor::execute_any_executable(AnyExecutable & any_exec)
528528
TRACEPOINT(
529529
rclcpp_executor_execute,
530530
static_cast<const void *>(any_exec.timer->get_timer_handle().get()));
531-
execute_timer(any_exec.timer);
531+
execute_timer(any_exec.timer, any_exec.data);
532532
}
533533
if (any_exec.subscription) {
534534
TRACEPOINT(
@@ -698,9 +698,9 @@ Executor::execute_subscription(rclcpp::SubscriptionBase::SharedPtr subscription)
698698
}
699699

700700
void
701-
Executor::execute_timer(rclcpp::TimerBase::SharedPtr timer)
701+
Executor::execute_timer(rclcpp::TimerBase::SharedPtr timer, const std::shared_ptr<void> & dataPtr)
702702
{
703-
timer->execute_callback();
703+
timer->execute_callback(dataPtr);
704704
}
705705

706706
void

rclcpp/src/rclcpp/executors/static_single_threaded_executor.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,13 @@ StaticSingleThreadedExecutor::execute_ready_executables(bool spin_once)
231231
if (i < entities_collector_->get_number_of_timers()) {
232232
if (wait_set_.timers[i] && entities_collector_->get_timer(i)->is_ready()) {
233233
auto timer = entities_collector_->get_timer(i);
234-
timer->call();
235-
execute_timer(std::move(timer));
234+
auto data = timer->call();
235+
if (!data) {
236+
// someone canceled the timer between is_ready and call
237+
continue;
238+
}
239+
240+
execute_timer(std::move(timer), data);
236241
if (spin_once) {
237242
return true;
238243
}

rclcpp/src/rclcpp/experimental/executors/events_executor/events_executor.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ EventsExecutor::EventsExecutor(
4040
// The timers manager can be used either to only track timers (in this case an expired
4141
// timer will generate an executor event and then it will be executed by the executor thread)
4242
// or it can also take care of executing expired timers in its dedicated thread.
43-
std::function<void(const rclcpp::TimerBase *)> timer_on_ready_cb = nullptr;
43+
std::function<void(const rclcpp::TimerBase *,
44+
const std::shared_ptr<void> &)> timer_on_ready_cb = nullptr;
4445
if (!execute_timers_separate_thread) {
45-
timer_on_ready_cb = [this](const rclcpp::TimerBase * timer_id) {
46-
ExecutorEvent event = {timer_id, -1, ExecutorEventType::TIMER_EVENT, 1};
46+
timer_on_ready_cb =
47+
[this](const rclcpp::TimerBase * timer_id, const std::shared_ptr<void> & data) {
48+
ExecutorEvent event = {timer_id, data, -1, ExecutorEventType::TIMER_EVENT, 1};
4749
this->events_queue_->enqueue(event);
4850
};
4951
}
@@ -88,7 +90,7 @@ EventsExecutor::EventsExecutor(
8890
}
8991

9092
ExecutorEvent event =
91-
{notify_waitable_entity_id, waitable_data, ExecutorEventType::WAITABLE_EVENT, 1};
93+
{notify_waitable_entity_id, nullptr, waitable_data, ExecutorEventType::WAITABLE_EVENT, 1};
9294
this->events_queue_->enqueue(event);
9395
});
9496

@@ -314,7 +316,7 @@ EventsExecutor::execute_event(const ExecutorEvent & event)
314316
case ExecutorEventType::TIMER_EVENT:
315317
{
316318
timers_manager_->execute_ready_timer(
317-
static_cast<const rclcpp::TimerBase *>(event.entity_key));
319+
static_cast<const rclcpp::TimerBase *>(event.entity_key), event.data);
318320
break;
319321
}
320322
case ExecutorEventType::WAITABLE_EVENT:
@@ -463,7 +465,7 @@ EventsExecutor::create_entity_callback(
463465
{
464466
std::function<void(size_t)>
465467
callback = [this, entity_key, event_type](size_t num_events) {
466-
ExecutorEvent event = {entity_key, -1, event_type, num_events};
468+
ExecutorEvent event = {entity_key, nullptr, -1, event_type, num_events};
467469
this->events_queue_->enqueue(event);
468470
};
469471
return callback;
@@ -475,7 +477,7 @@ EventsExecutor::create_waitable_callback(const rclcpp::Waitable * entity_key)
475477
std::function<void(size_t, int)>
476478
callback = [this, entity_key](size_t num_events, int waitable_data) {
477479
ExecutorEvent event =
478-
{entity_key, waitable_data, ExecutorEventType::WAITABLE_EVENT, num_events};
480+
{entity_key, nullptr, waitable_data, ExecutorEventType::WAITABLE_EVENT, num_events};
479481
this->events_queue_->enqueue(event);
480482
};
481483
return callback;

rclcpp/src/rclcpp/experimental/timers_manager.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ using rclcpp::experimental::TimersManager;
2727

2828
TimersManager::TimersManager(
2929
std::shared_ptr<rclcpp::Context> context,
30-
std::function<void(const rclcpp::TimerBase *)> on_ready_callback)
30+
std::function<void(const rclcpp::TimerBase *, const std::shared_ptr<void> &)> on_ready_callback)
31+
: on_ready_callback_(on_ready_callback),
32+
context_(context)
3133
{
32-
context_ = context;
33-
on_ready_callback_ = on_ready_callback;
3434
}
3535

3636
TimersManager::~TimersManager()
@@ -148,24 +148,30 @@ bool TimersManager::execute_head_timer()
148148
if (timer_ready) {
149149
// NOTE: here we always execute the timer, regardless of whether the
150150
// on_ready_callback is set or not.
151-
head_timer->call();
152-
head_timer->execute_callback();
151+
auto data = head_timer->call();
152+
if (!data) {
153+
// someone canceled the timer between is_ready and call
154+
return false;
155+
}
156+
head_timer->execute_callback(data);
153157
timers_heap.heapify_root();
154158
weak_timers_heap_.store(timers_heap);
155159
}
156160

157161
return timer_ready;
158162
}
159163

160-
void TimersManager::execute_ready_timer(const rclcpp::TimerBase * timer_id)
164+
void TimersManager::execute_ready_timer(
165+
const rclcpp::TimerBase * timer_id,
166+
const std::shared_ptr<void> & data)
161167
{
162168
TimerPtr ready_timer;
163169
{
164170
std::unique_lock<std::mutex> lock(timers_mutex_);
165171
ready_timer = weak_timers_heap_.get_timer(timer_id);
166172
}
167173
if (ready_timer) {
168-
ready_timer->execute_callback();
174+
ready_timer->execute_callback(data);
169175
}
170176
}
171177

@@ -213,11 +219,16 @@ void TimersManager::execute_ready_timers_unsafe()
213219
const size_t number_ready_timers = locked_heap.get_number_ready_timers();
214220
size_t executed_timers = 0;
215221
while (executed_timers < number_ready_timers && head_timer->is_ready()) {
216-
head_timer->call();
217-
if (on_ready_callback_) {
218-
on_ready_callback_(head_timer.get());
222+
auto data = head_timer->call();
223+
if (data) {
224+
if (on_ready_callback_) {
225+
on_ready_callback_(head_timer.get(), data);
226+
} else {
227+
head_timer->execute_callback(data);
228+
}
219229
} else {
220-
head_timer->execute_callback();
230+
// someone canceled the timer between is_ready and call
231+
// we don't do anything, as the timer is now 'processed'
221232
}
222233

223234
executed_timers++;

rclcpp/test/rclcpp/executors/test_events_queue.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ TEST(TestEventsQueue, SimpleQueueTest)
6868
// Lets push an event into the queue and get it back
6969
rclcpp::experimental::executors::ExecutorEvent push_event = {
7070
simple_queue.get(),
71+
nullptr,
7172
99,
7273
rclcpp::experimental::executors::ExecutorEventType::SUBSCRIPTION_EVENT,
7374
1};

0 commit comments

Comments
 (0)