Allow libc++ to be built on systems without POSIX threads
If you're crazy enough to want this sort of thing, then add
-D_LIBCPP_HAS_NO_THREADS to your CXXFLAGS and
--param=additiona_features=libcpp-has-no-threads to your lit commnad line.
http://reviews.llvm.org/D3969
llvm-svn: 217271
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: b3fcc67f8f13cd95d36ed29d0ae1308decb9e099
diff --git a/src/algorithm.cpp b/src/algorithm.cpp
index 6d5cf7c..e548856 100644
--- a/src/algorithm.cpp
+++ b/src/algorithm.cpp
@@ -47,12 +47,16 @@
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;
+#endif
unsigned __rs_default::__c_ = 0;
__rs_default::__rs_default()
{
+#ifndef _LIBCPP_HAS_NO_THREADS
pthread_mutex_lock(&__rs_mut);
+#endif
__c_ = 1;
}
@@ -63,8 +67,12 @@
__rs_default::~__rs_default()
{
+#ifndef _LIBCPP_HAS_NO_THREADS
if (--__c_ == 0)
pthread_mutex_unlock(&__rs_mut);
+#else
+ --__c_;
+#endif
}
__rs_default::result_type
diff --git a/src/condition_variable.cpp b/src/condition_variable.cpp
index 061d138..5fd5fc8 100644
--- a/src/condition_variable.cpp
+++ b/src/condition_variable.cpp
@@ -7,6 +7,10 @@
//
//===----------------------------------------------------------------------===//
+#include "__config"
+
+#ifndef _LIBCPP_HAS_NO_THREADS
+
#include "condition_variable"
#include "thread"
#include "system_error"
@@ -79,3 +83,5 @@
}
_LIBCPP_END_NAMESPACE_STD
+
+#endif // !_LIBCPP_HAS_NO_THREADS
diff --git a/src/debug.cpp b/src/debug.cpp
index d0e8679..60694a3 100644
--- a/src/debug.cpp
+++ b/src/debug.cpp
@@ -35,6 +35,7 @@
namespace
{
+#ifndef _LIBCPP_HAS_NO_THREADS
typedef mutex mutex_type;
typedef lock_guard<mutex_type> WLock;
typedef lock_guard<mutex_type> RLock;
@@ -45,6 +46,7 @@
static mutex_type m;
return m;
}
+#endif // !_LIBCPP_HAS_NO_THREADS
} // unnamed namespace
@@ -108,7 +110,9 @@
void*
__libcpp_db::__find_c_from_i(void* __i) const
{
+#ifndef _LIBCPP_HAS_NO_THREADS
RLock _(mut());
+#endif
__i_node* i = __find_iterator(__i);
_LIBCPP_ASSERT(i != nullptr, "iterator not found in debug database.");
return i->__c_ != nullptr ? i->__c_->__c_ : nullptr;
@@ -117,7 +121,9 @@
void
__libcpp_db::__insert_ic(void* __i, const void* __c)
{
+#ifndef _LIBCPP_HAS_NO_THREADS
WLock _(mut());
+#endif
if (__cbeg_ == __cend_)
return;
size_t hc = hash<const void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_);
@@ -138,7 +144,9 @@
__c_node*
__libcpp_db::__insert_c(void* __c)
{
+#ifndef _LIBCPP_HAS_NO_THREADS
WLock _(mut());
+#endif
if (__csz_ + 1 > static_cast<size_t>(__cend_ - __cbeg_))
{
size_t nc = __next_prime(2*static_cast<size_t>(__cend_ - __cbeg_) + 1);
@@ -184,7 +192,9 @@
void
__libcpp_db::__erase_i(void* __i)
{
+#ifndef _LIBCPP_HAS_NO_THREADS
WLock _(mut());
+#endif
if (__ibeg_ != __iend_)
{
size_t hi = hash<void*>()(__i) % static_cast<size_t>(__iend_ - __ibeg_);
@@ -215,7 +225,9 @@
void
__libcpp_db::__invalidate_all(void* __c)
{
+#ifndef _LIBCPP_HAS_NO_THREADS
WLock _(mut());
+#endif
if (__cend_ != __cbeg_)
{
size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_);
@@ -239,17 +251,23 @@
__c_node*
__libcpp_db::__find_c_and_lock(void* __c) const
{
+#ifndef _LIBCPP_HAS_NO_THREADS
mut().lock();
+#endif
if (__cend_ == __cbeg_)
{
+#ifndef _LIBCPP_HAS_NO_THREADS
mut().unlock();
+#endif
return nullptr;
}
size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_);
__c_node* p = __cbeg_[hc];
if (p == nullptr)
{
+#ifndef _LIBCPP_HAS_NO_THREADS
mut().unlock();
+#endif
return nullptr;
}
while (p->__c_ != __c)
@@ -257,7 +275,9 @@
p = p->__next_;
if (p == nullptr)
{
+#ifndef _LIBCPP_HAS_NO_THREADS
mut().unlock();
+#endif
return nullptr;
}
}
@@ -281,13 +301,17 @@
void
__libcpp_db::unlock() const
{
+#ifndef _LIBCPP_HAS_NO_THREADS
mut().unlock();
+#endif
}
void
__libcpp_db::__erase_c(void* __c)
{
+#ifndef _LIBCPP_HAS_NO_THREADS
WLock _(mut());
+#endif
if (__cend_ != __cbeg_)
{
size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_);
@@ -322,7 +346,9 @@
void
__libcpp_db::__iterator_copy(void* __i, const void* __i0)
{
+#ifndef _LIBCPP_HAS_NO_THREADS
WLock _(mut());
+#endif
__i_node* i = __find_iterator(__i);
__i_node* i0 = __find_iterator(__i0);
__c_node* c0 = i0 != nullptr ? i0->__c_ : nullptr;
@@ -348,7 +374,9 @@
bool
__libcpp_db::__dereferenceable(const void* __i) const
{
+#ifndef _LIBCPP_HAS_NO_THREADS
RLock _(mut());
+#endif
__i_node* i = __find_iterator(__i);
return i != nullptr && i->__c_ != nullptr && i->__c_->__dereferenceable(__i);
}
@@ -356,7 +384,9 @@
bool
__libcpp_db::__decrementable(const void* __i) const
{
+#ifndef _LIBCPP_HAS_NO_THREADS
RLock _(mut());
+#endif
__i_node* i = __find_iterator(__i);
return i != nullptr && i->__c_ != nullptr && i->__c_->__decrementable(__i);
}
@@ -364,7 +394,9 @@
bool
__libcpp_db::__addable(const void* __i, ptrdiff_t __n) const
{
+#ifndef _LIBCPP_HAS_NO_THREADS
RLock _(mut());
+#endif
__i_node* i = __find_iterator(__i);
return i != nullptr && i->__c_ != nullptr && i->__c_->__addable(__i, __n);
}
@@ -372,7 +404,9 @@
bool
__libcpp_db::__subscriptable(const void* __i, ptrdiff_t __n) const
{
+#ifndef _LIBCPP_HAS_NO_THREADS
RLock _(mut());
+#endif
__i_node* i = __find_iterator(__i);
return i != nullptr && i->__c_ != nullptr && i->__c_->__subscriptable(__i, __n);
}
@@ -380,7 +414,9 @@
bool
__libcpp_db::__less_than_comparable(const void* __i, const void* __j) const
{
+#ifndef _LIBCPP_HAS_NO_THREADS
RLock _(mut());
+#endif
__i_node* i = __find_iterator(__i);
__i_node* j = __find_iterator(__j);
__c_node* ci = i != nullptr ? i->__c_ : nullptr;
@@ -391,7 +427,9 @@
void
__libcpp_db::swap(void* c1, void* c2)
{
+#ifndef _LIBCPP_HAS_NO_THREADS
WLock _(mut());
+#endif
size_t hc = hash<void*>()(c1) % static_cast<size_t>(__cend_ - __cbeg_);
__c_node* p1 = __cbeg_[hc];
_LIBCPP_ASSERT(p1 != nullptr, "debug mode internal logic error swap A");
@@ -420,7 +458,9 @@
void
__libcpp_db::__insert_i(void* __i)
{
+#ifndef _LIBCPP_HAS_NO_THREADS
WLock _(mut());
+#endif
__insert_iterator(__i);
}
diff --git a/src/future.cpp b/src/future.cpp
index c67dc58..0c5c2c4 100644
--- a/src/future.cpp
+++ b/src/future.cpp
@@ -7,6 +7,10 @@
//
//===----------------------------------------------------------------------===//
+#include "__config"
+
+#ifndef _LIBCPP_HAS_NO_THREADS
+
#include "future"
#include "string"
@@ -298,3 +302,5 @@
}
_LIBCPP_END_NAMESPACE_STD
+
+#endif // !_LIBCPP_HAS_NO_THREADS
diff --git a/src/ios.cpp b/src/ios.cpp
index b71d7da..0bd500b 100644
--- a/src/ios.cpp
+++ b/src/ios.cpp
@@ -148,7 +148,7 @@
}
// xalloc
-#if __has_feature(cxx_atomic)
+#if __has_feature(cxx_atomic) && !_LIBCPP_HAS_NO_THREADS
atomic<int> ios_base::__xindex_ = ATOMIC_VAR_INIT(0);
#else
int ios_base::__xindex_ = 0;
diff --git a/src/memory.cpp b/src/memory.cpp
index 666673f..31e823c 100644
--- a/src/memory.cpp
+++ b/src/memory.cpp
@@ -9,8 +9,10 @@
#define _LIBCPP_BUILDING_MEMORY
#include "memory"
+#ifndef _LIBCPP_HAS_NO_THREADS
#include "mutex"
#include "thread"
+#endif
_LIBCPP_BEGIN_NAMESPACE_STD
@@ -119,7 +121,7 @@
#endif // _LIBCPP_NO_RTTI
-#if __has_feature(cxx_atomic)
+#if __has_feature(cxx_atomic) && !_LIBCPP_HAS_NO_THREADS
static const std::size_t __sp_mut_count = 16;
static pthread_mutex_t mut_back_imp[__sp_mut_count] =
@@ -172,7 +174,7 @@
return muts[hash<const void*>()(p) & (__sp_mut_count-1)];
}
-#endif // __has_feature(cxx_atomic)
+#endif // __has_feature(cxx_atomic) && !_LIBCPP_HAS_NO_THREADS
void
declare_reachable(void*)
diff --git a/src/mutex.cpp b/src/mutex.cpp
index 0767897..e56271d 100644
--- a/src/mutex.cpp
+++ b/src/mutex.cpp
@@ -14,6 +14,7 @@
#include "cassert"
_LIBCPP_BEGIN_NAMESPACE_STD
+#ifndef _LIBCPP_HAS_NO_THREADS
const defer_lock_t defer_lock = {};
const try_to_lock_t try_to_lock = {};
@@ -206,18 +207,42 @@
}
}
+#endif // !_LIBCPP_HAS_NO_THREADS
+
// If dispatch_once_f ever handles C++ exceptions, and if one can get to it
// without illegal macros (unexpected macros not beginning with _UpperCase or
// __lowercase), and if it stops spinning waiting threads, then call_once should
// call into dispatch_once_f instead of here. Relevant radar this code needs to
// 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;
+#endif
void
__call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
{
+#if defined(_LIBCPP_HAS_NO_THREADS)
+ if (flag == 0)
+ {
+#ifndef _LIBCPP_NO_EXCEPTIONS
+ try
+ {
+#endif // _LIBCPP_NO_EXCEPTIONS
+ flag = 1;
+ func(arg);
+ flag = ~0ul;
+#ifndef _LIBCPP_NO_EXCEPTIONS
+ }
+ catch (...)
+ {
+ flag = 0ul;
+ throw;
+ }
+#endif // _LIBCPP_NO_EXCEPTIONS
+ }
+#else // !_LIBCPP_HAS_NO_THREADS
pthread_mutex_lock(&mut);
while (flag == 1)
pthread_cond_wait(&cv, &mut);
@@ -248,6 +273,8 @@
}
else
pthread_mutex_unlock(&mut);
+#endif // !_LIBCPP_HAS_NO_THREADS
+
}
_LIBCPP_END_NAMESPACE_STD
diff --git a/src/shared_mutex.cpp b/src/shared_mutex.cpp
index dd78a16..2b78c1f 100644
--- a/src/shared_mutex.cpp
+++ b/src/shared_mutex.cpp
@@ -7,6 +7,9 @@
//
//===----------------------------------------------------------------------===//
+#include "__config"
+#ifndef _LIBCPP_HAS_NO_THREADS
+
#define _LIBCPP_BUILDING_SHARED_MUTEX
#include "shared_mutex"
@@ -99,3 +102,5 @@
_LIBCPP_END_NAMESPACE_STD
+
+#endif // !_LIBCPP_HAS_NO_THREADS
diff --git a/src/thread.cpp b/src/thread.cpp
index e6f57c4..0ced1e3 100644
--- a/src/thread.cpp
+++ b/src/thread.cpp
@@ -7,6 +7,9 @@
//
//===----------------------------------------------------------------------===//
+#include "__config"
+#ifndef _LIBCPP_HAS_NO_THREADS
+
#include "thread"
#include "exception"
#include "vector"
@@ -225,3 +228,5 @@
}
_LIBCPP_END_NAMESPACE_STD
+
+#endif // !_LIBCPP_HAS_NO_THREADS