#include "utils/Clock.h" #include #include Core::Clock::Clock() : index(0), last(0), sum(0), time(0) { } bool Core::Clock::update(Clock::Nanos& n) { Nanos current = 0; if(getNanos(current)) { return true; } else if(last == 0) { last = current; n = 0; return false; } index = (index + 1) & (LENGTH - 1); sum -= time[index]; time[index] = current - last; sum += time[index]; last = current; n = time[index]; return false; } float Core::Clock::getUpdatesPerSecond() const { return (LENGTH * 1000000000.0f) / static_cast(sum); } bool Core::Clock::getNanos(Clock::Nanos& n) { struct timespec ts; if(timespec_get(&ts, TIME_UTC) == 0) { return CORE_ERROR(Error::TIME_NOT_AVAILABLE); } n = static_cast(ts.tv_sec) * 1'000'000'000L + static_cast(ts.tv_nsec); return false; } bool Core::Clock::wait(Nanos nanos) const { struct timespec t; t.tv_nsec = nanos % 1'000'000'000; t.tv_sec = nanos / 1'000'000'000; return CORE_ERROR(Error::SLEEP_INTERRUPTED, thrd_sleep(&t, nullptr) != 0); }