Skip to content

Commit

Permalink
Add TimersManager::is_running() / Exit spin() before destroying stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Passerino committed Aug 18, 2023
1 parent 4131227 commit 8305c04
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rclcpp/include/rclcpp/executors/events_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class EventsExecutor : public rclcpp::Executor

/// Default destrcutor.
RCLCPP_PUBLIC
virtual ~EventsExecutor() = default;
virtual ~EventsExecutor();

/// Events executor implementation of spin.
/**
Expand Down
6 changes: 6 additions & 0 deletions rclcpp/include/rclcpp/executors/timers_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ class TimersManager
*/
std::chrono::nanoseconds get_head_timeout();

/**
* @brief Function to check if the timers thread is running
* @return true if timers thread is running
*/
bool is_running();

private:
RCLCPP_DISABLE_COPY(TimersManager)

Expand Down
20 changes: 20 additions & 0 deletions rclcpp/src/rclcpp/executors/events_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ EventsExecutor::EventsExecutor(
entities_collector_->add_waitable(executor_notifier_);
}

EventsExecutor::~EventsExecutor()
{
// Set 'spinning' to false and get previous value
auto executor_was_spinning = spinning.exchange(false);

if (executor_was_spinning) {
// Trigger the shutdown guard condition.
// This way, the 'events_queue_' will wake up since a new event has arrived.
// Then since 'spinning' is now false, the spin loop will exit.
shutdown_guard_condition_->trigger();

// The timers manager thread is stopped at the end of spin().
// We have to wait for timers manager thread to exit, otherwise
// the 'timers_manager_' will be destroyed while still being used on spin().
while (timers_manager_->is_running()) {
std::this_thread::sleep_for(1ms);
}
}
}

void
EventsExecutor::spin()
{
Expand Down
5 changes: 5 additions & 0 deletions rclcpp/src/rclcpp/executors/timers_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,8 @@ void TimersManager::remove_timer(TimerPtr timer)

timer->clear_on_reset_callback();
}

bool TimersManager::is_running()
{
return running_;
}

0 comments on commit 8305c04

Please sign in to comment.