2020-01-10 03:31:12 +00:00
|
|
|
#include "timer.h"
|
2021-01-25 16:20:38 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2020-01-10 03:31:12 +00:00
|
|
|
|
2021-06-28 10:16:48 +00:00
|
|
|
#ifdef _WIN32
|
2020-01-10 03:31:12 +00:00
|
|
|
#include "windows_headers.h"
|
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
2021-01-25 16:20:38 +00:00
|
|
|
#include <unistd.h>
|
2020-01-10 03:31:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
2021-06-28 10:16:48 +00:00
|
|
|
#ifdef _WIN32
|
2020-01-10 03:31:12 +00:00
|
|
|
|
|
|
|
static double s_counter_frequency;
|
|
|
|
static bool s_counter_initialized = false;
|
|
|
|
|
2021-01-28 10:11:31 +00:00
|
|
|
// This gets leaked... oh well.
|
|
|
|
static thread_local HANDLE s_sleep_timer;
|
|
|
|
static thread_local bool s_sleep_timer_created = false;
|
|
|
|
|
|
|
|
static HANDLE GetSleepTimer()
|
|
|
|
{
|
|
|
|
if (s_sleep_timer_created)
|
|
|
|
return s_sleep_timer;
|
|
|
|
|
|
|
|
s_sleep_timer_created = true;
|
|
|
|
s_sleep_timer = CreateWaitableTimer(nullptr, TRUE, nullptr);
|
|
|
|
if (!s_sleep_timer)
|
|
|
|
std::fprintf(stderr, "CreateWaitableTimer() failed, falling back to Sleep()\n");
|
|
|
|
|
|
|
|
return s_sleep_timer;
|
|
|
|
}
|
|
|
|
|
2020-01-10 03:31:12 +00:00
|
|
|
Timer::Value Timer::GetValue()
|
|
|
|
{
|
|
|
|
// even if this races, it should still result in the same value..
|
|
|
|
if (!s_counter_initialized)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER Freq;
|
|
|
|
QueryPerformanceFrequency(&Freq);
|
|
|
|
s_counter_frequency = static_cast<double>(Freq.QuadPart) / 1000000000.0;
|
|
|
|
s_counter_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Value ReturnValue;
|
|
|
|
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&ReturnValue));
|
|
|
|
return ReturnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Timer::ConvertValueToNanoseconds(Timer::Value value)
|
|
|
|
{
|
|
|
|
return (static_cast<double>(value) / s_counter_frequency);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Timer::ConvertValueToMilliseconds(Timer::Value value)
|
|
|
|
{
|
|
|
|
return ((static_cast<double>(value) / s_counter_frequency) / 1000000.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Timer::ConvertValueToSeconds(Timer::Value value)
|
|
|
|
{
|
|
|
|
return ((static_cast<double>(value) / s_counter_frequency) / 1000000000.0);
|
|
|
|
}
|
|
|
|
|
2021-01-25 16:20:38 +00:00
|
|
|
Timer::Value Timer::ConvertSecondsToValue(double s)
|
|
|
|
{
|
|
|
|
return static_cast<Value>((s * 1000000000.0) * s_counter_frequency);
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Value Timer::ConvertMillisecondsToValue(double ms)
|
|
|
|
{
|
|
|
|
return static_cast<Value>((ms * 1000000.0) * s_counter_frequency);
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Value Timer::ConvertNanosecondsToValue(double ns)
|
|
|
|
{
|
|
|
|
return static_cast<Value>(ns * s_counter_frequency);
|
|
|
|
}
|
|
|
|
|
2021-01-28 10:11:31 +00:00
|
|
|
void Timer::SleepUntil(Value value, bool exact)
|
|
|
|
{
|
|
|
|
if (exact)
|
|
|
|
{
|
|
|
|
while (GetValue() < value)
|
|
|
|
SleepUntil(value, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const std::int64_t diff = static_cast<std::int64_t>(value - GetValue());
|
|
|
|
if (diff <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
HANDLE timer = GetSleepTimer();
|
|
|
|
if (timer)
|
|
|
|
{
|
|
|
|
FILETIME ft;
|
|
|
|
GetSystemTimeAsFileTime(&ft);
|
|
|
|
|
|
|
|
LARGE_INTEGER fti;
|
|
|
|
fti.LowPart = ft.dwLowDateTime;
|
|
|
|
fti.HighPart = ft.dwHighDateTime;
|
|
|
|
fti.QuadPart += diff;
|
|
|
|
|
|
|
|
if (SetWaitableTimer(timer, &fti, 0, nullptr, nullptr, FALSE))
|
2021-04-12 10:26:50 +00:00
|
|
|
{
|
2021-01-28 10:11:31 +00:00
|
|
|
WaitForSingleObject(timer, INFINITE);
|
2021-04-12 10:26:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-01-28 10:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// falling back to sleep... bad.
|
|
|
|
Sleep(static_cast<DWORD>(static_cast<std::uint64_t>(diff) / 1000000));
|
|
|
|
}
|
|
|
|
}
|
2020-01-10 03:31:12 +00:00
|
|
|
|
2021-01-28 10:11:31 +00:00
|
|
|
#else
|
2020-01-10 03:31:12 +00:00
|
|
|
|
|
|
|
Timer::Value Timer::GetValue()
|
|
|
|
{
|
|
|
|
struct timespec tv;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &tv);
|
|
|
|
return ((Value)tv.tv_nsec + (Value)tv.tv_sec * 1000000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Timer::ConvertValueToNanoseconds(Timer::Value value)
|
|
|
|
{
|
|
|
|
return static_cast<double>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Timer::ConvertValueToMilliseconds(Timer::Value value)
|
|
|
|
{
|
|
|
|
return (static_cast<double>(value) / 1000000.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Timer::ConvertValueToSeconds(Timer::Value value)
|
|
|
|
{
|
|
|
|
return (static_cast<double>(value) / 1000000000.0);
|
|
|
|
}
|
|
|
|
|
2021-01-25 16:20:38 +00:00
|
|
|
Timer::Value Timer::ConvertSecondsToValue(double s)
|
|
|
|
{
|
|
|
|
return static_cast<Value>(s * 1000000000.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Value Timer::ConvertMillisecondsToValue(double ms)
|
|
|
|
{
|
|
|
|
return static_cast<Value>(ms * 1000000.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Value Timer::ConvertNanosecondsToValue(double ns)
|
|
|
|
{
|
|
|
|
return static_cast<Value>(ns);
|
|
|
|
}
|
|
|
|
|
2021-01-28 10:11:31 +00:00
|
|
|
void Timer::SleepUntil(Value value, bool exact)
|
2020-01-10 03:31:12 +00:00
|
|
|
{
|
2021-01-28 10:11:31 +00:00
|
|
|
if (exact)
|
|
|
|
{
|
|
|
|
while (GetValue() < value)
|
|
|
|
SleepUntil(value, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Apple doesn't have TIMER_ABSTIME, so fall back to nanosleep in such a case.
|
|
|
|
#ifdef __APPLE__
|
|
|
|
const Value current_time = GetValue();
|
|
|
|
if (value <= current_time)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const Value diff = value - current_time;
|
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = diff / UINT64_C(1000000000);
|
|
|
|
ts.tv_nsec = diff % UINT64_C(1000000000);
|
|
|
|
nanosleep(&ts, nullptr);
|
|
|
|
#else
|
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = value / UINT64_C(1000000000);
|
|
|
|
ts.tv_nsec = value % UINT64_C(1000000000);
|
|
|
|
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, nullptr);
|
2020-01-10 03:31:12 +00:00
|
|
|
#endif
|
2021-01-28 10:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-10 03:31:12 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Timer::Timer()
|
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::Reset()
|
|
|
|
{
|
|
|
|
m_tvStartValue = GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
double Timer::GetTimeSeconds() const
|
|
|
|
{
|
|
|
|
return ConvertValueToSeconds(GetValue() - m_tvStartValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Timer::GetTimeMilliseconds() const
|
|
|
|
{
|
|
|
|
return ConvertValueToMilliseconds(GetValue() - m_tvStartValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Timer::GetTimeNanoseconds() const
|
|
|
|
{
|
|
|
|
return ConvertValueToNanoseconds(GetValue() - m_tvStartValue);
|
|
|
|
}
|
|
|
|
|
2021-01-25 16:20:38 +00:00
|
|
|
void Timer::BusyWait(std::uint64_t ns)
|
|
|
|
{
|
|
|
|
const Value start = GetValue();
|
|
|
|
const Value end = start + ConvertNanosecondsToValue(static_cast<double>(ns));
|
|
|
|
if (end < start)
|
|
|
|
{
|
|
|
|
// overflow, unlikely
|
|
|
|
while (GetValue() > end)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (GetValue() < end)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::HybridSleep(std::uint64_t ns, std::uint64_t min_sleep_time)
|
|
|
|
{
|
|
|
|
const std::uint64_t start = GetValue();
|
|
|
|
const std::uint64_t end = start + ConvertNanosecondsToValue(static_cast<double>(ns));
|
|
|
|
if (end < start)
|
|
|
|
{
|
|
|
|
// overflow, unlikely
|
|
|
|
while (GetValue() > end)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::uint64_t current = GetValue();
|
|
|
|
while (current < end)
|
|
|
|
{
|
|
|
|
const std::uint64_t remaining = end - current;
|
|
|
|
if (remaining >= min_sleep_time)
|
|
|
|
NanoSleep(min_sleep_time);
|
|
|
|
|
|
|
|
current = GetValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::NanoSleep(std::uint64_t ns)
|
|
|
|
{
|
2021-06-28 10:16:48 +00:00
|
|
|
#if defined(_WIN32)
|
2021-01-28 10:11:31 +00:00
|
|
|
HANDLE timer = GetSleepTimer();
|
|
|
|
if (timer)
|
2021-01-25 16:20:38 +00:00
|
|
|
{
|
|
|
|
LARGE_INTEGER due_time;
|
|
|
|
due_time.QuadPart = -static_cast<std::int64_t>(static_cast<std::uint64_t>(ns) / 100u);
|
2021-01-28 10:11:31 +00:00
|
|
|
if (SetWaitableTimer(timer, &due_time, 0, nullptr, nullptr, FALSE))
|
|
|
|
WaitForSingleObject(timer, INFINITE);
|
2021-01-25 16:20:38 +00:00
|
|
|
else
|
|
|
|
std::fprintf(stderr, "SetWaitableTimer() failed: %08X\n", GetLastError());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Sleep(static_cast<std::uint32_t>(ns / 1000000));
|
|
|
|
}
|
|
|
|
#elif defined(__ANDROID__)
|
|
|
|
// Round down to the next millisecond.
|
|
|
|
usleep(static_cast<useconds_t>((ns / 1000000) * 1000));
|
|
|
|
#else
|
|
|
|
const struct timespec ts = {0, static_cast<long>(ns)};
|
|
|
|
nanosleep(&ts, nullptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Common
|