diff options
author | Chris Robinson <[email protected]> | 2014-05-01 14:15:31 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2014-05-01 14:15:31 -0700 |
commit | d90f684f162d4b91a58b61bb2a60ec1257c1dd70 (patch) | |
tree | aa209d2a153ddb4574b86bc31c9a8060531bb90a | |
parent | 804b08e0ca7931c304d6a2686b036a397cb65b0e (diff) |
Use a backup in case pthread_mutex_timedlock isn't available
-rw-r--r-- | Alc/threads.c | 24 | ||||
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | config.h.in | 3 |
3 files changed, 28 insertions, 1 deletions
diff --git a/Alc/threads.c b/Alc/threads.c index 81db4297..0325c104 100644 --- a/Alc/threads.c +++ b/Alc/threads.c @@ -447,7 +447,10 @@ void almtx_destroy(almtx_t *mtx) int almtx_timedlock(almtx_t *mtx, const struct timespec *ts) { - int ret = pthread_mutex_timedlock(mtx, ts); + int ret; + +#ifdef HAVE_PTHREAD_MUTEX_TIMEDLOCK + ret = pthread_mutex_timedlock(mtx, ts); switch(ret) { case 0: return althrd_success; @@ -455,6 +458,25 @@ int almtx_timedlock(almtx_t *mtx, const struct timespec *ts) case EBUSY: return althrd_busy; } return althrd_error; +#else + if(!mtx || !ts) + return althrd_error; + + while((ret=almtx_trylock(mtx)) == althrd_busy) + { + struct timespec now; + + if(ts->tv_sec < 0 || ts->tv_nsec < 0 || ts->tv_nsec >= 1000000000 || + altimespec_get(&now, AL_TIME_UTC) != AL_TIME_UTC) + return althrd_error; + if(now.tv_sec > ts->tv_sec || (now.tv_sec == ts->tv_sec && now.tv_nsec >= ts->tv_nsec)) + return althrd_timedout; + + althrd_yield(); + } + + return ret; +#endif } diff --git a/CMakeLists.txt b/CMakeLists.txt index e42288ad..3f1674a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -460,6 +460,8 @@ IF(NOT HAVE_WINDOWS_H) CHECK_SYMBOL_EXISTS(pthread_mutexattr_setkind_np "pthread.h;pthread_np.h" HAVE_PTHREAD_MUTEXATTR_SETKIND_NP) ENDIF() + CHECK_SYMBOL_EXISTS(pthread_mutex_timedlock pthread.h HAVE_PTHREAD_MUTEX_TIMEDLOCK) + CHECK_LIBRARY_EXISTS(rt clock_gettime "" HAVE_LIBRT) IF(HAVE_LIBRT) SET(EXTRA_LIBS rt ${EXTRA_LIBS}) diff --git a/config.h.in b/config.h.in index 33eb5b62..e8b944f6 100644 --- a/config.h.in +++ b/config.h.in @@ -184,3 +184,6 @@ /* Define if we have pthread_mutexattr_setkind_np() */ #cmakedefine HAVE_PTHREAD_MUTEXATTR_SETKIND_NP + +/* Define if we have pthread_mutex_timedlock() */ +#cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK |