[libcxxabi] Introduce an externally threaded libc++abi variant.

r281179 Introduced an externally threaded variant of the libc++ library. This
patch adds support for a similar library variant for libc++abi.

Differential revision: https://reviews.llvm.org/D27575

Reviewers: EricWF
llvm-svn: 290888
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: 97ba9fae1f536778b1aaff4220512626e6170728
diff --git a/src/config.h b/src/config.h
index 625ad1b..a2ece32 100644
--- a/src/config.h
+++ b/src/config.h
@@ -42,6 +42,7 @@
 
 // Try and deduce a threading api if one has not been explicitly set.
 #if !defined(_LIBCXXABI_HAS_NO_THREADS) && \
+    !defined(_LIBCXXABI_HAS_THREAD_API_EXTERNAL) && \
     !defined(_LIBCXXABI_USE_THREAD_API_PTHREAD)
   #if defined(_POSIX_THREADS) && _POSIX_THREADS >= 0
     #define _LIBCXXABI_USE_THREAD_API_PTHREAD
diff --git a/src/cxa_exception.cpp b/src/cxa_exception.cpp
index f84c0e8..757b3d4 100644
--- a/src/cxa_exception.cpp
+++ b/src/cxa_exception.cpp
@@ -12,7 +12,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "config.h"
-#include "threading_support.h"
 #include "cxxabi.h"
 
 #include <exception>        // for std::terminate
diff --git a/src/cxa_exception_storage.cpp b/src/cxa_exception_storage.cpp
index d18eb0f..549d747 100644
--- a/src/cxa_exception_storage.cpp
+++ b/src/cxa_exception_storage.cpp
@@ -14,7 +14,7 @@
 #include "cxa_exception.hpp"
 
 #include "config.h"
-#include "threading_support.h"
+#include <__threading_support>
 
 #if defined(_LIBCXXABI_HAS_NO_THREADS)
 
