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