Use CLOCK_MONOTONIC_RAW, if available, which is not subject to adjustment by NTP
authorSam Lantinga <slouken@libsdl.org>
Fri, 19 Jun 2015 23:49:00 -0700
changeset 9767 3622b6b734ac
parent 9766 7cfd071cd9a2
child 9768 fd870d4520be
Use CLOCK_MONOTONIC_RAW, if available, which is not subject to adjustment by NTP
src/timer/unix/SDL_systimer.c
--- a/src/timer/unix/SDL_systimer.c	Fri Jun 19 23:40:23 2015 -0700
+++ b/src/timer/unix/SDL_systimer.c	Fri Jun 19 23:49:00 2015 -0700
@@ -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;
@@ -69,7 +78,7 @@
 
     /* 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__)
@@ -101,7 +110,7 @@
     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__)
@@ -132,7 +141,7 @@
 #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;