Check for sem_timedwait(), which isn't available on some systems (including OpenBSD) SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Sun, 15 Jan 2012 03:13:08 -0500
branchSDL-1.2
changeset 6218f114f1434ff6
parent 6215 a84cb42bce7f
child 6224 6f013dd0add1
Check for sem_timedwait(), which isn't available on some systems (including OpenBSD)
configure.in
include/SDL_config.h.in
src/thread/pthread/SDL_syssem.c
     1.1 --- a/configure.in	Sat Jan 14 02:22:40 2012 -0500
     1.2 +++ b/configure.in	Sun Jan 15 03:13:08 2012 -0500
     1.3 @@ -1994,6 +1994,20 @@
     1.4                  ])
     1.5                  AC_MSG_RESULT($have_pthread_sem)
     1.6              fi
     1.7 +            if test x$have_pthread_sem = xyes; then
     1.8 +                AC_MSG_CHECKING(for sem_timedwait)
     1.9 +                have_sem_timedwait=no
    1.10 +                AC_TRY_LINK([
    1.11 +                  #include <pthread.h>
    1.12 +                  #include <semaphore.h>
    1.13 +                ],[
    1.14 +                  sem_timedwait(NULL, NULL);
    1.15 +                ],[
    1.16 +                have_sem_timedwait=yes
    1.17 +                AC_DEFINE(HAVE_SEM_TIMEDWAIT)
    1.18 +                ])
    1.19 +                AC_MSG_RESULT($have_sem_timedwait)
    1.20 +            fi
    1.21  
    1.22              # Restore the compiler flags and libraries
    1.23              CFLAGS="$ac_save_cflags"; LIBS="$ac_save_libs"
     2.1 --- a/include/SDL_config.h.in	Sat Jan 14 02:22:40 2012 -0500
     2.2 +++ b/include/SDL_config.h.in	Sun Jan 15 03:13:08 2012 -0500
     2.3 @@ -137,6 +137,7 @@
     2.4  #undef HAVE_CLOCK_GETTIME
     2.5  #undef HAVE_GETPAGESIZE
     2.6  #undef HAVE_MPROTECT
     2.7 +#undef HAVE_SEM_TIMEDWAIT
     2.8  
     2.9  #else
    2.10  /* We may need some replacement for stdarg.h here */
     3.1 --- a/src/thread/pthread/SDL_syssem.c	Sat Jan 14 02:22:40 2012 -0500
     3.2 +++ b/src/thread/pthread/SDL_syssem.c	Sun Jan 15 03:13:08 2012 -0500
     3.3 @@ -98,8 +98,12 @@
     3.4  int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
     3.5  {
     3.6  	int retval;
     3.7 +#ifdef HAVE_SEM_TIMEDWAIT
     3.8  	struct timeval now;
     3.9  	struct timespec ts_timeout;
    3.10 +#else
    3.11 +	Uint32 end;
    3.12 +#endif
    3.13  
    3.14  	if ( ! sem ) {
    3.15  		SDL_SetError("Passed a NULL semaphore");
    3.16 @@ -114,6 +118,7 @@
    3.17  		return SDL_SemWait(sem);
    3.18  	}
    3.19  
    3.20 +#ifdef HAVE_SEM_TIMEDWAIT
    3.21  	/* Setup the timeout. sem_timedwait doesn't wait for
    3.22  	 * a lapse of time, but until we reach a certain time.
    3.23  	 * This time is now plus the timeout.
    3.24 @@ -141,6 +146,15 @@
    3.25  
    3.26  	if (retval == -1)
    3.27  		SDL_SetError(strerror(errno));
    3.28 +#else
    3.29 +	end = SDL_GetTicks() + timeout;
    3.30 +	while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
    3.31 +		if ((SDL_GetTicks() - end) >= 0) {
    3.32 +			break;
    3.33 +		}
    3.34 +		SDL_Delay(0);
    3.35 +	}
    3.36 +#endif /* HAVE_SEM_TIMEDWAIT */
    3.37  
    3.38  	return retval;
    3.39  }