From 7348d1e71739bb7a5052375e6e918e92f5d1d6b0 Mon Sep 17 00:00:00 2001 From: cryptocode Date: Thu, 12 Mar 2020 15:22:55 +0100 Subject: [PATCH] Update tick count in timer::update (#2655) * Update tick count in timer::update * Return ticks from restart --- nano/core_test/timer.cpp | 2 ++ nano/lib/timer.cpp | 17 +++++++++++++---- nano/lib/timer.hpp | 8 +++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/nano/core_test/timer.cpp b/nano/core_test/timer.cpp index ffd65f46..1aa29739 100644 --- a/nano/core_test/timer.cpp +++ b/nano/core_test/timer.cpp @@ -42,6 +42,8 @@ TEST (timer, measure_and_compare) ASSERT_LT (t1.since_start (), 200ms); ASSERT_GT (t1.since_start (), 10ms); ASSERT_GE (t1.stop (), 50ms); + std::this_thread::sleep_for (50ms); + ASSERT_GT (t1.restart (), 10ms); } TEST (timer, cummulative_child) diff --git a/nano/lib/timer.cpp b/nano/lib/timer.cpp index ed2988a2..2acd1881 100644 --- a/nano/lib/timer.cpp +++ b/nano/lib/timer.cpp @@ -97,12 +97,14 @@ void nano::timer::start () } template -void nano::timer::restart () +UNIT nano::timer::restart () { + auto current = ticks; state = nano::timer_state::started; begin = CLOCK::now (); ticks = UNIT::zero (); measurements = 0; + return current; } template @@ -112,20 +114,27 @@ UNIT nano::timer::pause () return stop (); } +template +void nano::timer::update_ticks () +{ + auto end = CLOCK::now (); + ticks += std::chrono::duration_cast (end - begin); +} + template UNIT nano::timer::stop () { debug_assert (state == nano::timer_state::started); state = nano::timer_state::stopped; - auto end = CLOCK::now (); - ticks += std::chrono::duration_cast (end - begin); + update_ticks (); return ticks; } template -UNIT nano::timer::value () const +UNIT nano::timer::value () { + update_ticks (); return ticks; } diff --git a/nano/lib/timer.hpp b/nano/lib/timer.hpp index 61c201d3..423d6f6f 100644 --- a/nano/lib/timer.hpp +++ b/nano/lib/timer.hpp @@ -42,8 +42,9 @@ public: /** * Restarts the timer by setting start time to current time and resetting tick count. * This can be called in any timer state. + * @return duration */ - void restart (); + UNIT restart (); /** * Stops the timer and increases the measurement count. A timer can be started and paused @@ -59,9 +60,9 @@ public: UNIT stop (); /** - * Return current tick count. + * Updates and returns current tick count. */ - UNIT value () const; + UNIT value (); /** Returns the duration in UNIT since the timer was last started. */ UNIT since_start () const; @@ -94,5 +95,6 @@ private: UNIT ticks{ 0 }; UNIT minimum{ UNIT::zero () }; unsigned measurements{ 0 }; + void update_ticks (); }; }