ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
ROS 2 C++ Client Library with ROS Client Library
Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
rclcpp::CallbackGroup Class Reference

Public Member Functions

RCLCPP_PUBLIC CallbackGroup (CallbackGroupType group_type, rclcpp::Context::WeakPtr context, bool automatically_add_to_executor_with_node=true)
 Constructor for CallbackGroup. More...
 
RCLCPP_PUBLIC ~CallbackGroup ()
 Default destructor.
 
template<typename Function >
rclcpp::SubscriptionBase::SharedPtr find_subscription_ptrs_if (Function func) const
 
template<typename Function >
rclcpp::TimerBase::SharedPtr find_timer_ptrs_if (Function func) const
 
template<typename Function >
rclcpp::ServiceBase::SharedPtr find_service_ptrs_if (Function func) const
 
template<typename Function >
rclcpp::ClientBase::SharedPtr find_client_ptrs_if (Function func) const
 
template<typename Function >
rclcpp::Waitable::SharedPtr find_waitable_ptrs_if (Function func) const
 
RCLCPP_PUBLIC size_t size () const
 Get the total number of entities in this callback group. More...
 
RCLCPP_PUBLIC std::atomic_bool & can_be_taken_from ()
 Return a reference to the 'can be taken' atomic boolean. More...
 
RCLCPP_PUBLIC const CallbackGroupType & type () const
 Get the group type. More...
 
RCLCPP_PUBLIC void collect_all_ptrs (std::function< void(const rclcpp::SubscriptionBase::SharedPtr &)> sub_func, std::function< void(const rclcpp::ServiceBase::SharedPtr &)> service_func, std::function< void(const rclcpp::ClientBase::SharedPtr &)> client_func, std::function< void(const rclcpp::TimerBase::SharedPtr &)> timer_func, std::function< void(const rclcpp::Waitable::SharedPtr &)> waitable_func) const
 Collect all of the entity pointers contained in this callback group. More...
 
RCLCPP_PUBLIC std::atomic_bool & get_associated_with_executor_atomic ()
 Return a reference to the 'associated with executor' atomic boolean. More...
 
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. More...
 
RCLCPP_PUBLIC rclcpp::GuardCondition::SharedPtr get_notify_guard_condition ()
 Retrieve the guard condition used to signal changes to this callback group. More...
 
RCLCPP_PUBLIC void trigger_notify_guard_condition ()
 Trigger the notify guard condition.
 

Protected Member Functions

RCLCPP_PUBLIC void add_publisher (const rclcpp::PublisherBase::SharedPtr publisher_ptr)
 
RCLCPP_PUBLIC void add_subscription (const rclcpp::SubscriptionBase::SharedPtr subscription_ptr)
 
RCLCPP_PUBLIC void add_timer (const rclcpp::TimerBase::SharedPtr timer_ptr)
 
RCLCPP_PUBLIC void add_service (const rclcpp::ServiceBase::SharedPtr service_ptr)
 
RCLCPP_PUBLIC void add_client (const rclcpp::ClientBase::SharedPtr client_ptr)
 
RCLCPP_PUBLIC void add_waitable (const rclcpp::Waitable::SharedPtr waitable_ptr)
 
RCLCPP_PUBLIC void remove_waitable (const rclcpp::Waitable::SharedPtr waitable_ptr) noexcept
 

Protected Attributes

CallbackGroupType type_
 
std::mutex mutex_
 
std::atomic_bool associated_with_executor_
 
std::vector< rclcpp::SubscriptionBase::WeakPtr > subscription_ptrs_
 
std::vector< rclcpp::TimerBase::WeakPtr > timer_ptrs_
 
std::vector< rclcpp::ServiceBase::WeakPtr > service_ptrs_
 
std::vector< rclcpp::ClientBase::WeakPtr > client_ptrs_
 
std::vector< rclcpp::Waitable::WeakPtr > waitable_ptrs_
 
std::atomic_bool can_be_taken_from_
 
const bool automatically_add_to_executor_with_node_
 
std::shared_ptr< rclcpp::GuardConditionnotify_guard_condition_ = nullptr
 
std::recursive_mutex notify_guard_condition_mutex_
 
rclcpp::Context::WeakPtr context_
 

Friends

class rclcpp::node_interfaces::NodeServices
 
class rclcpp::node_interfaces::NodeTimers
 
class rclcpp::node_interfaces::NodeTopics
 
class rclcpp::node_interfaces::NodeWaitables
 

Detailed Description

Definition at line 52 of file callback_group.hpp.

