ROS 2 rclcpp + rcl - rolling  rolling-29de98cf
ROS 2 C++ Client Library with ROS Client Library
single_threaded_executor.cpp
1 // Copyright 2015 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 #include "rcpputils/scope_exit.hpp"
16 
17 #include "rclcpp/executors/single_threaded_executor.hpp"
18 #include "rclcpp/any_executable.hpp"
19 #include "rclcpp/utilities.hpp"
20 
22 
23 SingleThreadedExecutor::SingleThreadedExecutor(const rclcpp::ExecutorOptions & options)
24 : rclcpp::Executor(options) {}
25 
27 
28 void
30 {
31  if (spinning.exchange(true)) {
32  throw std::runtime_error("spin() called while already spinning");
33  }
34  RCPPUTILS_SCOPE_EXIT(
35  wait_result_.reset();
36  this->spinning.store(false);
37  this->cancel_requested_.store(false););
38  if (cancel_requested_.load()) {
39  return;
40  }
41 
42  // Clear any previous result and rebuild the waitset
43  this->wait_result_.reset();
44  this->entities_need_rebuild_ = true;
45 
46  while (rclcpp::ok(this->context_) && !cancel_requested_.load()) {
47  rclcpp::AnyExecutable any_executable;
48  if (get_next_executable(any_executable)) {
49  execute_any_executable(any_executable);
50  }
51  }
52 }
Coordinate the order and timing of available communication tasks.
Definition: executor.hpp:61
RCLCPP_PUBLIC bool get_next_executable(AnyExecutable &any_executable, std::chrono::nanoseconds timeout=std::chrono::nanoseconds(-1))
Wait for executable in ready state and populate union structure.
Definition: executor.cpp:937
std::shared_ptr< rclcpp::Context > context_
The context associated with this executor.
Definition: executor.hpp:586
RCLCPP_PUBLIC void execute_any_executable(AnyExecutable &any_exec)
Find the next available executable and do the work associated with it.
Definition: executor.cpp:504
std::atomic_bool cancel_requested_
Tracks a pending cancel request that has not yet been consumed by a spin.
Definition: executor.hpp:575
std::atomic_bool spinning
Spinning state, used to prevent multi threaded calls to spin.
Definition: executor.hpp:568
Single-threaded executor implementation.
virtual RCLCPP_PUBLIC ~SingleThreadedExecutor()
Default destructor.
RCLCPP_PUBLIC void spin() override
Single-threaded implementation of spin.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC bool ok(const rclcpp::Context::SharedPtr &context=rclcpp::contexts::get_global_default_context())
Check rclcpp's status.
Options to be passed to the executor constructor.