Use a backup in case pthread_mutex_timedlock isn't available

This commit is contained in:
Chris Robinson 2014-05-01 14:15:31 -07:00
parent 804b08e0ca
commit d90f684f16
3 changed files with 28 additions and 1 deletions

View File

@ -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
}

View File

@ -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})

View File

@ -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