diff options
Diffstat (limited to 'include/utils')
-rw-r--r-- | include/utils/threads.h | 90 |
1 files changed, 86 insertions, 4 deletions
diff --git a/include/utils/threads.h b/include/utils/threads.h index e0cb664..5c03965 100644 --- a/include/utils/threads.h +++ b/include/utils/threads.h @@ -21,6 +21,10 @@ #include <sys/types.h> #include <time.h> +#if defined(HAVE_PTHREADS) +# include <pthread.h> +#endif + // ------------------------------------------------------------------ // C API @@ -176,6 +180,8 @@ inline thread_id_t getThreadId() { return androidGetThreadId(); } +/*****************************************************************************/ + /* * Simple mutex class. The implementation is system-dependent. * @@ -212,11 +218,38 @@ private: // A mutex cannot be copied Mutex(const Mutex&); Mutex& operator = (const Mutex&); - void _init(); +#if defined(HAVE_PTHREADS) + pthread_mutex_t mMutex; +#else + void _init(); void* mState; +#endif }; +#if defined(HAVE_PTHREADS) + +inline Mutex::Mutex() { + pthread_mutex_init(&mMutex, NULL); +} +inline Mutex::Mutex(const char* name) { + pthread_mutex_init(&mMutex, NULL); +} +inline Mutex::~Mutex() { + pthread_mutex_destroy(&mMutex); +} +inline status_t Mutex::lock() { + return -pthread_mutex_lock(&mMutex); +} +inline void Mutex::unlock() { + pthread_mutex_unlock(&mMutex); +} +inline status_t Mutex::tryLock() { + return -pthread_mutex_trylock(&mMutex); +} + +#endif // HAVE_PTHREADS + /* * Automatic mutex. Declare one of these at the top of a function. * When the function returns, it will go out of scope, and release the @@ -225,6 +258,7 @@ private: typedef Mutex::Autolock AutoMutex; +/*****************************************************************************/ /* * Condition variable class. The implementation is system-dependent. @@ -240,9 +274,6 @@ public: ~Condition(); // Wait on the condition variable. Lock the mutex before calling. status_t wait(Mutex& mutex); - // Wait on the condition variable until the given time. Lock the mutex - // before calling. - status_t wait(Mutex& mutex, nsecs_t abstime); // same with relative timeout status_t waitRelative(Mutex& mutex, nsecs_t reltime); // Signal the condition variable, allowing one thread to continue. @@ -251,9 +282,60 @@ public: void broadcast(); private: +#if defined(HAVE_PTHREADS) + pthread_cond_t mCond; +#else void* mState; +#endif }; +#if defined(HAVE_PTHREADS) + +inline Condition::Condition() { + pthread_cond_init(&mCond, NULL); +} +inline Condition::~Condition() { + pthread_cond_destroy(&mCond); +} +inline status_t Condition::wait(Mutex& mutex) { + return -pthread_cond_wait(&mCond, &mutex.mMutex); +} +inline status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) { +#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE) + struct timespec ts; + ts.tv_sec = reltime/1000000000; + ts.tv_nsec = reltime%1000000000; + return -pthread_cond_timedwait_relative_np(&mCond, &mutex.mMutex, &ts); +#else // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE + struct timespec ts; +#if defined(HAVE_POSIX_CLOCKS) + clock_gettime(CLOCK_REALTIME, &ts); +#else // HAVE_POSIX_CLOCKS + // we don't support the clocks here. + struct timeval t; + gettimeofday(&t, NULL); + ts.tv_sec = t.tv_sec; + ts.tv_nsec= t.tv_usec*1000; +#endif // HAVE_POSIX_CLOCKS + ts.tv_sec += reltime/1000000000; + ts.tv_nsec+= reltime%1000000000; + if (ts.tv_nsec >= 1000000000) { + ts.tv_nsec -= 1000000000; + ts.tv_sec += 1; + } + return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts); +#endif // HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE +} +inline void Condition::signal() { + pthread_cond_signal(&mCond); +} +inline void Condition::broadcast() { + pthread_cond_broadcast(&mCond); +} + +#endif // HAVE_PTHREADS + +/*****************************************************************************/ /* * This is our spiffy thread object! |