Skip to content

Commit

Permalink
Allow libc++ to be built on systems without POSIX threads
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jroelofs committed Sep 5, 2014
1 parent d3e1213 commit b3fcc67
Show file tree
Hide file tree
Showing 268 changed files with 687 additions and 8 deletions.
6 changes: 6 additions & 0 deletions libcxx/include/__mutex_base
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

_LIBCPP_BEGIN_NAMESPACE_STD

#ifndef _LIBCPP_HAS_NO_THREADS

class _LIBCPP_TYPE_VIS mutex
{
pthread_mutex_t __m_;
Expand Down Expand Up @@ -315,6 +317,7 @@ private:
void __do_timed_wait(unique_lock<mutex>& __lk,
chrono::time_point<chrono::system_clock, chrono::nanoseconds>) _NOEXCEPT;
};
#endif // !_LIBCPP_HAS_NO_THREADS

template <class _To, class _Rep, class _Period>
inline _LIBCPP_INLINE_VISIBILITY
Expand All @@ -332,6 +335,7 @@ __ceil(chrono::duration<_Rep, _Period> __d)
return __r;
}

#ifndef _LIBCPP_HAS_NO_THREADS
template <class _Predicate>
void
condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred)
Expand Down Expand Up @@ -396,6 +400,8 @@ condition_variable::wait_for(unique_lock<mutex>& __lk,
_VSTD::move(__pred));
}

#endif // !_LIBCPP_HAS_NO_THREADS

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___MUTEX_BASE
6 changes: 6 additions & 0 deletions libcxx/include/atomic
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ void atomic_signal_fence(memory_order m) noexcept;
#pragma GCC system_header
#endif

#ifdef _LIBCPP_HAS_NO_THREADS
#error <atomic> is not supported on this single threaded system
#else // !_LIBCPP_HAS_NO_THREADS

_LIBCPP_BEGIN_NAMESPACE_STD

#if !__has_feature(cxx_atomic) && _GNUC_VER < 407
Expand Down Expand Up @@ -1779,4 +1783,6 @@ typedef atomic<uintmax_t> atomic_uintmax_t;

_LIBCPP_END_NAMESPACE_STD

#endif // !_LIBCPP_HAS_NO_THREADS

#endif // _LIBCPP_ATOMIC
4 changes: 4 additions & 0 deletions libcxx/include/condition_variable
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public:
#pragma GCC system_header
#endif

#ifndef _LIBCPP_HAS_NO_THREADS

_LIBCPP_BEGIN_NAMESPACE_STD

class _LIBCPP_TYPE_VIS condition_variable_any
Expand Down Expand Up @@ -253,4 +255,6 @@ void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);

_LIBCPP_END_NAMESPACE_STD

#endif // !_LIBCPP_HAS_NO_THREADS

#endif // _LIBCPP_CONDITION_VARIABLE
6 changes: 6 additions & 0 deletions libcxx/include/future
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
#pragma GCC system_header
#endif

#ifndef _LIBCPP_HAS_NO_THREADS
#error <future> is not supported on this single threaded system
#else // !_LIBCPP_HAS_NO_THREADS

_LIBCPP_BEGIN_NAMESPACE_STD

//enum class future_errc
Expand Down Expand Up @@ -2612,4 +2616,6 @@ future<void>::share()

_LIBCPP_END_NAMESPACE_STD

#endif // !_LIBCPP_HAS_NO_THREADS

#endif // _LIBCPP_FUTURE
4 changes: 2 additions & 2 deletions libcxx/include/ios
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ storage-class-specifier const error_category& iostream_category() noexcept;
#include <__locale>
#include <system_error>

#if __has_feature(cxx_atomic)
#if __has_feature(cxx_atomic) && !_LIBCPP_HAS_NO_THREADS
#include <atomic> // for __xindex_
#endif

Expand Down Expand Up @@ -367,7 +367,7 @@ private:
int* __index_;
size_t __event_size_;
size_t __event_cap_;
#if __has_feature(cxx_atomic)
#if __has_feature(cxx_atomic) && !_LIBCPP_HAS_NO_THREADS
static atomic<int> __xindex_;
#else
static int __xindex_;
Expand Down
6 changes: 3 additions & 3 deletions libcxx/include/memory
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
#include <cassert>
#endif

