Index: src/threading.h ================================================================== --- src/threading.h +++ src/threading.h @@ -18,10 +18,12 @@ #if !defined(OF_HAVE_THREADS) || \ (!defined(OF_HAVE_PTHREADS) && !defined(_WIN32)) # error No threads available! #endif + +#include #import "macros.h" #if defined(OF_HAVE_PTHREADS) # include @@ -204,10 +206,42 @@ if (WaitForSingleObject(condition->event, INFINITE) != WAIT_OBJECT_0) { of_mutex_lock(mutex); return false; } + + of_atomic_dec_int(&condition->count); + + if (!of_mutex_lock(mutex)) + return false; + + return true; +#endif +} + +static OF_INLINE bool +of_condition_timed_wait(of_condition_t *condition, of_mutex_t *mutex, + double timeout) +{ +#if defined(OF_HAVE_PTHREADS) + struct timespec ts; + + ts.tv_sec = (time_t)timeout; + ts.tv_nsec = lrint((timeout - ts.tv_sec) * 1000000000); + + return !pthread_cond_timedwait(condition, mutex, &ts); +#elif defined(_WIN32) + if (!of_mutex_unlock(mutex)) + return false; + + of_atomic_inc_int(&condition->count); + + if (WaitForSingleObject(condition->event, + timeout * 1000) != WAIT_OBJECT_0) { + of_mutex_lock(mutex); + return false; + } of_atomic_dec_int(&condition->count); if (!of_mutex_lock(mutex)) return false;