22 #include "rclcpp/callback_group.hpp"
23 #include "rclcpp/client.hpp"
24 #include "rclcpp/service.hpp"
25 #include "rclcpp/subscription_base.hpp"
26 #include "rclcpp/timer.hpp"
27 #include "rclcpp/waitable.hpp"
30 using rclcpp::CallbackGroupType;
32 CallbackGroup::CallbackGroup(
33 CallbackGroupType group_type,
34 const rclcpp::Context::WeakPtr & context,
35 bool automatically_add_to_executor_with_node)
36 : type_(group_type), associated_with_executor_(false),
37 can_be_taken_from_(true),
38 automatically_add_to_executor_with_node_(automatically_add_to_executor_with_node),
50 return can_be_taken_from_;
53 const CallbackGroupType &
62 std::lock_guard<std::mutex> lock(mutex_);
64 subscription_ptrs_.size() +
65 service_ptrs_.size() +
68 waitable_ptrs_.size();
72 const std::function<
void(
const rclcpp::SubscriptionBase::SharedPtr &)> & sub_func,
73 const std::function<
void(
const rclcpp::ServiceBase::SharedPtr &)> & service_func,
74 const std::function<
void(
const rclcpp::ClientBase::SharedPtr &)> & client_func,
75 const std::function<
void(
const rclcpp::TimerBase::SharedPtr &)> & timer_func,
76 const std::function<
void(
const rclcpp::Waitable::SharedPtr &)> & waitable_func)
const
78 std::lock_guard<std::mutex> lock(mutex_);
80 for (
const rclcpp::SubscriptionBase::WeakPtr & weak_ptr : subscription_ptrs_) {
81 rclcpp::SubscriptionBase::SharedPtr ref_ptr = weak_ptr.lock();
87 for (
const rclcpp::ServiceBase::WeakPtr & weak_ptr : service_ptrs_) {
88 rclcpp::ServiceBase::SharedPtr ref_ptr = weak_ptr.lock();
90 service_func(ref_ptr);
94 for (
const rclcpp::ClientBase::WeakPtr & weak_ptr : client_ptrs_) {
95 rclcpp::ClientBase::SharedPtr ref_ptr = weak_ptr.lock();
101 for (
const rclcpp::TimerBase::WeakPtr & weak_ptr : timer_ptrs_) {
102 rclcpp::TimerBase::SharedPtr ref_ptr = weak_ptr.lock();
108 for (
const rclcpp::Waitable::WeakPtr & weak_ptr : waitable_ptrs_) {
109 rclcpp::Waitable::SharedPtr ref_ptr = weak_ptr.lock();
111 waitable_func(ref_ptr);
119 return associated_with_executor_;
125 return automatically_add_to_executor_with_node_;
128 rclcpp::GuardCondition::SharedPtr
131 std::lock_guard<std::recursive_mutex> lock(notify_guard_condition_mutex_);
132 rclcpp::Context::SharedPtr context_ptr = context_.lock();
133 if (context_ptr && context_ptr->is_valid()) {
134 if (!notify_guard_condition_) {
135 notify_guard_condition_ = std::make_shared<rclcpp::GuardCondition>(context_ptr);
137 return notify_guard_condition_;
145 std::lock_guard<std::recursive_mutex> lock(notify_guard_condition_mutex_);
146 if (notify_guard_condition_) {
147 notify_guard_condition_->trigger();
152 CallbackGroup::add_subscription(
153 const rclcpp::SubscriptionBase::SharedPtr & subscription_ptr)
155 std::lock_guard<std::mutex> lock(mutex_);
156 subscription_ptrs_.push_back(subscription_ptr);
157 subscription_ptrs_.erase(
159 subscription_ptrs_.begin(),
160 subscription_ptrs_.end(),
161 [](
const rclcpp::SubscriptionBase::WeakPtr & x) {return x.expired();}),
162 subscription_ptrs_.end());
166 CallbackGroup::add_timer(
const rclcpp::TimerBase::SharedPtr & timer_ptr)
168 std::lock_guard<std::mutex> lock(mutex_);
169 timer_ptrs_.push_back(timer_ptr);
174 [](
const rclcpp::TimerBase::WeakPtr & x) {return x.expired();}),
179 CallbackGroup::add_service(
const rclcpp::ServiceBase::SharedPtr & service_ptr)
181 std::lock_guard<std::mutex> lock(mutex_);
182 service_ptrs_.push_back(service_ptr);
185 service_ptrs_.begin(),
187 [](
const rclcpp::ServiceBase::WeakPtr & x) {return x.expired();}),
188 service_ptrs_.end());
192 CallbackGroup::add_client(
const rclcpp::ClientBase::SharedPtr & client_ptr)
194 std::lock_guard<std::mutex> lock(mutex_);
195 client_ptrs_.push_back(client_ptr);
198 client_ptrs_.begin(),
200 [](
const rclcpp::ClientBase::WeakPtr & x) {return x.expired();}),
205 CallbackGroup::add_waitable(
const rclcpp::Waitable::SharedPtr & waitable_ptr)
207 std::lock_guard<std::mutex> lock(mutex_);
208 waitable_ptrs_.push_back(waitable_ptr);
209 waitable_ptrs_.erase(
211 waitable_ptrs_.begin(),
212 waitable_ptrs_.end(),
213 [](
const rclcpp::Waitable::WeakPtr & x) {return x.expired();}),
214 waitable_ptrs_.end());
218 CallbackGroup::remove_waitable(
const rclcpp::Waitable::SharedPtr & waitable_ptr) noexcept
220 std::lock_guard<std::mutex> lock(mutex_);
221 for (
auto iter = waitable_ptrs_.begin(); iter != waitable_ptrs_.end(); ++iter) {
222 const auto shared_ptr = iter->lock();
223 if (shared_ptr.get() == waitable_ptr.get()) {
224 waitable_ptrs_.erase(iter);
RCLCPP_PUBLIC ~CallbackGroup()
Default destructor.
RCLCPP_PUBLIC rclcpp::GuardCondition::SharedPtr get_notify_guard_condition()
Retrieve the guard condition used to signal changes to this callback group.
RCLCPP_PUBLIC std::atomic_bool & get_associated_with_executor_atomic()
Return a reference to the 'associated with executor' atomic boolean.
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 const CallbackGroupType & type() const
Get the group type.
RCLCPP_PUBLIC std::atomic_bool & can_be_taken_from()
Return a reference to the 'can be taken' atomic boolean.
RCLCPP_PUBLIC void collect_all_ptrs(const std::function< void(const rclcpp::SubscriptionBase::SharedPtr &)> &sub_func, const std::function< void(const rclcpp::ServiceBase::SharedPtr &)> &service_func, const std::function< void(const rclcpp::ClientBase::SharedPtr &)> &client_func, const std::function< void(const rclcpp::TimerBase::SharedPtr &)> &timer_func, const std::function< void(const rclcpp::Waitable::SharedPtr &)> &waitable_func) const
Collect all of the entity pointers contained in this callback group.
RCLCPP_PUBLIC size_t size() const
Get the total number of entities in this callback group.