rclcpp  master
C++ ROS Client Library API
executor.hpp
Go to the documentation of this file.
1 // Copyright 2014 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__EXECUTOR_HPP_
16 #define RCLCPP__EXECUTOR_HPP_
17 
18 #include <algorithm>
19 #include <cassert>
20 #include <chrono>
21 #include <cstdlib>
22 #include <iostream>
23 #include <list>
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
28 #include "rcl/guard_condition.h"
29 #include "rcl/wait.h"
30 
35 #include "rclcpp/utilities.hpp"
37 
38 namespace rclcpp
39 {
40 
41 // Forward declaration is used in convenience method signature.
42 class Node;
43 
44 namespace executor
45 {
46 
48 
54 
57 operator<<(std::ostream & os, const FutureReturnCode & future_return_code);
58 
61 to_string(const FutureReturnCode & future_return_code);
62 
64 
68 {
70  : memory_strategy(memory_strategies::create_default_strategy()),
71  context(rclcpp::contexts::default_context::get_global_default_context()),
72  max_conditions(0)
73  {}
74 
75  memory_strategy::MemoryStrategy::SharedPtr memory_strategy;
78 };
79 
80 static inline ExecutorArgs create_default_executor_arguments()
81 {
82  return ExecutorArgs();
83 }
84 
86 
95 class Executor
96 {
97 public:
99 
100 
101  // \param[in] ms The memory strategy to be used with this executor.
103  explicit Executor(const ExecutorArgs & args = ExecutorArgs());
104 
107  virtual ~Executor();
108 
110  // It is up to the implementation of Executor to implement spin.
111  virtual void
112  spin() = 0;
113 
115 
123  virtual void
124  add_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify = true);
125 
128  virtual void
129  add_node(std::shared_ptr<rclcpp::Node> node_ptr, bool notify = true);
130 
132 
139  virtual void
140  remove_node(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr, bool notify = true);
141 
144  virtual void
145  remove_node(std::shared_ptr<rclcpp::Node> node_ptr, bool notify = true);
146 
148 
154  template<typename RepT = int64_t, typename T = std::milli>
155  void
157  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node,
159  {
160  return spin_node_once_nanoseconds(
161  node,
162  std::chrono::duration_cast<std::chrono::nanoseconds>(timeout)
163  );
164  }
165 
167  template<typename NodeT = rclcpp::Node, typename RepT = int64_t, typename T = std::milli>
168  void
172  {
173  return spin_node_once_nanoseconds(
174  node->get_node_base_interface(),
176  );
177  }
178 
180 
184  void
185  spin_node_some(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node);
186 
189  void
190  spin_node_some(std::shared_ptr<rclcpp::Node> node);
191 
193 
204  virtual void
206 
208  virtual void
209  spin_once(std::chrono::nanoseconds timeout = std::chrono::nanoseconds(-1));
210 
212 
221  template<typename ResponseT, typename TimeRepT = int64_t, typename TimeT = std::milli>
226  {
227  // TODO(wjwwood): does not work recursively; can't call spin_node_until_future_complete
228  // inside a callback executed by an executor.
229 
230  // Check the future before entering the while loop.
231  // If the future is already complete, don't try to spin.
232  std::future_status status = future.wait_for(std::chrono::seconds(0));
233  if (status == std::future_status::ready) {
235  }
236 
237  auto end_time = std::chrono::steady_clock::now();
239  timeout);
240  if (timeout_ns > std::chrono::nanoseconds::zero()) {
241  end_time += timeout_ns;
242  }
243  std::chrono::nanoseconds timeout_left = timeout_ns;
244 
245  while (rclcpp::ok(this->context_)) {
246  // Do one item of work.
247  spin_once(timeout_left);
248  // Check if the future is set, return SUCCESS if it is.
249  status = future.wait_for(std::chrono::seconds(0));
250  if (status == std::future_status::ready) {
252  }
253  // If the original timeout is < 0, then this is blocking, never TIMEOUT.
254  if (timeout_ns < std::chrono::nanoseconds::zero()) {
255  continue;
256  }
257  // Otherwise check if we still have time to wait, return TIMEOUT if not.
258  auto now = std::chrono::steady_clock::now();
259  if (now >= end_time) {
261  }
262  // Subtract the elapsed time from the original timeout.
263  timeout_left = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - now);
264  }
265 
266  // The future did not complete before ok() returned false, return INTERRUPTED.
268  }
269 
271  /* This function can be called asynchonously from any thread. */
273  void
274  cancel();
275 
277 
283  void
284  set_memory_strategy(memory_strategy::MemoryStrategy::SharedPtr memory_strategy);
285 
286 protected:
288  void
289  spin_node_once_nanoseconds(
290  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node,
291  std::chrono::nanoseconds timeout);
292 
294 
298  void
299  execute_any_executable(AnyExecutable & any_exec);
300 
302  static void
303  execute_subscription(
304  rclcpp::SubscriptionBase::SharedPtr subscription);
305 
307  static void
308  execute_intra_process_subscription(
309  rclcpp::SubscriptionBase::SharedPtr subscription);
310 
312  static void
313  execute_timer(rclcpp::TimerBase::SharedPtr timer);
314 
316  static void
317  execute_service(rclcpp::ServiceBase::SharedPtr service);
318 
320  static void
321  execute_client(rclcpp::ClientBase::SharedPtr client);
322 
324  void
325  wait_for_work(std::chrono::nanoseconds timeout = std::chrono::nanoseconds(-1));
326 
328  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr
329  get_node_by_group(rclcpp::callback_group::CallbackGroup::SharedPtr group);
330 
332  rclcpp::callback_group::CallbackGroup::SharedPtr
333  get_group_by_timer(rclcpp::TimerBase::SharedPtr timer);
334 
336  void
337  get_next_timer(AnyExecutable & any_exec);
338 
340  bool
341  get_next_ready_executable(AnyExecutable & any_executable);
342 
344  bool
345  get_next_executable(
346  AnyExecutable & any_executable,
348 
350  std::atomic_bool spinning;
351 
354 
357 
359  memory_strategy::MemoryStrategy::SharedPtr memory_strategy_;
360 
363 
364 private:
365  RCLCPP_DISABLE_COPY(Executor)
366 
369 };
370 
371 } // namespace executor
372 } // namespace rclcpp
373 
374 #endif // RCLCPP__EXECUTOR_HPP_
void spin_node_once(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node, std::chrono::duration< RepT, T > timeout=std::chrono::duration< RepT, T >(-1))
Add a node to executor, execute the next available unit of work, and remove the node.
Definition: executor.hpp:156
DefaultContext::SharedPtr get_global_default_context()
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
void spin_node_once(std::shared_ptr< NodeT > node, std::chrono::duration< RepT, T > timeout=std::chrono::duration< RepT, T >(-1))
Convenience function which takes Node and forwards NodeBaseInterface.
Definition: executor.hpp:169
std::atomic_bool spinning
Spinning state, used to prevent multi threaded calls to spin and to cancel blocking spins...
Definition: executor.hpp:350
FutureReturnCode spin_until_future_complete(std::shared_future< ResponseT > &future, std::chrono::duration< TimeRepT, TimeT > timeout=std::chrono::duration< TimeRepT, TimeT >(-1))
Spin (blocking) until the future is complete, it times out waiting, or rclcpp is interrupted.
Definition: executor.hpp:223
Definition: any_executable.hpp:35
This header provides the get_node_topics_interface() template function.
Definition: allocator_common.hpp:24
T duration_cast(T... args)
std::ostream & operator<<(std::ostream &os, const FutureReturnCode &future_return_code)
rcl_wait_set_t rcl_get_zero_initialized_wait_set(void)
memory_strategy::MemoryStrategy::SharedPtr create_default_strategy()
rcl_guard_condition_t rcl_get_zero_initialized_guard_condition(void)
std::string to_string(const FutureReturnCode &future_return_code)
std::shared_ptr< rclcpp::Context > context
Definition: executor.hpp:76
memory_strategy::MemoryStrategy::SharedPtr memory_strategy_
The memory strategy: an interface for handling user-defined memory allocation strategies.
Definition: executor.hpp:359
Coordinate the order and timing of available communication tasks.
Definition: executor.hpp:95
ExecutorArgs()
Definition: executor.hpp:69
void spin_some(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr)
Create a default single-threaded executor and execute any immediately available work.
#define RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(...)
Definition: macros.hpp:51
FutureReturnCode
Return codes to be used with spin_until_future_complete.
Definition: executor.hpp:53
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
bool ok(rclcpp::Context::SharedPtr context=nullptr)
Check rclcpp&#39;s status.
size_t max_conditions
Definition: executor.hpp:77
T wait_for(T... args)
memory_strategy::MemoryStrategy::SharedPtr memory_strategy
Definition: executor.hpp:75
Definition: executor.hpp:67
void spin(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr)
Create a default single-threaded executor and spin the specified node.
std::shared_ptr< rclcpp::Context > context_
The context associated with this executor.
Definition: executor.hpp:362