ROS 2 rclcpp + rcl - jazzy  jazzy
ROS 2 C++ Client Library with ROS Client Library
events_executor.hpp
1 // Copyright 2023 iRobot Corporation.
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__EXPERIMENTAL__EXECUTORS__EVENTS_EXECUTOR__EVENTS_EXECUTOR_HPP_
16 #define RCLCPP__EXPERIMENTAL__EXECUTORS__EVENTS_EXECUTOR__EVENTS_EXECUTOR_HPP_
17 
18 #include <atomic>
19 #include <chrono>
20 #include <memory>
21 #include <vector>
22 
23 #include "rclcpp/executor.hpp"
24 #include "rclcpp/executors/executor_entities_collection.hpp"
25 #include "rclcpp/executors/executor_entities_collector.hpp"
26 #include "rclcpp/experimental/executors/events_executor/events_executor_event_types.hpp"
27 #include "rclcpp/experimental/executors/events_executor/events_queue.hpp"
28 #include "rclcpp/experimental/executors/events_executor/simple_events_queue.hpp"
29 #include "rclcpp/experimental/timers_manager.hpp"
30 #include "rclcpp/node.hpp"
31 
32 namespace rclcpp
33 {
34 namespace experimental
35 {
36 namespace executors
37 {
38 
40 
61 {
62  friend class EventsExecutorEntitiesCollector;
63 
64 public:
65  RCLCPP_SMART_PTR_DEFINITIONS(EventsExecutor)
66 
67 
74  RCLCPP_PUBLIC
75  explicit EventsExecutor(
76  rclcpp::experimental::executors::EventsQueue::UniquePtr events_queue = std::make_unique<
78  bool execute_timers_separate_thread = false,
80 
82  RCLCPP_PUBLIC
83  virtual ~EventsExecutor();
84 
86 
91  RCLCPP_PUBLIC
92  void
93  spin() override;
94 
96 
109  RCLCPP_PUBLIC
110  void
111  spin_some(std::chrono::nanoseconds max_duration = std::chrono::nanoseconds(0)) override;
112 
114 
127  RCLCPP_PUBLIC
128  void
129  spin_all(std::chrono::nanoseconds max_duration) override;
130 
132 
135  RCLCPP_PUBLIC
136  void
137  add_node(
138  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
139  bool notify = true) override;
140 
142 
145  RCLCPP_PUBLIC
146  void
147  add_node(std::shared_ptr<rclcpp::Node> node_ptr, bool notify = true) override;
148 
150 
153  RCLCPP_PUBLIC
154  void
155  remove_node(
156  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
157  bool notify = true) override;
158 
160 
163  RCLCPP_PUBLIC
164  void
165  remove_node(std::shared_ptr<rclcpp::Node> node_ptr, bool notify = true) override;
166 
168 
171  RCLCPP_PUBLIC
172  void
174  rclcpp::CallbackGroup::SharedPtr group_ptr,
175  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr,
176  bool notify = true) override;
177 
179 
182  RCLCPP_PUBLIC
183  void
185  rclcpp::CallbackGroup::SharedPtr group_ptr,
186  bool notify = true) override;
187 
189 
192  RCLCPP_PUBLIC
193  std::vector<rclcpp::CallbackGroup::WeakPtr>
194  get_all_callback_groups() override;
195 
197 
200  RCLCPP_PUBLIC
201  std::vector<rclcpp::CallbackGroup::WeakPtr>
203 
205 
208  RCLCPP_PUBLIC
209  std::vector<rclcpp::CallbackGroup::WeakPtr>
211 
212 protected:
214  RCLCPP_PUBLIC
215  void
216  spin_once_impl(std::chrono::nanoseconds timeout) override;
217 
219  RCLCPP_PUBLIC
220  void
221  spin_some_impl(std::chrono::nanoseconds max_duration, bool exhaustive);
222 
223 private:
224  RCLCPP_DISABLE_COPY(EventsExecutor)
225 
226 
227  void
228  execute_event(const ExecutorEvent & event);
229 
231  void
232  refresh_current_collection_from_callback_groups();
233 
235  void
236  refresh_current_collection(const rclcpp::executors::ExecutorEntitiesCollection & new_collection);
237 
239  std::function<void(size_t)>
240  create_entity_callback(void * entity_key, ExecutorEventType type);
241 
243  std::function<void(size_t, int)>
244  create_waitable_callback(const rclcpp::Waitable * waitable_id);
245 
247  void
248  add_notify_waitable_to_collection(
250 
252  template<typename CollectionType>
253  typename CollectionType::EntitySharedPtr
254  retrieve_entity(typename CollectionType::Key entity_id, CollectionType & collection)
255  {
256  // Check if the entity_id is in the collection
257  auto it = collection.find(entity_id);
258  if (it == collection.end()) {
259  return nullptr;
260  }
261 
262  // Check if the entity associated with the entity_id is valid
263  // and remove it from the collection if it isn't
264  auto entity = it->second.entity.lock();
265  if (!entity) {
266  collection.erase(it);
267  }
268 
269  // Return the retrieved entity (this can be a nullptr if the entity was not valid)
270  return entity;
271  }
272 
274  rclcpp::experimental::executors::EventsQueue::UniquePtr events_queue_;
275 
276  std::shared_ptr<rclcpp::executors::ExecutorEntitiesCollector> entities_collector_;
277  std::shared_ptr<rclcpp::executors::ExecutorNotifyWaitable> notify_waitable_;
278 
280  std::recursive_mutex collection_mutex_;
281  std::shared_ptr<rclcpp::executors::ExecutorEntitiesCollection> current_entities_collection_;
282 
284  std::atomic<bool> notify_waitable_event_pushed_ {false};
285 
287  std::shared_ptr<rclcpp::experimental::TimersManager> timers_manager_;
288 };
289 
290 } // namespace executors
291 } // namespace experimental
292 } // namespace rclcpp
293 
294 #endif // RCLCPP__EXPERIMENTAL__EXECUTORS__EVENTS_EXECUTOR__EVENTS_EXECUTOR_HPP_
Coordinate the order and timing of available communication tasks.
Definition: executor.hpp:65
RCLCPP_PUBLIC void remove_callback_group(rclcpp::CallbackGroup::SharedPtr group_ptr, bool notify=true) override
Remove callback group from the executor.
RCLCPP_PUBLIC void spin_once_impl(std::chrono::nanoseconds timeout) override
Internal implementation of spin_once.
RCLCPP_PUBLIC void spin() override
Events executor implementation of spin.
RCLCPP_PUBLIC std::vector< rclcpp::CallbackGroup::WeakPtr > get_all_callback_groups() override
Get callback groups that belong to executor.
virtual RCLCPP_PUBLIC ~EventsExecutor()
Default destructor.
RCLCPP_PUBLIC void add_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify=true) override
Add a node to the executor.
RCLCPP_PUBLIC void remove_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify=true) override
Remove a node from the executor.
RCLCPP_PUBLIC void spin_some_impl(std::chrono::nanoseconds max_duration, bool exhaustive)
Internal implementation of spin_some.
RCLCPP_PUBLIC void add_callback_group(rclcpp::CallbackGroup::SharedPtr group_ptr, rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify=true) override
Add a callback group to an executor.
RCLCPP_PUBLIC EventsExecutor(rclcpp::experimental::executors::EventsQueue::UniquePtr events_queue=std::make_unique< rclcpp::experimental::executors::SimpleEventsQueue >(), bool execute_timers_separate_thread=false, const rclcpp::ExecutorOptions &options=rclcpp::ExecutorOptions())
Default constructor. See the default constructor for Executor.
RCLCPP_PUBLIC std::vector< rclcpp::CallbackGroup::WeakPtr > get_automatically_added_callback_groups_from_nodes() override
Get callback groups that belong to executor.
RCLCPP_PUBLIC void spin_all(std::chrono::nanoseconds max_duration) override
Events executor implementation of spin all.
RCLCPP_PUBLIC std::vector< rclcpp::CallbackGroup::WeakPtr > get_manually_added_callback_groups() override
Get callback groups that belong to executor.
RCLCPP_PUBLIC void spin_some(std::chrono::nanoseconds max_duration=std::chrono::nanoseconds(0)) override
Events executor implementation of spin some.
This class implements an EventsQueue as a simple wrapper around a std::queue. It does not perform any...
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Options to be passed to the executor constructor.
Represent the total set of entities for a single executor.