diff --git a/include/ircd/timer.h b/include/ircd/timer.h index 17bb3ce1e..8f01d64c6 100644 --- a/include/ircd/timer.h +++ b/include/ircd/timer.h @@ -30,20 +30,25 @@ namespace ircd::util struct ircd::util::timer { + IRCD_OVERLOAD(stopped) using clock = std::chrono::steady_clock; nanoseconds accumulator; clock::time_point start; + bool stopped() const; template duration get() const; template duration at() const; void cont(); void stop(); timer(const std::function &); + timer(stopped_t); timer(); }; + +/// Default construction will start the timer. inline ircd::util::timer::timer() :accumulator{0ns} @@ -51,6 +56,14 @@ ircd::util::timer::timer() { } +/// timer(timer::stopped) +inline +ircd::util::timer::timer(stopped_t) +:accumulator{0ns} +,start{clock::time_point::min()} +{ +} + inline ircd::util::timer::timer(const std::function &func) :timer{} @@ -62,10 +75,10 @@ ircd::util::timer::timer(const std::function &func) inline void ircd::util::timer::stop() { - const auto now(clock::now()); - if(start == clock::time_point::min()) + if(stopped()) return; + const auto now(clock::now()); accumulator += std::chrono::duration_cast(now - start); start = clock::time_point::min(); } @@ -73,7 +86,7 @@ ircd::util::timer::stop() inline void ircd::util::timer::cont() { - if(start != clock::time_point::min()) + if(!stopped()) { const auto now(clock::now()); accumulator += std::chrono::duration_cast(now - start); @@ -98,3 +111,11 @@ const { return std::chrono::duration_cast(accumulator); } + +template +bool +ircd::util::timer::stopped() +const +{ + return start == clock::time_point::min(); +}