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