From e921b13e40ef3bbfe29baa530ae741dad9d74d56 Mon Sep 17 00:00:00 2001 From: Emerson Knapp Date: Wed, 31 Mar 2021 14:53:38 -0700 Subject: [PATCH] pause() and resume() instead of set_paused(bool) Signed-off-by: Emerson Knapp --- .../include/rosbag2_cpp/player_clock.hpp | 16 ++++++++---- rosbag2_cpp/src/rosbag2_cpp/player_clock.cpp | 25 +++++++++++++------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/rosbag2_cpp/include/rosbag2_cpp/player_clock.hpp b/rosbag2_cpp/include/rosbag2_cpp/player_clock.hpp index 992d047707..985c09d495 100644 --- a/rosbag2_cpp/include/rosbag2_cpp/player_clock.hpp +++ b/rosbag2_cpp/include/rosbag2_cpp/player_clock.hpp @@ -88,18 +88,24 @@ class PlayerClock double get_rate() const; /** - * Set the paused state of the clock. - * If currently in a `sleep_until`, this will wake the sleep to return false, - * so that the caller is not hanging forever. + * Stop the flow of time of the clock. + * If this changes the pause state, this will wake any waiting `sleep_until` */ ROSBAG2_CPP_PUBLIC - void set_paused(bool paused); + void pause(); + + /** + * Start the flow of time of the clock + * If this changes the pause state, this will wake any waiting `sleep_until` + */ + ROSBAG2_CPP_PUBLIC + void resume(); /** * Return whether the clock is currently paused. */ ROSBAG2_CPP_PUBLIC - bool get_paused() const; + bool is_paused() const; private: std::unique_ptr impl_; diff --git a/rosbag2_cpp/src/rosbag2_cpp/player_clock.cpp b/rosbag2_cpp/src/rosbag2_cpp/player_clock.cpp index 50f41a690f..70b92c15b1 100644 --- a/rosbag2_cpp/src/rosbag2_cpp/player_clock.cpp +++ b/rosbag2_cpp/src/rosbag2_cpp/player_clock.cpp @@ -115,26 +115,35 @@ double PlayerClock::get_rate() const return impl_->rate; } -void PlayerClock::set_paused(bool paused) +void PlayerClock::pause() { { std::lock_guard lock(impl_->mutex); - if (paused == impl_->paused) { + if (impl_->paused) { return; } // Note: needs to not be paused when taking snapshot, otherwise it will use last ros ref - if (!paused) { - impl_->paused = paused; - } impl_->snapshot(now()); - if (paused) { - impl_->paused = paused; + impl_->paused = true; + } + impl_->cv.notify_all(); +} + +void PlayerClock::resume() +{ + { + std::lock_guard lock(impl_->mutex); + if (!impl_->paused) { + return; } + // Note: needs to not be paused when taking snapshot, otherwise it will use last ros ref + impl_->paused = false; + impl_->snapshot(now()); } impl_->cv.notify_all(); } -bool PlayerClock::get_paused() const +bool PlayerClock::is_paused() const { std::lock_guard lock(impl_->mutex); return impl_->paused;