From f511a568c0249673a76955d1fce64413d8b4f16c Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 15 Jan 2012 03:34:14 -0500 Subject: [PATCH] Check for sem_timedwait(), which isn't available on some systems (including OpenBSD --- configure | 61 +++++++++++++++++++++++++++++++ configure.in | 14 +++++++ include/SDL_config_generated.h.in | 1 + src/thread/pthread/SDL_syssem.c | 14 +++++++ 4 files changed, 90 insertions(+) diff --git a/configure b/configure index 3717666f1..6348d3545 100755 --- a/configure +++ b/configure @@ -24485,6 +24485,67 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext { echo "$as_me:$LINENO: result: $have_pthread_sem" >&5 echo "${ECHO_T}$have_pthread_sem" >&6; } fi + if test x$have_pthread_sem = xyes; then + { echo "$as_me:$LINENO: checking for sem_timedwait" >&5 +echo $ECHO_N "checking for sem_timedwait... $ECHO_C" >&6; } + have_sem_timedwait=no + cat >conftest.$ac_ext <<_ACEOF +/* confdefs.h. */ +_ACEOF +cat confdefs.h >>conftest.$ac_ext +cat >>conftest.$ac_ext <<_ACEOF +/* end confdefs.h. */ + + #include + #include + +int +main () +{ + + sem_timedwait(NULL, NULL); + + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (ac_try="$ac_link" +case "(($ac_try" in + *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; + *) ac_try_echo=$ac_try;; +esac +eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5 + (eval "$ac_link") 2>conftest.er1 + ac_status=$? + grep -v '^ *+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && { + test -z "$ac_c_werror_flag" || + test ! -s conftest.err + } && test -s conftest$ac_exeext && + $as_test_x conftest$ac_exeext; then + + have_sem_timedwait=yes + cat >>confdefs.h <<\_ACEOF +#define HAVE_SEM_TIMEDWAIT 1 +_ACEOF + + +else + echo "$as_me: failed program was:" >&5 +sed 's/^/| /' conftest.$ac_ext >&5 + + +fi + +rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \ + conftest$ac_exeext conftest.$ac_ext + { echo "$as_me:$LINENO: result: $have_sem_timedwait" >&5 +echo "${ECHO_T}$have_sem_timedwait" >&6; } + fi { echo "$as_me:$LINENO: checking for pthread_spin_trylock" >&5 echo $ECHO_N "checking for pthread_spin_trylock... $ECHO_C" >&6; } diff --git a/configure.in b/configure.in index 501114941..6659d97f4 100644 --- a/configure.in +++ b/configure.in @@ -1742,6 +1742,20 @@ AC_HELP_STRING([--enable-pthread-sem], [use pthread semaphores [[default=yes]]]) ]) AC_MSG_RESULT($have_pthread_sem) fi + if test x$have_pthread_sem = xyes; then + AC_MSG_CHECKING(for sem_timedwait) + have_sem_timedwait=no + AC_TRY_LINK([ + #include + #include + ],[ + sem_timedwait(NULL, NULL); + ],[ + have_sem_timedwait=yes + AC_DEFINE(HAVE_SEM_TIMEDWAIT) + ]) + AC_MSG_RESULT($have_sem_timedwait) + fi AC_MSG_CHECKING(for pthread_spin_trylock) AC_TRY_LINK_FUNC(pthread_spin_trylock, [ diff --git a/include/SDL_config_generated.h.in b/include/SDL_config_generated.h.in index 78b04b527..0a9f40209 100644 --- a/include/SDL_config_generated.h.in +++ b/include/SDL_config_generated.h.in @@ -151,6 +151,7 @@ #undef HAVE_ICONV #undef HAVE_PTHREAD_SETNAME_NP #undef HAVE_PTHREAD_SET_NAME_NP +#undef HAVE_SEM_TIMEDWAIT #else /* We may need some replacement for stdarg.h here */ diff --git a/src/thread/pthread/SDL_syssem.c b/src/thread/pthread/SDL_syssem.c index 291ed371c..01d8f461a 100755 --- a/src/thread/pthread/SDL_syssem.c +++ b/src/thread/pthread/SDL_syssem.c @@ -103,8 +103,12 @@ int SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout) { int retval; +#ifdef HAVE_SEM_TIMEDWAIT struct timeval now; struct timespec ts_timeout; +#else + Uint32 end; +#endif if (!sem) { SDL_SetError("Passed a NULL semaphore"); @@ -119,6 +123,7 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout) return SDL_SemWait(sem); } +#ifdef HAVE_SEM_TIMEDWAIT /* Setup the timeout. sem_timedwait doesn't wait for * a lapse of time, but until we reach a certain time. * This time is now plus the timeout. @@ -147,6 +152,15 @@ SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout) if (retval < 0) { SDL_SetError("sem_timedwait() failed"); } +#else + end = SDL_GetTicks() + timeout; + while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) { + if ((SDL_GetTicks() - end) >= 0) { + break; + } + SDL_Delay(0); + } +#endif /* HAVE_SEM_TIMEDWAIT */ return retval; }