Skip to content

Commit 4b41d07

Browse files
Josh Finkenapojomovsky
authored andcommitted
Get in the upstream executor_notify_waitable
Get the upstream executor_notify_waitable.cpp/.hpp and the test_executors_timer_cancel_behavior unit-test
1 parent ad08ee9 commit 4b41d07

File tree

4 files changed

+506
-11
lines changed

4 files changed

+506
-11
lines changed

rclcpp/include/rclcpp/executors/executor_notify_waitable.hpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ class ExecutorNotifyWaitable : public rclcpp::Waitable
8888
std::shared_ptr<void>
8989
take_data() override;
9090

91+
/// Take the data from an entity ID so that it can be consumed with `execute`.
92+
/**
93+
* \param[in] id ID of the entity to take data from.
94+
* \return If available, data to be used, otherwise nullptr
95+
* \sa rclcpp::Waitable::take_data_by_entity_id
96+
*/
97+
RCLCPP_PUBLIC
98+
std::shared_ptr<void>
99+
take_data_by_entity_id(size_t id) override;
100+
101+
/// Set a callback to be called whenever the waitable becomes ready.
102+
/**
103+
* \param[in] callback callback to set
104+
* \sa rclcpp::Waitable::set_on_ready_callback
105+
*/
106+
RCLCPP_PUBLIC
107+
void
108+
set_on_ready_callback(std::function<void(size_t, int)> callback) override;
109+
91110
/// Add a guard condition to be waited on.
92111
/**
93112
* \param[in] guard_condition The guard condition to add.
@@ -96,13 +115,21 @@ class ExecutorNotifyWaitable : public rclcpp::Waitable
96115
void
97116
add_guard_condition(rclcpp::GuardCondition::WeakPtr guard_condition);
98117

118+
/// Unset any callback registered via set_on_ready_callback.
119+
/**
120+
* \sa rclcpp::Waitable::clear_on_ready_callback
121+
*/
122+
RCLCPP_PUBLIC
123+
void
124+
clear_on_ready_callback() override;
125+
99126
/// Remove a guard condition from being waited on.
100127
/**
101-
* \param[in] guard_condition The guard condition to remove.
128+
* \param[in] weak_guard_condition The guard condition to remove.
102129
*/
103130
RCLCPP_PUBLIC
104131
void
105-
remove_guard_condition(rclcpp::GuardCondition::WeakPtr guard_condition);
132+
remove_guard_condition(rclcpp::GuardCondition::WeakPtr weak_guard_condition);
106133

