Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
authorSam Lantinga <slouken@libsdl.org>
Sun, 15 Jan 2012 03:34:14 -0500
changeset 62198e94af244f2c
parent 6217 6952b11b7f46
child 6220 c36934808194
Check for sem_timedwait(), which isn't available on some systems (including OpenBSD
configure
configure.in
include/SDL_config_generated.h.in
src/thread/pthread/SDL_syssem.c
     1.1 --- a/configure	Sat Jan 14 13:21:19 2012 -0500
     1.2 +++ b/configure	Sun Jan 15 03:34:14 2012 -0500
     1.3 @@ -24485,6 +24485,67 @@
     1.4                  { echo "$as_me:$LINENO: result: $have_pthread_sem" >&5
     1.5  echo "${ECHO_T}$have_pthread_sem" >&6; }
     1.6              fi
     1.7 +            if test x$have_pthread_sem = xyes; then
     1.8 +                { echo "$as_me:$LINENO: checking for sem_timedwait" >&5
     1.9 +echo $ECHO_N "checking for sem_timedwait... $ECHO_C" >&6; }
    1.10 +                have_sem_timedwait=no
    1.11 +                cat >conftest.$ac_ext <<_ACEOF
    1.12 +/* confdefs.h.  */
    1.13 +_ACEOF
    1.14 +cat confdefs.h >>conftest.$ac_ext
    1.15 +cat >>conftest.$ac_ext <<_ACEOF
    1.16 +/* end confdefs.h.  */
    1.17 +
    1.18 +                  #include <pthread.h>
    1.19 +                  #include <semaphore.h>
    1.20 +
    1.21 +int
    1.22 +main ()
    1.23 +{
    1.24 +
    1.25 +                  sem_timedwait(NULL, NULL);
    1.26 +
    1.27 +  ;
    1.28 +  return 0;
    1.29 +}
    1.30 +_ACEOF
    1.31 +rm -f conftest.$ac_objext conftest$ac_exeext
    1.32 +if { (ac_try="$ac_link"
    1.33 +case "(($ac_try" in
    1.34 +  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
    1.35 +  *) ac_try_echo=$ac_try;;
    1.36 +esac
    1.37 +eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
    1.38 +  (eval "$ac_link") 2>conftest.er1
    1.39 +  ac_status=$?
    1.40 +  grep -v '^ *+' conftest.er1 >conftest.err
    1.41 +  rm -f conftest.er1
    1.42 +  cat conftest.err >&5
    1.43 +  echo "$as_me:$LINENO: \$? = $ac_status" >&5
    1.44 +  (exit $ac_status); } && {
    1.45 +	 test -z "$ac_c_werror_flag" ||
    1.46 +	 test ! -s conftest.err
    1.47 +       } && test -s conftest$ac_exeext &&
    1.48 +       $as_test_x conftest$ac_exeext; then
    1.49 +
    1.50 +                have_sem_timedwait=yes
    1.51 +                cat >>confdefs.h <<\_ACEOF
    1.52 +#define HAVE_SEM_TIMEDWAIT 1
    1.53 +_ACEOF
    1.54 +
    1.55 +
    1.56 +else
    1.57 +  echo "$as_me: failed program was:" >&5
    1.58 +sed 's/^/| /' conftest.$ac_ext >&5
    1.59 +
    1.60 +
    1.61 +fi
    1.62 +
    1.63 +rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \
    1.64 +      conftest$ac_exeext conftest.$ac_ext
    1.65 +                { echo "$as_me:$LINENO: result: $have_sem_timedwait" >&5
    1.66 +echo "${ECHO_T}$have_sem_timedwait" >&6; }
    1.67 +            fi
    1.68  
    1.69              { echo "$as_me:$LINENO: checking for pthread_spin_trylock" >&5
    1.70  echo $ECHO_N "checking for pthread_spin_trylock... $ECHO_C" >&6; }
     2.1 --- a/configure.in	Sat Jan 14 13:21:19 2012 -0500
     2.2 +++ b/configure.in	Sun Jan 15 03:34:14 2012 -0500
     2.3 @@ -1742,6 +1742,20 @@
     2.4                  ])
     2.5                  AC_MSG_RESULT($have_pthread_sem)
     2.6              fi
     2.7 +            if test x$have_pthread_sem = xyes; then
     2.8 +                AC_MSG_CHECKING(for sem_timedwait)
     2.9 +                have_sem_timedwait=no
    2.10 +                AC_TRY_LINK([
    2.11 +                  #include <pthread.h>
    2.12 +                  #include <semaphore.h>
    2.13 +                ],[
    2.14 +                  sem_timedwait(NULL, NULL);
    2.15 +                ],[
    2.16 +                have_sem_timedwait=yes
    2.17 +                AC_DEFINE(HAVE_SEM_TIMEDWAIT)
    2.18 +                ])
    2.19 +                AC_MSG_RESULT($have_sem_timedwait)
    2.20 +            fi
    2.21  
    2.22              AC_MSG_CHECKING(for pthread_spin_trylock)
    2.23              AC_TRY_LINK_FUNC(pthread_spin_trylock, [
     3.1 --- a/include/SDL_config_generated.h.in	Sat Jan 14 13:21:19 2012 -0500
     3.2 +++ b/include/SDL_config_generated.h.in	Sun Jan 15 03:34:14 2012 -0500
     3.3 @@ -151,6 +151,7 @@
     3.4  #undef HAVE_ICONV
     3.5  #undef HAVE_PTHREAD_SETNAME_NP
     3.6  #undef HAVE_PTHREAD_SET_NAME_NP
     3.7 +#undef HAVE_SEM_TIMEDWAIT
     3.8  
     3.9  #else
    3.10  /* We may need some replacement for stdarg.h here */
     4.1 --- a/src/thread/pthread/SDL_syssem.c	Sat Jan 14 13:21:19 2012 -0500
     4.2 +++ b/src/thread/pthread/SDL_syssem.c	Sun Jan 15 03:34:14 2012 -0500
     4.3 @@ -103,8 +103,12 @@
     4.4  SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
     4.5  {
     4.6      int retval;
     4.7 +#ifdef HAVE_SEM_TIMEDWAIT
     4.8      struct timeval now;
     4.9      struct timespec ts_timeout;
    4.10 +#else
    4.11 +    Uint32 end;
    4.12 +#endif
    4.13  
    4.14      if (!sem) {
    4.15          SDL_SetError("Passed a NULL semaphore");
    4.16 @@ -119,6 +123,7 @@
    4.17          return SDL_SemWait(sem);
    4.18      }
    4.19  
    4.20 +#ifdef HAVE_SEM_TIMEDWAIT
    4.21      /* Setup the timeout. sem_timedwait doesn't wait for
    4.22      * a lapse of time, but until we reach a certain time.
    4.23      * This time is now plus the timeout.
    4.24 @@ -147,6 +152,15 @@
    4.25      if (retval < 0) {
    4.26          SDL_SetError("sem_timedwait() failed");
    4.27      }
    4.28 +#else
    4.29 +    end = SDL_GetTicks() + timeout;
    4.30 +    while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
    4.31 +        if ((SDL_GetTicks() - end) >= 0) {
    4.32 +            break;
    4.33 +        }
    4.34 +        SDL_Delay(0);
    4.35 +    }
    4.36 +#endif /* HAVE_SEM_TIMEDWAIT */
    4.37  
    4.38      return retval;
    4.39  }