2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_THREADING_SUPPORT
12 #define _LIBCPP_THREADING_SUPPORT
18 #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
19 #pragma GCC system_header
22 #if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
23 # include <__external_threading>
24 #elif !defined(_LIBCPP_HAS_NO_THREADS)
26 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
32 #include <__undef_macros>
34 #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
39 #define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
42 #if defined(__FreeBSD__) && defined(__clang__) && __has_attribute(no_thread_safety_analysis)
43 #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
45 #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
48 _LIBCPP_BEGIN_NAMESPACE_STD
50 #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
52 typedef pthread_mutex_t __libcpp_mutex_t;
53 #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
55 typedef pthread_mutex_t __libcpp_recursive_mutex_t;
58 typedef pthread_cond_t __libcpp_condvar_t;
59 #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
62 typedef pthread_once_t __libcpp_exec_once_flag;
63 #define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT
66 typedef pthread_t __libcpp_thread_id;
69 #define _LIBCPP_NULL_THREAD 0U
71 typedef pthread_t __libcpp_thread_t;
73 // Thrad Local Storage
74 typedef pthread_key_t __libcpp_tls_key;
76 #define _LIBCPP_TLS_DESTRUCTOR_CC
79 typedef void* __libcpp_mutex_t;
80 #define _LIBCPP_MUTEX_INITIALIZER 0
82 #if defined(_M_IX86) || defined(__i386__) || defined(_M_ARM) || defined(__arm__)
83 typedef void* __libcpp_recursive_mutex_t[6];
84 #elif defined(_M_AMD64) || defined(__x86_64__) || defined(_M_ARM64) || defined(__aarch64__)
85 typedef void* __libcpp_recursive_mutex_t[5];
87 # error Unsupported architecture
91 typedef void* __libcpp_condvar_t;
92 #define _LIBCPP_CONDVAR_INITIALIZER 0
95 typedef void* __libcpp_exec_once_flag;
96 #define _LIBCPP_EXEC_ONCE_INITIALIZER 0
99 typedef long __libcpp_thread_id;
102 #define _LIBCPP_NULL_THREAD 0U
104 typedef void* __libcpp_thread_t;
106 // Thread Local Storage
107 typedef long __libcpp_tls_key;
109 #define _LIBCPP_TLS_DESTRUCTOR_CC __stdcall
113 _LIBCPP_THREAD_ABI_VISIBILITY
114 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m);
116 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
117 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m);
119 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
120 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m);
122 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
123 int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m);
125 _LIBCPP_THREAD_ABI_VISIBILITY
126 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m);
128 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
129 int __libcpp_mutex_lock(__libcpp_mutex_t *__m);
131 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
132 bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m);
134 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
135 int __libcpp_mutex_unlock(__libcpp_mutex_t *__m);
137 _LIBCPP_THREAD_ABI_VISIBILITY
138 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m);
140 // Condition variable
141 _LIBCPP_THREAD_ABI_VISIBILITY
142 int __libcpp_condvar_signal(__libcpp_condvar_t* __cv);
144 _LIBCPP_THREAD_ABI_VISIBILITY
145 int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv);
147 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
148 int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m);
150 _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
151 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
154 _LIBCPP_THREAD_ABI_VISIBILITY
155 int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv);
158 _LIBCPP_THREAD_ABI_VISIBILITY
159 int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
160 void (*init_routine)(void));
163 _LIBCPP_THREAD_ABI_VISIBILITY
164 bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2);
166 _LIBCPP_THREAD_ABI_VISIBILITY
167 bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2);
170 _LIBCPP_THREAD_ABI_VISIBILITY
171 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t);
173 _LIBCPP_THREAD_ABI_VISIBILITY
174 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
177 _LIBCPP_THREAD_ABI_VISIBILITY
178 __libcpp_thread_id __libcpp_thread_get_current_id();
180 _LIBCPP_THREAD_ABI_VISIBILITY
181 __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t);
183 _LIBCPP_THREAD_ABI_VISIBILITY
184 int __libcpp_thread_join(__libcpp_thread_t *__t);
186 _LIBCPP_THREAD_ABI_VISIBILITY
187 int __libcpp_thread_detach(__libcpp_thread_t *__t);
189 _LIBCPP_THREAD_ABI_VISIBILITY
190 void __libcpp_thread_yield();
192 _LIBCPP_THREAD_ABI_VISIBILITY
193 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns);
195 // Thread local storage
196 _LIBCPP_THREAD_ABI_VISIBILITY
197 int __libcpp_tls_create(__libcpp_tls_key* __key,
198 void(_LIBCPP_TLS_DESTRUCTOR_CC* __at_exit)(void*));
200 _LIBCPP_THREAD_ABI_VISIBILITY
201 void *__libcpp_tls_get(__libcpp_tls_key __key);
203 _LIBCPP_THREAD_ABI_VISIBILITY
204 int __libcpp_tls_set(__libcpp_tls_key __key, void *__p);
206 #if (!defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
207 defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL)) && \
208 defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
210 int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
212 pthread_mutexattr_t attr;
213 int __ec = pthread_mutexattr_init(&attr);
216 __ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
218 pthread_mutexattr_destroy(&attr);
221 __ec = pthread_mutex_init(__m, &attr);
223 pthread_mutexattr_destroy(&attr);
226 __ec = pthread_mutexattr_destroy(&attr);
228 pthread_mutex_destroy(__m);
234 int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
236 return pthread_mutex_lock(__m);
239 bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
241 return pthread_mutex_trylock(__m) == 0;
244 int __libcpp_recursive_mutex_unlock(__libcpp_mutex_t *__m)
246 return pthread_mutex_unlock(__m);
249 int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
251 return pthread_mutex_destroy(__m);
254 int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
256 return pthread_mutex_lock(__m);
259 bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
261 return pthread_mutex_trylock(__m) == 0;
264 int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
266 return pthread_mutex_unlock(__m);
269 int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
271 return pthread_mutex_destroy(__m);
274 // Condition Variable
275 int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
277 return pthread_cond_signal(__cv);
280 int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
282 return pthread_cond_broadcast(__cv);
285 int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
287 return pthread_cond_wait(__cv, __m);
290 int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
293 return pthread_cond_timedwait(__cv, __m, __ts);
296 int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
298 return pthread_cond_destroy(__cv);
302 int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
303 void (*init_routine)(void)) {
304 return pthread_once(flag, init_routine);
308 // Returns non-zero if the thread ids are equal, otherwise 0
309 bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2)
311 return pthread_equal(t1, t2) != 0;
314 // Returns non-zero if t1 < t2, otherwise 0
315 bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
321 bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
325 int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
328 return pthread_create(__t, 0, __func, __arg);
331 __libcpp_thread_id __libcpp_thread_get_current_id()
333 return pthread_self();
336 __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
341 int __libcpp_thread_join(__libcpp_thread_t *__t)
343 return pthread_join(*__t, 0);
346 int __libcpp_thread_detach(__libcpp_thread_t *__t)
348 return pthread_detach(*__t);
351 void __libcpp_thread_yield()
356 void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
358 using namespace chrono;
359 seconds __s = duration_cast<seconds>(__ns);
361 typedef decltype(__ts.tv_sec) ts_sec;
362 _LIBCPP_CONSTEXPR ts_sec __ts_sec_max = numeric_limits<ts_sec>::max();
364 if (__s.count() < __ts_sec_max)
366 __ts.tv_sec = static_cast<ts_sec>(__s.count());
367 __ts.tv_nsec = static_cast<decltype(__ts.tv_nsec)>((__ns - __s).count());
371 __ts.tv_sec = __ts_sec_max;
372 __ts.tv_nsec = 999999999; // (10^9 - 1)
375 while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR);
378 // Thread local storage
379 int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *))
381 return pthread_key_create(__key, __at_exit);
384 void *__libcpp_tls_get(__libcpp_tls_key __key)
386 return pthread_getspecific(__key);
389 int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
391 return pthread_setspecific(__key, __p);
394 #endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL
396 _LIBCPP_END_NAMESPACE_STD
400 #endif // !_LIBCPP_HAS_NO_THREADS
402 #endif // _LIBCPP_THREADING_SUPPORT