Skip to content

Commit

Permalink
pause() and resume() instead of set_paused(bool)
Browse files Browse the repository at this point in the history
Signed-off-by: Emerson Knapp <eknapp@amazon.com>
  • Loading branch information
Emerson Knapp committed Mar 31, 2021
1 parent f0bb3cf commit e921b13
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
16 changes: 11 additions & 5 deletions rosbag2_cpp/include/rosbag2_cpp/player_clock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlayerClockImpl> impl_;
Expand Down
25 changes: 17 additions & 8 deletions rosbag2_cpp/src/rosbag2_cpp/player_clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,35 @@ double PlayerClock::get_rate() const
return impl_->rate;
}

void PlayerClock::set_paused(bool paused)
void PlayerClock::pause()
{
{
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> lock(impl_->mutex);
return impl_->paused;
Expand Down

0 comments on commit e921b13

Please sign in to comment.