From 54054f675c9d25f79103cf9ff90210d7be72d4c4 Mon Sep 17 00:00:00 2001 From: Alberto Soragna Date: Mon, 12 Oct 2020 13:19:55 +0000 Subject: [PATCH 1/2] add missing predicate to event_queue_cv wait --- .../src/rclcpp/executors/events_executor.cpp | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/rclcpp/src/rclcpp/executors/events_executor.cpp b/rclcpp/src/rclcpp/executors/events_executor.cpp index 9bf592130c..bc47db6e94 100644 --- a/rclcpp/src/rclcpp/executors/events_executor.cpp +++ b/rclcpp/src/rclcpp/executors/events_executor.cpp @@ -81,7 +81,7 @@ EventsExecutor::spin() RCLCPP_SCOPE_EXIT(this->spinning.store(false);); // When condition variable is notified, check this predicate to proceed - auto predicate = [this]() { return !event_queue_.empty(); }; + auto has_event_predicate = [this]() { return !event_queue_.empty(); }; // Local event queue std::queue local_event_queue; @@ -93,7 +93,7 @@ EventsExecutor::spin() { std::unique_lock lock(event_queue_mutex_); // We wait here until something has been pushed to the event queue - event_queue_cv_.wait(lock, predicate); + event_queue_cv_.wait(lock, has_event_predicate); // We got an event! Swap queues and execute events std::swap(local_event_queue, event_queue_); @@ -118,18 +118,21 @@ EventsExecutor::spin_some(std::chrono::nanoseconds max_duration) // - A timer triggers // - An executor event is received and processed + // When condition variable is notified, check this predicate to proceed + auto has_event_predicate = [this]() { return !event_queue_.empty(); }; + + std::queue local_event_queue; + // Select the smallest between input max_duration and timer timeout auto next_timer_timeout = timers_manager_->get_head_timeout(); if (next_timer_timeout < max_duration) { max_duration = next_timer_timeout; } - std::queue local_event_queue; - { // Wait until timeout or event std::unique_lock lock(event_queue_mutex_); - event_queue_cv_.wait_for(lock, max_duration); + event_queue_cv_.wait_for(lock, max_duration, has_event_predicate); std::swap(local_event_queue, event_queue_); } @@ -149,6 +152,9 @@ EventsExecutor::spin_all(std::chrono::nanoseconds max_duration) } RCLCPP_SCOPE_EXIT(this->spinning.store(false);); + // When condition variable is notified, check this predicate to proceed + auto has_event_predicate = [this]() { return !event_queue_.empty(); }; + std::queue local_event_queue; auto start = std::chrono::steady_clock::now(); @@ -167,7 +173,7 @@ EventsExecutor::spin_all(std::chrono::nanoseconds max_duration) { // Wait until timeout or event std::unique_lock lock(event_queue_mutex_); - event_queue_cv_.wait_for(lock, max_duration); + event_queue_cv_.wait_for(lock, max_duration, has_event_predicate); } // Keep executing until work available or timeout expired @@ -204,13 +210,16 @@ EventsExecutor::spin_once_impl(std::chrono::nanoseconds timeout) timeout = next_timer_timeout; } + // When condition variable is notified, check this predicate to proceed + auto has_event_predicate = [this]() { return !event_queue_.empty(); }; + EventQ event; bool has_event = false; { // Wait until timeout or event arrives std::unique_lock lock(event_queue_mutex_); - event_queue_cv_.wait_for(lock, timeout); + event_queue_cv_.wait_for(lock, timeout, has_event_predicate); // Grab first event from queue if it exists has_event = !event_queue_.empty(); From 78ac3242b4a576a408e527f4517e5444f05510d6 Mon Sep 17 00:00:00 2001 From: Alberto Soragna Date: Mon, 12 Oct 2020 15:09:04 +0100 Subject: [PATCH 2/2] Update events_executor.cpp Fix code style errors --- rclcpp/src/rclcpp/executors/events_executor.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rclcpp/src/rclcpp/executors/events_executor.cpp b/rclcpp/src/rclcpp/executors/events_executor.cpp index bc47db6e94..b49d97a757 100644 --- a/rclcpp/src/rclcpp/executors/events_executor.cpp +++ b/rclcpp/src/rclcpp/executors/events_executor.cpp @@ -81,7 +81,7 @@ EventsExecutor::spin() RCLCPP_SCOPE_EXIT(this->spinning.store(false);); // When condition variable is notified, check this predicate to proceed - auto has_event_predicate = [this]() { return !event_queue_.empty(); }; + auto has_event_predicate = [this]() {return !event_queue_.empty();}; // Local event queue std::queue local_event_queue; @@ -119,7 +119,7 @@ EventsExecutor::spin_some(std::chrono::nanoseconds max_duration) // - An executor event is received and processed // When condition variable is notified, check this predicate to proceed - auto has_event_predicate = [this]() { return !event_queue_.empty(); }; + auto has_event_predicate = [this]() {return !event_queue_.empty();}; std::queue local_event_queue; @@ -153,7 +153,7 @@ EventsExecutor::spin_all(std::chrono::nanoseconds max_duration) RCLCPP_SCOPE_EXIT(this->spinning.store(false);); // When condition variable is notified, check this predicate to proceed - auto has_event_predicate = [this]() { return !event_queue_.empty(); }; + auto has_event_predicate = [this]() {return !event_queue_.empty();}; std::queue local_event_queue; @@ -211,7 +211,7 @@ EventsExecutor::spin_once_impl(std::chrono::nanoseconds timeout) } // When condition variable is notified, check this predicate to proceed - auto has_event_predicate = [this]() { return !event_queue_.empty(); }; + auto has_event_predicate = [this]() {return !event_queue_.empty();}; EventQ event; bool has_event = false;