| 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___THREAD_SUPPORT_PTHREAD_H |
| 11 | #define _LIBCPP___THREAD_SUPPORT_PTHREAD_H |
| 12 | |
| 13 | #include <__chrono/convert_to_timespec.h> |
| 14 | #include <__chrono/duration.h> |
| 15 | #include <__config> |
| 16 | #include <ctime> |
| 17 | #include <errno.h> |
| 18 | #include <pthread.h> |
| 19 | #include <sched.h> |
| 20 | |
| 21 | #ifdef __MVS__ |
| 22 | # include <__support/ibm/nanosleep.h> |
| 23 | #endif |
| 24 | |
| 25 | // Some platforms require <bits/atomic_wide_counter.h> in order for |
| 26 | // PTHREAD_COND_INITIALIZER to be expanded. Normally that would come |
| 27 | // in via <pthread.h>, but it's a non-modular header on those platforms, |
| 28 | // so libc++'s <math.h> usually absorbs atomic_wide_counter.h into the |
| 29 | // module with <math.h> and makes atomic_wide_counter.h invisible. |
| 30 | // Include <math.h> here to work around that. |
| 31 | // This checks wheter a Clang module is built |
| 32 | #if __building_module(std) |
| 33 | # include <math.h> |
| 34 | #endif |
| 35 | |
| 36 | #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER |
| 37 | # pragma GCC system_header |
| 38 | #endif |
| 39 | |
| 40 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 41 | |
| 42 | using __libcpp_timespec_t _LIBCPP_NODEBUG = ::timespec; |
| 43 | |
| 44 | // |
| 45 | // Mutex |
| 46 | // |
| 47 | typedef pthread_mutex_t __libcpp_mutex_t; |
| 48 | #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER |
| 49 | |
| 50 | typedef pthread_mutex_t __libcpp_recursive_mutex_t; |
| 51 | |
| 52 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t* __m) { |
| 53 | pthread_mutexattr_t __attr; |
| 54 | int __ec = pthread_mutexattr_init(attr: &__attr); |
| 55 | if (__ec) |
| 56 | return __ec; |
| 57 | __ec = pthread_mutexattr_settype(attr: &__attr, kind: PTHREAD_MUTEX_RECURSIVE); |
| 58 | if (__ec) { |
| 59 | pthread_mutexattr_destroy(attr: &__attr); |
| 60 | return __ec; |
| 61 | } |
| 62 | __ec = pthread_mutex_init(mutex: __m, mutexattr: &__attr); |
| 63 | if (__ec) { |
| 64 | pthread_mutexattr_destroy(attr: &__attr); |
| 65 | return __ec; |
| 66 | } |
| 67 | __ec = pthread_mutexattr_destroy(attr: &__attr); |
| 68 | if (__ec) { |
| 69 | pthread_mutex_destroy(mutex: __m); |
| 70 | return __ec; |
| 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int |
| 76 | __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t* __m) { |
| 77 | return pthread_mutex_lock(mutex: __m); |
| 78 | } |
| 79 | |
| 80 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool |
| 81 | __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t* __m) { |
| 82 | return pthread_mutex_trylock(mutex: __m) == 0; |
| 83 | } |
| 84 | |
| 85 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int |
| 86 | __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t* __m) { |
| 87 | return pthread_mutex_unlock(mutex: __m); |
| 88 | } |
| 89 | |
| 90 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t* __m) { |
| 91 | return pthread_mutex_destroy(mutex: __m); |
| 92 | } |
| 93 | |
| 94 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_lock(__libcpp_mutex_t* __m) { |
| 95 | return pthread_mutex_lock(mutex: __m); |
| 96 | } |
| 97 | |
| 98 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS bool __libcpp_mutex_trylock(__libcpp_mutex_t* __m) { |
| 99 | return pthread_mutex_trylock(mutex: __m) == 0; |
| 100 | } |
| 101 | |
| 102 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int __libcpp_mutex_unlock(__libcpp_mutex_t* __m) { |
| 103 | return pthread_mutex_unlock(mutex: __m); |
| 104 | } |
| 105 | |
| 106 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_mutex_destroy(__libcpp_mutex_t* __m) { return pthread_mutex_destroy(mutex: __m); } |
| 107 | |
| 108 | // |
| 109 | // Condition Variable |
| 110 | // |
| 111 | typedef pthread_cond_t __libcpp_condvar_t; |
| 112 | #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER |
| 113 | |
| 114 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_signal(__libcpp_condvar_t* __cv) { return pthread_cond_signal(cond: __cv); } |
| 115 | |
| 116 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv) { |
| 117 | return pthread_cond_broadcast(cond: __cv); |
| 118 | } |
| 119 | |
| 120 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int |
| 121 | __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m) { |
| 122 | return pthread_cond_wait(cond: __cv, mutex: __m); |
| 123 | } |
| 124 | |
| 125 | inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_THREAD_SAFETY_ANALYSIS int |
| 126 | __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, __libcpp_timespec_t* __ts) { |
| 127 | return pthread_cond_timedwait(cond: __cv, mutex: __m, abstime: __ts); |
| 128 | } |
| 129 | |
| 130 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv) { |
| 131 | return pthread_cond_destroy(cond: __cv); |
| 132 | } |
| 133 | |
| 134 | // |
| 135 | // Execute once |
| 136 | // |
| 137 | typedef pthread_once_t __libcpp_exec_once_flag; |
| 138 | #define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT |
| 139 | |
| 140 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_execute_once(__libcpp_exec_once_flag* __flag, void (*__init_routine)()) { |
| 141 | return pthread_once(once_control: __flag, __init_routine); |
| 142 | } |
| 143 | |
| 144 | // |
| 145 | // Thread id |
| 146 | // |
| 147 | #if defined(__MVS__) |
| 148 | typedef unsigned long long __libcpp_thread_id; |
| 149 | #else |
| 150 | typedef pthread_t __libcpp_thread_id; |
| 151 | #endif |
| 152 | |
| 153 | // Returns non-zero if the thread ids are equal, otherwise 0 |
| 154 | inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_id_equal(__libcpp_thread_id __t1, __libcpp_thread_id __t2) { |
| 155 | return __t1 == __t2; |
| 156 | } |
| 157 | |
| 158 | // Returns non-zero if t1 < t2, otherwise 0 |
| 159 | inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_id_less(__libcpp_thread_id __t1, __libcpp_thread_id __t2) { |
| 160 | return __t1 < __t2; |
| 161 | } |
| 162 | |
| 163 | // |
| 164 | // Thread |
| 165 | // |
| 166 | #define _LIBCPP_NULL_THREAD ((__libcpp_thread_t())) |
| 167 | typedef pthread_t __libcpp_thread_t; |
| 168 | |
| 169 | inline _LIBCPP_HIDE_FROM_ABI __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t) { |
| 170 | #if defined(__MVS__) |
| 171 | return __t->__; |
| 172 | #else |
| 173 | return *__t; |
| 174 | #endif |
| 175 | } |
| 176 | |
| 177 | inline _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_isnull(const __libcpp_thread_t* __t) { |
| 178 | return __libcpp_thread_get_id(__t) == 0; |
| 179 | } |
| 180 | |
| 181 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg) { |
| 182 | return pthread_create(newthread: __t, attr: nullptr, start_routine: __func, __arg); |
| 183 | } |
| 184 | |
| 185 | inline _LIBCPP_HIDE_FROM_ABI __libcpp_thread_id __libcpp_thread_get_current_id() { |
| 186 | const __libcpp_thread_t __current_thread = pthread_self(); |
| 187 | return __libcpp_thread_get_id(t: &__current_thread); |
| 188 | } |
| 189 | |
| 190 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_join(__libcpp_thread_t* __t) { return pthread_join(th: *__t, thread_return: nullptr); } |
| 191 | |
| 192 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_thread_detach(__libcpp_thread_t* __t) { return pthread_detach(th: *__t); } |
| 193 | |
| 194 | inline _LIBCPP_HIDE_FROM_ABI void __libcpp_thread_yield() { sched_yield(); } |
| 195 | |
| 196 | inline _LIBCPP_HIDE_FROM_ABI void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns) { |
| 197 | __libcpp_timespec_t __ts = std::__convert_to_timespec<__libcpp_timespec_t>(__ns); |
| 198 | while (nanosleep(requested_time: &__ts, remaining: &__ts) == -1 && errno == EINTR) |
| 199 | ; |
| 200 | } |
| 201 | |
| 202 | // |
| 203 | // Thread local storage |
| 204 | // |
| 205 | #define _LIBCPP_TLS_DESTRUCTOR_CC /* nothing */ |
| 206 | |
| 207 | typedef pthread_key_t __libcpp_tls_key; |
| 208 | |
| 209 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)) { |
| 210 | return pthread_key_create(__key, destr_function: __at_exit); |
| 211 | } |
| 212 | |
| 213 | inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_tls_get(__libcpp_tls_key __key) { return pthread_getspecific(__key); } |
| 214 | |
| 215 | inline _LIBCPP_HIDE_FROM_ABI int __libcpp_tls_set(__libcpp_tls_key __key, void* __p) { |
| 216 | return pthread_setspecific(__key, pointer: __p); |
| 217 | } |
| 218 | |
| 219 | _LIBCPP_END_NAMESPACE_STD |
| 220 | |
| 221 | #endif // _LIBCPP___THREAD_SUPPORT_PTHREAD_H |
| 222 | |