Skip to content

Commit ba7f439

Browse files
Mauro Passerinojefferyyjhsu
authored andcommitted
Add support for timers on reset callback
Signed-off-by: Mauro Passerino <[email protected]>
1 parent b562f49 commit ba7f439

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

irobot_events_executor/include/rclcpp/timers_manager.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,13 @@ class TimersManager
430430
std::make_heap(owned_heap_.begin(), owned_heap_.end(), timer_greater);
431431
}
432432

433+
void clear_callbacks()
434+
{
435+
for (TimerPtr & t : owned_heap_) {
436+
t->clear_on_reset_callback();
437+
}
438+
}
439+
433440
/**
434441
* @brief Friend declaration to allow the `validate_and_lock()` function to access the
435442
* underlying heap container

irobot_events_executor/src/rclcpp/timers_manager.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ void TimersManager::add_timer(rclcpp::TimerBase::SharedPtr timer)
4141
timers_updated_ = timers_updated_ || added;
4242
}
4343

44+
timer->set_on_reset_callback([this](){
45+
{
46+
std::unique_lock<std::mutex> lock(timers_mutex_);
47+
timers_updated_ = true;
48+
}
49+
timers_cv_.notify_one();
50+
});
51+
4452
if (added) {
4553
// Notify that a timer has been added
4654
timers_cv_.notify_one();
@@ -265,6 +273,10 @@ void TimersManager::clear()
265273
{
266274
// Lock mutex and then clear all data structures
267275
rclcpp::Lock lock(timers_mutex_);
276+
277+
TimersHeap locked_heap = weak_timers_heap_.validate_and_lock();
278+
locked_heap.clear_callbacks();
279+
268280
weak_timers_heap_.clear();
269281

270282
timers_updated_ = true;
@@ -288,4 +300,5 @@ void TimersManager::remove_timer(TimerPtr timer)
288300
// Notify timers thread such that it can re-compute its timeout
289301
timers_cv_.notify_one();
290302
}
303+
timer->clear_on_reset_callback();
291304
}

0 commit comments

Comments
 (0)