Replace CLOCK_REALTIME by CLOCK_MONOTONIC as suggested by @janwas.

Add sanity checks
Fixes #5760

Tested by: @wraitii, @OptimusShepard
Differential Revision: https://code.wildfiregames.com/D3066
This was SVN commit r24507.
This commit is contained in:
Stan 2021-01-04 11:08:19 +00:00
parent 8de6d85127
commit bc00a6b8df

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2020 Wildfire Games.
/* Copyright (C) 2021 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -85,7 +85,7 @@ static Status InitResolution()
resolution = 1.0 / static_cast<double>(frequency.QuadPart);
#elif HAVE_CLOCK_GETTIME
struct timespec ts;
if (clock_getres(CLOCK_REALTIME, &ts) == 0)
if (clock_getres(CLOCK_MONOTONIC, &ts) == 0)
resolution = ts.tv_nsec * 1e-9;
else
resolution = 1e-9;
@ -106,9 +106,9 @@ void timer_Init()
#if OS_WIN
ENSURE(QueryPerformanceCounter(&start));
#elif HAVE_CLOCK_GETTIME
(void)clock_gettime(CLOCK_REALTIME, &start);
ENSURE(clock_gettime(CLOCK_MONOTONIC, &start) == 0);
#elif HAVE_GETTIMEOFDAY
gettimeofday(&start, 0);
ENSURE(gettimeofday(&start, 0) == 0);
#endif
static ModuleInitState initState;
@ -139,12 +139,12 @@ double timer_Time()
#elif HAVE_CLOCK_GETTIME
ENSURE(start.tv_sec || start.tv_nsec); // must have called timer_LatchStartTime first
struct timespec cur;
(void)clock_gettime(CLOCK_REALTIME, &cur);
ENSURE(clock_gettime(CLOCK_MONOTONIC, &cur) == 0);
t = (cur.tv_sec - start.tv_sec) + (cur.tv_nsec - start.tv_nsec) * resolution;
#elif HAVE_GETTIMEOFDAY
ENSURE(start.tv_sec || start.tv_usec); // must have called timer_LatchStartTime first
struct timeval cur;
gettimeofday(&cur, 0);
ENSURE(gettimeofday(&cur, 0) == 0);
t = (cur.tv_sec - start.tv_sec) + (cur.tv_usec - start.tv_usec) * resolution;
#else
# error "timer_Time: add timer implementation for this platform!"