From 995137aeb3f465861fd6c34dab0d9b0d53e6c833 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Sat, 26 Jun 2021 11:03:33 -0600 Subject: [PATCH] add PBD::WaitTimerRAII with reverse semantics from PBD::TimerRAII --- libs/pbd/pbd/timing.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/libs/pbd/pbd/timing.h b/libs/pbd/pbd/timing.h index 42bc90649f..14e4fe9a0d 100644 --- a/libs/pbd/pbd/timing.h +++ b/libs/pbd/pbd/timing.h @@ -103,6 +103,8 @@ public: return elapsed; } + bool started() const { return m_start_val != 0; } + /// @return Elapsed time in microseconds uint64_t elapsed () const { return m_last_val - m_start_val; @@ -219,6 +221,10 @@ private: int _queue_reset; }; +/** Provides an exception (and return path)-safe method to measure a timer + * interval. The timer is started at scope entry, and updated at scope exit + * (however that occurs) + */ class LIBPBD_API TimerRAII { public: @@ -227,6 +233,20 @@ class LIBPBD_API TimerRAII TimingStats& stats; }; +/** Reverse semantics from TimerRAII. This starts the timer at scope exit, + * and then updates it (computes interval) at scope entry. This is designed + * for use with a callback API like CoreAudio, where we want to time the + * interval between us being done with our work, and when our callback is + * next executed. + */ +class LIBPBD_API WaitTimerRAII +{ + public: + WaitTimerRAII (TimingStats& ts) : stats (ts) { if (stats.started()) { stats.update(); } } + ~WaitTimerRAII() { stats.start(); } + TimingStats& stats; +}; + class LIBPBD_API TimingData { public: