Skip to content

Commit

Permalink
add functions for setting/unsetting guard conditions
Browse files Browse the repository at this point in the history
Signed-off-by: Soragna, Alberto <alberto.soragna@gmail.com>
  • Loading branch information
alsora committed Oct 19, 2020
1 parent faf9d1e commit e9e237a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ class EventsExecutorEntitiesCollector final : public EventWaitable
void
unset_callback_group_entities_callbacks(rclcpp::CallbackGroup::SharedPtr group);

void
set_guard_condition_callback(const rcl_guard_condition_t * guard_condition);

void
unset_guard_condition_callback(const rcl_guard_condition_t * guard_condition);

/// Return true if the node belongs to the collector
/**
* \param[in] group_ptr a node base interface shared pointer
Expand Down
59 changes: 35 additions & 24 deletions rclcpp/src/rclcpp/executors/events_executor_entities_collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ EventsExecutorEntitiesCollector::~EventsExecutorEntitiesCollector()
auto node = pair.first.lock();
if (node) {
auto node_gc = pair.second;
rcl_ret_t ret = rcl_guard_condition_set_events_executor_callback(
nullptr, nullptr, nullptr,
node_gc,
false);

(void)ret; // Can't throw on destructors
unset_guard_condition_callback(node_gc);
}
}

Expand Down Expand Up @@ -131,16 +126,7 @@ EventsExecutorEntitiesCollector::add_callback_group(
if (is_new_node) {
// Set an event callback for the node's notify guard condition, so if new entities are added
// or removed to this node we will receive an event.
rcl_ret_t ret = rcl_guard_condition_set_events_executor_callback(
associated_executor_,
&EventsExecutor::push_event,
this,
node_ptr->get_notify_guard_condition(),
false /* Discard previous events */);

if (ret != RCL_RET_OK) {
throw std::runtime_error("Couldn't set node guard condition callback");
}
set_guard_condition_callback(node_ptr->get_notify_guard_condition());

// Store node's notify guard condition
rclcpp::node_interfaces::NodeBaseInterface::WeakPtr node_weak_ptr(node_ptr);
Expand Down Expand Up @@ -381,14 +367,7 @@ EventsExecutorEntitiesCollector::remove_callback_group_from_map(
// Node doesn't have more callback groups associated to the executor.
// Unset the event callback for the node's notify guard condition, to stop
// receiving events if entities are added or removed to this node.
rcl_ret_t ret = rcl_guard_condition_set_events_executor_callback(
nullptr, nullptr, nullptr,
node_ptr->get_notify_guard_condition(),
false);

if (ret != RCL_RET_OK) {
throw std::runtime_error("Couldn't set node guard condition callback");
}
unset_guard_condition_callback(node_ptr->get_notify_guard_condition());

// Remove guard condition from list
rclcpp::node_interfaces::NodeBaseInterface::WeakPtr weak_node_ptr(node_ptr);
Expand Down Expand Up @@ -498,3 +477,35 @@ EventsExecutorEntitiesCollector::get_automatically_added_callback_groups_from_no
}
return groups;
}

void
EventsExecutorEntitiesCollector::set_guard_condition_callback(
const rcl_guard_condition_t * guard_condition)
{
rcl_ret_t ret = rcl_guard_condition_set_events_executor_callback(
associated_executor_,
&EventsExecutor::push_event,
this,
guard_condition,
false /* Discard previous events */);

if (ret != RCL_RET_OK) {
throw std::runtime_error("Couldn't set guard condition event callback");
}
}

void
EventsExecutorEntitiesCollector::unset_guard_condition_callback(
const rcl_guard_condition_t * guard_condition)
{
rcl_ret_t ret = rcl_guard_condition_set_events_executor_callback(
nullptr,
nullptr,
nullptr,
guard_condition,
false /* Discard previous events */);

if (ret != RCL_RET_OK) {
throw std::runtime_error("Couldn't unset guard condition event callback");
}
}

0 comments on commit e9e237a

Please sign in to comment.