@@ -54,17 +54,17 @@
 
 namespace __cxxabiv1 {
 namespace {
-    __libcxxabi_tls_key key_;
-    __libcxxabi_exec_once_flag flag_ = _LIBCXXABI_EXEC_ONCE_INITIALIZER;
+    std::__libcpp_tls_key key_;
+    std::__libcpp_exec_once_flag flag_ = _LIBCPP_EXEC_ONCE_INITIALIZER;
 
     void destruct_ (void *p) {
         __free_with_fallback ( p );
-        if ( 0 != __libcxxabi_tls_set ( key_, NULL ) )	
+        if ( 0 != std::__libcpp_tls_set ( key_, NULL ) )
             abort_message("cannot zero out thread value for __cxa_get_globals()");
         }
 
     void construct_ () {
-        if ( 0 != __libcxxabi_tls_create ( &key_, destruct_ ) )
+        if ( 0 != std::__libcpp_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 != __libcxxabi_tls_set ( key_, retVal ) )
-               abort_message("__libcxxabi_tls_set failure in __cxa_get_globals()");
+            if ( 0 != std::__libcpp_tls_set ( key_, retVal ) )
+               abort_message("std::__libcpp_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 != __libcxxabi_execute_once(&flag_, construct_))
+        if (0 != std::__libcpp_execute_once(&flag_, construct_))
             abort_message("execute once failure in __cxa_get_globals_fast()");
 //        static int init = construct_();
-        return static_cast<__cxa_eh_globals*>(__libcxxabi_tls_get(key_));
+        return static_cast<__cxa_eh_globals*>(std::__libcpp_tls_get(key_));
         }
     
 }
diff --git a/src/cxa_guard.cpp b/src/cxa_guard.cpp
index 1c468ff..97f2aaa 100644
--- a/src/cxa_guard.cpp
+++ b/src/cxa_guard.cpp
@@ -11,7 +11,7 @@
 
 #include "abort_message.h"
 #include "config.h"
-#include "threading_support.h"
+#include <__threading_support>
 
 #include <stdint.h>
 
@@ -20,8 +20,8 @@
     which will turn around and try to call __cxa_guard_acquire reentrantly.
     For this reason, the headers of this file are as restricted as possible.
     Previous implementations of this code for __APPLE__ have used
-    __libcxxabi_mutex_lock and the abort_message utility without problem.  This
-    implementation also uses __libcxxabi_condvar_wait which has tested
+    std::__libcpp_mutex_lock and the abort_message utility without problem. This
+    implementation also uses std::__libcpp_condvar_wait which has tested
     to not be a problem.
 */
 
@@ -67,8 +67,8 @@
 #endif
 
 #ifndef _LIBCXXABI_HAS_NO_THREADS
-__libcxxabi_mutex_t guard_mut = _LIBCXXABI_MUTEX_INITIALIZER;
-__libcxxabi_condvar_t guard_cv = _LIBCXXABI_CONDVAR_INITIALIZER;
+std::__libcpp_mutex_t guard_mut = _LIBCPP_MUTEX_INITIALIZER;
+std::__libcpp_condvar_t guard_cv = _LIBCPP_CONDVAR_INITIALIZER;
 #endif
 
 #if defined(__APPLE__) && !defined(__arm__)
@@ -173,13 +173,13 @@
 #ifndef _LIBCXXABI_HAS_NO_THREADS
 _LIBCXXABI_FUNC_VIS int __cxa_guard_acquire(guard_type *guard_object) {
     char* initialized = (char*)guard_object;
-    if (__libcxxabi_mutex_lock(&guard_mut))
+    if (std::__libcpp_mutex_lock(&guard_mut))
         abort_message("__cxa_guard_acquire failed to acquire mutex");
     int result = *initialized == 0;
     if (result)
     {
 #if defined(__APPLE__) && !defined(__arm__)
-        const lock_type id = __libcxxabi_thread_get_port();
+        const lock_type id = std::__libcpp_thread_get_port();
         lock_type lock = get_lock(*guard_object);
         if (lock)
         {
@@ -188,7 +188,7 @@
                 abort_message("__cxa_guard_acquire detected deadlock");
             do
             {
-                if (__libcxxabi_condvar_wait(&guard_cv, &guard_mut))
+                if (std::__libcpp_condvar_wait(&guard_cv, &guard_mut))
                     abort_message("__cxa_guard_acquire condition variable wait failed");
                 lock = get_lock(*guard_object);
             } while (lock);
@@ -200,36 +200,36 @@
             set_lock(*guard_object, id);
 #else  // !__APPLE__ || __arm__
         while (get_lock(*guard_object))
-            if (__libcxxabi_condvar_wait(&guard_cv, &guard_mut))
+            if (std::__libcpp_condvar_wait(&guard_cv, &guard_mut))
                 abort_message("__cxa_guard_acquire condition variable wait failed");
         result = *initialized == 0;
         if (result)
             set_lock(*guard_object, true);
 #endif  // !__APPLE__ || __arm__
     }
-    if (__libcxxabi_mutex_unlock(&guard_mut))
+    if (std::__libcpp_mutex_unlock(&guard_mut))
         abort_message("__cxa_guard_acquire failed to release mutex");
     return result;
 }
 
 _LIBCXXABI_FUNC_VIS void __cxa_guard_release(guard_type *guard_object) {
-    if (__libcxxabi_mutex_lock(&guard_mut))
+    if (std::__libcpp_mutex_lock(&guard_mut))
         abort_message("__cxa_guard_release failed to acquire mutex");
     *guard_object = 0;
     set_initialized(guard_object);
-    if (__libcxxabi_mutex_unlock(&guard_mut))
+    if (std::__libcpp_mutex_unlock(&guard_mut))
         abort_message("__cxa_guard_release failed to release mutex");
-    if (__libcxxabi_condvar_broadcast(&guard_cv))
+    if (std::__libcpp_condvar_broadcast(&guard_cv))
         abort_message("__cxa_guard_release failed to broadcast condition variable");
 }
 
 _LIBCXXABI_FUNC_VIS void __cxa_guard_abort(guard_type *guard_object) {
-    if (__libcxxabi_mutex_lock(&guard_mut))
+    if (std::__libcpp_mutex_lock(&guard_mut))
         abort_message("__cxa_guard_abort failed to acquire mutex");
     *guard_object = 0;
-    if (__libcxxabi_mutex_unlock(&guard_mut))
+    if (std::__libcpp_mutex_unlock(&guard_mut))
         abort_message("__cxa_guard_abort failed to release mutex");
-    if (__libcxxabi_condvar_broadcast(&guard_cv))
+    if (std::__libcpp_condvar_broadcast(&guard_cv))
         abort_message("__cxa_guard_abort failed to broadcast condition variable");
 }
 
diff --git a/src/cxa_thread_atexit.cpp b/src/cxa_thread_atexit.cpp
index 3ddaf0c..748b0eb 100644
--- a/src/cxa_thread_atexit.cpp
+++ b/src/cxa_thread_atexit.cpp
@@ -9,7 +9,7 @@
 
 #include "abort_message.h"
 #include "cxxabi.h"
-#include "threading_support.h"
+#include <__threading_support>
 #include <cstdlib>
 
 namespace __cxxabiv1 {
@@ -39,7 +39,7 @@
   //   destructors of any objects with static storage duration.
   //
   // - thread_local destructors on non-main threads run on the first iteration
-  //   through the __libcxxabi_tls_key destructors.
+  //   through the __libccpp_tls_key destructors.
   //   std::notify_all_at_thread_exit() and similar functions must be careful to
   //   wait until the second iteration to provide their intended ordering
   //   guarantees.
@@ -66,7 +66,7 @@
   // True if the destructors are currently scheduled to run on this thread
   __thread bool dtors_alive = false;
   // Used to trigger destructors on thread exit; value is ignored
-  __libcxxabi_tls_key dtors_key;
+  std::__libcpp_tls_key dtors_key;
 
   void run_dtors(void*) {
     while (auto head = dtors) {
@@ -80,16 +80,16 @@
 
   struct DtorsManager {
     DtorsManager() {
-      // There is intentionally no matching __libcxxabi_tls_delete call, as
+      // There is intentionally no matching std::__libcpp_tls_delete call, as
       // __cxa_thread_atexit() may be called arbitrarily late (for example, from
       // global destructors or atexit() handlers).
-      if (__libcxxabi_tls_create(&dtors_key, run_dtors) != 0) {
-        abort_message("__libcxxabi_tls_create() failed in __cxa_thread_atexit()");
+      if (std::__libcpp_tls_create(&dtors_key, run_dtors) != 0) {
+        abort_message("std::__libcpp_tls_create() failed in __cxa_thread_atexit()");
       }
     }
 
     ~DtorsManager() {
-      // __libcxxabi_tls_key destructors do not run on threads that call exit()
+      // std::__libcpp_tls_key destructors do not run on threads that call exit()
       // (including when the main thread returns from main()), so we explicitly
       // call the destructor here.  This runs at exit time (potentially earlier
       // if libc++abi is dlclose()'d).  Any thread_locals initialized after this
@@ -110,12 +110,12 @@
     if (__cxa_thread_atexit_impl) {
       return __cxa_thread_atexit_impl(dtor, obj, dso_symbol);
     } else {
-      // Initialize the dtors __libcxxabi_tls_key (uses __cxa_guard_*() for
+      // Initialize the dtors std::__libcpp_tls_key (uses __cxa_guard_*() for
       // one-time initialization and __cxa_atexit() for destruction)
       static DtorsManager manager;
 
       if (!dtors_alive) {
-        if (__libcxxabi_tls_set(dtors_key, &dtors_key) != 0) {
+        if (std::__libcpp_tls_set(dtors_key, &dtors_key) != 0) {
           return -1;
         }
         dtors_alive = true;
diff --git a/src/fallback_malloc.cpp b/src/fallback_malloc.cpp
index 5f52ece..3f8441e 100644
--- a/src/fallback_malloc.cpp
+++ b/src/fallback_malloc.cpp
@@ -10,7 +10,7 @@
 #include "fallback_malloc.h"
 
 #include "config.h"
-#include "threading_support.h"
+#include <__threading_support>
 
 #include <cstdlib> // for malloc, calloc, free
 #include <cstring> // for memset
@@ -29,7 +29,8 @@
 
 // When POSIX threads are not available, make the mutex operations a nop
 #ifndef _LIBCXXABI_HAS_NO_THREADS
-static __libcxxabi_mutex_t heap_mutex = _LIBCXXABI_MUTEX_INITIALIZER;
+_LIBCPP_SAFE_STATIC
+static std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER;
 #else
 static void * heap_mutex = 0;
 #endif
@@ -37,8 +38,10 @@
 class mutexor {
 public:
 #ifndef _LIBCXXABI_HAS_NO_THREADS
-    mutexor ( __libcxxabi_mutex_t *m ) : mtx_(m) { __libcxxabi_mutex_lock ( mtx_ ); }
-    ~mutexor () { __libcxxabi_mutex_unlock ( mtx_ ); }
+    mutexor ( std::__libcpp_mutex_t *m ) : mtx_(m) {
+      std::__libcpp_mutex_lock ( mtx_ );
+    }
+    ~mutexor () { std::__libcpp_mutex_unlock ( mtx_ ); }
 #else
     mutexor ( void * ) {}
     ~mutexor () {}
@@ -47,9 +50,9 @@
     mutexor ( const mutexor &rhs );
     mutexor & operator = ( const mutexor &rhs );
 #ifndef _LIBCXXABI_HAS_NO_THREADS
-    __libcxxabi_mutex_t *mtx_;
+    std::__libcpp_mutex_t *mtx_;
 #endif
-    };
+};
 
 
 static const size_t HEAP_SIZE = 512;
diff --git a/src/threading_support.h b/src/threading_support.h
deleted file mode 100644
index 78a225f..0000000
--- a/src/threading_support.h
+++ /dev/null
@@ -1,107 +0,0 @@
-//===------------------------ threading_support.h -------------------------===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is dual licensed under the MIT and the University of Illinois Open
-// Source Licenses. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef _LIBCXXABI_THREADING_SUPPORT_H
-#define _LIBCXXABI_THREADING_SUPPORT_H
-
-#include "__cxxabi_config.h"
-#include "config.h"
-
-#ifndef _LIBCXXABI_HAS_NO_THREADS
-
-#if defined(_LIBCXXABI_USE_THREAD_API_PTHREAD)
-#include <pthread.h>
-
-#define _LIBCXXABI_THREAD_ABI_VISIBILITY inline _LIBCXXABI_INLINE_VISIBILITY
-
-// Mutex
-typedef pthread_mutex_t __libcxxabi_mutex_t;
-#define _LIBCXXABI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
-
-_LIBCXXABI_THREAD_ABI_VISIBILITY
-int __libcxxabi_mutex_lock(__libcxxabi_mutex_t *mutex) {
-  return pthread_mutex_lock(mutex);
-}
-
-_LIBCXXABI_THREAD_ABI_VISIBILITY
-int __libcxxabi_mutex_unlock(__libcxxabi_mutex_t *mutex) {
-  return pthread_mutex_unlock(mutex);
-}
-
-// Condition variable
-typedef pthread_cond_t __libcxxabi_condvar_t;
-#define _LIBCXXABI_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
-
-_LIBCXXABI_THREAD_ABI_VISIBILITY
-int __libcxxabi_condvar_wait(__libcxxabi_condvar_t *cv,
-                             __libcxxabi_mutex_t *mutex) {
-  return pthread_cond_wait(cv, mutex);
-}
-
-_LIBCXXABI_THREAD_ABI_VISIBILITY
-int __libcxxabi_condvar_broadcast(__libcxxabi_condvar_t *cv) {
-  return pthread_cond_broadcast(cv);
-}
-
-// Execute once
-typedef pthread_once_t __libcxxabi_exec_once_flag;
-#define _LIBCXXABI_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT
-
-_LIBCXXABI_THREAD_ABI_VISIBILITY
-int __libcxxabi_execute_once(__libcxxabi_exec_once_flag *flag,
-                             void (*init_routine)(void)) {
-  return pthread_once(flag, init_routine);
-}
-
-// Thread id
-#if defined(__APPLE__) && !defined(__arm__)
-_LIBCXXABI_THREAD_ABI_VISIBILITY
-mach_port_t __libcxxabi_thread_get_port()
-{
-    return pthread_mach_thread_np(pthread_self());
-}
-#endif
-
-// Thread
-typedef pthread_t __libcxxabi_thread_t;
-
-_LIBCXXABI_THREAD_ABI_VISIBILITY
-int __libcxxabi_thread_create(__libcxxabi_thread_t* __t,
-                           void* (*__func)(void*), void* __arg)
-{
-    return pthread_create(__t, 0, __func, __arg);
-}
-
-_LIBCXXABI_THREAD_ABI_VISIBILITY
-int __libcxxabi_thread_join(__libcxxabi_thread_t* __t)
-{
-    return pthread_join(*__t, 0);
-}
-
-// TLS
-typedef pthread_key_t __libcxxabi_tls_key;
-
-_LIBCXXABI_THREAD_ABI_VISIBILITY
-int __libcxxabi_tls_create(__libcxxabi_tls_key *key,
-                           void (*destructor)(void *)) {
-  return pthread_key_create(key, destructor);
-}
-
-_LIBCXXABI_THREAD_ABI_VISIBILITY
-void *__libcxxabi_tls_get(__libcxxabi_tls_key key) {
-  return pthread_getspecific(key);
-}
-
-_LIBCXXABI_THREAD_ABI_VISIBILITY
-int __libcxxabi_tls_set(__libcxxabi_tls_key key, void *value) {
-  return pthread_setspecific(key, value);
-}
-#endif // _LIBCXXABI_USE_THREAD_API_PTHREAD
-#endif // !_LIBCXXABI_HAS_NO_THREADS
-#endif // _LIBCXXABI_THREADING_SUPPORT_H