1*58b9f456SAndroid Build Coastguard Worker// -*- C++ -*- 2*58b9f456SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===// 3*58b9f456SAndroid Build Coastguard Worker// 4*58b9f456SAndroid Build Coastguard Worker// The LLVM Compiler Infrastructure 5*58b9f456SAndroid Build Coastguard Worker// 6*58b9f456SAndroid Build Coastguard Worker// This file is dual licensed under the MIT and the University of Illinois Open 7*58b9f456SAndroid Build Coastguard Worker// Source Licenses. See LICENSE.TXT for details. 8*58b9f456SAndroid Build Coastguard Worker// 9*58b9f456SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===// 10*58b9f456SAndroid Build Coastguard Worker 11*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_THREADING_SUPPORT 12*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_THREADING_SUPPORT 13*58b9f456SAndroid Build Coastguard Worker 14*58b9f456SAndroid Build Coastguard Worker#include <__config> 15*58b9f456SAndroid Build Coastguard Worker#include <chrono> 16*58b9f456SAndroid Build Coastguard Worker#include <errno.h> 17*58b9f456SAndroid Build Coastguard Worker 18*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER 19*58b9f456SAndroid Build Coastguard Worker#pragma GCC system_header 20*58b9f456SAndroid Build Coastguard Worker#endif 21*58b9f456SAndroid Build Coastguard Worker 22*58b9f456SAndroid Build Coastguard Worker#if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) 23*58b9f456SAndroid Build Coastguard Worker# include <__external_threading> 24*58b9f456SAndroid Build Coastguard Worker#elif !defined(_LIBCPP_HAS_NO_THREADS) 25*58b9f456SAndroid Build Coastguard Worker 26*58b9f456SAndroid Build Coastguard Worker#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) 27*58b9f456SAndroid Build Coastguard Worker# include <pthread.h> 28*58b9f456SAndroid Build Coastguard Worker# include <sched.h> 29*58b9f456SAndroid Build Coastguard Worker#endif 30*58b9f456SAndroid Build Coastguard Worker 31*58b9f456SAndroid Build Coastguard Worker_LIBCPP_PUSH_MACROS 32*58b9f456SAndroid Build Coastguard Worker#include <__undef_macros> 33*58b9f456SAndroid Build Coastguard Worker 34*58b9f456SAndroid Build Coastguard Worker#if defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \ 35*58b9f456SAndroid Build Coastguard Worker defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL) || \ 36*58b9f456SAndroid Build Coastguard Worker defined(_LIBCPP_HAS_THREAD_API_WIN32) 37*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_FUNC_VIS 38*58b9f456SAndroid Build Coastguard Worker#else 39*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY 40*58b9f456SAndroid Build Coastguard Worker#endif 41*58b9f456SAndroid Build Coastguard Worker 42*58b9f456SAndroid Build Coastguard Worker#if defined(__FreeBSD__) && defined(__clang__) && __has_attribute(no_thread_safety_analysis) 43*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis)) 44*58b9f456SAndroid Build Coastguard Worker#else 45*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS 46*58b9f456SAndroid Build Coastguard Worker#endif 47*58b9f456SAndroid Build Coastguard Worker 48*58b9f456SAndroid Build Coastguard Worker_LIBCPP_BEGIN_NAMESPACE_STD 49*58b9f456SAndroid Build Coastguard Worker 50*58b9f456SAndroid Build Coastguard Worker#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) 51*58b9f456SAndroid Build Coastguard Worker// Mutex 52*58b9f456SAndroid Build Coastguard Workertypedef pthread_mutex_t __libcpp_mutex_t; 53*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 54*58b9f456SAndroid Build Coastguard Worker 55*58b9f456SAndroid Build Coastguard Workertypedef pthread_mutex_t __libcpp_recursive_mutex_t; 56*58b9f456SAndroid Build Coastguard Worker 57*58b9f456SAndroid Build Coastguard Worker// Condition Variable 58*58b9f456SAndroid Build Coastguard Workertypedef pthread_cond_t __libcpp_condvar_t; 59*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER 60*58b9f456SAndroid Build Coastguard Worker 61*58b9f456SAndroid Build Coastguard Worker// Execute once 62*58b9f456SAndroid Build Coastguard Workertypedef pthread_once_t __libcpp_exec_once_flag; 63*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT 64*58b9f456SAndroid Build Coastguard Worker 65*58b9f456SAndroid Build Coastguard Worker// Thread id 66*58b9f456SAndroid Build Coastguard Workertypedef pthread_t __libcpp_thread_id; 67*58b9f456SAndroid Build Coastguard Worker 68*58b9f456SAndroid Build Coastguard Worker// Thread 69*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_NULL_THREAD 0U 70*58b9f456SAndroid Build Coastguard Worker 71*58b9f456SAndroid Build Coastguard Workertypedef pthread_t __libcpp_thread_t; 72*58b9f456SAndroid Build Coastguard Worker 73*58b9f456SAndroid Build Coastguard Worker// Thread Local Storage 74*58b9f456SAndroid Build Coastguard Workertypedef pthread_key_t __libcpp_tls_key; 75*58b9f456SAndroid Build Coastguard Worker 76*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_TLS_DESTRUCTOR_CC 77*58b9f456SAndroid Build Coastguard Worker#else 78*58b9f456SAndroid Build Coastguard Worker// Mutex 79*58b9f456SAndroid Build Coastguard Workertypedef void* __libcpp_mutex_t; 80*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_MUTEX_INITIALIZER 0 81*58b9f456SAndroid Build Coastguard Worker 82*58b9f456SAndroid Build Coastguard Worker#if defined(_M_IX86) || defined(__i386__) || defined(_M_ARM) || defined(__arm__) 83*58b9f456SAndroid Build Coastguard Workertypedef void* __libcpp_recursive_mutex_t[6]; 84*58b9f456SAndroid Build Coastguard Worker#elif defined(_M_AMD64) || defined(__x86_64__) || defined(_M_ARM64) || defined(__aarch64__) 85*58b9f456SAndroid Build Coastguard Workertypedef void* __libcpp_recursive_mutex_t[5]; 86*58b9f456SAndroid Build Coastguard Worker#else 87*58b9f456SAndroid Build Coastguard Worker# error Unsupported architecture 88*58b9f456SAndroid Build Coastguard Worker#endif 89*58b9f456SAndroid Build Coastguard Worker 90*58b9f456SAndroid Build Coastguard Worker// Condition Variable 91*58b9f456SAndroid Build Coastguard Workertypedef void* __libcpp_condvar_t; 92*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_CONDVAR_INITIALIZER 0 93*58b9f456SAndroid Build Coastguard Worker 94*58b9f456SAndroid Build Coastguard Worker// Execute Once 95*58b9f456SAndroid Build Coastguard Workertypedef void* __libcpp_exec_once_flag; 96*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_EXEC_ONCE_INITIALIZER 0 97*58b9f456SAndroid Build Coastguard Worker 98*58b9f456SAndroid Build Coastguard Worker// Thread ID 99*58b9f456SAndroid Build Coastguard Workertypedef long __libcpp_thread_id; 100*58b9f456SAndroid Build Coastguard Worker 101*58b9f456SAndroid Build Coastguard Worker// Thread 102*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_NULL_THREAD 0U 103*58b9f456SAndroid Build Coastguard Worker 104*58b9f456SAndroid Build Coastguard Workertypedef void* __libcpp_thread_t; 105*58b9f456SAndroid Build Coastguard Worker 106*58b9f456SAndroid Build Coastguard Worker// Thread Local Storage 107*58b9f456SAndroid Build Coastguard Workertypedef long __libcpp_tls_key; 108*58b9f456SAndroid Build Coastguard Worker 109*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP_TLS_DESTRUCTOR_CC __stdcall 110*58b9f456SAndroid Build Coastguard Worker#endif 111*58b9f456SAndroid Build Coastguard Worker 112*58b9f456SAndroid Build Coastguard Worker// Mutex 113*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 114*58b9f456SAndroid Build Coastguard Workerint __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m); 115*58b9f456SAndroid Build Coastguard Worker 116*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS 117*58b9f456SAndroid Build Coastguard Workerint __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m); 118*58b9f456SAndroid Build Coastguard Worker 119*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS 120*58b9f456SAndroid Build Coastguard Workerbool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m); 121*58b9f456SAndroid Build Coastguard Worker 122*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS 123*58b9f456SAndroid Build Coastguard Workerint __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m); 124*58b9f456SAndroid Build Coastguard Worker 125*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 126*58b9f456SAndroid Build Coastguard Workerint __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m); 127*58b9f456SAndroid Build Coastguard Worker 128*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS 129*58b9f456SAndroid Build Coastguard Workerint __libcpp_mutex_lock(__libcpp_mutex_t *__m); 130*58b9f456SAndroid Build Coastguard Worker 131*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS 132*58b9f456SAndroid Build Coastguard Workerbool __libcpp_mutex_trylock(__libcpp_mutex_t *__m); 133*58b9f456SAndroid Build Coastguard Worker 134*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS 135*58b9f456SAndroid Build Coastguard Workerint __libcpp_mutex_unlock(__libcpp_mutex_t *__m); 136*58b9f456SAndroid Build Coastguard Worker 137*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 138*58b9f456SAndroid Build Coastguard Workerint __libcpp_mutex_destroy(__libcpp_mutex_t *__m); 139*58b9f456SAndroid Build Coastguard Worker 140*58b9f456SAndroid Build Coastguard Worker// Condition variable 141*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 142*58b9f456SAndroid Build Coastguard Workerint __libcpp_condvar_signal(__libcpp_condvar_t* __cv); 143*58b9f456SAndroid Build Coastguard Worker 144*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 145*58b9f456SAndroid Build Coastguard Workerint __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv); 146*58b9f456SAndroid Build Coastguard Worker 147*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS 148*58b9f456SAndroid Build Coastguard Workerint __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m); 149*58b9f456SAndroid Build Coastguard Worker 150*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS 151*58b9f456SAndroid Build Coastguard Workerint __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m, 152*58b9f456SAndroid Build Coastguard Worker timespec *__ts); 153*58b9f456SAndroid Build Coastguard Worker 154*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 155*58b9f456SAndroid Build Coastguard Workerint __libcpp_condvar_destroy(__libcpp_condvar_t* __cv); 156*58b9f456SAndroid Build Coastguard Worker 157*58b9f456SAndroid Build Coastguard Worker// Execute once 158*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 159*58b9f456SAndroid Build Coastguard Workerint __libcpp_execute_once(__libcpp_exec_once_flag *flag, 160*58b9f456SAndroid Build Coastguard Worker void (*init_routine)(void)); 161*58b9f456SAndroid Build Coastguard Worker 162*58b9f456SAndroid Build Coastguard Worker// Thread id 163*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 164*58b9f456SAndroid Build Coastguard Workerbool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2); 165*58b9f456SAndroid Build Coastguard Worker 166*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 167*58b9f456SAndroid Build Coastguard Workerbool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2); 168*58b9f456SAndroid Build Coastguard Worker 169*58b9f456SAndroid Build Coastguard Worker// Thread 170*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 171*58b9f456SAndroid Build Coastguard Workerbool __libcpp_thread_isnull(const __libcpp_thread_t *__t); 172*58b9f456SAndroid Build Coastguard Worker 173*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 174*58b9f456SAndroid Build Coastguard Workerint __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *), 175*58b9f456SAndroid Build Coastguard Worker void *__arg); 176*58b9f456SAndroid Build Coastguard Worker 177*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 178*58b9f456SAndroid Build Coastguard Worker__libcpp_thread_id __libcpp_thread_get_current_id(); 179*58b9f456SAndroid Build Coastguard Worker 180*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 181*58b9f456SAndroid Build Coastguard Worker__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t); 182*58b9f456SAndroid Build Coastguard Worker 183*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 184*58b9f456SAndroid Build Coastguard Workerint __libcpp_thread_join(__libcpp_thread_t *__t); 185*58b9f456SAndroid Build Coastguard Worker 186*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 187*58b9f456SAndroid Build Coastguard Workerint __libcpp_thread_detach(__libcpp_thread_t *__t); 188*58b9f456SAndroid Build Coastguard Worker 189*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 190*58b9f456SAndroid Build Coastguard Workervoid __libcpp_thread_yield(); 191*58b9f456SAndroid Build Coastguard Worker 192*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 193*58b9f456SAndroid Build Coastguard Workervoid __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns); 194*58b9f456SAndroid Build Coastguard Worker 195*58b9f456SAndroid Build Coastguard Worker// Thread local storage 196*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 197*58b9f456SAndroid Build Coastguard Workerint __libcpp_tls_create(__libcpp_tls_key* __key, 198*58b9f456SAndroid Build Coastguard Worker void(_LIBCPP_TLS_DESTRUCTOR_CC* __at_exit)(void*)); 199*58b9f456SAndroid Build Coastguard Worker 200*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 201*58b9f456SAndroid Build Coastguard Workervoid *__libcpp_tls_get(__libcpp_tls_key __key); 202*58b9f456SAndroid Build Coastguard Worker 203*58b9f456SAndroid Build Coastguard Worker_LIBCPP_THREAD_ABI_VISIBILITY 204*58b9f456SAndroid Build Coastguard Workerint __libcpp_tls_set(__libcpp_tls_key __key, void *__p); 205*58b9f456SAndroid Build Coastguard Worker 206*58b9f456SAndroid Build Coastguard Worker#if (!defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \ 207*58b9f456SAndroid Build Coastguard Worker defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL)) && \ 208*58b9f456SAndroid Build Coastguard Worker defined(_LIBCPP_HAS_THREAD_API_PTHREAD) 209*58b9f456SAndroid Build Coastguard Worker 210*58b9f456SAndroid Build Coastguard Workerint __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m) 211*58b9f456SAndroid Build Coastguard Worker{ 212*58b9f456SAndroid Build Coastguard Worker pthread_mutexattr_t attr; 213*58b9f456SAndroid Build Coastguard Worker int __ec = pthread_mutexattr_init(&attr); 214*58b9f456SAndroid Build Coastguard Worker if (__ec) 215*58b9f456SAndroid Build Coastguard Worker return __ec; 216*58b9f456SAndroid Build Coastguard Worker __ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 217*58b9f456SAndroid Build Coastguard Worker if (__ec) { 218*58b9f456SAndroid Build Coastguard Worker pthread_mutexattr_destroy(&attr); 219*58b9f456SAndroid Build Coastguard Worker return __ec; 220*58b9f456SAndroid Build Coastguard Worker } 221*58b9f456SAndroid Build Coastguard Worker __ec = pthread_mutex_init(__m, &attr); 222*58b9f456SAndroid Build Coastguard Worker if (__ec) { 223*58b9f456SAndroid Build Coastguard Worker pthread_mutexattr_destroy(&attr); 224*58b9f456SAndroid Build Coastguard Worker return __ec; 225*58b9f456SAndroid Build Coastguard Worker } 226*58b9f456SAndroid Build Coastguard Worker __ec = pthread_mutexattr_destroy(&attr); 227*58b9f456SAndroid Build Coastguard Worker if (__ec) { 228*58b9f456SAndroid Build Coastguard Worker pthread_mutex_destroy(__m); 229*58b9f456SAndroid Build Coastguard Worker return __ec; 230*58b9f456SAndroid Build Coastguard Worker } 231*58b9f456SAndroid Build Coastguard Worker return 0; 232*58b9f456SAndroid Build Coastguard Worker} 233*58b9f456SAndroid Build Coastguard Worker 234*58b9f456SAndroid Build Coastguard Workerint __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m) 235*58b9f456SAndroid Build Coastguard Worker{ 236*58b9f456SAndroid Build Coastguard Worker return pthread_mutex_lock(__m); 237*58b9f456SAndroid Build Coastguard Worker} 238*58b9f456SAndroid Build Coastguard Worker 239*58b9f456SAndroid Build Coastguard Workerbool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m) 240*58b9f456SAndroid Build Coastguard Worker{ 241*58b9f456SAndroid Build Coastguard Worker return pthread_mutex_trylock(__m) == 0; 242*58b9f456SAndroid Build Coastguard Worker} 243*58b9f456SAndroid Build Coastguard Worker 244*58b9f456SAndroid Build Coastguard Workerint __libcpp_recursive_mutex_unlock(__libcpp_mutex_t *__m) 245*58b9f456SAndroid Build Coastguard Worker{ 246*58b9f456SAndroid Build Coastguard Worker return pthread_mutex_unlock(__m); 247*58b9f456SAndroid Build Coastguard Worker} 248*58b9f456SAndroid Build Coastguard Worker 249*58b9f456SAndroid Build Coastguard Workerint __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m) 250*58b9f456SAndroid Build Coastguard Worker{ 251*58b9f456SAndroid Build Coastguard Worker return pthread_mutex_destroy(__m); 252*58b9f456SAndroid Build Coastguard Worker} 253*58b9f456SAndroid Build Coastguard Worker 254*58b9f456SAndroid Build Coastguard Workerint __libcpp_mutex_lock(__libcpp_mutex_t *__m) 255*58b9f456SAndroid Build Coastguard Worker{ 256*58b9f456SAndroid Build Coastguard Worker return pthread_mutex_lock(__m); 257*58b9f456SAndroid Build Coastguard Worker} 258*58b9f456SAndroid Build Coastguard Worker 259*58b9f456SAndroid Build Coastguard Workerbool __libcpp_mutex_trylock(__libcpp_mutex_t *__m) 260*58b9f456SAndroid Build Coastguard Worker{ 261*58b9f456SAndroid Build Coastguard Worker return pthread_mutex_trylock(__m) == 0; 262*58b9f456SAndroid Build Coastguard Worker} 263*58b9f456SAndroid Build Coastguard Worker 264*58b9f456SAndroid Build Coastguard Workerint __libcpp_mutex_unlock(__libcpp_mutex_t *__m) 265*58b9f456SAndroid Build Coastguard Worker{ 266*58b9f456SAndroid Build Coastguard Worker return pthread_mutex_unlock(__m); 267*58b9f456SAndroid Build Coastguard Worker} 268*58b9f456SAndroid Build Coastguard Worker 269*58b9f456SAndroid Build Coastguard Workerint __libcpp_mutex_destroy(__libcpp_mutex_t *__m) 270*58b9f456SAndroid Build Coastguard Worker{ 271*58b9f456SAndroid Build Coastguard Worker return pthread_mutex_destroy(__m); 272*58b9f456SAndroid Build Coastguard Worker} 273*58b9f456SAndroid Build Coastguard Worker 274*58b9f456SAndroid Build Coastguard Worker// Condition Variable 275*58b9f456SAndroid Build Coastguard Workerint __libcpp_condvar_signal(__libcpp_condvar_t *__cv) 276*58b9f456SAndroid Build Coastguard Worker{ 277*58b9f456SAndroid Build Coastguard Worker return pthread_cond_signal(__cv); 278*58b9f456SAndroid Build Coastguard Worker} 279*58b9f456SAndroid Build Coastguard Worker 280*58b9f456SAndroid Build Coastguard Workerint __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv) 281*58b9f456SAndroid Build Coastguard Worker{ 282*58b9f456SAndroid Build Coastguard Worker return pthread_cond_broadcast(__cv); 283*58b9f456SAndroid Build Coastguard Worker} 284*58b9f456SAndroid Build Coastguard Worker 285*58b9f456SAndroid Build Coastguard Workerint __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m) 286*58b9f456SAndroid Build Coastguard Worker{ 287*58b9f456SAndroid Build Coastguard Worker return pthread_cond_wait(__cv, __m); 288*58b9f456SAndroid Build Coastguard Worker} 289*58b9f456SAndroid Build Coastguard Worker 290*58b9f456SAndroid Build Coastguard Workerint __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m, 291*58b9f456SAndroid Build Coastguard Worker timespec *__ts) 292*58b9f456SAndroid Build Coastguard Worker{ 293*58b9f456SAndroid Build Coastguard Worker return pthread_cond_timedwait(__cv, __m, __ts); 294*58b9f456SAndroid Build Coastguard Worker} 295*58b9f456SAndroid Build Coastguard Worker 296*58b9f456SAndroid Build Coastguard Workerint __libcpp_condvar_destroy(__libcpp_condvar_t *__cv) 297*58b9f456SAndroid Build Coastguard Worker{ 298*58b9f456SAndroid Build Coastguard Worker return pthread_cond_destroy(__cv); 299*58b9f456SAndroid Build Coastguard Worker} 300*58b9f456SAndroid Build Coastguard Worker 301*58b9f456SAndroid Build Coastguard Worker// Execute once 302*58b9f456SAndroid Build Coastguard Workerint __libcpp_execute_once(__libcpp_exec_once_flag *flag, 303*58b9f456SAndroid Build Coastguard Worker void (*init_routine)(void)) { 304*58b9f456SAndroid Build Coastguard Worker return pthread_once(flag, init_routine); 305*58b9f456SAndroid Build Coastguard Worker} 306*58b9f456SAndroid Build Coastguard Worker 307*58b9f456SAndroid Build Coastguard Worker// Thread id 308*58b9f456SAndroid Build Coastguard Worker// Returns non-zero if the thread ids are equal, otherwise 0 309*58b9f456SAndroid Build Coastguard Workerbool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2) 310*58b9f456SAndroid Build Coastguard Worker{ 311*58b9f456SAndroid Build Coastguard Worker return pthread_equal(t1, t2) != 0; 312*58b9f456SAndroid Build Coastguard Worker} 313*58b9f456SAndroid Build Coastguard Worker 314*58b9f456SAndroid Build Coastguard Worker// Returns non-zero if t1 < t2, otherwise 0 315*58b9f456SAndroid Build Coastguard Workerbool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2) 316*58b9f456SAndroid Build Coastguard Worker{ 317*58b9f456SAndroid Build Coastguard Worker return t1 < t2; 318*58b9f456SAndroid Build Coastguard Worker} 319*58b9f456SAndroid Build Coastguard Worker 320*58b9f456SAndroid Build Coastguard Worker// Thread 321*58b9f456SAndroid Build Coastguard Workerbool __libcpp_thread_isnull(const __libcpp_thread_t *__t) { 322*58b9f456SAndroid Build Coastguard Worker return *__t == 0; 323*58b9f456SAndroid Build Coastguard Worker} 324*58b9f456SAndroid Build Coastguard Worker 325*58b9f456SAndroid Build Coastguard Workerint __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *), 326*58b9f456SAndroid Build Coastguard Worker void *__arg) 327*58b9f456SAndroid Build Coastguard Worker{ 328*58b9f456SAndroid Build Coastguard Worker return pthread_create(__t, 0, __func, __arg); 329*58b9f456SAndroid Build Coastguard Worker} 330*58b9f456SAndroid Build Coastguard Worker 331*58b9f456SAndroid Build Coastguard Worker__libcpp_thread_id __libcpp_thread_get_current_id() 332*58b9f456SAndroid Build Coastguard Worker{ 333*58b9f456SAndroid Build Coastguard Worker return pthread_self(); 334*58b9f456SAndroid Build Coastguard Worker} 335*58b9f456SAndroid Build Coastguard Worker 336*58b9f456SAndroid Build Coastguard Worker__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t) 337*58b9f456SAndroid Build Coastguard Worker{ 338*58b9f456SAndroid Build Coastguard Worker return *__t; 339*58b9f456SAndroid Build Coastguard Worker} 340*58b9f456SAndroid Build Coastguard Worker 341*58b9f456SAndroid Build Coastguard Workerint __libcpp_thread_join(__libcpp_thread_t *__t) 342*58b9f456SAndroid Build Coastguard Worker{ 343*58b9f456SAndroid Build Coastguard Worker return pthread_join(*__t, 0); 344*58b9f456SAndroid Build Coastguard Worker} 345*58b9f456SAndroid Build Coastguard Worker 346*58b9f456SAndroid Build Coastguard Workerint __libcpp_thread_detach(__libcpp_thread_t *__t) 347*58b9f456SAndroid Build Coastguard Worker{ 348*58b9f456SAndroid Build Coastguard Worker return pthread_detach(*__t); 349*58b9f456SAndroid Build Coastguard Worker} 350*58b9f456SAndroid Build Coastguard Worker 351*58b9f456SAndroid Build Coastguard Workervoid __libcpp_thread_yield() 352*58b9f456SAndroid Build Coastguard Worker{ 353*58b9f456SAndroid Build Coastguard Worker sched_yield(); 354*58b9f456SAndroid Build Coastguard Worker} 355*58b9f456SAndroid Build Coastguard Worker 356*58b9f456SAndroid Build Coastguard Workervoid __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns) 357*58b9f456SAndroid Build Coastguard Worker{ 358*58b9f456SAndroid Build Coastguard Worker using namespace chrono; 359*58b9f456SAndroid Build Coastguard Worker seconds __s = duration_cast<seconds>(__ns); 360*58b9f456SAndroid Build Coastguard Worker timespec __ts; 361*58b9f456SAndroid Build Coastguard Worker typedef decltype(__ts.tv_sec) ts_sec; 362*58b9f456SAndroid Build Coastguard Worker _LIBCPP_CONSTEXPR ts_sec __ts_sec_max = numeric_limits<ts_sec>::max(); 363*58b9f456SAndroid Build Coastguard Worker 364*58b9f456SAndroid Build Coastguard Worker if (__s.count() < __ts_sec_max) 365*58b9f456SAndroid Build Coastguard Worker { 366*58b9f456SAndroid Build Coastguard Worker __ts.tv_sec = static_cast<ts_sec>(__s.count()); 367*58b9f456SAndroid Build Coastguard Worker __ts.tv_nsec = static_cast<decltype(__ts.tv_nsec)>((__ns - __s).count()); 368*58b9f456SAndroid Build Coastguard Worker } 369*58b9f456SAndroid Build Coastguard Worker else 370*58b9f456SAndroid Build Coastguard Worker { 371*58b9f456SAndroid Build Coastguard Worker __ts.tv_sec = __ts_sec_max; 372*58b9f456SAndroid Build Coastguard Worker __ts.tv_nsec = 999999999; // (10^9 - 1) 373*58b9f456SAndroid Build Coastguard Worker } 374*58b9f456SAndroid Build Coastguard Worker 375*58b9f456SAndroid Build Coastguard Worker while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR); 376*58b9f456SAndroid Build Coastguard Worker} 377*58b9f456SAndroid Build Coastguard Worker 378*58b9f456SAndroid Build Coastguard Worker// Thread local storage 379*58b9f456SAndroid Build Coastguard Workerint __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *)) 380*58b9f456SAndroid Build Coastguard Worker{ 381*58b9f456SAndroid Build Coastguard Worker return pthread_key_create(__key, __at_exit); 382*58b9f456SAndroid Build Coastguard Worker} 383*58b9f456SAndroid Build Coastguard Worker 384*58b9f456SAndroid Build Coastguard Workervoid *__libcpp_tls_get(__libcpp_tls_key __key) 385*58b9f456SAndroid Build Coastguard Worker{ 386*58b9f456SAndroid Build Coastguard Worker return pthread_getspecific(__key); 387*58b9f456SAndroid Build Coastguard Worker} 388*58b9f456SAndroid Build Coastguard Worker 389*58b9f456SAndroid Build Coastguard Workerint __libcpp_tls_set(__libcpp_tls_key __key, void *__p) 390*58b9f456SAndroid Build Coastguard Worker{ 391*58b9f456SAndroid Build Coastguard Worker return pthread_setspecific(__key, __p); 392*58b9f456SAndroid Build Coastguard Worker} 393*58b9f456SAndroid Build Coastguard Worker 394*58b9f456SAndroid Build Coastguard Worker#endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL 395*58b9f456SAndroid Build Coastguard Worker 396*58b9f456SAndroid Build Coastguard Worker_LIBCPP_END_NAMESPACE_STD 397*58b9f456SAndroid Build Coastguard Worker 398*58b9f456SAndroid Build Coastguard Worker_LIBCPP_POP_MACROS 399*58b9f456SAndroid Build Coastguard Worker 400*58b9f456SAndroid Build Coastguard Worker#endif // !_LIBCPP_HAS_NO_THREADS 401*58b9f456SAndroid Build Coastguard Worker 402*58b9f456SAndroid Build Coastguard Worker#endif // _LIBCPP_THREADING_SUPPORT 403