17 #include "rclcpp/guard_condition.hpp"
19 #include "rclcpp/exceptions.hpp"
20 #include "rclcpp/logging.hpp"
26 const rclcpp::Context::SharedPtr & context,
31 throw std::invalid_argument(
"context argument unexpectedly nullptr");
35 &this->rcl_guard_condition_,
36 context->get_rcl_context().get(),
37 guard_condition_options);
39 rclcpp::exceptions::throw_from_rcl_error(ret,
"failed to create guard condition");
43 GuardCondition::~GuardCondition()
48 rclcpp::exceptions::throw_from_rcl_error(ret);
49 }
catch (
const std::exception & exception) {
52 "failed to finalize guard condition: %s", exception.what());
60 return rcl_guard_condition_;
66 return rcl_guard_condition_;
74 rclcpp::exceptions::throw_from_rcl_error(ret);
78 std::lock_guard<std::recursive_mutex> lock(reentrant_mutex_);
80 if (on_trigger_callback_) {
81 on_trigger_callback_(1);
91 return in_use_by_wait_set_.exchange(in_use_state);
97 std::lock_guard<std::recursive_mutex> lock(reentrant_mutex_);
100 if (&wait_set != wait_set_) {
101 throw std::runtime_error(
"guard condition has already been added to a wait set.");
104 wait_set_ = &wait_set;
109 rclcpp::exceptions::throw_from_rcl_error(
110 ret,
"failed to add guard condition to wait set");
117 std::lock_guard<std::recursive_mutex> lock(reentrant_mutex_);
120 on_trigger_callback_ = callback;
123 callback(unread_count_);
127 on_trigger_callback_ =
nullptr;
RCLCPP_PUBLIC void trigger()
Signal that the condition has been met, notifying both the wait set and listeners,...
RCLCPP_PUBLIC GuardCondition(const rclcpp::Context::SharedPtr &context=rclcpp::contexts::get_global_default_context(), rcl_guard_condition_options_t guard_condition_options=rcl_guard_condition_get_default_options())
Construct the guard condition, optionally specifying which Context to use.
RCLCPP_PUBLIC rcl_guard_condition_t & get_rcl_guard_condition()
Return the underlying rcl guard condition structure.
RCLCPP_PUBLIC void set_on_trigger_callback(std::function< void(size_t)> callback)
Set a callback to be called whenever the guard condition is triggered.
RCLCPP_PUBLIC void add_to_wait_set(rcl_wait_set_t &wait_set)
Adds the guard condition to a waitset.
RCLCPP_PUBLIC bool exchange_in_use_by_wait_set_state(bool in_use_state)
Exchange the "in use by wait set" state for this guard condition.
RCL_PUBLIC RCL_WARN_UNUSED rcl_guard_condition_t rcl_get_zero_initialized_guard_condition(void)
Return a rcl_guard_condition_t struct with members set to NULL.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_trigger_guard_condition(rcl_guard_condition_t *guard_condition)
Trigger a rcl guard condition.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_guard_condition_init(rcl_guard_condition_t *guard_condition, rcl_context_t *context, const rcl_guard_condition_options_t options)
Initialize a rcl guard_condition.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_guard_condition_fini(rcl_guard_condition_t *guard_condition)
Finalize a rcl_guard_condition_t.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC Logger get_logger(const std::string &name)
Return a named logger.
Options available for a rcl guard condition.
Handle for a rcl guard condition.
Container for subscription's, guard condition's, etc to be waited on.
#define RCL_RET_OK
Success return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_wait_set_add_guard_condition(rcl_wait_set_t *wait_set, const rcl_guard_condition_t *guard_condition, size_t *index)
Store a pointer to the guard condition in the next empty spot in the set.