#if __has_feature(cxx_atomic)
#if __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)
# include <atomic>
#endif

Expand Down Expand Up @@ -5262,7 +5262,7 @@ inline _LIBCPP_INLINE_VISIBILITY
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);

#if __has_feature(cxx_atomic)
#if __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)

class _LIBCPP_TYPE_VIS __sp_mut
{
Expand Down Expand Up @@ -5388,7 +5388,7 @@ atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v
return atomic_compare_exchange_weak(__p, __v, __w);
}

#endif // __has_feature(cxx_atomic)
#endif // __has_feature(cxx_atomic) && !defined(_LIBCPP_HAS_NO_THREADS)

//enum class
struct _LIBCPP_TYPE_VIS pointer_safety
Expand Down
4 changes: 4 additions & 0 deletions libcxx/include/mutex
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ template<class Callable, class ...Args>

_LIBCPP_BEGIN_NAMESPACE_STD

#ifndef _LIBCPP_HAS_NO_THREADS

class _LIBCPP_TYPE_VIS recursive_mutex
{
pthread_mutex_t __m_;
Expand Down Expand Up @@ -425,6 +427,8 @@ lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)

#endif // _LIBCPP_HAS_NO_VARIADICS

#endif // !_LIBCPP_HAS_NO_THREADS

struct _LIBCPP_TYPE_VIS_ONLY once_flag;

#ifndef _LIBCPP_HAS_NO_VARIADICS
Expand Down
6 changes: 6 additions & 0 deletions libcxx/include/shared_mutex
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ template <class Mutex>
#pragma GCC system_header
#endif

#ifdef _LIBCPP_HAS_NO_THREADS
#error <shared_mutex> is not supported on this single threaded system
#else // !_LIBCPP_HAS_NO_THREADS

_LIBCPP_BEGIN_NAMESPACE_STD

class _LIBCPP_TYPE_VIS shared_timed_mutex
Expand Down Expand Up @@ -414,6 +418,8 @@ swap(shared_lock<_Mutex>& __x, shared_lock<_Mutex>& __y) _NOEXCEPT

_LIBCPP_END_NAMESPACE_STD

#endif // !_LIBCPP_HAS_NO_THREADS

#endif // _LIBCPP_STD_VER > 11

#endif // _LIBCPP_SHARED_MUTEX
6 changes: 6 additions & 0 deletions libcxx/include/thread
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ void sleep_for(const chrono::duration<Rep, Period>& rel_time);

#define __STDCPP_THREADS__ __cplusplus

#ifdef _LIBCPP_HAS_NO_THREADS
#error <thread> is not supported on this single threaded system
#else // !_LIBCPP_HAS_NO_THREADS

_LIBCPP_BEGIN_NAMESPACE_STD

template <class _Tp>
Expand Down Expand Up @@ -455,4 +459,6 @@ void yield() _NOEXCEPT {sched_yield();}

_LIBCPP_END_NAMESPACE_STD

#endif // !_LIBCPP_HAS_NO_THREADS

#endif // _LIBCPP_THREAD
8 changes: 8 additions & 0 deletions libcxx/src/algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ template bool __insertion_sort_incomplete<__less<long double>&, long double*>(lo

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;
}

Expand All @@ -63,8 +67,12 @@ __rs_default::__rs_default(const __rs_default&)

__rs_default::~__rs_default()
{
#ifndef _LIBCPP_HAS_NO_THREADS
if (--__c_ == 0)
pthread_mutex_unlock(&__rs_mut);
#else
--__c_;
#endif
}

__rs_default::result_type
Expand Down
6 changes: 6 additions & 0 deletions libcxx/src/condition_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
//
//===----------------------------------------------------------------------===//

#include "__config"

#ifndef _LIBCPP_HAS_NO_THREADS

#include "condition_variable"
#include "thread"
#include "system_error"
Expand Down Expand Up @@ -79,3 +83,5 @@ notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
}

_LIBCPP_END_NAMESPACE_STD

#endif // !_LIBCPP_HAS_NO_THREADS
Loading

0 comments on commit b3fcc67

Please sign in to comment.