diff options
author | Jean-Baptiste Queru <jbq@google.com> | 2009-11-15 12:06:20 -0800 |
---|---|---|
committer | Jean-Baptiste Queru <jbq@google.com> | 2009-11-15 12:06:23 -0800 |
commit | daeaa536fab8f9a71227578aab212fa5d4daaf00 (patch) | |
tree | 1de0bdf145436d2ce71bb665124fc24871ace417 /libs/utils/Threads.cpp | |
parent | 4511f47c50b39b02eb04eaac8e6a660c29ea8444 (diff) | |
parent | cc8c35cee5de7fdf2d79a1a3716120b64301cdfe (diff) | |
download | frameworks_native-daeaa536fab8f9a71227578aab212fa5d4daaf00.zip frameworks_native-daeaa536fab8f9a71227578aab212fa5d4daaf00.tar.gz frameworks_native-daeaa536fab8f9a71227578aab212fa5d4daaf00.tar.bz2 |
merge from eclair
Diffstat (limited to 'libs/utils/Threads.cpp')
-rw-r--r-- | libs/utils/Threads.cpp | 308 |
1 files changed, 24 insertions, 284 deletions
diff --git a/libs/utils/Threads.cpp b/libs/utils/Threads.cpp index 9287c0b..ec3db09 100644 --- a/libs/utils/Threads.cpp +++ b/libs/utils/Threads.cpp @@ -38,10 +38,6 @@ # define HAVE_CREATETHREAD // Cygwin, vs. HAVE__BEGINTHREADEX for MinGW #endif -#if defined(HAVE_FUTEX) -#include <private/utils/futex_synchro.h> -#endif - #if defined(HAVE_PRCTL) #include <sys/prctl.h> #endif @@ -56,10 +52,6 @@ using namespace android; // ---------------------------------------------------------------------------- #if defined(HAVE_PTHREADS) -#if 0 -#pragma mark - -#pragma mark PTHREAD -#endif // ---------------------------------------------------------------------------- /* @@ -163,10 +155,6 @@ android_thread_id_t androidGetThreadId() // ---------------------------------------------------------------------------- #elif defined(HAVE_WIN32_THREADS) -#if 0 -#pragma mark - -#pragma mark WIN32_THREADS -#endif // ---------------------------------------------------------------------------- /* @@ -252,11 +240,6 @@ android_thread_id_t androidGetThreadId() // ---------------------------------------------------------------------------- -#if 0 -#pragma mark - -#pragma mark Common Thread functions -#endif - int androidCreateThread(android_thread_func_t fn, void* arg) { return createThreadEtc(fn, arg); @@ -294,112 +277,23 @@ namespace android { * =========================================================================== */ -#if 0 -#pragma mark - -#pragma mark Mutex -#endif - -#if defined(HAVE_PTHREADS) && !defined(HAVE_FUTEX) -/* - * Simple pthread wrapper. - */ +#if defined(HAVE_PTHREADS) +// implemented as inlines in threads.h +#elif defined(HAVE_WIN32_THREADS) Mutex::Mutex() { - _init(); -} - -Mutex::Mutex(const char* name) -{ - // XXX: name not used for now - _init(); -} - -void Mutex::_init() -{ - pthread_mutex_t* pMutex = new pthread_mutex_t; - pthread_mutex_init(pMutex, NULL); - mState = pMutex; -} - -Mutex::~Mutex() -{ - delete (pthread_mutex_t*) mState; -} - -status_t Mutex::lock() -{ - int res; - while ((res=pthread_mutex_lock((pthread_mutex_t*) mState)) == EINTR) ; - return -res; -} - -void Mutex::unlock() -{ - pthread_mutex_unlock((pthread_mutex_t*) mState); -} - -status_t Mutex::tryLock() -{ - int res; - while ((res=pthread_mutex_trylock((pthread_mutex_t*) mState)) == EINTR) ; - return -res; -} - -#elif defined(HAVE_FUTEX) -#if 0 -#pragma mark - -#endif + HANDLE hMutex; -#define STATE ((futex_mutex_t*) (&mState)) + assert(sizeof(hMutex) == sizeof(mState)); -Mutex::Mutex() -{ - _init(); + hMutex = CreateMutex(NULL, FALSE, NULL); + mState = (void*) hMutex; } Mutex::Mutex(const char* name) { - _init(); -} - -void -Mutex::_init() -{ - futex_mutex_init(STATE); -} - -Mutex::~Mutex() -{ -} - -status_t Mutex::lock() -{ - int res; - while ((res=futex_mutex_lock(STATE, FUTEX_WAIT_INFINITE)) == EINTR) ; - return -res; -} - -void Mutex::unlock() -{ - futex_mutex_unlock(STATE); -} - -status_t Mutex::tryLock() -{ - int res; - while ((res=futex_mutex_trylock(STATE)) == EINTR) ; - return -res; -} -#undef STATE - -#elif defined(HAVE_WIN32_THREADS) -#if 0 -#pragma mark - -#endif - -Mutex::Mutex() -{ + // XXX: name not used for now HANDLE hMutex; assert(sizeof(hMutex) == sizeof(mState)); @@ -408,11 +302,13 @@ Mutex::Mutex() mState = (void*) hMutex; } -Mutex::Mutex(const char* name) +Mutex::Mutex(int type, const char* name) { - // XXX: name not used for now + // XXX: type and name not used for now HANDLE hMutex; + assert(sizeof(hMutex) == sizeof(mState)); + hMutex = CreateMutex(NULL, FALSE, NULL); mState = (void*) hMutex; } @@ -456,161 +352,9 @@ status_t Mutex::tryLock() * =========================================================================== */ -#if 0 -#pragma mark - -#pragma mark Condition -#endif - -#if defined(HAVE_PTHREADS) && !defined(HAVE_FUTEX) - -/* - * Constructor. This is a simple pthread wrapper. - */ -Condition::Condition() -{ - pthread_cond_t* pCond = new pthread_cond_t; - - pthread_cond_init(pCond, NULL); - mState = pCond; -} - -/* - * Destructor. - */ -Condition::~Condition() -{ - pthread_cond_destroy((pthread_cond_t*) mState); - delete (pthread_cond_t*) mState; -} - -/* - * Wait on a condition variable. Lock the mutex before calling. - */ - -status_t Condition::wait(Mutex& mutex) -{ - assert(mutex.mState != NULL); - - int cc; - while ((cc = pthread_cond_wait((pthread_cond_t*)mState, - (pthread_mutex_t*) mutex.mState)) == EINTR) ; - return -cc; -} - -status_t Condition::wait(Mutex& mutex, nsecs_t abstime) -{ - assert(mutex.mState != NULL); - - struct timespec ts; - ts.tv_sec = abstime/1000000000; - ts.tv_nsec = abstime-(ts.tv_sec*1000000000); - - int cc; - while ((cc = pthread_cond_timedwait((pthread_cond_t*)mState, - (pthread_mutex_t*) mutex.mState, &ts)) == EINTR) ; - return -cc; -} - -status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) -{ - return wait(mutex, systemTime()+reltime); -} - -/* - * Signal the condition variable, allowing one thread to continue. - */ -void Condition::signal() -{ - pthread_cond_signal((pthread_cond_t*) mState); -} - -/* - * Signal the condition variable, allowing all threads to continue. - */ -void Condition::broadcast() -{ - pthread_cond_broadcast((pthread_cond_t*) mState); -} - -#elif defined(HAVE_FUTEX) -#if 0 -#pragma mark - -#endif - -#define STATE ((futex_cond_t*) (&mState)) - -/* - * Constructor. This is a simple pthread wrapper. - */ -Condition::Condition() -{ - futex_cond_init(STATE); -} - -/* - * Destructor. - */ -Condition::~Condition() -{ -} - -/* - * Wait on a condition variable. Lock the mutex before calling. - */ - -status_t Condition::wait(Mutex& mutex) -{ - assert(mutex.mState != NULL); - - int res; - while ((res = futex_cond_wait(STATE, - (futex_mutex_t*)(&mutex.mState), FUTEX_WAIT_INFINITE)) == -EINTR) ; - - return -res; -} - -status_t Condition::wait(Mutex& mutex, nsecs_t abstime) -{ - nsecs_t reltime = abstime - systemTime(); - if (reltime <= 0) return true; - return waitRelative(mutex, reltime); -} - -status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) -{ - assert(mutex.mState != NULL); - int res; - unsigned msec = ns2ms(reltime); - if(msec == 0) - return true; - // This code will not time out at the correct time if interrupted by signals - while ((res = futex_cond_wait(STATE, - (futex_mutex_t*)(&mutex.mState), msec)) == -EINTR) ; - return res; -} - -/* - * Signal the condition variable, allowing one thread to continue. - */ -void Condition::signal() -{ - futex_cond_signal(STATE); -} - -/* - * Signal the condition variable, allowing all threads to continue. - */ -void Condition::broadcast() -{ - futex_cond_broadcast(STATE); -} - -#undef STATE - +#if defined(HAVE_PTHREADS) +// implemented as inlines in threads.h #elif defined(HAVE_WIN32_THREADS) -#if 0 -#pragma mark - -#endif /* * Windows doesn't have a condition variable solution. It's possible @@ -753,17 +497,13 @@ status_t Condition::wait(Mutex& mutex) return ((WinCondition*)mState)->wait(condState, hMutex, NULL); } -status_t Condition::wait(Mutex& mutex, nsecs_t abstime) +status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) { WinCondition* condState = (WinCondition*) mState; HANDLE hMutex = (HANDLE) mutex.mState; + nsecs_t absTime = systemTime()+reltime; - return ((WinCondition*)mState)->wait(condState, hMutex, &abstime); -} - -status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) -{ - return wait(mutex, systemTime()+reltime); + return ((WinCondition*)mState)->wait(condState, hMutex, &absTime); } /* @@ -841,11 +581,6 @@ void Condition::broadcast() // ---------------------------------------------------------------------------- -#if 0 -#pragma mark - -#pragma mark Thread::Thread -#endif - /* * This is our thread object! */ @@ -920,6 +655,11 @@ int Thread::_threadLoop(void* user) wp<Thread> weak(strong); self->mHoldSelf.clear(); +#if HAVE_ANDROID_OS + // this is very useful for debugging with gdb + self->mTid = gettid(); +#endif + bool first = true; do { @@ -950,7 +690,7 @@ int Thread::_threadLoop(void* user) self->mExitPending = true; self->mLock.lock(); self->mRunning = false; - self->mThreadExitedCondition.signal(); + self->mThreadExitedCondition.broadcast(); self->mLock.unlock(); break; } @@ -958,7 +698,7 @@ int Thread::_threadLoop(void* user) // Release our strong reference, to let a chance to the thread // to die a peaceful death. strong.clear(); - // And immediately, reacquire a strong reference for the next loop + // And immediately, re-acquire a strong reference for the next loop strong = weak.promote(); } while(strong != 0); |