X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fplatform%2Fposix%2Fos-threads.h;h=8b56731adfa2918f2a290e71dd4acc62ce2b7709;hb=a75e3a5a63546d6f7e670bc2a7a1931887a5d2a0;hp=c49260ca68d88f1153bb9106237059edb2d8eeb7;hpb=9b53a148ade63466655e6e5d8cb0ca2b64a76ae3;p=deb_libcec.git diff --git a/src/lib/platform/posix/os-threads.h b/src/lib/platform/posix/os-threads.h index c49260c..8b56731 100644 --- a/src/lib/platform/posix/os-threads.h +++ b/src/lib/platform/posix/os-threads.h @@ -48,13 +48,19 @@ namespace PLATFORM inline struct timespec GetAbsTime(uint64_t iIncreaseBy = 0) { - struct timespec abstime; - struct timeval now; - gettimeofday(&now, NULL); - iIncreaseBy += now.tv_usec / 1000; - abstime.tv_sec = now.tv_sec + (time_t)(iIncreaseBy / 1000); - abstime.tv_nsec = (int32_t)((iIncreaseBy % (uint32_t)1000) * (uint32_t)1000000); - return abstime; + struct timespec now; + #ifdef __APPLE__ + struct timeval tv; + gettimeofday(&tv, NULL); + now.tv_sec = tv.tv_sec; + now.tv_nsec = tv.tv_usec * 1000; + #else + clock_gettime(CLOCK_REALTIME, &now); + #endif + now.tv_nsec += iIncreaseBy % 1000 * 1000000; + now.tv_sec += iIncreaseBy / 1000 + now.tv_nsec / 1000000000; + now.tv_nsec %= 1000000000; + return now; } typedef pthread_t thread_t; @@ -69,19 +75,45 @@ namespace PLATFORM #define MutexTryLock(mutex) (pthread_mutex_trylock(&mutex) == 0) #define MutexUnlock(mutex) pthread_mutex_unlock(&mutex) - typedef pthread_cond_t condition_t; - #define ConditionCreate(cond) pthread_cond_init(&cond, NULL) - #define ConditionDelete(cond) pthread_cond_destroy(&cond) - #define ConditionSignal(cond) pthread_cond_signal(&cond) - #define ConditionBroadcast(cond) pthread_cond_broadcast(&cond) - inline bool ConditionWait(condition_t &cond, mutex_t &mutex, uint32_t iTimeout) + class CConditionImpl { - sched_yield(); - if (iTimeout > 0) + public: + CConditionImpl(void) { - struct timespec timeout = GetAbsTime(iTimeout); - return (pthread_cond_timedwait(&cond, &mutex, &timeout) == 0); + pthread_cond_init(&m_condition, NULL); } - return (pthread_cond_wait(&cond, &mutex) == 0); - } + + virtual ~CConditionImpl(void) + { + pthread_cond_destroy(&m_condition); + } + + void Signal(void) + { + pthread_cond_signal(&m_condition); + } + + void Broadcast(void) + { + pthread_cond_broadcast(&m_condition); + } + + bool Wait(mutex_t &mutex) + { + sched_yield(); + return (pthread_cond_wait(&m_condition, &mutex) == 0); + } + + bool Wait(mutex_t &mutex, uint32_t iTimeoutMs) + { + if (iTimeoutMs == 0) + return Wait(mutex); + + sched_yield(); + struct timespec timeout = GetAbsTime(iTimeoutMs); + return (pthread_cond_timedwait(&m_condition, &mutex, &timeout) == 0); + } + + pthread_cond_t m_condition; + }; }