From fb24401f5db4052ebf641a1de56759a7c46e0574 Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Thu, 20 Jun 2024 21:29:06 +0000 Subject: [PATCH] Fix checking of the guard condition. Previously, when we were checking if a guard condition was triggered, we would actually check and reset it. But that means that when we later on went to set the appropriate things in the wait_set, we wouldn't actually set the guard_condition true, since it was false by that point. Instead, add in two separate methods; the one that just returns whether it is triggered, which we call when checking, and the one that checks and resets, which we call when setting it in the wait_set. Signed-off-by: Chris Lalancette --- rmw_zenoh_cpp/src/detail/guard_condition.cpp | 9 +++++++-- rmw_zenoh_cpp/src/detail/guard_condition.hpp | 2 ++ rmw_zenoh_cpp/src/rmw_zenoh.cpp | 4 +--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/rmw_zenoh_cpp/src/detail/guard_condition.cpp b/rmw_zenoh_cpp/src/detail/guard_condition.cpp index feb6ea48..76f1c1f8 100644 --- a/rmw_zenoh_cpp/src/detail/guard_condition.cpp +++ b/rmw_zenoh_cpp/src/detail/guard_condition.cpp @@ -53,16 +53,21 @@ void GuardCondition::detach_condition() condition_variable_ = nullptr; } +bool GuardCondition::has_triggered() const +{ + std::lock_guard lock(internal_mutex_); + return has_triggered_; +} + ///============================================================================= bool GuardCondition::get_and_reset_trigger() { std::lock_guard lock(internal_mutex_); bool ret = has_triggered_; - // There is no data associated with the guard condition, so as soon as the callers asks about the - // state, we can immediately reset and get ready for the next trigger. has_triggered_ = false; return ret; } + } // namespace rmw_zenoh_cpp diff --git a/rmw_zenoh_cpp/src/detail/guard_condition.hpp b/rmw_zenoh_cpp/src/detail/guard_condition.hpp index 07e1e6b0..9adb737e 100644 --- a/rmw_zenoh_cpp/src/detail/guard_condition.hpp +++ b/rmw_zenoh_cpp/src/detail/guard_condition.hpp @@ -35,6 +35,8 @@ class GuardCondition final void detach_condition(); + bool has_triggered() const; + bool get_and_reset_trigger(); private: diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index 87aebac8..7ea0dea2 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -3143,14 +3143,12 @@ static bool has_triggered_condition( rmw_clients_t * clients, rmw_events_t * events) { - static_cast(events); - if (guard_conditions) { for (size_t i = 0; i < guard_conditions->guard_condition_count; ++i) { rmw_zenoh_cpp::GuardCondition * gc = static_cast(guard_conditions->guard_conditions[i]); if (gc != nullptr) { - if (gc->get_and_reset_trigger()) { + if (gc->has_triggered()) { return true; } }