Commit 7ed41da0b08c4d7ca6e84a763b2e8debf5dd1f08

Sam Lantinga 2014-11-28T04:37:50

Fixed bug 2411 - Even if built with --enable-clock_gettime, SDL2 still calls gettimeofday() Ben Swick Makes SDL_syscond.c and SDL_syssem.c use clock_gettime(CLOCK_REALTIME) when HAVE_CLOCK_GETTIME is defined.

diff --git a/src/thread/pthread/SDL_syscond.c b/src/thread/pthread/SDL_syscond.c
index c15df86..77aebb0 100644
--- a/src/thread/pthread/SDL_syscond.c
+++ b/src/thread/pthread/SDL_syscond.c
@@ -21,6 +21,7 @@
 #include "../../SDL_internal.h"
 
 #include <sys/time.h>
+#include <time.h>
 #include <unistd.h>
 #include <errno.h>
 #include <pthread.h>
@@ -98,17 +99,26 @@ int
 SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
 {
     int retval;
+#ifndef HAVE_CLOCK_GETTIME
     struct timeval delta;
+#endif
     struct timespec abstime;
 
     if (!cond) {
         return SDL_SetError("Passed a NULL condition variable");
     }
 
+#ifdef HAVE_CLOCK_GETTIME
+    clock_gettime(CLOCK_REALTIME, &abstime);
+
+    abstime.tv_nsec += (ms % 1000) * 1000000;
+    abstime.tv_sec += ms / 1000;
+#else
     gettimeofday(&delta, NULL);
 
     abstime.tv_sec = delta.tv_sec + (ms / 1000);
     abstime.tv_nsec = (delta.tv_usec + (ms % 1000) * 1000) * 1000;
+#endif
     if (abstime.tv_nsec > 1000000000) {
         abstime.tv_sec += 1;
         abstime.tv_nsec -= 1000000000;
diff --git a/src/thread/pthread/SDL_syssem.c b/src/thread/pthread/SDL_syssem.c
index 6d84e1b..1d5faa1 100644
--- a/src/thread/pthread/SDL_syssem.c
+++ b/src/thread/pthread/SDL_syssem.c
@@ -24,6 +24,7 @@
 #include <pthread.h>
 #include <semaphore.h>
 #include <sys/time.h>
+#include <time.h>
 
 #include "SDL_thread.h"
 #include "SDL_timer.h"
@@ -102,7 +103,9 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
 {
     int retval;
 #ifdef HAVE_SEM_TIMEDWAIT
+#ifndef HAVE_CLOCK_GETTIME
     struct timeval now;
+#endif
     struct timespec ts_timeout;
 #else
     Uint32 end;
@@ -125,22 +128,26 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
     * a lapse of time, but until we reach a certain time.
     * This time is now plus the timeout.
     */
+#ifdef HAVE_CLOCK_GETTIME
+    clock_gettime(CLOCK_REALTIME, &ts_timeout);
+
+    /* Add our timeout to current time */
+    ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
+    ts_timeout.tv_sec += timeout / 1000;
+#else
     gettimeofday(&now, NULL);
 
     /* Add our timeout to current time */
-    now.tv_usec += (timeout % 1000) * 1000;
-    now.tv_sec += timeout / 1000;
+    ts_timeout.tv_sec = now.tv_sec + (timeout / 1000);
+    ts_timeout.tv_nsec = (now.tv_usec + (timeout % 1000) * 1000) * 1000;
+#endif
 
     /* Wrap the second if needed */
-    if ( now.tv_usec >= 1000000 ) {
-        now.tv_usec -= 1000000;
-        now.tv_sec ++;
+    if (ts_timeout.tv_nsec > 1000000000) {
+        ts_timeout.tv_sec += 1;
+        ts_timeout.tv_nsec -= 1000000000;
     }
 
-    /* Convert to timespec */
-    ts_timeout.tv_sec = now.tv_sec;
-    ts_timeout.tv_nsec = now.tv_usec * 1000;
-
     /* Wait. */
     do {
         retval = sem_timedwait(&sem->sem, &ts_timeout);