Skip to content

Commit 41c8f8a

Browse files
author
Mauro Passerino
committed
EntitiesCollector GCs to push single event
Signed-off-by: Mauro Passerino <[email protected]>
1 parent 7cadae3 commit 41c8f8a

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

rclcpp/include/rclcpp/guard_condition.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class GuardCondition
102102

103103
RCLCPP_PUBLIC
104104
void
105-
set_on_trigger_callback(std::function<void(size_t, int)> callback);
105+
set_on_trigger_callback(std::function<void(size_t)> callback);
106106

107107
protected:
108108
rclcpp::Context::SharedPtr context_;

rclcpp/src/rclcpp/executors/events_executor_entities_collector.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,21 @@ void
478478
EventsExecutorEntitiesCollector::set_guard_condition_callback(
479479
rclcpp::GuardCondition * guard_condition)
480480
{
481-
guard_condition->set_on_trigger_callback(create_waitable_callback(this));
481+
auto gc_callback = [this](size_t num_events) {
482+
// Override num events (we don't care more than a single event)
483+
num_events = 1;
484+
int gc_id = -1;
485+
ExecutorEvent event = {this, gc_id, WAITABLE_EVENT, num_events};
486+
// Event queue mutex scope
487+
{
488+
std::unique_lock<std::mutex> lock(associated_executor_->push_mutex_);
489+
associated_executor_->events_queue_->push(event);
490+
}
491+
// Notify that the event queue has some events in it.
492+
associated_executor_->events_queue_cv_.notify_one();
493+
};
494+
495+
guard_condition->set_on_trigger_callback(gc_callback);
482496
}
483497

484498
void
@@ -569,7 +583,8 @@ EventsExecutorEntitiesCollector::add_waitable(rclcpp::Waitable::SharedPtr waitab
569583
{
570584
weak_waitables_map_.emplace(waitable.get(), waitable);
571585

572-
waitable->set_listener_callback([] (size_t, int){});
586+
waitable->set_listener_callback(
587+
create_waitable_callback(waitable.get()));
573588
}
574589

575590
std::function<void(size_t)>

rclcpp/src/rclcpp/guard_condition.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,18 @@ GuardCondition::add_to_wait_set(rcl_wait_set_t * wait_set) const
9696
}
9797

9898
void
99-
GuardCondition::set_on_trigger_callback(std::function<void(size_t, int)> callback)
99+
GuardCondition::set_on_trigger_callback(std::function<void(size_t)> callback)
100100
{
101-
on_trigger_callback_ = std::bind(callback, std::placeholders::_1, -1);
101+
if (callback) {
102+
on_trigger_callback_ = callback;
102103

103-
if (unread_count_) {
104-
on_trigger_callback_(unread_count_);
105-
unread_count_ = 0;
104+
if (unread_count_) {
105+
callback(unread_count_);
106+
unread_count_ = 0;
107+
}
108+
return;
106109
}
110+
111+
on_trigger_callback_ = nullptr;
107112
}
108113
} // namespace rclcpp

rclcpp/src/rclcpp/subscription_intra_process_base.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,18 @@ SubscriptionIntraProcessBase::get_actual_qos() const
4343
void
4444
SubscriptionIntraProcessBase::set_listener_callback(std::function<void(size_t, int)> callback)
4545
{
46-
on_new_message_callback_ = std::bind(callback, std::placeholders::_1, -1);
47-
48-
if (unread_count_ && on_new_message_callback_) {
49-
on_new_message_callback_(unread_count_);
50-
unread_count_ = 0;
46+
if (callback) {
47+
on_new_message_callback_ = std::bind(callback, std::placeholders::_1, -1);
48+
if (unread_count_) {
49+
if (unread_count_ < qos_profile_.depth) {
50+
on_new_message_callback_(unread_count_);
51+
} else {
52+
on_new_message_callback_(qos_profile_.depth);
53+
}
54+
unread_count_ = 0;
55+
}
56+
return;
5157
}
58+
59+
on_new_message_callback_ = nullptr;
5260
}

0 commit comments

Comments
 (0)