mirror of
https://github.com/llvm-mirror/libcxx.git
synced 2025-10-23 01:18:52 +08:00
threading_support: refactor for Win32 threading
Refactor the header to allow us to implement alternate threading models with alternate data structures. Take the opportunity to clang-format the area. This will allow us to avoid re-declaring the interfaces for Win32 threading. NFC git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@290850 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -31,8 +31,11 @@
|
||||
__libcpp_has_include(<__external_threading>)
|
||||
#include <__external_threading>
|
||||
#else
|
||||
|
||||
#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#endif
|
||||
|
||||
#if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
|
||||
#define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_FUNC_VIS
|
||||
@@ -42,88 +45,115 @@
|
||||
|
||||
_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
|
||||
|
||||
// Condition Variable
|
||||
typedef pthread_cond_t __libcpp_condvar_t;
|
||||
#define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
|
||||
|
||||
// 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_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);
|
||||
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;
|
||||
// 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);
|
||||
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)
|
||||
#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
|
||||
|
||||
int __libcpp_recursive_mutex_init(__libcpp_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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (__ec) {
|
||||
pthread_mutexattr_destroy(&attr);
|
||||
return __ec;
|
||||
}
|
||||
__ec = pthread_mutexattr_destroy(&attr);
|
||||
if (__ec)
|
||||
{
|
||||
if (__ec) {
|
||||
pthread_mutex_destroy(__m);
|
||||
return __ec;
|
||||
}
|
||||
@@ -150,7 +180,7 @@ int __libcpp_mutex_destroy(__libcpp_mutex_t* __m)
|
||||
return pthread_mutex_destroy(__m);
|
||||
}
|
||||
|
||||
// Condition variable
|
||||
// Condition Variable
|
||||
int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
|
||||
{
|
||||
return pthread_cond_signal(__cv);
|
||||
@@ -166,7 +196,8 @@ 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)
|
||||
int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
|
||||
timespec *__ts)
|
||||
{
|
||||
return pthread_cond_timedwait(__cv, __m, __ts);
|
||||
}
|
||||
@@ -189,7 +220,8 @@ bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
|
||||
}
|
||||
|
||||
// Thread
|
||||
int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg)
|
||||
int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
|
||||
void *__arg)
|
||||
{
|
||||
return pthread_create(__t, 0, __func, __arg);
|
||||
}
|
||||
@@ -235,7 +267,7 @@ void __libcpp_tls_set(__libcpp_tls_key __key, void* __p)
|
||||
pthread_setspecific(__key, __p);
|
||||
}
|
||||
|
||||
#endif // _LIBCPP_HAS_THREAD_API_PTHREAD || _LIBCPP_BUILDING_EXTERNAL_THREADS
|
||||
#endif // _LIBCPP_HAS_THREAD_API_PTHREAD
|
||||
|
||||
_LIBCPP_END_NAMESPACE_STD
|
||||
|
||||
|
@@ -6,5 +6,5 @@
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#define _LIBCPP_BUILDING_EXTERNAL_THREADS
|
||||
#define _LIBCPP_HAS_THREAD_API_PTHREAD
|
||||
#include <__threading_support>
|
||||
|
Reference in New Issue
Block a user