2015-10-05 00:44:40 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <time.h>
|
2016-05-28 10:15:36 +00:00
|
|
|
#include <mutex>
|
2015-10-05 00:44:40 +00:00
|
|
|
#include <Poco/ScopedLock.h>
|
|
|
|
#include <common/Common.h>
|
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <common/apple_rt.h>
|
|
|
|
#endif
|
2015-10-05 00:44:40 +00:00
|
|
|
|
2017-02-07 19:21:59 +00:00
|
|
|
/** Differs from Poco::Stopwatch only by using 'clock_gettime' instead of 'gettimeofday',
|
|
|
|
* returns nanoseconds instead of microseconds, and also by other minor differencies.
|
2015-10-05 00:44:40 +00:00
|
|
|
*/
|
|
|
|
class Stopwatch
|
|
|
|
{
|
|
|
|
public:
|
2017-02-07 19:21:59 +00:00
|
|
|
/** CLOCK_MONOTONIC works relatively efficient (~15 million calls/sec) and doesn't lead to syscall.
|
|
|
|
* Pass CLOCK_MONOTONIC_COARSE, if you need better performance with acceptable cost of several milliseconds of inaccuracy.
|
2015-10-05 00:44:40 +00:00
|
|
|
*/
|
|
|
|
Stopwatch(clockid_t clock_type_ = CLOCK_MONOTONIC) : clock_type(clock_type_) { restart(); }
|
|
|
|
|
|
|
|
void start() { setStart(); is_running = true; }
|
|
|
|
void stop() { updateElapsed(); is_running = false; }
|
|
|
|
void restart() { elapsed_ns = 0; start(); }
|
|
|
|
UInt64 elapsed() const { updateElapsed(); return elapsed_ns; }
|
|
|
|
double elapsedSeconds() const { updateElapsed(); return static_cast<double>(elapsed_ns) / 1000000000ULL; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable UInt64 start_ns;
|
|
|
|
mutable UInt64 elapsed_ns;
|
|
|
|
clockid_t clock_type;
|
|
|
|
bool is_running;
|
|
|
|
|
|
|
|
void setStart()
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(clock_type, &ts);
|
|
|
|
start_ns = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateElapsed() const
|
|
|
|
{
|
|
|
|
if (is_running)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(clock_type, &ts);
|
|
|
|
UInt64 current_ns = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
|
|
|
|
elapsed_ns += current_ns - start_ns;
|
|
|
|
start_ns = current_ns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class StopwatchWithLock : public Stopwatch
|
|
|
|
{
|
|
|
|
public:
|
2016-11-18 02:34:34 +00:00
|
|
|
/** If specified amount of time has passed and timer is not locked right now, then restarts timer and returns true.
|
|
|
|
* Otherwise returns false.
|
|
|
|
* This is done atomically.
|
2015-10-05 00:44:40 +00:00
|
|
|
*/
|
|
|
|
bool lockTestAndRestart(double seconds)
|
|
|
|
{
|
2016-11-18 02:34:34 +00:00
|
|
|
std::unique_lock<std::mutex> lock(mutex, std::defer_lock);
|
|
|
|
if (!lock.try_lock())
|
|
|
|
return false;
|
2015-10-05 00:44:40 +00:00
|
|
|
|
|
|
|
if (elapsedSeconds() >= seconds)
|
|
|
|
{
|
|
|
|
restart();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-18 02:34:34 +00:00
|
|
|
struct Lock
|
|
|
|
{
|
|
|
|
StopwatchWithLock * parent = nullptr;
|
|
|
|
std::unique_lock<std::mutex> lock;
|
|
|
|
|
|
|
|
Lock() {}
|
|
|
|
|
|
|
|
operator bool() const { return parent != nullptr; }
|
|
|
|
|
|
|
|
Lock(StopwatchWithLock * parent, std::unique_lock<std::mutex> && lock)
|
|
|
|
: parent(parent), lock(std::move(lock))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Lock(Lock &&) = default;
|
|
|
|
|
|
|
|
~Lock()
|
|
|
|
{
|
|
|
|
if (parent)
|
|
|
|
parent->restart();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** If specified amount of time has passed and timer is not locked right now, then returns Lock object,
|
|
|
|
* which locks timer and, on destruction, restarts timer and releases the lock.
|
|
|
|
* Otherwise returns object, that is implicitly casting to false.
|
|
|
|
* This is done atomically.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* if (auto lock = timer.lockTestAndRestartAfter(1))
|
|
|
|
* /// do some work, that must be done in one thread and not more frequently than each second.
|
|
|
|
*/
|
|
|
|
Lock lockTestAndRestartAfter(double seconds)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(mutex, std::defer_lock);
|
|
|
|
if (!lock.try_lock())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (elapsedSeconds() >= seconds)
|
|
|
|
return Lock(this, std::move(lock));
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2015-10-05 00:44:40 +00:00
|
|
|
private:
|
2016-05-28 10:15:36 +00:00
|
|
|
std::mutex mutex;
|
2015-10-05 00:44:40 +00:00
|
|
|
};
|