OSDN Git Service

auto import from //depot/cupcake/@136654
authorThe Android Open Source Project <initial-contribution@android.com>
Fri, 6 Mar 2009 01:04:47 +0000 (17:04 -0800)
committerThe Android Open Source Project <initial-contribution@android.com>
Fri, 6 Mar 2009 01:04:47 +0000 (17:04 -0800)
android/thread_init.cpp

index a256024..bb9bef1 100644 (file)
 #include "omx_core.h"
 
 using namespace android;
-bool gotkey=false;
-pthread_key_t ptkey=NULL;
+static pthread_key_t ptkey=NULL;
 
-void keydestructor(void*)
+static void keydestructor(void*)
 {
     // This thread is about to exit, so we can un-initialize
     // PV for this thread.
     UninitializeForThread();
 }
 
+static pthread_once_t create_tls_entry_once = PTHREAD_ONCE_INIT;
+
+static void CreateTLSEntry() {
+    LOG_ALWAYS_FATAL_IF(
+            0 != pthread_key_create(&ptkey, keydestructor),
+            "Ran out of TLS entries");
+}
+
 template<class DestructClass>
 class LogAppenderDestructDealloc : public OsclDestructDealloc 
 {
@@ -49,22 +56,9 @@ public:
     }
 };
 
-
 bool InitializeForThread()
 {
-    // TODO: fix the race condition here that surfaces when
-    // two threads call the initializer at pretty much the
-    // same time. This does not happen in practice, but we
-    // should guard against it anyway.
-
-    if (!gotkey)
-    {
-        if (0 != pthread_key_create(&ptkey, keydestructor)) {
-            LOGE("Out of TLS keys");
-            return false;
-        }
-        gotkey = true;
-    }
+    pthread_once(&create_tls_entry_once, &CreateTLSEntry);
 
     if (NULL == pthread_getspecific(ptkey)) {
         // PV hasn't yet been initialized for this thread;