diff options
author | Asiri Rathnayake <asiri.rathnayake@arm.com> | 2016-09-11 21:46:40 +0000 |
---|---|---|
committer | Asiri Rathnayake <asiri.rathnayake@arm.com> | 2016-09-11 21:46:40 +0000 |
commit | 8c2bf45da9d830c11dbc577330dfc958608dc78d (patch) | |
tree | 097f317afc053ca2e65db10b9cc6082c522cd32d /libcxx/include | |
parent | 958b699883f6e05e930fd4c40b3d1054839e5d1d (diff) | |
download | bcm5719-llvm-8c2bf45da9d830c11dbc577330dfc958608dc78d.tar.gz bcm5719-llvm-8c2bf45da9d830c11dbc577330dfc958608dc78d.zip |
[libcxx] Introduce an externally-threaded libc++ variant.
This patch further decouples libc++ from pthread, allowing libc++ to be built
against other threading systems. There are two main use cases:
- Building libc++ against a thread library other than pthreads.
- Building libc++ with an "external" thread API, allowing a separate library to
provide the implementation of that API.
The two use cases are quite similar, the second one being sligtly more
de-coupled than the first. The cmake option LIBCXX_HAS_EXTERNAL_THREAD_API
enables both kinds of builds. One needs to place an <__external_threading>
header file containing an implementation of the "libc++ thread API" declared
in the <__threading_support> header.
For the second use case, the implementation of the libc++ thread API can
delegate to a custom "external" thread API where the implementation of this
external API is provided in a seperate library. This mechanism allows toolchain
vendors to distribute a build of libc++ with a custom thread-porting-layer API
(which is the "external" API above), platform vendors (recipients of the
toolchain/libc++) are then required to provide their implementation of this API
to be linked with (end-user) C++ programs.
Note that the second use case still requires establishing the basic types that
get passed between the external thread library and the libc++ library
(e.g. __libcpp_mutex_t). These cannot be opaque pointer types (libc++ sources
won't compile otherwise). It should also be noted that the second use case can
have a slight performance penalty; as all the thread constructs need to cross a
library boundary through an additional function call.
When the header <__external_threading> is omitted, libc++ is built with the
"libc++ thread API" (declared in <__threading_support>) as the "external" thread
API (basic types are pthread based). An implementation (pthread based) of this
API is provided in test/support/external_threads.cpp, which is built into a
separate DSO and linked in when running the libc++ test suite. A test run
therefore demonstrates the second use case (less the intermediate custom API).
Differential revision: https://reviews.llvm.org/D21968
Reviewers: bcraig, compnerd, EricWF, mclow.lists
llvm-svn: 281179
Diffstat (limited to 'libcxx/include')
-rw-r--r-- | libcxx/include/__config | 9 | ||||
-rw-r--r-- | libcxx/include/__config_site.in | 1 | ||||
-rw-r--r-- | libcxx/include/__threading_support | 113 | ||||
-rw-r--r-- | libcxx/include/thread | 8 |
4 files changed, 85 insertions, 46 deletions
diff --git a/libcxx/include/__config b/libcxx/include/__config index b462191e436..c4e11013ca7 100644 --- a/libcxx/include/__config +++ b/libcxx/include/__config @@ -831,7 +831,9 @@ extern "C" void __sanitizer_annotate_contiguous_container( #endif // Thread API -#if !defined(_LIBCPP_HAS_NO_THREADS) && !defined(_LIBCPP_HAS_THREAD_API_PTHREAD) +#if !defined(_LIBCPP_HAS_NO_THREADS) && \ + !defined(_LIBCPP_HAS_THREAD_API_PTHREAD) && \ + !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) # if defined(__FreeBSD__) || \ defined(__NetBSD__) || \ defined(__linux__) || \ @@ -849,6 +851,11 @@ extern "C" void __sanitizer_annotate_contiguous_container( _LIBCPP_HAS_NO_THREADS is not defined. #endif +#if defined(_LIBCPP_HAS_NO_THREADS) && defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) +# error _LIBCPP_HAS_EXTERNAL_THREAD_API may not be defined when \ + _LIBCPP_HAS_NO_THREADS is defined. +#endif + #if defined(_LIBCPP_HAS_NO_MONOTONIC_CLOCK) && !defined(_LIBCPP_HAS_NO_THREADS) # error _LIBCPP_HAS_NO_MONOTONIC_CLOCK may only be defined when \ _LIBCPP_HAS_NO_THREADS is defined. diff --git a/libcxx/include/__config_site.in b/libcxx/include/__config_site.in index 6c2b7bb446d..dce30bc3921 100644 --- a/libcxx/include/__config_site.in +++ b/libcxx/include/__config_site.in @@ -20,5 +20,6 @@ #cmakedefine _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS #cmakedefine _LIBCPP_HAS_MUSL_LIBC #cmakedefine _LIBCPP_HAS_THREAD_API_PTHREAD +#cmakedefine _LIBCPP_HAS_THREAD_API_EXTERNAL #endif // _LIBCPP_CONFIG_SITE diff --git a/libcxx/include/__threading_support b/libcxx/include/__threading_support index c9a4ea9d094..b36a52ec9cc 100644 --- a/libcxx/include/__threading_support +++ b/libcxx/include/__threading_support @@ -19,20 +19,83 @@ #ifndef _LIBCPP_HAS_NO_THREADS -#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) +#if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) && (!defined(__has_include) || __has_include(<__external_threading>)) +#include <__external_threading> +#else #include <pthread.h> #include <sched.h> #endif -_LIBCPP_BEGIN_NAMESPACE_STD +#if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) +#define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_FUNC_VIS +#else +#define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY +#endif -#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) +_LIBCPP_BEGIN_NAMESPACE_STD // Mutex -#define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER typedef pthread_mutex_t __libcpp_mutex_t; +#define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER + +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_recursive_mutex_init(__libcpp_mutex_t* __m); +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_mutex_lock(__libcpp_mutex_t* __m); +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_mutex_trylock(__libcpp_mutex_t* __m); +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_mutex_unlock(__libcpp_mutex_t* __m); +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_mutex_destroy(__libcpp_mutex_t* __m); + +// Condition variable +typedef pthread_cond_t __libcpp_condvar_t; +#define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_condvar_signal(__libcpp_condvar_t* __cv); +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv); +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m); +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, timespec* __ts); +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv); + +// Thread id +typedef pthread_t __libcpp_thread_id; +_LIBCPP_THREAD_ABI_VISIBILITY +bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2); +_LIBCPP_THREAD_ABI_VISIBILITY +bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2); + +// Thread +typedef pthread_t __libcpp_thread_t; +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg); +_LIBCPP_THREAD_ABI_VISIBILITY +__libcpp_thread_id __libcpp_thread_get_current_id(); +_LIBCPP_THREAD_ABI_VISIBILITY +__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t); +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_thread_join(__libcpp_thread_t* __t); +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_thread_detach(__libcpp_thread_t* __t); +_LIBCPP_THREAD_ABI_VISIBILITY +void __libcpp_thread_yield(); + +// Thread local storage +typedef pthread_key_t __libcpp_tls_key; +_LIBCPP_THREAD_ABI_VISIBILITY +int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)); +_LIBCPP_THREAD_ABI_VISIBILITY +void* __libcpp_tls_get(__libcpp_tls_key __key); +_LIBCPP_THREAD_ABI_VISIBILITY +void __libcpp_tls_set(__libcpp_tls_key __key, void* __p); + +#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) || defined(_LIBCPP_BUILDING_EXTERNAL_THREADS) -inline _LIBCPP_ALWAYS_INLINE int __libcpp_recursive_mutex_init(__libcpp_mutex_t* __m) { pthread_mutexattr_t attr; @@ -59,144 +122,112 @@ int __libcpp_recursive_mutex_init(__libcpp_mutex_t* __m) return 0; } -inline _LIBCPP_ALWAYS_INLINE int __libcpp_mutex_lock(__libcpp_mutex_t* __m) { return pthread_mutex_lock(__m); } -inline _LIBCPP_ALWAYS_INLINE int __libcpp_mutex_trylock(__libcpp_mutex_t* __m) { return pthread_mutex_trylock(__m); } -inline _LIBCPP_ALWAYS_INLINE int __libcpp_mutex_unlock(__libcpp_mutex_t* __m) { return pthread_mutex_unlock(__m); } -inline _LIBCPP_ALWAYS_INLINE int __libcpp_mutex_destroy(__libcpp_mutex_t* __m) { return pthread_mutex_destroy(__m); } // Condition variable -#define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER -typedef pthread_cond_t __libcpp_condvar_t; - -inline _LIBCPP_ALWAYS_INLINE int __libcpp_condvar_signal(__libcpp_condvar_t* __cv) { return pthread_cond_signal(__cv); } -inline _LIBCPP_ALWAYS_INLINE int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv) { return pthread_cond_broadcast(__cv); } -inline _LIBCPP_ALWAYS_INLINE int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m) { return pthread_cond_wait(__cv, __m); } -inline _LIBCPP_ALWAYS_INLINE int __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, timespec* __ts) { return pthread_cond_timedwait(__cv, __m, __ts); } -inline _LIBCPP_ALWAYS_INLINE int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv) { return pthread_cond_destroy(__cv); } -// Thread id -typedef pthread_t __libcpp_thread_id; - // Returns non-zero if the thread ids are equal, otherwise 0 -inline _LIBCPP_ALWAYS_INLINE bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2) { return pthread_equal(t1, t2) != 0; } // Returns non-zero if t1 < t2, otherwise 0 -inline _LIBCPP_ALWAYS_INLINE bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2) { return t1 < t2; } // Thread -typedef pthread_t __libcpp_thread_t; - -inline _LIBCPP_ALWAYS_INLINE int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg) { return pthread_create(__t, 0, __func, __arg); } -inline _LIBCPP_ALWAYS_INLINE __libcpp_thread_id __libcpp_thread_get_current_id() { return pthread_self(); } -inline _LIBCPP_ALWAYS_INLINE __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t) { return *__t; } -inline _LIBCPP_ALWAYS_INLINE int __libcpp_thread_join(__libcpp_thread_t* __t) { return pthread_join(*__t, 0); } -inline _LIBCPP_ALWAYS_INLINE int __libcpp_thread_detach(__libcpp_thread_t* __t) { return pthread_detach(*__t); } -inline _LIBCPP_ALWAYS_INLINE void __libcpp_thread_yield() { sched_yield(); } // Thread local storage -typedef pthread_key_t __libcpp_tl_key; - -inline _LIBCPP_ALWAYS_INLINE -int __libcpp_tl_create(__libcpp_tl_key* __key, void (*__at_exit)(void*)) +int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)) { return pthread_key_create(__key, __at_exit); } -inline _LIBCPP_ALWAYS_INLINE -void* __libcpp_tl_get(__libcpp_tl_key __key) +void* __libcpp_tls_get(__libcpp_tls_key __key) { return pthread_getspecific(__key); } -inline _LIBCPP_ALWAYS_INLINE -void __libcpp_tl_set(__libcpp_tl_key __key, void* __p) +void __libcpp_tls_set(__libcpp_tls_key __key, void* __p) { pthread_setspecific(__key, __p); } -#else // !_LIBCPP_HAS_THREAD_API_PTHREAD - #error "No thread API selected." -#endif +#endif // _LIBCPP_HAS_THREAD_API_PTHREAD || _LIBCPP_BUILDING_EXTERNAL_THREADS _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/thread b/libcxx/include/thread index 21cc7610fd0..7fe4c77e1b1 100644 --- a/libcxx/include/thread +++ b/libcxx/include/thread @@ -137,7 +137,7 @@ public: template <class _Tp> class __thread_specific_ptr { - __libcpp_tl_key __key_; + __libcpp_tls_key __key_; // Only __thread_local_data() may construct a __thread_specific_ptr // and only with _Tp == __thread_struct. @@ -155,7 +155,7 @@ public: ~__thread_specific_ptr(); _LIBCPP_INLINE_VISIBILITY - pointer get() const {return static_cast<_Tp*>(__libcpp_tl_get(__key_));} + pointer get() const {return static_cast<_Tp*>(__libcpp_tls_get(__key_));} _LIBCPP_INLINE_VISIBILITY pointer operator*() const {return *get();} _LIBCPP_INLINE_VISIBILITY @@ -173,7 +173,7 @@ __thread_specific_ptr<_Tp>::__at_thread_exit(void* __p) template <class _Tp> __thread_specific_ptr<_Tp>::__thread_specific_ptr() { - int __ec = __libcpp_tl_create( + int __ec = __libcpp_tls_create( &__key_, &__thread_specific_ptr::__at_thread_exit); if (__ec) @@ -196,7 +196,7 @@ __thread_specific_ptr<_Tp>::set_pointer(pointer __p) { _LIBCPP_ASSERT(get() == nullptr, "Attempting to overwrite thread local data"); - __libcpp_tl_set(__key_, __p); + __libcpp_tls_set(__key_, __p); } class _LIBCPP_TYPE_VIS thread; |