OSDN Git Service

brightness tuning
[gb-231r1-is01/GB_2.3_IS01.git] / frameworks / base / libs / utils / Threads.cpp
index 0b360f4..21d789e 100644 (file)
@@ -362,26 +362,72 @@ namespace android {
  */
 
 #if defined(HAVE_PTHREADS)
-// implemented as inlines in threads.h
-#elif defined(HAVE_WIN32_THREADS)
+Condition::Condition() {
+    pthread_cond_init(&mCond, NULL);
+}
+Condition::Condition(int type) {
+    if (type == SHARED) {
+        pthread_condattr_t attr;
+        pthread_condattr_init(&attr);
+        pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+        pthread_cond_init(&mCond, &attr);
+        pthread_condattr_destroy(&attr);
+    } else {
+        pthread_cond_init(&mCond, NULL);
+    }
+}
+Condition::~Condition() {
+    pthread_cond_destroy(&mCond);
+}
+status_t Condition::wait(Mutex& mutex) {
+    return -pthread_cond_wait(&mCond, &mutex.mMutex);
+}
+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
+}
+void Condition::signal() {
+    pthread_cond_signal(&mCond);
+}
+void Condition::broadcast() {
+    pthread_cond_broadcast(&mCond);
+}
 
+#elif defined(HAVE_WIN32_THREADS)
 Mutex::Mutex()
-{
-    HANDLE hMutex;
 
+  HANDLE hMutex;
     assert(sizeof(hMutex) == sizeof(mState));
-
     hMutex = CreateMutex(NULL, FALSE, NULL);
-    mState = (void*) hMutex;
-}
+   mState = (void*) hMutex;
 
 Mutex::Mutex(const char* name)
-{
-    // XXX: name not used for now
-    HANDLE hMutex;
 
+   // XXX: name not used for now
+   HANDLE hMutex;
     assert(sizeof(hMutex) == sizeof(mState));
-
     hMutex = CreateMutex(NULL, FALSE, NULL);
     mState = (void*) hMutex;
 }
@@ -437,7 +483,37 @@ status_t Mutex::tryLock()
  */
 
 #if defined(HAVE_PTHREADS)
-// implemented as inlines in threads.h
+Mutex::Mutex() {
+    pthread_mutex_init(&mMutex, NULL);
+}
+Mutex::Mutex(const char* name) {
+    pthread_mutex_init(&mMutex, NULL);
+}
+Mutex::Mutex(int type, const char* name) {
+    if (type == SHARED) {
+        pthread_mutexattr_t attr;
+        pthread_mutexattr_init(&attr);
+        pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+        pthread_mutex_init(&mMutex, &attr);
+        pthread_mutexattr_destroy(&attr);
+    } else {
+        pthread_mutex_init(&mMutex, NULL);
+    }
+}
+Mutex::~Mutex() {
+    pthread_mutex_destroy(&mMutex);
+}
+status_t Mutex::lock() {
+    return -pthread_mutex_lock(&mMutex);
+}
+void Mutex::unlock() {
+    pthread_mutex_unlock(&mMutex);
+}
+status_t Mutex::tryLock() {
+    return -pthread_mutex_trylock(&mMutex);
+}
+
+
 #elif defined(HAVE_WIN32_THREADS)
 
 /*