0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-01-03 19:34:29 +01:00

ircd::util: Improve timer.

This commit is contained in:
Jason Volk 2017-09-23 19:36:32 -07:00
parent c138c53449
commit befb76a9d9

View file

@ -30,20 +30,25 @@ namespace ircd::util
struct ircd::util::timer struct ircd::util::timer
{ {
IRCD_OVERLOAD(stopped)
using clock = std::chrono::steady_clock; using clock = std::chrono::steady_clock;
nanoseconds accumulator; nanoseconds accumulator;
clock::time_point start; clock::time_point start;
bool stopped() const;
template<class duration = std::chrono::seconds> duration get() const; template<class duration = std::chrono::seconds> duration get() const;
template<class duration = std::chrono::seconds> duration at() const; template<class duration = std::chrono::seconds> duration at() const;
void cont(); void cont();
void stop(); void stop();
timer(const std::function<void ()> &); timer(const std::function<void ()> &);
timer(stopped_t);
timer(); timer();
}; };
/// Default construction will start the timer.
inline inline
ircd::util::timer::timer() ircd::util::timer::timer()
:accumulator{0ns} :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 inline
ircd::util::timer::timer(const std::function<void ()> &func) ircd::util::timer::timer(const std::function<void ()> &func)
:timer{} :timer{}
@ -62,10 +75,10 @@ ircd::util::timer::timer(const std::function<void ()> &func)
inline void inline void
ircd::util::timer::stop() ircd::util::timer::stop()
{ {
const auto now(clock::now()); if(stopped())
if(start == clock::time_point::min())
return; return;
const auto now(clock::now());
accumulator += std::chrono::duration_cast<decltype(accumulator)>(now - start); accumulator += std::chrono::duration_cast<decltype(accumulator)>(now - start);
start = clock::time_point::min(); start = clock::time_point::min();
} }
@ -73,7 +86,7 @@ ircd::util::timer::stop()
inline void inline void
ircd::util::timer::cont() ircd::util::timer::cont()
{ {
if(start != clock::time_point::min()) if(!stopped())
{ {
const auto now(clock::now()); const auto now(clock::now());
accumulator += std::chrono::duration_cast<decltype(accumulator)>(now - start); accumulator += std::chrono::duration_cast<decltype(accumulator)>(now - start);
@ -98,3 +111,11 @@ const
{ {
return std::chrono::duration_cast<duration>(accumulator); return std::chrono::duration_cast<duration>(accumulator);
} }
template<class duration>
bool
ircd::util::timer::stopped()
const
{
return start == clock::time_point::min();
}