Constructor & Destructor Documentation

◆ CallbackGroup()

CallbackGroup::CallbackGroup ( CallbackGroupType  group_type,
rclcpp::Context::WeakPtr  context,
bool  automatically_add_to_executor_with_node = true 
)
explicit

Constructor for CallbackGroup.

Callback Groups have a type, either 'Mutually Exclusive' or 'Reentrant' and when creating one the type must be specified.

Callbacks in Reentrant Callback Groups must be able to:

  • run at the same time as themselves (reentrant)
  • run at the same time as other callbacks in their group
  • run at the same time as other callbacks in other groups

Callbacks in Mutually Exclusive Callback Groups:

  • will not be run multiple times simultaneously (non-reentrant)
  • will not be run at the same time as other callbacks in their group
  • but must run at the same time as callbacks in other groups

Additionally, callback groups have a property which determines whether or not they are added to an executor with their associated node automatically. When creating a callback group the automatically_add_to_executor_with_node argument determines this behavior, and if true it will cause the newly created callback group to be added to an executor with the node when the Executor::add_node method is used. If false, this callback group will not be added automatically and would have to be added to an executor manually using the Executor::add_callback_group method.

Whether the node was added to the executor before creating the callback group, or after, is irrelevant; the callback group will be automatically added to the executor in either case.

Parameters
[in]group_typeThe type of the callback group.
[in]contextA weak pointer to the context associated with this callback group.
[in]automatically_add_to_executor_with_nodeA boolean that determines whether a callback group is automatically added to an executor with the node with which it is associated.

Definition at line 32 of file callback_group.cpp.

Member Function Documentation

◆ automatically_add_to_executor_with_node()

bool CallbackGroup::automatically_add_to_executor_with_node ( ) const

Return true if this callback group should be automatically added to an executor by the node.

Returns
boolean true if this callback group should be automatically added to an executor when the associated node is added, otherwise false.

Definition at line 122 of file callback_group.cpp.

◆ can_be_taken_from()

std::atomic_bool & CallbackGroup::can_be_taken_from ( )

Return a reference to the 'can be taken' atomic boolean.

The resulting bool will be true in the case that no executor is currently using an executable entity from this group. The resulting bool will be false in the case that an executor is currently using an executable entity from this group, and the group policy doesn't allow a second take (eg mutual exclusion)

Returns
a reference to the flag

Definition at line 48 of file callback_group.cpp.

◆ collect_all_ptrs()

void CallbackGroup::collect_all_ptrs ( std::function< void(const rclcpp::SubscriptionBase::SharedPtr &)>  sub_func,
std::function< void(const rclcpp::ServiceBase::SharedPtr &)>  service_func,
std::function< void(const rclcpp::ClientBase::SharedPtr &)>  client_func,
std::function< void(const rclcpp::TimerBase::SharedPtr &)>  timer_func,
std::function< void(const rclcpp::Waitable::SharedPtr &)>  waitable_func 
) const

Collect all of the entity pointers contained in this callback group.

Parameters
[in]sub_funcFunction to execute for each subscription
[in]service_funcFunction to execute for each service
[in]client_funcFunction to execute for each client
[in]timer_funcFunction to execute for each timer
[in]waitable_fuincFunction to execute for each waitable

Definition at line 70 of file callback_group.cpp.

◆ get_associated_with_executor_atomic()

std::atomic_bool & CallbackGroup::get_associated_with_executor_atomic ( )

Return a reference to the 'associated with executor' atomic boolean.

When a callback group is added to an executor this boolean is checked to ensure it has not already been added to another executor. If it has not been, then this boolean is set to true to indicate it is now associated with an executor.

When the callback group is removed from the executor, this atomic boolean is set back to false.

Returns
the 'associated with executor' atomic boolean

Definition at line 116 of file callback_group.cpp.

◆ get_notify_guard_condition()

rclcpp::GuardCondition::SharedPtr CallbackGroup::get_notify_guard_condition ( )

Retrieve the guard condition used to signal changes to this callback group.

Returns
guard condition if it is valid, otherwise nullptr.

Definition at line 128 of file callback_group.cpp.

◆ size()

size_t CallbackGroup::size ( ) const

Get the total number of entities in this callback group.

Returns
the number of entities in the callback group.

Definition at line 60 of file callback_group.cpp.

◆ type()

const CallbackGroupType & CallbackGroup::type ( ) const

Get the group type.

Returns
the group type

Definition at line 54 of file callback_group.cpp.


The documentation for this class was generated from the following files: