diff --git a/src/thread/pthread/SDL_syscond.c b/src/thread/pthread/SDL_syscond.c index c15df8632..77aebb0d7 100644 --- a/src/thread/pthread/SDL_syscond.c +++ b/src/thread/pthread/SDL_syscond.c @@ -21,6 +21,7 @@ #include "../../SDL_internal.h" #include +#include #include #include #include @@ -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 6d84e1b89..1d5faa180 100644 --- a/src/thread/pthread/SDL_syssem.c +++ b/src/thread/pthread/SDL_syssem.c @@ -24,6 +24,7 @@ #include #include #include +#include #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);