Improve pthread_mutex_timedlock detection, fix Android function definition

This commit is contained in:
Marc-André Moreau 2021-05-27 16:42:19 -04:00 committed by akallabeth
parent 555944f027
commit a8355d4117
2 changed files with 21 additions and 13 deletions

View File

@ -355,17 +355,9 @@ else()
endif()
if(NOT WIN32)
CHECK_SYMBOL_EXISTS(pthread_mutex_timedlock pthread.h HAVE_PTHREAD_MUTEX_TIMEDLOCK_SYMBOL)
if (NOT HAVE_PTHREAD_MUTEX_TIMEDLOCK_SYMBOL)
CHECK_LIBRARY_EXISTS(pthread pthread_mutex_timedlock "" HAVE_PTHREAD_MUTEX_TIMEDLOCK_LIB)
endif (NOT HAVE_PTHREAD_MUTEX_TIMEDLOCK_SYMBOL)
if (NOT HAVE_PTHREAD_MUTEX_TIMEDLOCK_LIB)
CHECK_LIBRARY_EXISTS(pthreads pthread_mutex_timedlock "" HAVE_PTHREAD_MUTEX_TIMEDLOCK_LIBS)
endif (NOT HAVE_PTHREAD_MUTEX_TIMEDLOCK_LIB)
if (HAVE_PTHREAD_MUTEX_TIMEDLOCK_SYMBOL OR HAVE_PTHREAD_MUTEX_TIMEDLOCK_LIB OR HAVE_PTHREAD_MUTEX_TIMEDLOCK_LIBS)
set(HAVE_PTHREAD_MUTEX_TIMEDLOCK ON)
endif (HAVE_PTHREAD_MUTEX_TIMEDLOCK_SYMBOL OR HAVE_PTHREAD_MUTEX_TIMEDLOCK_LIB OR HAVE_PTHREAD_MUTEX_TIMEDLOCK_LIBS)
list(APPEND CMAKE_REQUIRED_LIBRARIES pthread)
check_symbol_exists(pthread_mutex_timedlock pthread.h HAVE_PTHREAD_MUTEX_TIMEDLOCK)
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES pthread)
endif()
# Enable address sanitizer, where supported and when required

View File

@ -115,7 +115,10 @@ int _mach_safe_clock_gettime(int clk_id, struct timespec* t)
#endif
/* Drop in replacement for pthread_mutex_timedlock
/**
* Drop in replacement for pthread_mutex_timedlock
* http://code.google.com/p/android/issues/detail?id=7807
* http://aleksmaus.blogspot.ca/2011/12/missing-pthreadmutextimedlock-on.html
*/
#if !defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK)
#include <pthread.h>
@ -127,7 +130,20 @@ static long long ts_difftime(const struct timespec* o, const struct timespec* n)
return newValue - oldValue;
}
static int pthread_mutex_timedlock(pthread_mutex_t* mutex, const struct timespec* timeout)
#ifdef ANDROID
#if (__ANDROID_API__ >= 21)
#define CONST_NEEDED const
#else
#define CONST_NEEDED
#endif
#define STATIC_NEEDED
#else /* ANDROID */
#define CONST_NEEDED const
#define STATIC_NEEDED static
#endif
STATIC_NEEDED int pthread_mutex_timedlock(pthread_mutex_t* mutex,
CONST_NEEDED struct timespec* timeout)
{
struct timespec timenow;
struct timespec sleepytime;