Skip to content

Commit

Permalink
Use CLOCK_MONOTONIC_RAW, if available, which is not subject to adjust…
Browse files Browse the repository at this point in the history
…ment by NTP
  • Loading branch information
slouken committed Jun 20, 2015
1 parent f71fa2f commit 903df4a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/timer/unix/SDL_systimer.c
Expand Up @@ -48,6 +48,15 @@
#include <mach/mach_time.h>
#endif

/* Use CLOCK_MONOTONIC_RAW, if available, which is not subject to adjustment by NTP */
#if HAVE_CLOCK_GETTIME
#ifdef CLOCK_MONOTONIC_RAW
#define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC_RAW
#else
#define SDL_MONOTONIC_CLOCK CLOCK_MONOTONIC
#endif
#endif

/* The first ticks value of the application */
#if HAVE_CLOCK_GETTIME
static struct timespec start_ts;
Expand All @@ -69,7 +78,7 @@ SDL_TicksInit(void)

/* Set first ticks value */
#if HAVE_CLOCK_GETTIME
if (clock_gettime(CLOCK_MONOTONIC, &start_ts) == 0) {
if (clock_gettime(SDL_MONOTONIC_CLOCK, &start_ts) == 0) {
has_monotonic_time = SDL_TRUE;
} else
#elif defined(__APPLE__)
Expand Down Expand Up @@ -101,7 +110,7 @@ SDL_GetTicks(void)
if (has_monotonic_time) {
#if HAVE_CLOCK_GETTIME
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
clock_gettime(SDL_MONOTONIC_CLOCK, &now);
ticks = (now.tv_sec - start_ts.tv_sec) * 1000 + (now.tv_nsec -
start_ts.tv_nsec) / 1000000;
#elif defined(__APPLE__)
Expand Down Expand Up @@ -132,7 +141,7 @@ SDL_GetPerformanceCounter(void)
#if HAVE_CLOCK_GETTIME
struct timespec now;

clock_gettime(CLOCK_MONOTONIC, &now);
clock_gettime(SDL_MONOTONIC_CLOCK, &now);
ticks = now.tv_sec;
ticks *= 1000000000;
ticks += now.tv_nsec;
Expand Down

0 comments on commit 903df4a

Please sign in to comment.