Refactor pthread usage of libcxx.

This patch extracts out all the pthread dependencies of libcxx into the
new header __threading_support. The motivation is to make it easy to
re-target libcxx into platforms that do not support pthread.

Original patch from Fulvio Esposito ([email protected]) - D11781

Applied with tweaks - D19412

Change-Id: I301111f0075de93dd8129416e06babc195aa936b
llvm-svn: 268734
diff --git a/libcxx/src/mutex.cpp b/libcxx/src/mutex.cpp
index 127e67a..9f808ca 100644
--- a/libcxx/src/mutex.cpp
+++ b/libcxx/src/mutex.cpp
@@ -23,13 +23,13 @@
 
 mutex::~mutex()
 {
-    pthread_mutex_destroy(&__m_);
+    __libcpp_mutex_destroy(&__m_);
 }
 
 void
 mutex::lock()
 {
-    int ec = pthread_mutex_lock(&__m_);
+    int ec = __libcpp_mutex_lock(&__m_);
     if (ec)
         __throw_system_error(ec, "mutex lock failed");
 }
@@ -37,13 +37,13 @@
 bool
 mutex::try_lock() _NOEXCEPT
 {
-    return pthread_mutex_trylock(&__m_) == 0;
+    return __libcpp_mutex_trylock(&__m_) == 0;
 }
 
 void
 mutex::unlock() _NOEXCEPT
 {
-    int ec = pthread_mutex_unlock(&__m_);
+    int ec = __libcpp_mutex_unlock(&__m_);
     (void)ec;
     assert(ec == 0);
 }
@@ -52,36 +52,14 @@
 
 recursive_mutex::recursive_mutex()
 {
-    pthread_mutexattr_t attr;
-    int ec = pthread_mutexattr_init(&attr);
+    int ec = __libcpp_recursive_mutex_init(&__m_);
     if (ec)
-        goto fail;
-    ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
-    if (ec)
-    {
-        pthread_mutexattr_destroy(&attr);
-        goto fail;
-    }
-    ec = pthread_mutex_init(&__m_, &attr);
-    if (ec)
-    {
-        pthread_mutexattr_destroy(&attr);
-        goto fail;
-    }
-    ec = pthread_mutexattr_destroy(&attr);
-    if (ec)
-    {
-        pthread_mutex_destroy(&__m_);
-        goto fail;
-    }
-    return;
-fail:
-    __throw_system_error(ec, "recursive_mutex constructor failed");
+        __throw_system_error(ec, "recursive_mutex constructor failed");
 }
 
 recursive_mutex::~recursive_mutex()
 {
-    int e = pthread_mutex_destroy(&__m_);
+    int e = __libcpp_mutex_destroy(&__m_);
     (void)e;
     assert(e == 0);
 }
@@ -89,7 +67,7 @@
 void
 recursive_mutex::lock()
 {
-    int ec = pthread_mutex_lock(&__m_);
+    int ec = __libcpp_mutex_lock(&__m_);
     if (ec)
         __throw_system_error(ec, "recursive_mutex lock failed");
 }
@@ -97,7 +75,7 @@
 void
 recursive_mutex::unlock() _NOEXCEPT
 {
-    int e = pthread_mutex_unlock(&__m_);
+    int e = __libcpp_mutex_unlock(&__m_);
     (void)e;
     assert(e == 0);
 }
@@ -105,7 +83,7 @@
 bool
 recursive_mutex::try_lock() _NOEXCEPT
 {
-    return pthread_mutex_trylock(&__m_) == 0;
+    return __libcpp_mutex_trylock(&__m_) == 0;
 }
 
 // timed_mutex
@@ -165,9 +143,9 @@
 void
 recursive_timed_mutex::lock()
 {
-    pthread_t id = pthread_self();
+    __libcpp_thread_id id = __libcpp_thread_get_current_id();
     unique_lock<mutex> lk(__m_);
-    if (pthread_equal(id, __id_))
+    if (__libcpp_thread_id_equal(id, __id_))
     {
         if (__count_ == numeric_limits<size_t>::max())
             __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
@@ -183,9 +161,9 @@
 bool
 recursive_timed_mutex::try_lock() _NOEXCEPT
 {
-    pthread_t id = pthread_self();
+    __libcpp_thread_id id = __libcpp_thread_get_current_id();
     unique_lock<mutex> lk(__m_, try_to_lock);
-    if (lk.owns_lock() && (__count_ == 0 || pthread_equal(id, __id_)))
+    if (lk.owns_lock() && (__count_ == 0 || __libcpp_thread_id_equal(id, __id_)))
     {
         if (__count_ == numeric_limits<size_t>::max())
             return false;
@@ -217,8 +195,8 @@
 // keep in sync with:  7741191.
 
 #ifndef _LIBCPP_HAS_NO_THREADS
-static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
-static pthread_cond_t  cv  = PTHREAD_COND_INITIALIZER;
+static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
+static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
 #endif
 
 /// NOTE: Changes to flag are done via relaxed atomic stores
@@ -247,9 +225,9 @@
 #endif  // _LIBCPP_NO_EXCEPTIONS
     }
 #else // !_LIBCPP_HAS_NO_THREADS
-    pthread_mutex_lock(&mut);
+    __libcpp_mutex_lock(&mut);
     while (flag == 1)
-        pthread_cond_wait(&cv, &mut);
+        __libcpp_condvar_wait(&cv, &mut);
     if (flag == 0)
     {
 #ifndef _LIBCPP_NO_EXCEPTIONS
@@ -257,26 +235,26 @@
         {
 #endif  // _LIBCPP_NO_EXCEPTIONS
             __libcpp_relaxed_store(&flag, 1ul);
-            pthread_mutex_unlock(&mut);
+            __libcpp_mutex_unlock(&mut);
             func(arg);
-            pthread_mutex_lock(&mut);
+            __libcpp_mutex_lock(&mut);
             __libcpp_relaxed_store(&flag, ~0ul);
-            pthread_mutex_unlock(&mut);
-            pthread_cond_broadcast(&cv);
+            __libcpp_mutex_unlock(&mut);
+            __libcpp_condvar_broadcast(&cv);
 #ifndef _LIBCPP_NO_EXCEPTIONS
         }
         catch (...)
         {
-            pthread_mutex_lock(&mut);
+            __libcpp_mutex_lock(&mut);
             __libcpp_relaxed_store(&flag, 0ul);
-            pthread_mutex_unlock(&mut);
-            pthread_cond_broadcast(&cv);
+            __libcpp_mutex_unlock(&mut);
+            __libcpp_condvar_broadcast(&cv);
             throw;
         }
 #endif  // _LIBCPP_NO_EXCEPTIONS
     }
     else
-        pthread_mutex_unlock(&mut);
+        __libcpp_mutex_unlock(&mut);
 #endif // !_LIBCPP_HAS_NO_THREADS
 
 }