X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fplatform%2Fposix%2Fos-threads.h;h=7b7fc3c118273812ee99001f99b46f2acfe26baf;hb=103eb041ef46f322b3c374da47feabab6c378004;hp=48e1e355ca16e8ee18965852cda41bfd48329c63;hpb=97cd34d8145e0cecf05e7a348b2a37614f2a9060;p=deb_libcec.git diff --git a/src/lib/platform/posix/os-threads.h b/src/lib/platform/posix/os-threads.h index 48e1e35..7b7fc3c 100644 --- a/src/lib/platform/posix/os-threads.h +++ b/src/lib/platform/posix/os-threads.h @@ -2,7 +2,7 @@ /* * This file is part of the libCEC(R) library. * - * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. + * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved. * libCEC(R) is an original work, containing original code. * * libCEC(R) is a trademark of Pulse-Eight Limited. @@ -48,19 +48,26 @@ 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; + #define INVALID_THREAD_VALUE 0 #define ThreadsCreate(thread, func, arg) (pthread_create(&thread, NULL, (void *(*) (void *))func, (void *)arg) == 0) - #define ThreadsWait(thread, retval) (pthread_join(thread, retval) == 0) + #define ThreadsWait(thread, retval) (thread ? pthread_join(thread, retval) == 0 : true) typedef pthread_mutex_t mutex_t; #define MutexCreate(mutex) pthread_mutex_init(&mutex, GetRecursiveMutexAttribute()); @@ -92,17 +99,22 @@ namespace PLATFORM pthread_cond_broadcast(&m_condition); } - bool Wait(mutex_t &mutex, uint32_t iTimeoutMs) + bool Wait(mutex_t &mutex) { sched_yield(); - if (iTimeoutMs > 0) - { - struct timespec timeout = GetAbsTime(iTimeoutMs); - return (pthread_cond_timedwait(&m_condition, &mutex, &timeout) == 0); - } 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; }; }