ROS 2 rclcpp + rcl - rolling  rolling-20536064
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 
31 namespace rclcpp
32 {
33 namespace experimental
34 {
35 namespace executors
36 {
37 
39 
59 class
60 [[deprecated(
61  "rclcpp::experimental::executors::EventsExecutor is deprecated and will be removed in "
62  "m-turtle. use rclcpp::executors::EventsCBGExecutor in single-threaded mode instead")]]
64 {
65 public:
66  RCLCPP_SMART_PTR_DEFINITIONS(EventsExecutor)
67 
68 
75  RCLCPP_PUBLIC
78  rclcpp::experimental::executors::EventsQueue::UniquePtr events_queue = std::make_unique<
80  bool execute_timers_separate_thread = false);
81 
83  RCLCPP_PUBLIC
84  virtual ~EventsExecutor();
85 
87 
92  RCLCPP_PUBLIC
93  void
94  spin() override;
95 
97 
110  RCLCPP_PUBLIC
111  void
112  spin_some(std::chrono::nanoseconds max_duration = std::chrono::nanoseconds(0)) override;
113 
115 
128  RCLCPP_PUBLIC
129  void
130  spin_all(std::chrono::nanoseconds max_duration) override;
131 
132 protected:
134  RCLCPP_PUBLIC
135  void
136  spin_once_impl(std::chrono::nanoseconds timeout) override;
137 
139  RCLCPP_PUBLIC
140  void
141  spin_some_impl(std::chrono::nanoseconds max_duration, bool exhaustive);
142 
144  RCLCPP_PUBLIC
145  void
146  handle_updated_entities(bool notify) override;
147 
148 private:
149  RCLCPP_DISABLE_COPY(EventsExecutor)
150 
151 
152  void
153  execute_event(const ExecutorEvent & event);
154 
156  void
157  setup_notify_waitable();
158 
160  void
161  refresh_current_collection(const rclcpp::executors::ExecutorEntitiesCollection & new_collection);
162 
164  std::function<void(size_t)>
165  create_entity_callback(void * entity_key, ExecutorEventType type);
166 
168  std::function<void(size_t, int)>
169  create_waitable_callback(const rclcpp::Waitable * waitable_id);
170 
172  void
173  add_notify_waitable_to_collection(
175 
177  template<typename CollectionType>
178  typename CollectionType::EntitySharedPtr
179  retrieve_entity(typename CollectionType::Key entity_id, CollectionType & collection)
180  {
181  // Note: we lock the mutex because we assume that you are trying to get an element from the
182  // current collection... If there will be a use-case to retrieve elements also from other
183  // collections, we can move the mutex back to the calling codes.
184  std::lock_guard<std::mutex> guard(mutex_);
185 
186  // Check if the entity_id is in the collection
187  auto it = collection.find(entity_id);
188  if (it == collection.end()) {
189  return nullptr;
190  }
191 
192  // Check if the entity associated with the entity_id is valid
193  // and remove it from the collection if it isn't
194  auto entity = it->second.entity.lock();
195  if (!entity) {
196  collection.erase(it);
197  }
198 
199  // Return the retrieved entity (this can be a nullptr if the entity was not valid)
200  return entity;
201  }
202 
204  rclcpp::experimental::executors::EventsQueue::UniquePtr events_queue_;
205 
207  std::shared_ptr<rclcpp::experimental::TimersManager> timers_manager_;
208 };
209 
210 } // namespace executors
211 } // namespace experimental
212 } // namespace rclcpp
213 
214 #endif // RCLCPP__EXPERIMENTAL__EXECUTORS__EVENTS_EXECUTOR__EVENTS_EXECUTOR_HPP_
Coordinate the order and timing of available communication tasks.
Definition: executor.hpp:64
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.
RCLCPP_PUBLIC void spin_some(const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr &node_ptr)
Create a default single-threaded executor and execute any immediately available work.
Definition: executors.cpp:30
RCLCPP_PUBLIC void spin(const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr &node_ptr)
Create a default single-threaded executor and spin the specified node.
Definition: executors.cpp:39
RCLCPP_PUBLIC void spin_all(const rclcpp::node_interfaces::NodeBaseInterface::SharedPtr &node_ptr, std::chrono::nanoseconds max_duration)
Create a default single-threaded executor and execute all available work exhaustively.
Definition: executors.cpp:19
Options to be passed to the executor constructor.
Represent the total set of entities for a single executor.