The first thought was to implement it as a loop with a call to pthread_mutex_trylock and some sleep until we are able to get the lock or until the timeout happens, felt a bit awkward.
I took a look a the Mono for Android source code and that's exactly what they do there (./mono/io-layer/mono-mutex.c), so go figure, looks like this solution might not be that bad after all.
Here is how it's implemented in Mono for example:
int
pthread_mutex_timedlock (pthread_mutex_t *mutex, CONST_NEEDED struct timespec *timeout)
{
struct timeval timenow;
struct timespec sleepytime;
int retcode;
/* This is just to avoid a completely busy wait */
sleepytime.tv_sec = 0;
sleepytime.tv_nsec = 10000000; /* 10ms */
while ((retcode = pthread_mutex_trylock (mutex)) == EBUSY) {
gettimeofday (&timenow, NULL);
if (timenow.tv_sec >= timeout->tv_sec &&
(timenow.tv_usec * 1000) >= timeout->tv_nsec) {
return ETIMEDOUT;
}
nanosleep (&sleepytime, NULL);
}
return retcode;
}