107134
/// Get the number of ready guard_conditions
108135
/**
@@ -118,6 +145,8 @@ class ExecutorNotifyWaitable : public rclcpp::Waitable
118145

119146
std::mutex guard_condition_mutex_;
120147

148+
std::function<void(size_t)> on_ready_callback_;
149+
121150
/// The collection of guard conditions to be waited on.
122151
std::set<rclcpp::GuardCondition::WeakPtr,
123152
std::owner_less<rclcpp::GuardCondition::WeakPtr>> notify_guard_conditions_;

rclcpp/src/rclcpp/executors/executor_notify_waitable.cpp

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,76 @@ ExecutorNotifyWaitable::take_data()
9999
return nullptr;
100100
}
101101

102+
std::shared_ptr<void>
103+
ExecutorNotifyWaitable::take_data_by_entity_id(size_t id)
104+
{
105+
(void) id;
106+
return nullptr;
107+
}
108+
109+
void
110+
ExecutorNotifyWaitable::set_on_ready_callback(std::function<void(size_t, int)> callback)
111+
{
112+
// The second argument of the callback could be used to identify which guard condition
113+
// triggered the event.
114+
// We could indicate which of the guard conditions was triggered, but the executor
115+
// is already going to check that.
116+
auto gc_callback = [callback](size_t count) {
117+
callback(count, 0);
118+
};
119+
120+
std::lock_guard<std::mutex> lock(guard_condition_mutex_);
121+
122+
on_ready_callback_ = gc_callback;
123+
for (auto weak_gc : notify_guard_conditions_) {
124+
auto gc = weak_gc.lock();
125+
if (!gc) {
126+
continue;
127+
}
128+
gc->set_on_trigger_callback(on_ready_callback_);
129+
}
130+
}
131+
132+
RCLCPP_PUBLIC
133+
void
134+
ExecutorNotifyWaitable::clear_on_ready_callback()
135+
{
136+
std::lock_guard<std::mutex> lock(guard_condition_mutex_);
137+
138+
on_ready_callback_ = nullptr;
139+
for (auto weak_gc : notify_guard_conditions_) {
140+
auto gc = weak_gc.lock();
141+
if (!gc) {
142+
continue;
143+
}
144+
gc->set_on_trigger_callback(nullptr);
145+
}
146+
}
147+
102148
void
103149
ExecutorNotifyWaitable::add_guard_condition(rclcpp::GuardCondition::WeakPtr weak_guard_condition)
104150
{
105151
std::lock_guard<std::mutex> lock(guard_condition_mutex_);
106152
auto guard_condition = weak_guard_condition.lock();
107153
if (guard_condition && notify_guard_conditions_.count(weak_guard_condition) == 0) {
108154
notify_guard_conditions_.insert(weak_guard_condition);
155+
if (on_ready_callback_) {
156+
guard_condition->set_on_trigger_callback(on_ready_callback_);
157+
}
109158
}
110159
}
111160

112161
void
113-
ExecutorNotifyWaitable::remove_guard_condition(rclcpp::GuardCondition::WeakPtr guard_condition)
162+
ExecutorNotifyWaitable::remove_guard_condition(rclcpp::GuardCondition::WeakPtr weak_guard_condition)
114163
{
115164
std::lock_guard<std::mutex> lock(guard_condition_mutex_);
116-
if (notify_guard_conditions_.count(guard_condition) != 0) {
117-
notify_guard_conditions_.erase(guard_condition);
165+
if (notify_guard_conditions_.count(weak_guard_condition) != 0) {
166+
notify_guard_conditions_.erase(weak_guard_condition);
167+
auto guard_condition = weak_guard_condition.lock();
168+
// If this notify waitable doesn't have an on_ready_callback, then there's nothing to unset
169+
if (guard_condition && on_ready_callback_) {
170+
guard_condition->set_on_trigger_callback(nullptr);
171+
}
118172
}
119173
}
120174

rclcpp/test/rclcpp/CMakeLists.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -628,18 +628,22 @@ if(TARGET test_interface_traits)
628628
target_link_libraries(test_interface_traits ${PROJECT_NAME})
629629
endif()
630630

631-
# TODO(brawner) remove when destroying Node for Connext is resolved. See:
632-
# https://github.com/ros2/rclcpp/issues/1250
633631
ament_add_gtest(
634632
test_executors
635633
executors/test_executors.cpp
636634
APPEND_LIBRARY_DIRS "${append_library_dirs}"
637635
TIMEOUT 180)
638636
if(TARGET test_executors)
639-
ament_target_dependencies(test_executors
640-
"rcl"
641-
"test_msgs")
642-
target_link_libraries(test_executors ${PROJECT_NAME})
637+
target_link_libraries(test_executors ${PROJECT_NAME} rcl::rcl ${test_msgs_TARGETS})
638+
endif()
639+
640+
ament_add_gtest(
641+
test_executors_timer_cancel_behavior
642+
executors/test_executors_timer_cancel_behavior.cpp
643+
APPEND_LIBRARY_DIRS "${append_library_dirs}"
644+
TIMEOUT 180)
645+
if(TARGET test_executors)
646+
target_link_libraries(test_executors_timer_cancel_behavior ${PROJECT_NAME} ${rosgraph_msgs_TARGETS})
643647
endif()
644648

645649
ament_add_gtest(test_static_single_threaded_executor executors/test_static_single_threaded_executor.cpp

0 commit comments

Comments
 (0)