ROS 2 rclcpp + rcl - jazzy  jazzy
ROS 2 C++ Client Library with ROS Client Library
executor_entities_collector.hpp
1 // Copyright 2023 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__EXECUTORS__EXECUTOR_ENTITIES_COLLECTOR_HPP_
16 #define RCLCPP__EXECUTORS__EXECUTOR_ENTITIES_COLLECTOR_HPP_
17 
18 #include <map>
19 #include <memory>
20 #include <mutex>
21 #include <set>
22 #include <vector>
23 
24 #include "rcpputils/thread_safety_annotations.hpp"
25 
26 #include <rclcpp/any_executable.hpp>
27 #include <rclcpp/node_interfaces/node_base.hpp>
28 #include <rclcpp/callback_group.hpp>
29 #include <rclcpp/executors/executor_notify_waitable.hpp>
30 #include <rclcpp/visibility_control.hpp>
31 #include <rclcpp/wait_set.hpp>
32 #include <rclcpp/wait_result.hpp>
33 
34 namespace rclcpp
35 {
36 namespace executors
37 {
38 
40 
57 {
58 public:
60 
64  RCLCPP_PUBLIC
66  std::shared_ptr<ExecutorNotifyWaitable> notify_waitable);
67 
69  RCLCPP_PUBLIC
71 
73 
76  bool has_pending() const;
77 
79 
83  RCLCPP_PUBLIC
84  void
85  add_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
86 
88 
93  RCLCPP_PUBLIC
94  void
95  remove_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);
96 
98 
102  RCLCPP_PUBLIC
103  void
104  add_callback_group(rclcpp::CallbackGroup::SharedPtr group_ptr);
105 
107 
112  RCLCPP_PUBLIC
113  void
114  remove_callback_group(rclcpp::CallbackGroup::SharedPtr group_ptr);
115 
117 
121  RCLCPP_PUBLIC
122  std::vector<rclcpp::CallbackGroup::WeakPtr>
123  get_all_callback_groups() const;
124 
126 
130  RCLCPP_PUBLIC
131  std::vector<rclcpp::CallbackGroup::WeakPtr>
133 
135 
139  RCLCPP_PUBLIC
140  std::vector<rclcpp::CallbackGroup::WeakPtr>
142 
144 
148  RCLCPP_PUBLIC
149  void
151 
152 protected:
153  using NodeCollection = std::set<
154  rclcpp::node_interfaces::NodeBaseInterface::WeakPtr,
155  std::owner_less<rclcpp::node_interfaces::NodeBaseInterface::WeakPtr>>;
156 
157  using CallbackGroupCollection = std::set<
158  rclcpp::CallbackGroup::WeakPtr,
159  std::owner_less<rclcpp::CallbackGroup::WeakPtr>>;
160 
161  using WeakNodesToGuardConditionsMap = std::map<
162  rclcpp::node_interfaces::NodeBaseInterface::WeakPtr,
163  rclcpp::GuardCondition::WeakPtr,
164  std::owner_less<rclcpp::node_interfaces::NodeBaseInterface::WeakPtr>>;
165 
166  using WeakGroupsToGuardConditionsMap = std::map<
167  rclcpp::CallbackGroup::WeakPtr,
168  rclcpp::GuardCondition::WeakPtr,
169  std::owner_less<rclcpp::CallbackGroup::WeakPtr>>;
170 
172 
183  RCLCPP_PUBLIC
184  NodeCollection::iterator
185  remove_weak_node(NodeCollection::iterator weak_node) RCPPUTILS_TSA_REQUIRES(mutex_);
186 
188 
200  RCLCPP_PUBLIC
201  CallbackGroupCollection::iterator
203  CallbackGroupCollection::iterator weak_group_it,
204  CallbackGroupCollection & collection) RCPPUTILS_TSA_REQUIRES(mutex_);
205 
207 
211  RCLCPP_PUBLIC
212  void
214  rclcpp::CallbackGroup::SharedPtr group_ptr,
215  CallbackGroupCollection & collection) RCPPUTILS_TSA_REQUIRES(mutex_);
216 
218  RCLCPP_PUBLIC
219  void
220  process_queues() RCPPUTILS_TSA_REQUIRES(mutex_);
221 
224  RCLCPP_PUBLIC
225  void
227  const NodeCollection & nodes_to_check) RCPPUTILS_TSA_REQUIRES(mutex_);
228 
230  RCLCPP_PUBLIC
231  void
232  prune_invalid_nodes_and_groups() RCPPUTILS_TSA_REQUIRES(mutex_);
233 
235  mutable std::mutex mutex_;
236 
238  CallbackGroupCollection manually_added_groups_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
239 
241  CallbackGroupCollection automatically_added_groups_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
242 
244  NodeCollection weak_nodes_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
245 
247  WeakNodesToGuardConditionsMap weak_nodes_to_guard_conditions_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
248 
250  WeakGroupsToGuardConditionsMap weak_groups_to_guard_conditions_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
251 
253  NodeCollection pending_added_nodes_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
254 
256  NodeCollection pending_removed_nodes_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
257 
259  CallbackGroupCollection pending_manually_added_groups_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
260 
262  CallbackGroupCollection pending_manually_removed_groups_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
263 
265  std::shared_ptr<ExecutorNotifyWaitable> notify_waitable_;
266 };
267 } // namespace executors
268 } // namespace rclcpp
269 //
270 #endif // RCLCPP__EXECUTORS__EXECUTOR_ENTITIES_COLLECTOR_HPP_
Class to monitor a set of nodes and callback groups for changes in entity membership.
RCLCPP_PUBLIC NodeCollection::iterator remove_weak_node(NodeCollection::iterator weak_node) RCPPUTILS_TSA_REQUIRES(mutex_)
Implementation of removing a node from the collector.
RCLCPP_PUBLIC void add_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr)
Add a node to the entity collector.
RCLCPP_PUBLIC void prune_invalid_nodes_and_groups() RCPPUTILS_TSA_REQUIRES(mutex_)
Check all nodes and group for expired weak pointers and remove them.
std::shared_ptr< ExecutorNotifyWaitable > notify_waitable_
Waitable to add guard conditions to.
CallbackGroupCollection pending_manually_added_groups_ RCPPUTILS_TSA_GUARDED_BY(mutex_)
callback groups that have been added since the last update.
RCLCPP_PUBLIC void update_collections()
Update the underlying collections.
RCLCPP_PUBLIC void remove_callback_group(rclcpp::CallbackGroup::SharedPtr group_ptr)
Remove a callback group from the entity collector.
NodeCollection pending_added_nodes_ RCPPUTILS_TSA_GUARDED_BY(mutex_)
nodes that have been added since the last update.
RCLCPP_PUBLIC std::vector< rclcpp::CallbackGroup::WeakPtr > get_all_callback_groups() const
Get all callback groups known to this entity collector.
bool has_pending() const
Indicate if the entities collector has pending additions or removals.
RCLCPP_PUBLIC void process_queues() RCPPUTILS_TSA_REQUIRES(mutex_)
Iterate over queued added/remove nodes and callback_groups.
NodeCollection weak_nodes_ RCPPUTILS_TSA_GUARDED_BY(mutex_)
nodes that are associated with the executor
RCLCPP_PUBLIC ExecutorEntitiesCollector(std::shared_ptr< ExecutorNotifyWaitable > notify_waitable)
Constructor.
std::mutex mutex_
mutex to protect collections and pending queues
WeakNodesToGuardConditionsMap weak_nodes_to_guard_conditions_ RCPPUTILS_TSA_GUARDED_BY(mutex_)
Track guard conditions associated with added nodes.
WeakGroupsToGuardConditionsMap weak_groups_to_guard_conditions_ RCPPUTILS_TSA_GUARDED_BY(mutex_)
Track guard conditions associated with added callback groups.
RCLCPP_PUBLIC void add_automatically_associated_callback_groups(const NodeCollection &nodes_to_check) RCPPUTILS_TSA_REQUIRES(mutex_)
CallbackGroupCollection manually_added_groups_ RCPPUTILS_TSA_GUARDED_BY(mutex_)
Callback groups that were added via add_callback_group
CallbackGroupCollection pending_manually_removed_groups_ RCPPUTILS_TSA_GUARDED_BY(mutex_)
callback groups that have been removed since the last update.
RCLCPP_PUBLIC std::vector< rclcpp::CallbackGroup::WeakPtr > get_automatically_added_callback_groups() const
Get automatically-added callback groups known to this entity collector.
CallbackGroupCollection automatically_added_groups_ RCPPUTILS_TSA_GUARDED_BY(mutex_)
Callback groups that were added by their association with added nodes.
NodeCollection pending_removed_nodes_ RCPPUTILS_TSA_GUARDED_BY(mutex_)
nodes that have been removed since the last update.
RCLCPP_PUBLIC CallbackGroupCollection::iterator remove_weak_callback_group(CallbackGroupCollection::iterator weak_group_it, CallbackGroupCollection &collection) RCPPUTILS_TSA_REQUIRES(mutex_)
Implementation of removing a callback group from the collector.
RCLCPP_PUBLIC void add_callback_group_to_collection(rclcpp::CallbackGroup::SharedPtr group_ptr, CallbackGroupCollection &collection) RCPPUTILS_TSA_REQUIRES(mutex_)
Implementation of adding a callback group.
RCLCPP_PUBLIC void remove_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr)
Remove a node from the entity collector.
RCLCPP_PUBLIC std::vector< rclcpp::CallbackGroup::WeakPtr > get_manually_added_callback_groups() const
Get manually-added callback groups known to this entity collector.
RCLCPP_PUBLIC void add_callback_group(rclcpp::CallbackGroup::SharedPtr group_ptr)
Add a callback group to the entity collector.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.