@@ -144,6 +144,7 @@ std::vector<rclcpp::CallbackGroup::WeakPtr>
144144Executor::get_all_callback_groups ()
145145{
146146 std::vector<rclcpp::CallbackGroup::WeakPtr> groups;
147+ std::lock_guard<std::mutex> guard{mutex_};
147148 for (const auto & group_node_ptr : weak_groups_associated_with_executor_to_nodes_) {
148149 groups.push_back (group_node_ptr.first );
149150 }
@@ -157,6 +158,7 @@ std::vector<rclcpp::CallbackGroup::WeakPtr>
157158Executor::get_manually_added_callback_groups ()
158159{
159160 std::vector<rclcpp::CallbackGroup::WeakPtr> groups;
161+ std::lock_guard<std::mutex> guard{mutex_};
160162 for (auto const & group_node_ptr : weak_groups_associated_with_executor_to_nodes_) {
161163 groups.push_back (group_node_ptr.first );
162164 }
@@ -167,6 +169,7 @@ std::vector<rclcpp::CallbackGroup::WeakPtr>
167169Executor::get_automatically_added_callback_groups_from_nodes ()
168170{
169171 std::vector<rclcpp::CallbackGroup::WeakPtr> groups;
172+ std::lock_guard<std::mutex> guard{mutex_};
170173 for (auto const & group_node_ptr : weak_groups_to_nodes_associated_with_executor_) {
171174 groups.push_back (group_node_ptr.first );
172175 }
@@ -233,7 +236,6 @@ Executor::add_callback_group_to_map(
233236 }
234237 }
235238 // Add the node's notify condition to the guard condition handles
236- std::unique_lock<std::mutex> lock (memory_strategy_mutex_);
237239 memory_strategy_->add_guard_condition (node_ptr->get_notify_guard_condition ());
238240 }
239241}
@@ -244,6 +246,7 @@ Executor::add_callback_group(
244246 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
245247 bool notify)
246248{
249+ std::lock_guard<std::mutex> guard{mutex_};
247250 this ->add_callback_group_to_map (
248251 group_ptr,
249252 node_ptr,
@@ -259,6 +262,7 @@ Executor::add_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_pt
259262 if (has_executor.exchange (true )) {
260263 throw std::runtime_error (" Node has already been added to an executor." );
261264 }
265+ std::lock_guard<std::mutex> guard{mutex_};
262266 for (auto & weak_group : node_ptr->get_callback_groups ()) {
263267 auto group_ptr = weak_group.lock ();
264268 if (group_ptr != nullptr && !group_ptr->get_associated_with_executor_atomic ().load () &&
@@ -307,7 +311,6 @@ Executor::remove_callback_group_from_map(
307311 throw_from_rcl_error (ret, " Failed to trigger guard condition on callback group remove" );
308312 }
309313 }
310- std::unique_lock<std::mutex> lock (memory_strategy_mutex_);
311314 memory_strategy_->remove_guard_condition (node_ptr->get_notify_guard_condition ());
312315 }
313316}
@@ -317,6 +320,7 @@ Executor::remove_callback_group(
317320 rclcpp::CallbackGroup::SharedPtr group_ptr,
318321 bool notify)
319322{
323+ std::lock_guard<std::mutex> guard{mutex_};
320324 this ->remove_callback_group_from_map (
321325 group_ptr,
322326 weak_groups_associated_with_executor_to_nodes_,
@@ -336,6 +340,7 @@ Executor::remove_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node
336340 throw std::runtime_error (" Node needs to be associated with an executor." );
337341 }
338342
343+ std::lock_guard<std::mutex> guard{mutex_};
339344 bool found_node = false ;
340345 auto node_it = weak_nodes_.begin ();
341346 while (node_it != weak_nodes_.end ()) {
@@ -489,6 +494,7 @@ Executor::set_memory_strategy(rclcpp::memory_strategy::MemoryStrategy::SharedPtr
489494 if (memory_strategy == nullptr ) {
490495 throw std::runtime_error (" Received NULL memory strategy in executor." );
491496 }
497+ std::lock_guard<std::mutex> guard{mutex_};
492498 memory_strategy_ = memory_strategy;
493499}
494500
662668Executor::wait_for_work (std::chrono::nanoseconds timeout)
663669{
664670 {
665- std::unique_lock <std::mutex> lock (memory_strategy_mutex_ );
671+ std::lock_guard <std::mutex> guard (mutex_ );
666672
667673 // Check weak_nodes_ to find any callback group that is not owned
668674 // by an executor and add it to the list of callbackgroups for
@@ -741,12 +747,14 @@ Executor::wait_for_work(std::chrono::nanoseconds timeout)
741747
742748 // check the null handles in the wait set and remove them from the handles in memory strategy
743749 // for callback-based entities
750+ std::lock_guard<std::mutex> guard (mutex_);
744751 memory_strategy_->remove_null_handles (&wait_set_);
745752}
746753
747754rclcpp::node_interfaces::NodeBaseInterface::SharedPtr
748755Executor::get_node_by_group (
749- rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap weak_groups_to_nodes,
756+ const rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap &
757+ weak_groups_to_nodes,
750758 rclcpp::CallbackGroup::SharedPtr group)
751759{
752760 if (!group) {
@@ -764,6 +772,7 @@ Executor::get_node_by_group(
764772rclcpp::CallbackGroup::SharedPtr
765773Executor::get_group_by_timer (rclcpp::TimerBase::SharedPtr timer)
766774{
775+ std::lock_guard<std::mutex> guard{mutex_};
767776 for (const auto & pair : weak_groups_associated_with_executor_to_nodes_) {
768777 auto group = pair.first .lock ();
769778 if (!group) {
@@ -804,9 +813,11 @@ Executor::get_next_ready_executable(AnyExecutable & any_executable)
804813bool
805814Executor::get_next_ready_executable_from_map (
806815 AnyExecutable & any_executable,
807- rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap weak_groups_to_nodes)
816+ const rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap &
817+ weak_groups_to_nodes)
808818{
809819 bool success = false ;
820+ std::lock_guard<std::mutex> guard{mutex_};
810821 // Check the timers to see if there are any that are ready
811822 memory_strategy_->get_next_timer (any_executable, weak_groups_to_nodes);
812823 if (any_executable.timer ) {
@@ -893,7 +904,8 @@ Executor::get_next_executable(AnyExecutable & any_executable, std::chrono::nanos
893904bool
894905Executor::has_node (
895906 const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
896- rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap weak_groups_to_nodes) const
907+ const rclcpp::memory_strategy::MemoryStrategy::WeakCallbackGroupsToNodesMap &
908+ weak_groups_to_nodes) const
897909{
898910 return std::find_if (
899911 weak_groups_to_nodes.begin (),
0 commit comments