Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 1 | // -*- 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> |
| 15 | |
| 16 | #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER |
| 17 | #pragma GCC system_header |
| 18 | #endif |
| 19 | |
| 20 | #ifndef _LIBCPP_HAS_NO_THREADS |
| 21 | |
Asiri Rathnayake | f6600b7 | 2016-09-13 09:32:32 +0000 | [diff] [blame] | 22 | // These checks are carefully arranged so as not to trigger a gcc pre-processor |
| 23 | // defect which causes it to fail to parse the __has_include check below, the |
| 24 | // redundancy is intentional. |
| 25 | #if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) |
| 26 | #if !defined(__clang__) && (_GNUC_VER < 500) |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 27 | #include <__external_threading> |
Asiri Rathnayake | f6600b7 | 2016-09-13 09:32:32 +0000 | [diff] [blame] | 28 | #define _LIBCPP_EXTERNAL_THREADING |
| 29 | #elif !defined(__has_include) || __has_include(<__external_threading>) |
| 30 | #include <__external_threading> |
| 31 | #define _LIBCPP_EXTERNAL_THREADING |
| 32 | #endif |
| 33 | #endif |
| 34 | |
| 35 | #if !defined(_LIBCPP_EXTERNAL_THREADING) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 36 | #include <pthread.h> |
| 37 | #include <sched.h> |
| 38 | #endif |
| 39 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 40 | #if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) |
| 41 | #define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_FUNC_VIS |
| 42 | #else |
| 43 | #define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY |
| 44 | #endif |
| 45 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 46 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 47 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 48 | // Mutex |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 49 | typedef pthread_mutex_t __libcpp_mutex_t; |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 50 | #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 51 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 52 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 53 | int __libcpp_recursive_mutex_init(__libcpp_mutex_t* __m); |
| 54 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 55 | int __libcpp_mutex_lock(__libcpp_mutex_t* __m); |
| 56 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 57 | int __libcpp_mutex_trylock(__libcpp_mutex_t* __m); |
| 58 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 59 | int __libcpp_mutex_unlock(__libcpp_mutex_t* __m); |
| 60 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 61 | int __libcpp_mutex_destroy(__libcpp_mutex_t* __m); |
| 62 | |
| 63 | // Condition variable |
| 64 | typedef pthread_cond_t __libcpp_condvar_t; |
| 65 | #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER |
| 66 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 67 | int __libcpp_condvar_signal(__libcpp_condvar_t* __cv); |
| 68 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 69 | int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv); |
| 70 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 71 | int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m); |
| 72 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 73 | int __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, timespec* __ts); |
| 74 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 75 | int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv); |
| 76 | |
| 77 | // Thread id |
| 78 | typedef pthread_t __libcpp_thread_id; |
| 79 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 80 | bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2); |
| 81 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 82 | bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2); |
| 83 | |
| 84 | // Thread |
| 85 | typedef pthread_t __libcpp_thread_t; |
| 86 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 87 | int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg); |
| 88 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 89 | __libcpp_thread_id __libcpp_thread_get_current_id(); |
| 90 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 91 | __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t); |
| 92 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 93 | int __libcpp_thread_join(__libcpp_thread_t* __t); |
| 94 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 95 | int __libcpp_thread_detach(__libcpp_thread_t* __t); |
| 96 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 97 | void __libcpp_thread_yield(); |
| 98 | |
| 99 | // Thread local storage |
| 100 | typedef pthread_key_t __libcpp_tls_key; |
| 101 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 102 | int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)); |
| 103 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 104 | void* __libcpp_tls_get(__libcpp_tls_key __key); |
| 105 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 106 | void __libcpp_tls_set(__libcpp_tls_key __key, void* __p); |
| 107 | |
| 108 | #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) || defined(_LIBCPP_BUILDING_EXTERNAL_THREADS) |
| 109 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 110 | int __libcpp_recursive_mutex_init(__libcpp_mutex_t* __m) |
| 111 | { |
| 112 | pthread_mutexattr_t attr; |
| 113 | int __ec = pthread_mutexattr_init(&attr); |
| 114 | if (__ec) return __ec; |
| 115 | __ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 116 | if (__ec) |
| 117 | { |
| 118 | pthread_mutexattr_destroy(&attr); |
| 119 | return __ec; |
| 120 | } |
| 121 | __ec = pthread_mutex_init(__m, &attr); |
| 122 | if (__ec) |
| 123 | { |
| 124 | pthread_mutexattr_destroy(&attr); |
| 125 | return __ec; |
| 126 | } |
| 127 | __ec = pthread_mutexattr_destroy(&attr); |
| 128 | if (__ec) |
| 129 | { |
| 130 | pthread_mutex_destroy(__m); |
| 131 | return __ec; |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 136 | int __libcpp_mutex_lock(__libcpp_mutex_t* __m) |
| 137 | { |
| 138 | return pthread_mutex_lock(__m); |
| 139 | } |
| 140 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 141 | int __libcpp_mutex_trylock(__libcpp_mutex_t* __m) |
| 142 | { |
| 143 | return pthread_mutex_trylock(__m); |
| 144 | } |
| 145 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 146 | int __libcpp_mutex_unlock(__libcpp_mutex_t* __m) |
| 147 | { |
| 148 | return pthread_mutex_unlock(__m); |
| 149 | } |
| 150 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 151 | int __libcpp_mutex_destroy(__libcpp_mutex_t* __m) |
| 152 | { |
| 153 | return pthread_mutex_destroy(__m); |
| 154 | } |
| 155 | |
| 156 | // Condition variable |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 157 | int __libcpp_condvar_signal(__libcpp_condvar_t* __cv) |
| 158 | { |
| 159 | return pthread_cond_signal(__cv); |
| 160 | } |
| 161 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 162 | int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv) |
| 163 | { |
| 164 | return pthread_cond_broadcast(__cv); |
| 165 | } |
| 166 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 167 | int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m) |
| 168 | { |
| 169 | return pthread_cond_wait(__cv, __m); |
| 170 | } |
| 171 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 172 | int __libcpp_condvar_timedwait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m, timespec* __ts) |
| 173 | { |
| 174 | return pthread_cond_timedwait(__cv, __m, __ts); |
| 175 | } |
| 176 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 177 | int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv) |
| 178 | { |
| 179 | return pthread_cond_destroy(__cv); |
| 180 | } |
| 181 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 182 | // Returns non-zero if the thread ids are equal, otherwise 0 |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 183 | bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2) |
| 184 | { |
| 185 | return pthread_equal(t1, t2) != 0; |
| 186 | } |
| 187 | |
| 188 | // Returns non-zero if t1 < t2, otherwise 0 |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 189 | bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2) |
| 190 | { |
| 191 | return t1 < t2; |
| 192 | } |
| 193 | |
| 194 | // Thread |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 195 | int __libcpp_thread_create(__libcpp_thread_t* __t, void* (*__func)(void*), void* __arg) |
| 196 | { |
| 197 | return pthread_create(__t, 0, __func, __arg); |
| 198 | } |
| 199 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 200 | __libcpp_thread_id __libcpp_thread_get_current_id() |
| 201 | { |
| 202 | return pthread_self(); |
| 203 | } |
| 204 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 205 | __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t* __t) |
| 206 | { |
| 207 | return *__t; |
| 208 | } |
| 209 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 210 | int __libcpp_thread_join(__libcpp_thread_t* __t) |
| 211 | { |
| 212 | return pthread_join(*__t, 0); |
| 213 | } |
| 214 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 215 | int __libcpp_thread_detach(__libcpp_thread_t* __t) |
| 216 | { |
| 217 | return pthread_detach(*__t); |
| 218 | } |
| 219 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 220 | void __libcpp_thread_yield() |
| 221 | { |
| 222 | sched_yield(); |
| 223 | } |
| 224 | |
| 225 | // Thread local storage |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 226 | int __libcpp_tls_create(__libcpp_tls_key* __key, void (*__at_exit)(void*)) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 227 | { |
| 228 | return pthread_key_create(__key, __at_exit); |
| 229 | } |
| 230 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 231 | void* __libcpp_tls_get(__libcpp_tls_key __key) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 232 | { |
| 233 | return pthread_getspecific(__key); |
| 234 | } |
| 235 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 236 | void __libcpp_tls_set(__libcpp_tls_key __key, void* __p) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 237 | { |
| 238 | pthread_setspecific(__key, __p); |
| 239 | } |
| 240 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 241 | #endif // _LIBCPP_HAS_THREAD_API_PTHREAD || _LIBCPP_BUILDING_EXTERNAL_THREADS |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 242 | |
| 243 | _LIBCPP_END_NAMESPACE_STD |
| 244 | |
| 245 | #endif // _LIBCPP_HAS_NO_THREADS |
| 246 | |
| 247 | #endif // _LIBCPP_THREADING_SUPPORT |