annotate libcxx/include/__threading_support @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 0572611fdcc8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // -*- C++ -*-
anatofuz
parents:
diff changeset
2 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
3 //
anatofuz
parents:
diff changeset
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
5 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
7 //
anatofuz
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
9
anatofuz
parents:
diff changeset
10 #ifndef _LIBCPP_THREADING_SUPPORT
anatofuz
parents:
diff changeset
11 #define _LIBCPP_THREADING_SUPPORT
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #include <__config>
anatofuz
parents:
diff changeset
14 #include <chrono>
anatofuz
parents:
diff changeset
15 #include <iosfwd>
anatofuz
parents:
diff changeset
16 #include <errno.h>
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
anatofuz
parents:
diff changeset
19 #pragma GCC system_header
anatofuz
parents:
diff changeset
20 #endif
anatofuz
parents:
diff changeset
21
anatofuz
parents:
diff changeset
22 #if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
anatofuz
parents:
diff changeset
23 # include <__external_threading>
anatofuz
parents:
diff changeset
24 #elif !defined(_LIBCPP_HAS_NO_THREADS)
anatofuz
parents:
diff changeset
25
anatofuz
parents:
diff changeset
26 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
anatofuz
parents:
diff changeset
27 # include <pthread.h>
anatofuz
parents:
diff changeset
28 # include <sched.h>
anatofuz
parents:
diff changeset
29 #elif defined(_LIBCPP_HAS_THREAD_API_C11)
anatofuz
parents:
diff changeset
30 # include <threads.h>
anatofuz
parents:
diff changeset
31 #endif
anatofuz
parents:
diff changeset
32
anatofuz
parents:
diff changeset
33 #if defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
anatofuz
parents:
diff changeset
34 defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL) || \
anatofuz
parents:
diff changeset
35 defined(_LIBCPP_HAS_THREAD_API_WIN32)
anatofuz
parents:
diff changeset
36 #define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_FUNC_VIS
anatofuz
parents:
diff changeset
37 #else
anatofuz
parents:
diff changeset
38 #define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
39 #endif
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 #if defined(__FreeBSD__) && defined(__clang__) && __has_attribute(no_thread_safety_analysis)
anatofuz
parents:
diff changeset
42 #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
anatofuz
parents:
diff changeset
43 #else
anatofuz
parents:
diff changeset
44 #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
anatofuz
parents:
diff changeset
45 #endif
anatofuz
parents:
diff changeset
46
anatofuz
parents:
diff changeset
47 typedef ::timespec __libcpp_timespec_t;
anatofuz
parents:
diff changeset
48 #endif // !defined(_LIBCPP_HAS_NO_THREADS)
anatofuz
parents:
diff changeset
49
anatofuz
parents:
diff changeset
50 _LIBCPP_PUSH_MACROS
anatofuz
parents:
diff changeset
51 #include <__undef_macros>
anatofuz
parents:
diff changeset
52
anatofuz
parents:
diff changeset
53 _LIBCPP_BEGIN_NAMESPACE_STD
anatofuz
parents:
diff changeset
54
anatofuz
parents:
diff changeset
55 #if !defined(_LIBCPP_HAS_NO_THREADS)
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
anatofuz
parents:
diff changeset
58 // Mutex
anatofuz
parents:
diff changeset
59 typedef pthread_mutex_t __libcpp_mutex_t;
anatofuz
parents:
diff changeset
60 #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 typedef pthread_mutex_t __libcpp_recursive_mutex_t;
anatofuz
parents:
diff changeset
63
anatofuz
parents:
diff changeset
64 // Condition Variable
anatofuz
parents:
diff changeset
65 typedef pthread_cond_t __libcpp_condvar_t;
anatofuz
parents:
diff changeset
66 #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
anatofuz
parents:
diff changeset
67
anatofuz
parents:
diff changeset
68 // Execute once
anatofuz
parents:
diff changeset
69 typedef pthread_once_t __libcpp_exec_once_flag;
anatofuz
parents:
diff changeset
70 #define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 // Thread id
anatofuz
parents:
diff changeset
73 typedef pthread_t __libcpp_thread_id;
anatofuz
parents:
diff changeset
74
anatofuz
parents:
diff changeset
75 // Thread
anatofuz
parents:
diff changeset
76 #define _LIBCPP_NULL_THREAD 0U
anatofuz
parents:
diff changeset
77
anatofuz
parents:
diff changeset
78 typedef pthread_t __libcpp_thread_t;
anatofuz
parents:
diff changeset
79
anatofuz
parents:
diff changeset
80 // Thread Local Storage
anatofuz
parents:
diff changeset
81 typedef pthread_key_t __libcpp_tls_key;
anatofuz
parents:
diff changeset
82
anatofuz
parents:
diff changeset
83 #define _LIBCPP_TLS_DESTRUCTOR_CC
anatofuz
parents:
diff changeset
84 #elif defined(_LIBCPP_HAS_THREAD_API_C11)
anatofuz
parents:
diff changeset
85 // Mutex
anatofuz
parents:
diff changeset
86 typedef mtx_t __libcpp_mutex_t;
anatofuz
parents:
diff changeset
87 // mtx_t is a struct so using {} for initialization is valid.
anatofuz
parents:
diff changeset
88 #define _LIBCPP_MUTEX_INITIALIZER {}
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 typedef mtx_t __libcpp_recursive_mutex_t;
anatofuz
parents:
diff changeset
91
anatofuz
parents:
diff changeset
92 // Condition Variable
anatofuz
parents:
diff changeset
93 typedef cnd_t __libcpp_condvar_t;
anatofuz
parents:
diff changeset
94 // cnd_t is a struct so using {} for initialization is valid.
anatofuz
parents:
diff changeset
95 #define _LIBCPP_CONDVAR_INITIALIZER {}
anatofuz
parents:
diff changeset
96
anatofuz
parents:
diff changeset
97 // Execute once
anatofuz
parents:
diff changeset
98 typedef once_flag __libcpp_exec_once_flag;
anatofuz
parents:
diff changeset
99 #define _LIBCPP_EXEC_ONCE_INITIALIZER ONCE_FLAG_INIT
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 // Thread id
anatofuz
parents:
diff changeset
102 typedef thrd_t __libcpp_thread_id;
anatofuz
parents:
diff changeset
103
anatofuz
parents:
diff changeset
104 // Thread
anatofuz
parents:
diff changeset
105 #define _LIBCPP_NULL_THREAD 0U
anatofuz
parents:
diff changeset
106
anatofuz
parents:
diff changeset
107 typedef thrd_t __libcpp_thread_t;
anatofuz
parents:
diff changeset
108
anatofuz
parents:
diff changeset
109 // Thread Local Storage
anatofuz
parents:
diff changeset
110 typedef tss_t __libcpp_tls_key;
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 #define _LIBCPP_TLS_DESTRUCTOR_CC
anatofuz
parents:
diff changeset
113 #elif !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
anatofuz
parents:
diff changeset
114 // Mutex
anatofuz
parents:
diff changeset
115 typedef void* __libcpp_mutex_t;
anatofuz
parents:
diff changeset
116 #define _LIBCPP_MUTEX_INITIALIZER 0
anatofuz
parents:
diff changeset
117
anatofuz
parents:
diff changeset
118 #if defined(_M_IX86) || defined(__i386__) || defined(_M_ARM) || defined(__arm__)
anatofuz
parents:
diff changeset
119 typedef void* __libcpp_recursive_mutex_t[6];
anatofuz
parents:
diff changeset
120 #elif defined(_M_AMD64) || defined(__x86_64__) || defined(_M_ARM64) || defined(__aarch64__)
anatofuz
parents:
diff changeset
121 typedef void* __libcpp_recursive_mutex_t[5];
anatofuz
parents:
diff changeset
122 #else
anatofuz
parents:
diff changeset
123 # error Unsupported architecture
anatofuz
parents:
diff changeset
124 #endif
anatofuz
parents:
diff changeset
125
anatofuz
parents:
diff changeset
126 // Condition Variable
anatofuz
parents:
diff changeset
127 typedef void* __libcpp_condvar_t;
anatofuz
parents:
diff changeset
128 #define _LIBCPP_CONDVAR_INITIALIZER 0
anatofuz
parents:
diff changeset
129
anatofuz
parents:
diff changeset
130 // Execute Once
anatofuz
parents:
diff changeset
131 typedef void* __libcpp_exec_once_flag;
anatofuz
parents:
diff changeset
132 #define _LIBCPP_EXEC_ONCE_INITIALIZER 0
anatofuz
parents:
diff changeset
133
anatofuz
parents:
diff changeset
134 // Thread ID
anatofuz
parents:
diff changeset
135 typedef long __libcpp_thread_id;
anatofuz
parents:
diff changeset
136
anatofuz
parents:
diff changeset
137 // Thread
anatofuz
parents:
diff changeset
138 #define _LIBCPP_NULL_THREAD 0U
anatofuz
parents:
diff changeset
139
anatofuz
parents:
diff changeset
140 typedef void* __libcpp_thread_t;
anatofuz
parents:
diff changeset
141
anatofuz
parents:
diff changeset
142 // Thread Local Storage
anatofuz
parents:
diff changeset
143 typedef long __libcpp_tls_key;
anatofuz
parents:
diff changeset
144
anatofuz
parents:
diff changeset
145 #define _LIBCPP_TLS_DESTRUCTOR_CC __stdcall
anatofuz
parents:
diff changeset
146 #endif // !defined(_LIBCPP_HAS_THREAD_API_PTHREAD) && !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
anatofuz
parents:
diff changeset
147
anatofuz
parents:
diff changeset
148 #if !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
anatofuz
parents:
diff changeset
149 // Mutex
anatofuz
parents:
diff changeset
150 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
151 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m);
anatofuz
parents:
diff changeset
152
anatofuz
parents:
diff changeset
153 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
anatofuz
parents:
diff changeset
154 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m);
anatofuz
parents:
diff changeset
155
anatofuz
parents:
diff changeset
156 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
anatofuz
parents:
diff changeset
157 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m);
anatofuz
parents:
diff changeset
158
anatofuz
parents:
diff changeset
159 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
anatofuz
parents:
diff changeset
160 int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m);
anatofuz
parents:
diff changeset
161
anatofuz
parents:
diff changeset
162 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
163 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m);
anatofuz
parents:
diff changeset
164
anatofuz
parents:
diff changeset
165 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
anatofuz
parents:
diff changeset
166 int __libcpp_mutex_lock(__libcpp_mutex_t *__m);
anatofuz
parents:
diff changeset
167
anatofuz
parents:
diff changeset
168 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
anatofuz
parents:
diff changeset
169 bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m);
anatofuz
parents:
diff changeset
170
anatofuz
parents:
diff changeset
171 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
anatofuz
parents:
diff changeset
172 int __libcpp_mutex_unlock(__libcpp_mutex_t *__m);
anatofuz
parents:
diff changeset
173
anatofuz
parents:
diff changeset
174 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
175 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m);
anatofuz
parents:
diff changeset
176
anatofuz
parents:
diff changeset
177 // Condition variable
anatofuz
parents:
diff changeset
178 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
179 int __libcpp_condvar_signal(__libcpp_condvar_t* __cv);
anatofuz
parents:
diff changeset
180
anatofuz
parents:
diff changeset
181 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
182 int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv);
anatofuz
parents:
diff changeset
183
anatofuz
parents:
diff changeset
184 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
anatofuz
parents:
diff changeset
185 int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m);
anatofuz
parents:
diff changeset
186
anatofuz
parents:
diff changeset
187 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
anatofuz
parents:
diff changeset
188 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
anatofuz
parents:
diff changeset
189 __libcpp_timespec_t *__ts);
anatofuz
parents:
diff changeset
190
anatofuz
parents:
diff changeset
191 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
192 int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv);
anatofuz
parents:
diff changeset
193
anatofuz
parents:
diff changeset
194 // Execute once
anatofuz
parents:
diff changeset
195 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
196 int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
anatofuz
parents:
diff changeset
197 void (*init_routine)());
anatofuz
parents:
diff changeset
198
anatofuz
parents:
diff changeset
199 // Thread id
anatofuz
parents:
diff changeset
200 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
201 bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2);
anatofuz
parents:
diff changeset
202
anatofuz
parents:
diff changeset
203 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
204 bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2);
anatofuz
parents:
diff changeset
205
anatofuz
parents:
diff changeset
206 // Thread
anatofuz
parents:
diff changeset
207 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
208 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t);
anatofuz
parents:
diff changeset
209
anatofuz
parents:
diff changeset
210 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
211 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
anatofuz
parents:
diff changeset
212 void *__arg);
anatofuz
parents:
diff changeset
213
anatofuz
parents:
diff changeset
214 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
215 __libcpp_thread_id __libcpp_thread_get_current_id();
anatofuz
parents:
diff changeset
216
anatofuz
parents:
diff changeset
217 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
218 __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t);
anatofuz
parents:
diff changeset
219
anatofuz
parents:
diff changeset
220 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
221 int __libcpp_thread_join(__libcpp_thread_t *__t);
anatofuz
parents:
diff changeset
222
anatofuz
parents:
diff changeset
223 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
224 int __libcpp_thread_detach(__libcpp_thread_t *__t);
anatofuz
parents:
diff changeset
225
anatofuz
parents:
diff changeset
226 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
227 void __libcpp_thread_yield();
anatofuz
parents:
diff changeset
228
anatofuz
parents:
diff changeset
229 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
230 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns);
anatofuz
parents:
diff changeset
231
anatofuz
parents:
diff changeset
232 // Thread local storage
anatofuz
parents:
diff changeset
233 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
234 int __libcpp_tls_create(__libcpp_tls_key* __key,
anatofuz
parents:
diff changeset
235 void(_LIBCPP_TLS_DESTRUCTOR_CC* __at_exit)(void*));
anatofuz
parents:
diff changeset
236
anatofuz
parents:
diff changeset
237 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
238 void *__libcpp_tls_get(__libcpp_tls_key __key);
anatofuz
parents:
diff changeset
239
anatofuz
parents:
diff changeset
240 _LIBCPP_THREAD_ABI_VISIBILITY
anatofuz
parents:
diff changeset
241 int __libcpp_tls_set(__libcpp_tls_key __key, void *__p);
anatofuz
parents:
diff changeset
242
anatofuz
parents:
diff changeset
243 #endif // !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
anatofuz
parents:
diff changeset
244
anatofuz
parents:
diff changeset
245 #if (!defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
anatofuz
parents:
diff changeset
246 defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL))
anatofuz
parents:
diff changeset
247
anatofuz
parents:
diff changeset
248 namespace __thread_detail {
anatofuz
parents:
diff changeset
249
anatofuz
parents:
diff changeset
250 inline __libcpp_timespec_t __convert_to_timespec(const chrono::nanoseconds& __ns)
anatofuz
parents:
diff changeset
251 {
anatofuz
parents:
diff changeset
252 using namespace chrono;
anatofuz
parents:
diff changeset
253 seconds __s = duration_cast<seconds>(__ns);
anatofuz
parents:
diff changeset
254 __libcpp_timespec_t __ts;
anatofuz
parents:
diff changeset
255 typedef decltype(__ts.tv_sec) __ts_sec;
anatofuz
parents:
diff changeset
256 const __ts_sec __ts_sec_max = numeric_limits<__ts_sec>::max();
anatofuz
parents:
diff changeset
257
anatofuz
parents:
diff changeset
258 if (__s.count() < __ts_sec_max)
anatofuz
parents:
diff changeset
259 {
anatofuz
parents:
diff changeset
260 __ts.tv_sec = static_cast<__ts_sec>(__s.count());
anatofuz
parents:
diff changeset
261 __ts.tv_nsec = static_cast<decltype(__ts.tv_nsec)>((__ns - __s).count());
anatofuz
parents:
diff changeset
262 }
anatofuz
parents:
diff changeset
263 else
anatofuz
parents:
diff changeset
264 {
anatofuz
parents:
diff changeset
265 __ts.tv_sec = __ts_sec_max;
anatofuz
parents:
diff changeset
266 __ts.tv_nsec = 999999999; // (10^9 - 1)
anatofuz
parents:
diff changeset
267 }
anatofuz
parents:
diff changeset
268
anatofuz
parents:
diff changeset
269 return __ts;
anatofuz
parents:
diff changeset
270 }
anatofuz
parents:
diff changeset
271
anatofuz
parents:
diff changeset
272 }
anatofuz
parents:
diff changeset
273
anatofuz
parents:
diff changeset
274 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
anatofuz
parents:
diff changeset
275
anatofuz
parents:
diff changeset
276 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
anatofuz
parents:
diff changeset
277 {
anatofuz
parents:
diff changeset
278 pthread_mutexattr_t attr;
anatofuz
parents:
diff changeset
279 int __ec = pthread_mutexattr_init(&attr);
anatofuz
parents:
diff changeset
280 if (__ec)
anatofuz
parents:
diff changeset
281 return __ec;
anatofuz
parents:
diff changeset
282 __ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
anatofuz
parents:
diff changeset
283 if (__ec) {
anatofuz
parents:
diff changeset
284 pthread_mutexattr_destroy(&attr);
anatofuz
parents:
diff changeset
285 return __ec;
anatofuz
parents:
diff changeset
286 }
anatofuz
parents:
diff changeset
287 __ec = pthread_mutex_init(__m, &attr);
anatofuz
parents:
diff changeset
288 if (__ec) {
anatofuz
parents:
diff changeset
289 pthread_mutexattr_destroy(&attr);
anatofuz
parents:
diff changeset
290 return __ec;
anatofuz
parents:
diff changeset
291 }
anatofuz
parents:
diff changeset
292 __ec = pthread_mutexattr_destroy(&attr);
anatofuz
parents:
diff changeset
293 if (__ec) {
anatofuz
parents:
diff changeset
294 pthread_mutex_destroy(__m);
anatofuz
parents:
diff changeset
295 return __ec;
anatofuz
parents:
diff changeset
296 }
anatofuz
parents:
diff changeset
297 return 0;
anatofuz
parents:
diff changeset
298 }
anatofuz
parents:
diff changeset
299
anatofuz
parents:
diff changeset
300 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
anatofuz
parents:
diff changeset
301 {
anatofuz
parents:
diff changeset
302 return pthread_mutex_lock(__m);
anatofuz
parents:
diff changeset
303 }
anatofuz
parents:
diff changeset
304
anatofuz
parents:
diff changeset
305 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
anatofuz
parents:
diff changeset
306 {
anatofuz
parents:
diff changeset
307 return pthread_mutex_trylock(__m) == 0;
anatofuz
parents:
diff changeset
308 }
anatofuz
parents:
diff changeset
309
anatofuz
parents:
diff changeset
310 int __libcpp_recursive_mutex_unlock(__libcpp_mutex_t *__m)
anatofuz
parents:
diff changeset
311 {
anatofuz
parents:
diff changeset
312 return pthread_mutex_unlock(__m);
anatofuz
parents:
diff changeset
313 }
anatofuz
parents:
diff changeset
314
anatofuz
parents:
diff changeset
315 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
anatofuz
parents:
diff changeset
316 {
anatofuz
parents:
diff changeset
317 return pthread_mutex_destroy(__m);
anatofuz
parents:
diff changeset
318 }
anatofuz
parents:
diff changeset
319
anatofuz
parents:
diff changeset
320 int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
anatofuz
parents:
diff changeset
321 {
anatofuz
parents:
diff changeset
322 return pthread_mutex_lock(__m);
anatofuz
parents:
diff changeset
323 }
anatofuz
parents:
diff changeset
324
anatofuz
parents:
diff changeset
325 bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
anatofuz
parents:
diff changeset
326 {
anatofuz
parents:
diff changeset
327 return pthread_mutex_trylock(__m) == 0;
anatofuz
parents:
diff changeset
328 }
anatofuz
parents:
diff changeset
329
anatofuz
parents:
diff changeset
330 int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
anatofuz
parents:
diff changeset
331 {
anatofuz
parents:
diff changeset
332 return pthread_mutex_unlock(__m);
anatofuz
parents:
diff changeset
333 }
anatofuz
parents:
diff changeset
334
anatofuz
parents:
diff changeset
335 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
anatofuz
parents:
diff changeset
336 {
anatofuz
parents:
diff changeset
337 return pthread_mutex_destroy(__m);
anatofuz
parents:
diff changeset
338 }
anatofuz
parents:
diff changeset
339
anatofuz
parents:
diff changeset
340 // Condition Variable
anatofuz
parents:
diff changeset
341 int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
anatofuz
parents:
diff changeset
342 {
anatofuz
parents:
diff changeset
343 return pthread_cond_signal(__cv);
anatofuz
parents:
diff changeset
344 }
anatofuz
parents:
diff changeset
345
anatofuz
parents:
diff changeset
346 int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
anatofuz
parents:
diff changeset
347 {
anatofuz
parents:
diff changeset
348 return pthread_cond_broadcast(__cv);
anatofuz
parents:
diff changeset
349 }
anatofuz
parents:
diff changeset
350
anatofuz
parents:
diff changeset
351 int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
anatofuz
parents:
diff changeset
352 {
anatofuz
parents:
diff changeset
353 return pthread_cond_wait(__cv, __m);
anatofuz
parents:
diff changeset
354 }
anatofuz
parents:
diff changeset
355
anatofuz
parents:
diff changeset
356 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
anatofuz
parents:
diff changeset
357 __libcpp_timespec_t *__ts)
anatofuz
parents:
diff changeset
358 {
anatofuz
parents:
diff changeset
359 return pthread_cond_timedwait(__cv, __m, __ts);
anatofuz
parents:
diff changeset
360 }
anatofuz
parents:
diff changeset
361
anatofuz
parents:
diff changeset
362 int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
anatofuz
parents:
diff changeset
363 {
anatofuz
parents:
diff changeset
364 return pthread_cond_destroy(__cv);
anatofuz
parents:
diff changeset
365 }
anatofuz
parents:
diff changeset
366
anatofuz
parents:
diff changeset
367 // Execute once
anatofuz
parents:
diff changeset
368 int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
anatofuz
parents:
diff changeset
369 void (*init_routine)()) {
anatofuz
parents:
diff changeset
370 return pthread_once(flag, init_routine);
anatofuz
parents:
diff changeset
371 }
anatofuz
parents:
diff changeset
372
anatofuz
parents:
diff changeset
373 // Thread id
anatofuz
parents:
diff changeset
374 // Returns non-zero if the thread ids are equal, otherwise 0
anatofuz
parents:
diff changeset
375 bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2)
anatofuz
parents:
diff changeset
376 {
anatofuz
parents:
diff changeset
377 return pthread_equal(t1, t2) != 0;
anatofuz
parents:
diff changeset
378 }
anatofuz
parents:
diff changeset
379
anatofuz
parents:
diff changeset
380 // Returns non-zero if t1 < t2, otherwise 0
anatofuz
parents:
diff changeset
381 bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
anatofuz
parents:
diff changeset
382 {
anatofuz
parents:
diff changeset
383 return t1 < t2;
anatofuz
parents:
diff changeset
384 }
anatofuz
parents:
diff changeset
385
anatofuz
parents:
diff changeset
386 // Thread
anatofuz
parents:
diff changeset
387 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
anatofuz
parents:
diff changeset
388 return *__t == 0;
anatofuz
parents:
diff changeset
389 }
anatofuz
parents:
diff changeset
390
anatofuz
parents:
diff changeset
391 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
anatofuz
parents:
diff changeset
392 void *__arg)
anatofuz
parents:
diff changeset
393 {
anatofuz
parents:
diff changeset
394 return pthread_create(__t, 0, __func, __arg);
anatofuz
parents:
diff changeset
395 }
anatofuz
parents:
diff changeset
396
anatofuz
parents:
diff changeset
397 __libcpp_thread_id __libcpp_thread_get_current_id()
anatofuz
parents:
diff changeset
398 {
anatofuz
parents:
diff changeset
399 return pthread_self();
anatofuz
parents:
diff changeset
400 }
anatofuz
parents:
diff changeset
401
anatofuz
parents:
diff changeset
402 __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
anatofuz
parents:
diff changeset
403 {
anatofuz
parents:
diff changeset
404 return *__t;
anatofuz
parents:
diff changeset
405 }
anatofuz
parents:
diff changeset
406
anatofuz
parents:
diff changeset
407 int __libcpp_thread_join(__libcpp_thread_t *__t)
anatofuz
parents:
diff changeset
408 {
anatofuz
parents:
diff changeset
409 return pthread_join(*__t, 0);
anatofuz
parents:
diff changeset
410 }
anatofuz
parents:
diff changeset
411
anatofuz
parents:
diff changeset
412 int __libcpp_thread_detach(__libcpp_thread_t *__t)
anatofuz
parents:
diff changeset
413 {
anatofuz
parents:
diff changeset
414 return pthread_detach(*__t);
anatofuz
parents:
diff changeset
415 }
anatofuz
parents:
diff changeset
416
anatofuz
parents:
diff changeset
417 void __libcpp_thread_yield()
anatofuz
parents:
diff changeset
418 {
anatofuz
parents:
diff changeset
419 sched_yield();
anatofuz
parents:
diff changeset
420 }
anatofuz
parents:
diff changeset
421
anatofuz
parents:
diff changeset
422 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
anatofuz
parents:
diff changeset
423 {
anatofuz
parents:
diff changeset
424 __libcpp_timespec_t __ts = __thread_detail::__convert_to_timespec(__ns);
anatofuz
parents:
diff changeset
425 while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR);
anatofuz
parents:
diff changeset
426 }
anatofuz
parents:
diff changeset
427
anatofuz
parents:
diff changeset
428 // Thread local storage
anatofuz
parents:
diff changeset
429 int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *))
anatofuz
parents:
diff changeset
430 {
anatofuz
parents:
diff changeset
431 return pthread_key_create(__key, __at_exit);
anatofuz
parents:
diff changeset
432 }
anatofuz
parents:
diff changeset
433
anatofuz
parents:
diff changeset
434 void *__libcpp_tls_get(__libcpp_tls_key __key)
anatofuz
parents:
diff changeset
435 {
anatofuz
parents:
diff changeset
436 return pthread_getspecific(__key);
anatofuz
parents:
diff changeset
437 }
anatofuz
parents:
diff changeset
438
anatofuz
parents:
diff changeset
439 int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
anatofuz
parents:
diff changeset
440 {
anatofuz
parents:
diff changeset
441 return pthread_setspecific(__key, __p);
anatofuz
parents:
diff changeset
442 }
anatofuz
parents:
diff changeset
443
anatofuz
parents:
diff changeset
444 #elif defined(_LIBCPP_HAS_THREAD_API_C11)
anatofuz
parents:
diff changeset
445
anatofuz
parents:
diff changeset
446 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
anatofuz
parents:
diff changeset
447 {
anatofuz
parents:
diff changeset
448 return mtx_init(__m, mtx_plain | mtx_recursive) == thrd_success ? 0 : EINVAL;
anatofuz
parents:
diff changeset
449 }
anatofuz
parents:
diff changeset
450
anatofuz
parents:
diff changeset
451 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
anatofuz
parents:
diff changeset
452 {
anatofuz
parents:
diff changeset
453 return mtx_lock(__m) == thrd_success ? 0 : EINVAL;
anatofuz
parents:
diff changeset
454 }
anatofuz
parents:
diff changeset
455
anatofuz
parents:
diff changeset
456 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
anatofuz
parents:
diff changeset
457 {
anatofuz
parents:
diff changeset
458 return mtx_trylock(__m) == thrd_success;
anatofuz
parents:
diff changeset
459 }
anatofuz
parents:
diff changeset
460
anatofuz
parents:
diff changeset
461 int __libcpp_recursive_mutex_unlock(__libcpp_mutex_t *__m)
anatofuz
parents:
diff changeset
462 {
anatofuz
parents:
diff changeset
463 return mtx_unlock(__m) == thrd_success ? 0 : EINVAL;
anatofuz
parents:
diff changeset
464 }
anatofuz
parents:
diff changeset
465
anatofuz
parents:
diff changeset
466 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
anatofuz
parents:
diff changeset
467 {
anatofuz
parents:
diff changeset
468 mtx_destroy(__m);
anatofuz
parents:
diff changeset
469 return 0;
anatofuz
parents:
diff changeset
470 }
anatofuz
parents:
diff changeset
471
anatofuz
parents:
diff changeset
472 int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
anatofuz
parents:
diff changeset
473 {
anatofuz
parents:
diff changeset
474 return mtx_lock(__m) == thrd_success ? 0 : EINVAL;
anatofuz
parents:
diff changeset
475 }
anatofuz
parents:
diff changeset
476
anatofuz
parents:
diff changeset
477 bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
anatofuz
parents:
diff changeset
478 {
anatofuz
parents:
diff changeset
479 return mtx_trylock(__m) == thrd_success;
anatofuz
parents:
diff changeset
480 }
anatofuz
parents:
diff changeset
481
anatofuz
parents:
diff changeset
482 int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
anatofuz
parents:
diff changeset
483 {
anatofuz
parents:
diff changeset
484 return mtx_unlock(__m) == thrd_success ? 0 : EINVAL;
anatofuz
parents:
diff changeset
485 }
anatofuz
parents:
diff changeset
486
anatofuz
parents:
diff changeset
487 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
anatofuz
parents:
diff changeset
488 {
anatofuz
parents:
diff changeset
489 mtx_destroy(__m);
anatofuz
parents:
diff changeset
490 return 0;
anatofuz
parents:
diff changeset
491 }
anatofuz
parents:
diff changeset
492
anatofuz
parents:
diff changeset
493 // Condition Variable
anatofuz
parents:
diff changeset
494 int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
anatofuz
parents:
diff changeset
495 {
anatofuz
parents:
diff changeset
496 return cnd_signal(__cv) == thrd_success ? 0 : EINVAL;
anatofuz
parents:
diff changeset
497 }
anatofuz
parents:
diff changeset
498
anatofuz
parents:
diff changeset
499 int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
anatofuz
parents:
diff changeset
500 {
anatofuz
parents:
diff changeset
501 return cnd_broadcast(__cv) == thrd_success ? 0 : EINVAL;
anatofuz
parents:
diff changeset
502 }
anatofuz
parents:
diff changeset
503
anatofuz
parents:
diff changeset
504 int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
anatofuz
parents:
diff changeset
505 {
anatofuz
parents:
diff changeset
506 return cnd_wait(__cv, __m) == thrd_success ? 0 : EINVAL;
anatofuz
parents:
diff changeset
507 }
anatofuz
parents:
diff changeset
508
anatofuz
parents:
diff changeset
509 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
anatofuz
parents:
diff changeset
510 timespec *__ts)
anatofuz
parents:
diff changeset
511 {
anatofuz
parents:
diff changeset
512 int __ec = cnd_timedwait(__cv, __m, __ts);
anatofuz
parents:
diff changeset
513 return __ec == thrd_timedout ? ETIMEDOUT : __ec;
anatofuz
parents:
diff changeset
514 }
anatofuz
parents:
diff changeset
515
anatofuz
parents:
diff changeset
516 int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
anatofuz
parents:
diff changeset
517 {
anatofuz
parents:
diff changeset
518 cnd_destroy(__cv);
anatofuz
parents:
diff changeset
519 return 0;
anatofuz
parents:
diff changeset
520 }
anatofuz
parents:
diff changeset
521
anatofuz
parents:
diff changeset
522 // Execute once
anatofuz
parents:
diff changeset
523 int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
anatofuz
parents:
diff changeset
524 void (*init_routine)(void)) {
anatofuz
parents:
diff changeset
525 ::call_once(flag, init_routine);
anatofuz
parents:
diff changeset
526 return 0;
anatofuz
parents:
diff changeset
527 }
anatofuz
parents:
diff changeset
528
anatofuz
parents:
diff changeset
529 // Thread id
anatofuz
parents:
diff changeset
530 // Returns non-zero if the thread ids are equal, otherwise 0
anatofuz
parents:
diff changeset
531 bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2)
anatofuz
parents:
diff changeset
532 {
anatofuz
parents:
diff changeset
533 return thrd_equal(t1, t2) != 0;
anatofuz
parents:
diff changeset
534 }
anatofuz
parents:
diff changeset
535
anatofuz
parents:
diff changeset
536 // Returns non-zero if t1 < t2, otherwise 0
anatofuz
parents:
diff changeset
537 bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
anatofuz
parents:
diff changeset
538 {
anatofuz
parents:
diff changeset
539 return t1 < t2;
anatofuz
parents:
diff changeset
540 }
anatofuz
parents:
diff changeset
541
anatofuz
parents:
diff changeset
542 // Thread
anatofuz
parents:
diff changeset
543 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
anatofuz
parents:
diff changeset
544 return *__t == 0;
anatofuz
parents:
diff changeset
545 }
anatofuz
parents:
diff changeset
546
anatofuz
parents:
diff changeset
547 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
anatofuz
parents:
diff changeset
548 void *__arg)
anatofuz
parents:
diff changeset
549 {
anatofuz
parents:
diff changeset
550 int __ec = thrd_create(__t, reinterpret_cast<thrd_start_t>(__func), __arg);
anatofuz
parents:
diff changeset
551 return __ec == thrd_nomem ? ENOMEM : __ec;
anatofuz
parents:
diff changeset
552 }
anatofuz
parents:
diff changeset
553
anatofuz
parents:
diff changeset
554 __libcpp_thread_id __libcpp_thread_get_current_id()
anatofuz
parents:
diff changeset
555 {
anatofuz
parents:
diff changeset
556 return thrd_current();
anatofuz
parents:
diff changeset
557 }
anatofuz
parents:
diff changeset
558
anatofuz
parents:
diff changeset
559 __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
anatofuz
parents:
diff changeset
560 {
anatofuz
parents:
diff changeset
561 return *__t;
anatofuz
parents:
diff changeset
562 }
anatofuz
parents:
diff changeset
563
anatofuz
parents:
diff changeset
564 int __libcpp_thread_join(__libcpp_thread_t *__t)
anatofuz
parents:
diff changeset
565 {
anatofuz
parents:
diff changeset
566 return thrd_join(*__t, nullptr) == thrd_success ? 0 : EINVAL;
anatofuz
parents:
diff changeset
567 }
anatofuz
parents:
diff changeset
568
anatofuz
parents:
diff changeset
569 int __libcpp_thread_detach(__libcpp_thread_t *__t)
anatofuz
parents:
diff changeset
570 {
anatofuz
parents:
diff changeset
571 return thrd_detach(*__t) == thrd_success ? 0 : EINVAL;
anatofuz
parents:
diff changeset
572 }
anatofuz
parents:
diff changeset
573
anatofuz
parents:
diff changeset
574 void __libcpp_thread_yield()
anatofuz
parents:
diff changeset
575 {
anatofuz
parents:
diff changeset
576 thrd_yield();
anatofuz
parents:
diff changeset
577 }
anatofuz
parents:
diff changeset
578
anatofuz
parents:
diff changeset
579 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
anatofuz
parents:
diff changeset
580 {
anatofuz
parents:
diff changeset
581 __libcpp_timespec_t __ts = __thread_detail::__convert_to_timespec(__ns);
anatofuz
parents:
diff changeset
582 thrd_sleep(&__ts, nullptr);
anatofuz
parents:
diff changeset
583 }
anatofuz
parents:
diff changeset
584
anatofuz
parents:
diff changeset
585 // Thread local storage
anatofuz
parents:
diff changeset
586 int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *))
anatofuz
parents:
diff changeset
587 {
anatofuz
parents:
diff changeset
588 return tss_create(__key, __at_exit) == thrd_success ? 0 : EINVAL;
anatofuz
parents:
diff changeset
589 }
anatofuz
parents:
diff changeset
590
anatofuz
parents:
diff changeset
591 void *__libcpp_tls_get(__libcpp_tls_key __key)
anatofuz
parents:
diff changeset
592 {
anatofuz
parents:
diff changeset
593 return tss_get(__key);
anatofuz
parents:
diff changeset
594 }
anatofuz
parents:
diff changeset
595
anatofuz
parents:
diff changeset
596 int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
anatofuz
parents:
diff changeset
597 {
anatofuz
parents:
diff changeset
598 return tss_set(__key, __p) == thrd_success ? 0 : EINVAL;
anatofuz
parents:
diff changeset
599 }
anatofuz
parents:
diff changeset
600
anatofuz
parents:
diff changeset
601 #endif
anatofuz
parents:
diff changeset
602
anatofuz
parents:
diff changeset
603 #endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL
anatofuz
parents:
diff changeset
604
anatofuz
parents:
diff changeset
605 class _LIBCPP_TYPE_VIS thread;
anatofuz
parents:
diff changeset
606 class _LIBCPP_TYPE_VIS __thread_id;
anatofuz
parents:
diff changeset
607
anatofuz
parents:
diff changeset
608 namespace this_thread
anatofuz
parents:
diff changeset
609 {
anatofuz
parents:
diff changeset
610
anatofuz
parents:
diff changeset
611 _LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
anatofuz
parents:
diff changeset
612
anatofuz
parents:
diff changeset
613 } // this_thread
anatofuz
parents:
diff changeset
614
anatofuz
parents:
diff changeset
615 template<> struct hash<__thread_id>;
anatofuz
parents:
diff changeset
616
anatofuz
parents:
diff changeset
617 class _LIBCPP_TEMPLATE_VIS __thread_id
anatofuz
parents:
diff changeset
618 {
anatofuz
parents:
diff changeset
619 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
anatofuz
parents:
diff changeset
620 // NULL is the no-thread value on Darwin. Someone needs to check
anatofuz
parents:
diff changeset
621 // on other platforms. We assume 0 works everywhere for now.
anatofuz
parents:
diff changeset
622 __libcpp_thread_id __id_;
anatofuz
parents:
diff changeset
623
anatofuz
parents:
diff changeset
624 public:
anatofuz
parents:
diff changeset
625 _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
626 __thread_id() _NOEXCEPT : __id_(0) {}
anatofuz
parents:
diff changeset
627
anatofuz
parents:
diff changeset
628 friend _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
629 bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
anatofuz
parents:
diff changeset
630 { // don't pass id==0 to underlying routines
anatofuz
parents:
diff changeset
631 if (__x.__id_ == 0) return __y.__id_ == 0;
anatofuz
parents:
diff changeset
632 if (__y.__id_ == 0) return false;
anatofuz
parents:
diff changeset
633 return __libcpp_thread_id_equal(__x.__id_, __y.__id_);
anatofuz
parents:
diff changeset
634 }
anatofuz
parents:
diff changeset
635 friend _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
636 bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
anatofuz
parents:
diff changeset
637 {return !(__x == __y);}
anatofuz
parents:
diff changeset
638 friend _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
639 bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
anatofuz
parents:
diff changeset
640 { // id==0 is always less than any other thread_id
anatofuz
parents:
diff changeset
641 if (__x.__id_ == 0) return __y.__id_ != 0;
anatofuz
parents:
diff changeset
642 if (__y.__id_ == 0) return false;
anatofuz
parents:
diff changeset
643 return __libcpp_thread_id_less(__x.__id_, __y.__id_);
anatofuz
parents:
diff changeset
644 }
anatofuz
parents:
diff changeset
645 friend _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
646 bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
anatofuz
parents:
diff changeset
647 {return !(__y < __x);}
anatofuz
parents:
diff changeset
648 friend _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
649 bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
anatofuz
parents:
diff changeset
650 {return __y < __x ;}
anatofuz
parents:
diff changeset
651 friend _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
652 bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
anatofuz
parents:
diff changeset
653 {return !(__x < __y);}
anatofuz
parents:
diff changeset
654
anatofuz
parents:
diff changeset
655 _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
656 void __reset() { __id_ = 0; }
anatofuz
parents:
diff changeset
657
anatofuz
parents:
diff changeset
658 template<class _CharT, class _Traits>
anatofuz
parents:
diff changeset
659 friend
anatofuz
parents:
diff changeset
660 _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
661 basic_ostream<_CharT, _Traits>&
anatofuz
parents:
diff changeset
662 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id);
anatofuz
parents:
diff changeset
663
anatofuz
parents:
diff changeset
664 private:
anatofuz
parents:
diff changeset
665 _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
666 __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
anatofuz
parents:
diff changeset
667
anatofuz
parents:
diff changeset
668 friend __thread_id this_thread::get_id() _NOEXCEPT;
anatofuz
parents:
diff changeset
669 friend class _LIBCPP_TYPE_VIS thread;
anatofuz
parents:
diff changeset
670 friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
anatofuz
parents:
diff changeset
671 };
anatofuz
parents:
diff changeset
672
anatofuz
parents:
diff changeset
673 namespace this_thread
anatofuz
parents:
diff changeset
674 {
anatofuz
parents:
diff changeset
675
anatofuz
parents:
diff changeset
676 inline _LIBCPP_INLINE_VISIBILITY
anatofuz
parents:
diff changeset
677 __thread_id
anatofuz
parents:
diff changeset
678 get_id() _NOEXCEPT
anatofuz
parents:
diff changeset
679 {
anatofuz
parents:
diff changeset
680 return __libcpp_thread_get_current_id();
anatofuz
parents:
diff changeset
681 }
anatofuz
parents:
diff changeset
682
anatofuz
parents:
diff changeset
683 } // this_thread
anatofuz
parents:
diff changeset
684
anatofuz
parents:
diff changeset
685 #endif // !_LIBCPP_HAS_NO_THREADS
anatofuz
parents:
diff changeset
686
anatofuz
parents:
diff changeset
687 _LIBCPP_END_NAMESPACE_STD
anatofuz
parents:
diff changeset
688
anatofuz
parents:
diff changeset
689 _LIBCPP_POP_MACROS
anatofuz
parents:
diff changeset
690
anatofuz
parents:
diff changeset
691 #endif // _LIBCPP_THREADING_SUPPORT