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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
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);