ROS 2 rclcpp + rcl - rolling  rolling-20536064
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 
21 
22 SingleThreadedExecutor::SingleThreadedExecutor(const rclcpp::ExecutorOptions & options)
23 : rclcpp::Executor(options) {}
24 
26 
27 void
29 {
30  if (spinning.exchange(true)) {
31  throw std::runtime_error("spin() called while already spinning");
32  }
33  RCPPUTILS_SCOPE_EXIT(
34  wait_result_.reset();
35  this->spinning.store(false);
36  this->cancel_requested_.store(false););
37  if (cancel_requested_.load()) {
38  return;
39  }
40 
41  // Clear any previous result and rebuild the waitset
42  this->wait_result_.reset();
43  this->entities_need_rebuild_ = true;
44 
45  while (rclcpp::ok(this->context_) && !cancel_requested_.load()) {
46  rclcpp::AnyExecutable any_executable;
47  if (get_next_executable(any_executable)) {
48  execute_any_executable(any_executable);
49  }
50  }
51 }
Coordinate the order and timing of available communication tasks.
Definition: executor.hpp:64
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:589
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:578
std::atomic_bool spinning
Spinning state, used to prevent multi threaded calls to spin.
Definition: executor.hpp:571
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.