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 git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@268734 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -23,13 +23,13 @@ const adopt_lock_t adopt_lock = {};
|
||||
|
||||
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 @@ mutex::lock()
|
||||
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 @@ mutex::unlock() _NOEXCEPT
|
||||
|
||||
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 @@ recursive_mutex::~recursive_mutex()
|
||||
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 @@ recursive_mutex::lock()
|
||||
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 @@ recursive_mutex::unlock() _NOEXCEPT
|
||||
bool
|
||||
recursive_mutex::try_lock() _NOEXCEPT
|
||||
{
|
||||
return pthread_mutex_trylock(&__m_) == 0;
|
||||
return __libcpp_mutex_trylock(&__m_) == 0;
|
||||
}
|
||||
|
||||
// timed_mutex
|
||||
@@ -165,9 +143,9 @@ recursive_timed_mutex::~recursive_timed_mutex()
|
||||
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 @@ recursive_timed_mutex::lock()
|
||||
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 @@ recursive_timed_mutex::unlock() _NOEXCEPT
|
||||
// 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 @@ __call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
|
||||
#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 @@ __call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
|
||||
{
|
||||
#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
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user