Skip to content

Commit

Permalink
Fixed bug 2411 - Even if built with --enable-clock_gettime, SDL2 stil…
Browse files Browse the repository at this point in the history
…l calls gettimeofday()

Ben Swick

Makes SDL_syscond.c and SDL_syssem.c use clock_gettime(CLOCK_REALTIME) when HAVE_CLOCK_GETTIME is defined.
  • Loading branch information
slouken committed Nov 28, 2014
1 parent ef559dd commit 7ed41da
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/thread/pthread/SDL_syscond.c
Expand Up @@ -21,6 +21,7 @@
#include "../../SDL_internal.h"

#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 16 additions & 9 deletions src/thread/pthread/SDL_syssem.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 7ed41da

Please sign in to comment.