summaryrefslogtreecommitdiffstats
path: root/include/utils
diff options
context:
space:
mode:
authorMathias Agopian <mathias@google.com>2009-07-12 23:11:20 -0700
committerMathias Agopian <mathias@google.com>2009-07-13 15:14:23 -0700
commitb1c4ca5ad21ae63cd5af2cfc1be82ba176d25195 (patch)
tree5a70f426d9b4cd75638a1b17ae2abd264c59eb55 /include/utils
parent56e7c2d6ebbde59555041e3550cbba6824486db1 (diff)
downloadframeworks_base-b1c4ca5ad21ae63cd5af2cfc1be82ba176d25195.zip
frameworks_base-b1c4ca5ad21ae63cd5af2cfc1be82ba176d25195.tar.gz
frameworks_base-b1c4ca5ad21ae63cd5af2cfc1be82ba176d25195.tar.bz2
implement Mutex and Condition with pthread instead of calling futex directly.
internally pthread uses futex. the implementation consists of simple inlines there are no implementation files anymore.
Diffstat (limited to 'include/utils')
-rw-r--r--include/utils/threads.h90
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!