Summary: This patch attempts to clean up the macro configuration mess in `<__threading_support>`, specifically the mess involving external threading variants. Additionally this patch adds design documentation for `<__threading_support>` and the configuration macros it uses. The primary change in this patch is separating the idea of an "external API" provided by `<__external_threading>` and the idea of having an external threading library. Now `_LIBCPP_HAS_THREAD_API_EXTERNAL` means that libc++ should use `<__external_threading>` and that the header is expected to exist. Additionally the new macro `_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL` is now used to configure for using an "external library" with the default threading API. Reviewers: compnerd, rmaprath Subscribers: smeenai, cfe-commits, mgorny Differential Revision: https://reviews.llvm.org/D28316 git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@291275 91177308-0d34-0410-b5e6-96231b3b80d8
333 lines
8.1 KiB
C++
333 lines
8.1 KiB
C++
// -*- C++ -*-
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is dual licensed under the MIT and the University of Illinois Open
|
|
// Source Licenses. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef _LIBCPP_THREADING_SUPPORT
|
|
#define _LIBCPP_THREADING_SUPPORT
|
|
|
|
#include <__config>
|
|
|
|
#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
|
|
#pragma GCC system_header
|
|
#endif
|
|
|
|
#if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
|
|
# include <__external_threading>
|
|
#elif !defined(_LIBCPP_HAS_NO_THREADS)
|
|
|
|
#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
|
|
# include <pthread.h>
|
|
# include <sched.h>
|
|
#endif
|
|
|
|
#if defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
|
|
defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL)
|
|
#define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_FUNC_VIS
|
|
#else
|
|
#define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
|
|
#endif
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
|
|
// Mutex
|
|
typedef pthread_mutex_t __libcpp_mutex_t;
|
|
#define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
|
|
|
typedef pthread_mutex_t __libcpp_recursive_mutex_t;
|
|
|
|
// Condition Variable
|
|
typedef pthread_cond_t __libcpp_condvar_t;
|
|
#define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
|
|
|
|
// Execute once
|
|
typedef pthread_once_t __libcpp_exec_once_flag;
|
|
#define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT
|
|
|
|
// Thread id
|
|
typedef pthread_t __libcpp_thread_id;
|
|
|
|
// Thread
|
|
typedef pthread_t __libcpp_thread_t;
|
|
|
|
// Thrad Local Storage
|
|
typedef pthread_key_t __libcpp_tls_key;
|
|
#endif
|
|
|
|
// Mutex
|
|
_LIBCPP_THREAD_ABI_VISIBILITY
|
|
int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m);
|
|
|
|
_LIBCPP_THREAD_ABI_VISIBILITY
|
|
int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m);
|
|
|
|
_LIBCPP_THREAD_ABI_VISIBILITY
|
|
int __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m);
|
|
|
|
_LIBCPP_THREAD_ABI_VISIBILITY
|
|
int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m);
|
|
|
|
_LIBCPP_THREAD_ABI_VISIBILITY
|
|
int __libcpp_recursive_mutex_destroy(__libcpp_recursive_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
|
|
_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);
|
|
|
|
// Execute once
|
|
_LIBCPP_THREAD_ABI_VISIBILITY
|
|
int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
|
|
void (*init_routine)(void));
|
|
|
|
// Thread id
|
|
#if defined(__APPLE__) && !defined(__arm__)
|
|
_LIBCPP_THREAD_ABI_VISIBILITY
|
|
mach_port_t __libcpp_thread_get_port();
|
|
#endif
|
|
|
|
_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
|
|
_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
|
|
_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
|
|
int __libcpp_tls_set(__libcpp_tls_key __key, void *__p);
|
|
|
|
#if !defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
|
|
defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL)
|
|
|
|
#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
|
|
|
|
int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
|
|
{
|
|
pthread_mutexattr_t attr;
|
|
int __ec = pthread_mutexattr_init(&attr);
|
|
if (__ec)
|
|
return __ec;
|
|
__ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
if (__ec) {
|
|
pthread_mutexattr_destroy(&attr);
|
|
return __ec;
|
|
}
|
|
__ec = pthread_mutex_init(__m, &attr);
|
|
if (__ec) {
|
|
pthread_mutexattr_destroy(&attr);
|
|
return __ec;
|
|
}
|
|
__ec = pthread_mutexattr_destroy(&attr);
|
|
if (__ec) {
|
|
pthread_mutex_destroy(__m);
|
|
return __ec;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
|
|
{
|
|
return pthread_mutex_lock(__m);
|
|
}
|
|
|
|
int __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
|
|
{
|
|
return pthread_mutex_trylock(__m);
|
|
}
|
|
|
|
int __libcpp_recursive_mutex_unlock(__libcpp_mutex_t *__m)
|
|
{
|
|
return pthread_mutex_unlock(__m);
|
|
}
|
|
|
|
int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
|
|
{
|
|
return pthread_mutex_destroy(__m);
|
|
}
|
|
|
|
int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
|
|
{
|
|
return pthread_mutex_lock(__m);
|
|
}
|
|
|
|
int __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
|
|
{
|
|
return pthread_mutex_trylock(__m);
|
|
}
|
|
|
|
int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
|
|
{
|
|
return pthread_mutex_unlock(__m);
|
|
}
|
|
|
|
int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
|
|
{
|
|
return pthread_mutex_destroy(__m);
|
|
}
|
|
|
|
// Condition Variable
|
|
int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
|
|
{
|
|
return pthread_cond_signal(__cv);
|
|
}
|
|
|
|
int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
|
|
{
|
|
return pthread_cond_broadcast(__cv);
|
|
}
|
|
|
|
int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
|
|
{
|
|
return pthread_cond_wait(__cv, __m);
|
|
}
|
|
|
|
int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
|
|
timespec *__ts)
|
|
{
|
|
return pthread_cond_timedwait(__cv, __m, __ts);
|
|
}
|
|
|
|
int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
|
|
{
|
|
return pthread_cond_destroy(__cv);
|
|
}
|
|
|
|
// Execute once
|
|
int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
|
|
void (*init_routine)(void)) {
|
|
return pthread_once(flag, init_routine);
|
|
}
|
|
|
|
// Thread id
|
|
#if defined(__APPLE__) && !defined(__arm__)
|
|
mach_port_t __libcpp_thread_get_port() {
|
|
return pthread_mach_thread_np(pthread_self());
|
|
}
|
|
#endif
|
|
|
|
// Returns non-zero if the thread ids are equal, otherwise 0
|
|
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
|
|
bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
|
|
{
|
|
return t1 < t2;
|
|
}
|
|
|
|
// Thread
|
|
int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
|
|
void *__arg)
|
|
{
|
|
return pthread_create(__t, 0, __func, __arg);
|
|
}
|
|
|
|
__libcpp_thread_id __libcpp_thread_get_current_id()
|
|
{
|
|
return pthread_self();
|
|
}
|
|
|
|
__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
|
|
{
|
|
return *__t;
|
|
}
|
|
|
|
int __libcpp_thread_join(__libcpp_thread_t *__t)
|
|
{
|
|
return pthread_join(*__t, 0);
|
|
}
|
|
|
|
int __libcpp_thread_detach(__libcpp_thread_t *__t)
|
|
{
|
|
return pthread_detach(*__t);
|
|
}
|
|
|
|
void __libcpp_thread_yield()
|
|
{
|
|
sched_yield();
|
|
}
|
|
|
|
// Thread local storage
|
|
int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *))
|
|
{
|
|
return pthread_key_create(__key, __at_exit);
|
|
}
|
|
|
|
void *__libcpp_tls_get(__libcpp_tls_key __key)
|
|
{
|
|
return pthread_getspecific(__key);
|
|
}
|
|
|
|
int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
|
|
{
|
|
return pthread_setspecific(__key, __p);
|
|
}
|
|
|
|
#endif // _LIBCPP_HAS_THREAD_API_PTHREAD
|
|
|
|
#endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
#endif // !_LIBCPP_HAS_NO_THREADS
|
|
|
|
#endif // _LIBCPP_THREADING_SUPPORT
|