[libcxxabi] Refactor pthread usage into a separate API

This patch refactors all pthread uses of libc++abi into a separate API. This
is the first step towards supporting an externlly-threaded libc++abi library.

I've followed the conventions already used in the libc++ library for the same
purpose.

Patch from: Saleem Abdulrasool and Asiri Rathnayake

Reviewed by: compnerd, EricWF

Differential revisions:
  https://reviews.llvm.org/D18482 (original)
  https://reviews.llvm.org/D24864 (final)

git-svn-id: https://llvm.org/svn/llvm-project/libcxxabi/trunk@284128 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/src/cxa_exception_storage.cpp b/src/cxa_exception_storage.cpp
index ec69094..d18eb0f 100644
--- a/src/cxa_exception_storage.cpp
+++ b/src/cxa_exception_storage.cpp
@@ -14,6 +14,7 @@
 #include "cxa_exception.hpp"
 
 #include "config.h"
+#include "threading_support.h"
 
 #if defined(_LIBCXXABI_HAS_NO_THREADS)
 
@@ -44,28 +45,27 @@
 
 #else
 
-#include <pthread.h>
 #include "abort_message.h"
 #include "fallback_malloc.h"
 
-//  In general, we treat all pthread errors as fatal.
+//  In general, we treat all threading errors as fatal.
 //  We cannot call std::terminate() because that will in turn
 //  call __cxa_get_globals() and cause infinite recursion.
 
 namespace __cxxabiv1 {
 namespace {
-    pthread_key_t  key_;
-    pthread_once_t flag_ = PTHREAD_ONCE_INIT;
+    __libcxxabi_tls_key key_;
+    __libcxxabi_exec_once_flag flag_ = _LIBCXXABI_EXEC_ONCE_INITIALIZER;
 
     void destruct_ (void *p) {
         __free_with_fallback ( p );
-        if ( 0 != ::pthread_setspecific ( key_, NULL ) ) 
+        if ( 0 != __libcxxabi_tls_set ( key_, NULL ) )	
             abort_message("cannot zero out thread value for __cxa_get_globals()");
         }
 
     void construct_ () {
-        if ( 0 != pthread_key_create ( &key_, destruct_ ) )
-            abort_message("cannot create pthread key for __cxa_get_globals()");
+        if ( 0 != __libcxxabi_tls_create ( &key_, destruct_ ) )
+            abort_message("cannot create thread specific key for __cxa_get_globals()");
         }
 }   
 
@@ -80,8 +80,8 @@
                         (__calloc_with_fallback (1, sizeof (__cxa_eh_globals)));
             if ( NULL == retVal )
                 abort_message("cannot allocate __cxa_eh_globals");
-            if ( 0 != pthread_setspecific ( key_, retVal ) )
-               abort_message("pthread_setspecific failure in __cxa_get_globals()");
+            if ( 0 != __libcxxabi_tls_set ( key_, retVal ) )
+               abort_message("__libcxxabi_tls_set failure in __cxa_get_globals()");
            }
         return retVal;
         }
@@ -92,10 +92,10 @@
     // libc++abi.
     __cxa_eh_globals * __cxa_get_globals_fast () {
     //  First time through, create the key.
-        if (0 != pthread_once(&flag_, construct_))
-            abort_message("pthread_once failure in __cxa_get_globals_fast()");
+        if (0 != __libcxxabi_execute_once(&flag_, construct_))
+            abort_message("execute once failure in __cxa_get_globals_fast()");
 //        static int init = construct_();
-        return static_cast<__cxa_eh_globals*>(::pthread_getspecific(key_));
+        return static_cast<__cxa_eh_globals*>(__libcxxabi_tls_get(key_));
         }
     
 }