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 (fulvio.esposito@outlook.com) - D11781
Applied with tweaks - D19412
Change-Id: I301111f0075de93dd8129416e06babc195aa936b
llvm-svn: 268734
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: c7e4239fabf2c876449697caf05a342b01bf0312
diff --git a/src/algorithm.cpp b/src/algorithm.cpp
index e548856..e9752b0 100644
--- a/src/algorithm.cpp
+++ b/src/algorithm.cpp
@@ -48,14 +48,14 @@
template unsigned __sort5<__less<long double>&, long double*>(long double*, long double*, long double*, long double*, long double*, __less<long double>&);
#ifndef _LIBCPP_HAS_NO_THREADS
-static pthread_mutex_t __rs_mut = PTHREAD_MUTEX_INITIALIZER;
+static __libcpp_mutex_t __rs_mut = _LIBCPP_MUTEX_INITIALIZER;
#endif
unsigned __rs_default::__c_ = 0;
__rs_default::__rs_default()
{
#ifndef _LIBCPP_HAS_NO_THREADS
- pthread_mutex_lock(&__rs_mut);
+ __libcpp_mutex_lock(&__rs_mut);
#endif
__c_ = 1;
}
@@ -69,7 +69,7 @@
{
#ifndef _LIBCPP_HAS_NO_THREADS
if (--__c_ == 0)
- pthread_mutex_unlock(&__rs_mut);
+ __libcpp_mutex_unlock(&__rs_mut);
#else
--__c_;
#endif
diff --git a/src/condition_variable.cpp b/src/condition_variable.cpp
index 5fd5fc8..bfb4bf3 100644
--- a/src/condition_variable.cpp
+++ b/src/condition_variable.cpp
@@ -20,19 +20,19 @@
condition_variable::~condition_variable()
{
- pthread_cond_destroy(&__cv_);
+ __libcpp_condvar_destroy(&__cv_);
}
void
condition_variable::notify_one() _NOEXCEPT
{
- pthread_cond_signal(&__cv_);
+ __libcpp_condvar_signal(&__cv_);
}
void
condition_variable::notify_all() _NOEXCEPT
{
- pthread_cond_broadcast(&__cv_);
+ __libcpp_condvar_broadcast(&__cv_);
}
void
@@ -41,7 +41,7 @@
if (!lk.owns_lock())
__throw_system_error(EPERM,
"condition_variable::wait: mutex not locked");
- int ec = pthread_cond_wait(&__cv_, lk.mutex()->native_handle());
+ int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle());
if (ec)
__throw_system_error(ec, "condition_variable wait failed");
}
@@ -71,7 +71,7 @@
ts.tv_sec = ts_sec_max;
ts.tv_nsec = giga::num - 1;
}
- int ec = pthread_cond_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
+ int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
if (ec != 0 && ec != ETIMEDOUT)
__throw_system_error(ec, "condition_variable timed_wait failed");
}
diff --git a/src/memory.cpp b/src/memory.cpp
index 5b81f26..08f2259 100644
--- a/src/memory.cpp
+++ b/src/memory.cpp
@@ -127,12 +127,12 @@
#if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
static const std::size_t __sp_mut_count = 16;
-static pthread_mutex_t mut_back_imp[__sp_mut_count] =
+static __libcpp_mutex_t mut_back_imp[__sp_mut_count] =
{
- PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
- PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
- PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
- PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER
+ _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
+ _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
+ _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER,
+ _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER, _LIBCPP_MUTEX_INITIALIZER
};
static mutex* mut_back = reinterpret_cast<std::mutex*>(mut_back_imp);
diff --git a/src/mutex.cpp b/src/mutex.cpp
index 127e67a..9f808ca 100644
--- a/src/mutex.cpp
+++ b/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
}
diff --git a/src/thread.cpp b/src/thread.cpp
index 2243a34..406e71d 100644
--- a/src/thread.cpp
+++ b/src/thread.cpp
@@ -46,7 +46,7 @@
void
thread::join()
{
- int ec = pthread_join(__t_, 0);
+ int ec = __libcpp_thread_join(&__t_);
#ifndef _LIBCPP_NO_EXCEPTIONS
if (ec)
throw system_error(error_code(ec, system_category()), "thread::join failed");
@@ -62,7 +62,7 @@
int ec = EINVAL;
if (__t_ != 0)
{
- ec = pthread_detach(__t_);
+ ec = __libcpp_thread_detach(&__t_);
if (ec == 0)
__t_ = 0;
}