ROS 2 rclcpp + rcl - kilted  kilted
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(wait_result_.reset();this->spinning.store(false););
34 
35  // Clear any previous result and rebuild the waitset
36  this->wait_result_.reset();
37  this->entities_need_rebuild_ = true;
38 
39  while (rclcpp::ok(this->context_) && spinning.load()) {
40  rclcpp::AnyExecutable any_executable;
41  if (get_next_executable(any_executable)) {
42  execute_any_executable(any_executable);
43  }
44  }
45 }
Coordinate the order and timing of available communication tasks.
Definition: executor.hpp:65
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:897
std::shared_ptr< rclcpp::Context > context_
The context associated with this executor.
Definition: executor.hpp:572
RCLCPP_PUBLIC void execute_any_executable(AnyExecutable &any_exec)
Find the next available executable and do the work associated with it.
Definition: executor.cpp:467
std::atomic_bool spinning
Spinning state, used to prevent multi threaded calls to spin and to cancel blocking spins.
Definition: executor.hpp:561
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(rclcpp::Context::SharedPtr context=nullptr)
Check rclcpp's status.
Options to be passed to the executor constructor.