ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
callback_group.hpp
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__CALLBACK_GROUP_HPP_
16 #define RCLCPP__CALLBACK_GROUP_HPP_
17 
18 #include <atomic>
19 #include <functional>
20 #include <memory>
21 #include <mutex>
22 #include <vector>
23 
24 #include "rclcpp/client.hpp"
25 #include "rclcpp/context.hpp"
26 #include "rclcpp/guard_condition.hpp"
27 #include "rclcpp/publisher_base.hpp"
28 #include "rclcpp/service.hpp"
29 #include "rclcpp/subscription_base.hpp"
30 #include "rclcpp/timer.hpp"
31 #include "rclcpp/visibility_control.hpp"
32 #include "rclcpp/waitable.hpp"
33 
34 namespace rclcpp
35 {
36 
37 // Forward declarations for friend statement in class CallbackGroup
38 namespace node_interfaces
39 {
40 class NodeServices;
41 class NodeTimers;
42 class NodeTopics;
43 class NodeWaitables;
44 } // namespace node_interfaces
45 
46 enum class CallbackGroupType
47 {
48  MutuallyExclusive,
49  Reentrant
50 };
51 
53 {
58 
59 public:
60  RCLCPP_SMART_PTR_DEFINITIONS(CallbackGroup)
61 
62 
96  RCLCPP_PUBLIC
97  explicit CallbackGroup(
98  CallbackGroupType group_type,
100 
102  RCLCPP_PUBLIC
103  ~CallbackGroup();
104 
105  template<typename Function>
106  rclcpp::SubscriptionBase::SharedPtr
107  find_subscription_ptrs_if(Function func) const
108  {
109  return _find_ptrs_if_impl<rclcpp::SubscriptionBase, Function>(func, subscription_ptrs_);
110  }
111 
112  template<typename Function>
113  rclcpp::TimerBase::SharedPtr
114  find_timer_ptrs_if(Function func) const
115  {
116  return _find_ptrs_if_impl<rclcpp::TimerBase, Function>(func, timer_ptrs_);
117  }
118 
119  template<typename Function>
120  rclcpp::ServiceBase::SharedPtr
121  find_service_ptrs_if(Function func) const
122  {
123  return _find_ptrs_if_impl<rclcpp::ServiceBase, Function>(func, service_ptrs_);
124  }
125 
126  template<typename Function>
127  rclcpp::ClientBase::SharedPtr
128  find_client_ptrs_if(Function func) const
129  {
130  return _find_ptrs_if_impl<rclcpp::ClientBase, Function>(func, client_ptrs_);
131  }
132 
133  template<typename Function>
134  rclcpp::Waitable::SharedPtr
135  find_waitable_ptrs_if(Function func) const
136  {
137  return _find_ptrs_if_impl<rclcpp::Waitable, Function>(func, waitable_ptrs_);
138  }
139 
140  RCLCPP_PUBLIC
141  std::atomic_bool &
142  can_be_taken_from();
143 
144  RCLCPP_PUBLIC
145  const CallbackGroupType &
146  type() const;
147 
148  RCLCPP_PUBLIC
149  void collect_all_ptrs(
150  std::function<void(const rclcpp::SubscriptionBase::SharedPtr &)> sub_func,
151  std::function<void(const rclcpp::ServiceBase::SharedPtr &)> service_func,
152  std::function<void(const rclcpp::ClientBase::SharedPtr &)> client_func,
153  std::function<void(const rclcpp::TimerBase::SharedPtr &)> timer_func,
154  std::function<void(const rclcpp::Waitable::SharedPtr &)> waitable_func) const;
155 
157 
168  RCLCPP_PUBLIC
169  std::atomic_bool &
171 
173 
177  RCLCPP_PUBLIC
178  bool
180 
182  RCLCPP_PUBLIC
183  rclcpp::GuardCondition::SharedPtr
184  get_notify_guard_condition(const rclcpp::Context::SharedPtr context_ptr);
185 
187  RCLCPP_PUBLIC
188  void
190 
191 protected:
192  RCLCPP_DISABLE_COPY(CallbackGroup)
193 
194  RCLCPP_PUBLIC
195  void
196  add_publisher(const rclcpp::PublisherBase::SharedPtr publisher_ptr);
197 
198  RCLCPP_PUBLIC
199  void
200  add_subscription(const rclcpp::SubscriptionBase::SharedPtr subscription_ptr);
201 
202  RCLCPP_PUBLIC
203  void
204  add_timer(const rclcpp::TimerBase::SharedPtr timer_ptr);
205 
206  RCLCPP_PUBLIC
207  void
208  add_service(const rclcpp::ServiceBase::SharedPtr service_ptr);
209 
210  RCLCPP_PUBLIC
211  void
212  add_client(const rclcpp::ClientBase::SharedPtr client_ptr);
213 
214  RCLCPP_PUBLIC
215  void
216  add_waitable(const rclcpp::Waitable::SharedPtr waitable_ptr);
217 
218  RCLCPP_PUBLIC
219  void
220  remove_waitable(const rclcpp::Waitable::SharedPtr waitable_ptr) noexcept;
221 
222  CallbackGroupType type_;
223  // Mutex to protect the subsequent vectors of pointers.
224  mutable std::mutex mutex_;
225  std::atomic_bool associated_with_executor_;
226  std::vector<rclcpp::SubscriptionBase::WeakPtr> subscription_ptrs_;
227  std::vector<rclcpp::TimerBase::WeakPtr> timer_ptrs_;
228  std::vector<rclcpp::ServiceBase::WeakPtr> service_ptrs_;
229  std::vector<rclcpp::ClientBase::WeakPtr> client_ptrs_;
230  std::vector<rclcpp::Waitable::WeakPtr> waitable_ptrs_;
231  std::atomic_bool can_be_taken_from_;
232  const bool automatically_add_to_executor_with_node_;
233  // defer the creation of the guard condition
234  std::shared_ptr<rclcpp::GuardCondition> notify_guard_condition_ = nullptr;
235  std::recursive_mutex notify_guard_condition_mutex_;
236 
237 private:
238  template<typename TypeT, typename Function>
239  typename TypeT::SharedPtr _find_ptrs_if_impl(
240  Function func, const std::vector<typename TypeT::WeakPtr> & vect_ptrs) const
241  {
242  std::lock_guard<std::mutex> lock(mutex_);
243  for (auto & weak_ptr : vect_ptrs) {
244  auto ref_ptr = weak_ptr.lock();
245  if (ref_ptr && func(ref_ptr)) {
246  return ref_ptr;
247  }
248  }
249  return typename TypeT::SharedPtr();
250  }
251 };
252 
253 } // namespace rclcpp
254 
255 #endif // RCLCPP__CALLBACK_GROUP_HPP_
RCLCPP_PUBLIC ~CallbackGroup()
Default destructor.
RCLCPP_PUBLIC std::atomic_bool & get_associated_with_executor_atomic()
Return a reference to the 'associated with executor' atomic boolean.
RCLCPP_PUBLIC rclcpp::GuardCondition::SharedPtr get_notify_guard_condition(const rclcpp::Context::SharedPtr context_ptr)
Defer creating the notify guard condition and return it.
RCLCPP_PUBLIC void trigger_notify_guard_condition()
Trigger the notify guard condition.
RCLCPP_PUBLIC bool automatically_add_to_executor_with_node() const
Return true if this callback group should be automatically added to an executor by the node.
RCLCPP_PUBLIC CallbackGroup(CallbackGroupType group_type, bool automatically_add_to_executor_with_node=true)
Constructor for CallbackGroup.
Implementation of the NodeServices part of the Node API.
Implementation of the NodeTimers part of the Node API.
Definition: node_timers.hpp:32
Implementation of the NodeTopics part of the Node API.
Definition: node_topics.hpp:42
Implementation of the NodeWaitables part of the Node API.
RCLCPP_PUBLIC void remove_waitable(rclcpp::Waitable::SharedPtr waitable_ptr, rclcpp::CallbackGroup::SharedPtr group) noexcept override
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.