Here are the couple of links that I found very useful for the C/C++ cross-platform development:
http://nadeausoftware.com/articles/2011/12/c_c_tip_how_list_compiler_predefined_macros
http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system
Showing posts with label C. Show all posts
Showing posts with label C. Show all posts
Tuesday, December 31, 2013
Friday, December 30, 2011
Missing pthread_mutex_timedlock on Android
I stumbled upon a problem today that the pthread_mutex_timedlock function definition is missing on Android, though it's declared in the pthread.h.
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:
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;
}
Saturday, October 08, 2011
Subscribe to:
Posts (Atom)

