blob: 202490062729f890251c452090c3205024ac9f23 [file] [log] [blame]
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_THREADING_SUPPORT
12#define _LIBCPP_THREADING_SUPPORT
13
14#include <__config>
Asiri Rathnayake1b939612017-02-09 09:31:41 +000015#include <chrono>
16#include <errno.h>
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +000017
18#ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
19#pragma GCC system_header
20#endif
21
Asiri Rathnayake040945b2016-09-11 21:46:40 +000022#if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
Eric Fiselier66134e82017-01-06 20:05:40 +000023# include <__external_threading>
24#elif !defined(_LIBCPP_HAS_NO_THREADS)
25
26#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
27# include <pthread.h>
28# include <sched.h>
29#endif
30
Eric Fiselier018a3d52017-05-31 22:07:49 +000031_LIBCPP_PUSH_MACROS
32#include <__undef_macros>
33
Peter Collingbourne61494b52018-02-21 21:36:18 +000034#if defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
35 defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL) || \
36 defined(_LIBCPP_HAS_THREAD_API_WIN32)
37#define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_FUNC_VIS
38#else
39#define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
40#endif
41
Dimitry Andric362353a2017-01-26 18:37:18 +000042#if defined(__FreeBSD__) && defined(__clang__) && __has_attribute(no_thread_safety_analysis)
43#define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
44#else
45#define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
46#endif
47
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +000048_LIBCPP_BEGIN_NAMESPACE_STD
49
Peter Collingbourne61494b52018-02-21 21:36:18 +000050#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +000051// Mutex
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +000052typedef pthread_mutex_t __libcpp_mutex_t;
Asiri Rathnayake040945b2016-09-11 21:46:40 +000053#define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +000054
Saleem Abdulrasool3451a652017-01-05 17:54:45 +000055typedef pthread_mutex_t __libcpp_recursive_mutex_t;
56
Saleem Abdulrasool1d192372017-01-03 02:00:31 +000057// Condition Variable
Asiri Rathnayake040945b2016-09-11 21:46:40 +000058typedef pthread_cond_t __libcpp_condvar_t;
59#define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
Saleem Abdulrasool1d192372017-01-03 02:00:31 +000060
Asiri Rathnayakee262e7b2017-01-03 12:59:50 +000061// Execute once
62typedef pthread_once_t __libcpp_exec_once_flag;
63#define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT
64
65// Thread id
Saleem Abdulrasool1d192372017-01-03 02:00:31 +000066typedef pthread_t __libcpp_thread_id;
67
68// Thread
Asiri Rathnayakea1d7d2f2017-01-16 12:19:54 +000069#define _LIBCPP_NULL_THREAD 0U
70
Saleem Abdulrasool1d192372017-01-03 02:00:31 +000071typedef pthread_t __libcpp_thread_t;
72
Fangrui Song35a0c2c2018-09-17 07:40:42 +000073// Thread Local Storage
Saleem Abdulrasool1d192372017-01-03 02:00:31 +000074typedef pthread_key_t __libcpp_tls_key;
Saleem Abdulrasool678eadd2017-01-07 03:07:45 +000075
76#define _LIBCPP_TLS_DESTRUCTOR_CC
77#else
Peter Collingbourne59f23892018-01-23 01:59:43 +000078// Mutex
79typedef void* __libcpp_mutex_t;
80#define _LIBCPP_MUTEX_INITIALIZER 0
81
82#if defined(_M_IX86) || defined(__i386__) || defined(_M_ARM) || defined(__arm__)
83typedef void* __libcpp_recursive_mutex_t[6];
84#elif defined(_M_AMD64) || defined(__x86_64__) || defined(_M_ARM64) || defined(__aarch64__)
85typedef void* __libcpp_recursive_mutex_t[5];
86#else
87# error Unsupported architecture
88#endif
Saleem Abdulrasool678eadd2017-01-07 03:07:45 +000089
90// Condition Variable
Peter Collingbourne59f23892018-01-23 01:59:43 +000091typedef void* __libcpp_condvar_t;
92#define _LIBCPP_CONDVAR_INITIALIZER 0
Saleem Abdulrasool678eadd2017-01-07 03:07:45 +000093
94// Execute Once
Peter Collingbourne59f23892018-01-23 01:59:43 +000095typedef void* __libcpp_exec_once_flag;
96#define _LIBCPP_EXEC_ONCE_INITIALIZER 0
Saleem Abdulrasool678eadd2017-01-07 03:07:45 +000097
98// Thread ID
Peter Collingbourne59f23892018-01-23 01:59:43 +000099typedef long __libcpp_thread_id;
Saleem Abdulrasool678eadd2017-01-07 03:07:45 +0000100
101// Thread
Asiri Rathnayakea1d7d2f2017-01-16 12:19:54 +0000102#define _LIBCPP_NULL_THREAD 0U
103
Peter Collingbourne59f23892018-01-23 01:59:43 +0000104typedef void* __libcpp_thread_t;
Saleem Abdulrasool678eadd2017-01-07 03:07:45 +0000105
106// Thread Local Storage
Peter Collingbourne59f23892018-01-23 01:59:43 +0000107typedef long __libcpp_tls_key;
Saleem Abdulrasool678eadd2017-01-07 03:07:45 +0000108
Peter Collingbourne59f23892018-01-23 01:59:43 +0000109#define _LIBCPP_TLS_DESTRUCTOR_CC __stdcall
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000110#endif
111
112// Mutex
113_LIBCPP_THREAD_ABI_VISIBILITY
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000114int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m);
115
Dimitry Andric362353a2017-01-26 18:37:18 +0000116_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000117int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m);
118
Dimitry Andric362353a2017-01-26 18:37:18 +0000119_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
Eric Fiselier30d12e32017-01-14 10:27:12 +0000120bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m);
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000121
Dimitry Andric362353a2017-01-26 18:37:18 +0000122_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000123int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m);
124
125_LIBCPP_THREAD_ABI_VISIBILITY
126int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m);
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000127
Dimitry Andric362353a2017-01-26 18:37:18 +0000128_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000129int __libcpp_mutex_lock(__libcpp_mutex_t *__m);
130
Dimitry Andric362353a2017-01-26 18:37:18 +0000131_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
Eric Fiselier30d12e32017-01-14 10:27:12 +0000132bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m);
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000133
Dimitry Andric362353a2017-01-26 18:37:18 +0000134_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000135int __libcpp_mutex_unlock(__libcpp_mutex_t *__m);
136
137_LIBCPP_THREAD_ABI_VISIBILITY
138int __libcpp_mutex_destroy(__libcpp_mutex_t *__m);
139
140// Condition variable
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000141_LIBCPP_THREAD_ABI_VISIBILITY
142int __libcpp_condvar_signal(__libcpp_condvar_t* __cv);
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000143
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000144_LIBCPP_THREAD_ABI_VISIBILITY
145int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv);
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000146
Dimitry Andric362353a2017-01-26 18:37:18 +0000147_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000148int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m);
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000149
Dimitry Andric362353a2017-01-26 18:37:18 +0000150_LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000151int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
152 timespec *__ts);
153
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000154_LIBCPP_THREAD_ABI_VISIBILITY
155int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv);
156
Asiri Rathnayakee262e7b2017-01-03 12:59:50 +0000157// Execute once
158_LIBCPP_THREAD_ABI_VISIBILITY
159int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
160 void (*init_routine)(void));
161
162// Thread id
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000163_LIBCPP_THREAD_ABI_VISIBILITY
164bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2);
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000165
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000166_LIBCPP_THREAD_ABI_VISIBILITY
167bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2);
168
169// Thread
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000170_LIBCPP_THREAD_ABI_VISIBILITY
Asiri Rathnayakea1d7d2f2017-01-16 12:19:54 +0000171bool __libcpp_thread_isnull(const __libcpp_thread_t *__t);
172
173_LIBCPP_THREAD_ABI_VISIBILITY
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000174int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
175 void *__arg);
176
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000177_LIBCPP_THREAD_ABI_VISIBILITY
178__libcpp_thread_id __libcpp_thread_get_current_id();
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000179
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000180_LIBCPP_THREAD_ABI_VISIBILITY
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000181__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t);
182
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000183_LIBCPP_THREAD_ABI_VISIBILITY
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000184int __libcpp_thread_join(__libcpp_thread_t *__t);
185
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000186_LIBCPP_THREAD_ABI_VISIBILITY
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000187int __libcpp_thread_detach(__libcpp_thread_t *__t);
188
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000189_LIBCPP_THREAD_ABI_VISIBILITY
190void __libcpp_thread_yield();
191
Asiri Rathnayake1b939612017-02-09 09:31:41 +0000192_LIBCPP_THREAD_ABI_VISIBILITY
Joerg Sonnenberger5a0c9c32017-02-09 14:12:29 +0000193void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns);
Asiri Rathnayake1b939612017-02-09 09:31:41 +0000194
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000195// Thread local storage
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000196_LIBCPP_THREAD_ABI_VISIBILITY
Saleem Abdulrasool678eadd2017-01-07 03:07:45 +0000197int __libcpp_tls_create(__libcpp_tls_key* __key,
198 void(_LIBCPP_TLS_DESTRUCTOR_CC* __at_exit)(void*));
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000199
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000200_LIBCPP_THREAD_ABI_VISIBILITY
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000201void *__libcpp_tls_get(__libcpp_tls_key __key);
202
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000203_LIBCPP_THREAD_ABI_VISIBILITY
Asiri Rathnayakee262e7b2017-01-03 12:59:50 +0000204int __libcpp_tls_set(__libcpp_tls_key __key, void *__p);
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000205
Peter Collingbourne59f23892018-01-23 01:59:43 +0000206#if (!defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
207 defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL)) && \
208 defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
Asiri Rathnayake040945b2016-09-11 21:46:40 +0000209
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000210int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000211{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000212 pthread_mutexattr_t attr;
213 int __ec = pthread_mutexattr_init(&attr);
214 if (__ec)
215 return __ec;
216 __ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
217 if (__ec) {
218 pthread_mutexattr_destroy(&attr);
219 return __ec;
220 }
221 __ec = pthread_mutex_init(__m, &attr);
222 if (__ec) {
223 pthread_mutexattr_destroy(&attr);
224 return __ec;
225 }
226 __ec = pthread_mutexattr_destroy(&attr);
227 if (__ec) {
228 pthread_mutex_destroy(__m);
229 return __ec;
230 }
231 return 0;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000232}
233
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000234int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
235{
236 return pthread_mutex_lock(__m);
237}
238
Eric Fiselier30d12e32017-01-14 10:27:12 +0000239bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000240{
Eric Fiselier30d12e32017-01-14 10:27:12 +0000241 return pthread_mutex_trylock(__m) == 0;
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000242}
243
244int __libcpp_recursive_mutex_unlock(__libcpp_mutex_t *__m)
245{
246 return pthread_mutex_unlock(__m);
247}
248
249int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
250{
251 return pthread_mutex_destroy(__m);
252}
253
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000254int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000255{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000256 return pthread_mutex_lock(__m);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000257}
258
Eric Fiselier30d12e32017-01-14 10:27:12 +0000259bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000260{
Eric Fiselier30d12e32017-01-14 10:27:12 +0000261 return pthread_mutex_trylock(__m) == 0;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000262}
263
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000264int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000265{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000266 return pthread_mutex_unlock(__m);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000267}
268
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000269int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000270{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000271 return pthread_mutex_destroy(__m);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000272}
273
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000274// Condition Variable
275int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000276{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000277 return pthread_cond_signal(__cv);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000278}
279
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000280int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000281{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000282 return pthread_cond_broadcast(__cv);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000283}
284
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000285int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000286{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000287 return pthread_cond_wait(__cv, __m);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000288}
289
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000290int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
291 timespec *__ts)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000292{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000293 return pthread_cond_timedwait(__cv, __m, __ts);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000294}
295
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000296int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000297{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000298 return pthread_cond_destroy(__cv);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000299}
300
Asiri Rathnayakee262e7b2017-01-03 12:59:50 +0000301// Execute once
302int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
303 void (*init_routine)(void)) {
304 return pthread_once(flag, init_routine);
305}
306
307// Thread id
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000308// Returns non-zero if the thread ids are equal, otherwise 0
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000309bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2)
310{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000311 return pthread_equal(t1, t2) != 0;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000312}
313
314// Returns non-zero if t1 < t2, otherwise 0
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000315bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
316{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000317 return t1 < t2;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000318}
319
320// Thread
Asiri Rathnayakea1d7d2f2017-01-16 12:19:54 +0000321bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
322 return *__t == 0;
323}
324
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000325int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
326 void *__arg)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000327{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000328 return pthread_create(__t, 0, __func, __arg);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000329}
330
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000331__libcpp_thread_id __libcpp_thread_get_current_id()
332{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000333 return pthread_self();
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000334}
335
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000336__libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000337{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000338 return *__t;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000339}
340
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000341int __libcpp_thread_join(__libcpp_thread_t *__t)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000342{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000343 return pthread_join(*__t, 0);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000344}
345
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000346int __libcpp_thread_detach(__libcpp_thread_t *__t)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000347{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000348 return pthread_detach(*__t);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000349}
350
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000351void __libcpp_thread_yield()
352{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000353 sched_yield();
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000354}
355
Asiri Rathnayake1b939612017-02-09 09:31:41 +0000356void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
357{
358 using namespace chrono;
359 seconds __s = duration_cast<seconds>(__ns);
360 timespec __ts;
361 typedef decltype(__ts.tv_sec) ts_sec;
362 _LIBCPP_CONSTEXPR ts_sec __ts_sec_max = numeric_limits<ts_sec>::max();
363
364 if (__s.count() < __ts_sec_max)
365 {
366 __ts.tv_sec = static_cast<ts_sec>(__s.count());
367 __ts.tv_nsec = static_cast<decltype(__ts.tv_nsec)>((__ns - __s).count());
368 }
369 else
370 {
371 __ts.tv_sec = __ts_sec_max;
372 __ts.tv_nsec = 999999999; // (10^9 - 1)
373 }
374
375 while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR);
376}
377
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000378// Thread local storage
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000379int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *))
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000380{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000381 return pthread_key_create(__key, __at_exit);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000382}
383
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000384void *__libcpp_tls_get(__libcpp_tls_key __key)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000385{
Saleem Abdulrasool1d192372017-01-03 02:00:31 +0000386 return pthread_getspecific(__key);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000387}
388
Asiri Rathnayakee262e7b2017-01-03 12:59:50 +0000389int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000390{
Asiri Rathnayakee262e7b2017-01-03 12:59:50 +0000391 return pthread_setspecific(__key, __p);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000392}
393
Eric Fiselier66134e82017-01-06 20:05:40 +0000394#endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL
395
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000396_LIBCPP_END_NAMESPACE_STD
397
Eric Fiselier018a3d52017-05-31 22:07:49 +0000398_LIBCPP_POP_MACROS
399
Eric Fiselier66134e82017-01-06 20:05:40 +0000400#endif // !_LIBCPP_HAS_NO_THREADS
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000401
402#endif // _LIBCPP_THREADING_SUPPORT