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 | 4f2c83f | 2016-10-14 13:56:58 +0000 | [diff] [blame] | 22 | #ifndef __libcpp_has_include |
| 23 | #ifndef __has_include |
| 24 | #define __libcpp_has_include(x) 0 |
| 25 | #else |
| 26 | #define __libcpp_has_include(x) __has_include(x) |
| 27 | #endif |
Asiri Rathnayake | f6600b7 | 2016-09-13 09:32:32 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
Asiri Rathnayake | 4f2c83f | 2016-10-14 13:56:58 +0000 | [diff] [blame] | 30 | #if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) && \ |
Asiri Rathnayake | e29d9ff | 2017-01-03 11:32:31 +0000 | [diff] [blame^] | 31 | !__libcpp_has_include(<__external_threading>) |
| 32 | // If the <__external_threading> header is absent, build libc++ against a |
| 33 | // pthread-oriented thread api but leave out its implementation. This setup |
| 34 | // allows building+testing of an externally-threaded library variant (on any |
| 35 | // platform that supports pthreads). Here, an 'externally-threaded' library |
| 36 | // variant is one where the implementation of the libc++ thread api is provided |
| 37 | // as a separate library. |
| 38 | #define _LIBCPP_HAS_THREAD_API_EXTERNAL_PTHREAD |
| 39 | #endif |
| 40 | |
| 41 | #if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) && \ |
Asiri Rathnayake | 4f2c83f | 2016-10-14 13:56:58 +0000 | [diff] [blame] | 42 | __libcpp_has_include(<__external_threading>) |
| 43 | #include <__external_threading> |
| 44 | #else |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 45 | |
Asiri Rathnayake | e29d9ff | 2017-01-03 11:32:31 +0000 | [diff] [blame^] | 46 | #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) || \ |
| 47 | defined(_LIBCPP_HAS_THREAD_API_EXTERNAL_PTHREAD) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 48 | #include <pthread.h> |
| 49 | #include <sched.h> |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 50 | #endif |
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 | #if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) |
| 53 | #define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_FUNC_VIS |
| 54 | #else |
| 55 | #define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY |
| 56 | #endif |
| 57 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 58 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 59 | |
Asiri Rathnayake | e29d9ff | 2017-01-03 11:32:31 +0000 | [diff] [blame^] | 60 | #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) || \ |
| 61 | defined(_LIBCPP_HAS_THREAD_API_EXTERNAL_PTHREAD) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 62 | // Mutex |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 63 | typedef pthread_mutex_t __libcpp_mutex_t; |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 64 | #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 65 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 66 | // Condition Variable |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 67 | typedef pthread_cond_t __libcpp_condvar_t; |
| 68 | #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 69 | |
| 70 | // THread ID |
| 71 | typedef pthread_t __libcpp_thread_id; |
| 72 | |
| 73 | // Thread |
| 74 | typedef pthread_t __libcpp_thread_t; |
| 75 | |
| 76 | // Thrad Local Storage |
| 77 | typedef pthread_key_t __libcpp_tls_key; |
| 78 | #endif |
| 79 | |
| 80 | // Mutex |
| 81 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 82 | int __libcpp_recursive_mutex_init(__libcpp_mutex_t *__m); |
| 83 | |
| 84 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 85 | int __libcpp_mutex_lock(__libcpp_mutex_t *__m); |
| 86 | |
| 87 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 88 | int __libcpp_mutex_trylock(__libcpp_mutex_t *__m); |
| 89 | |
| 90 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 91 | int __libcpp_mutex_unlock(__libcpp_mutex_t *__m); |
| 92 | |
| 93 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 94 | int __libcpp_mutex_destroy(__libcpp_mutex_t *__m); |
| 95 | |
| 96 | // Condition variable |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 97 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 98 | int __libcpp_condvar_signal(__libcpp_condvar_t* __cv); |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 99 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 100 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 101 | int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv); |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 102 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 103 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 104 | int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m); |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 105 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 106 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 107 | int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m, |
| 108 | timespec *__ts); |
| 109 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 110 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 111 | int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv); |
| 112 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 113 | // Thread ID |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 114 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 115 | bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2); |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 116 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 117 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 118 | bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2); |
| 119 | |
| 120 | // Thread |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 121 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 122 | int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *), |
| 123 | void *__arg); |
| 124 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 125 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 126 | __libcpp_thread_id __libcpp_thread_get_current_id(); |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 127 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 128 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 129 | __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t); |
| 130 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 131 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 132 | int __libcpp_thread_join(__libcpp_thread_t *__t); |
| 133 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 134 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 135 | int __libcpp_thread_detach(__libcpp_thread_t *__t); |
| 136 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 137 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 138 | void __libcpp_thread_yield(); |
| 139 | |
| 140 | // Thread local storage |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 141 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 142 | int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *)); |
| 143 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 144 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 145 | void *__libcpp_tls_get(__libcpp_tls_key __key); |
| 146 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 147 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 148 | void __libcpp_tls_set(__libcpp_tls_key __key, void *__p); |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 149 | |
Asiri Rathnayake | e29d9ff | 2017-01-03 11:32:31 +0000 | [diff] [blame^] | 150 | #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) || \ |
| 151 | defined(_LIBCPP_BUILDING_THREAD_API_EXTERNAL_PTHREAD) |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 152 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 153 | int __libcpp_recursive_mutex_init(__libcpp_mutex_t *__m) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 154 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 155 | pthread_mutexattr_t attr; |
| 156 | int __ec = pthread_mutexattr_init(&attr); |
| 157 | if (__ec) |
| 158 | return __ec; |
| 159 | __ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 160 | if (__ec) { |
| 161 | pthread_mutexattr_destroy(&attr); |
| 162 | return __ec; |
| 163 | } |
| 164 | __ec = pthread_mutex_init(__m, &attr); |
| 165 | if (__ec) { |
| 166 | pthread_mutexattr_destroy(&attr); |
| 167 | return __ec; |
| 168 | } |
| 169 | __ec = pthread_mutexattr_destroy(&attr); |
| 170 | if (__ec) { |
| 171 | pthread_mutex_destroy(__m); |
| 172 | return __ec; |
| 173 | } |
| 174 | return 0; |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 177 | int __libcpp_mutex_lock(__libcpp_mutex_t *__m) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 178 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 179 | return pthread_mutex_lock(__m); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 182 | int __libcpp_mutex_trylock(__libcpp_mutex_t *__m) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 183 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 184 | return pthread_mutex_trylock(__m); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 187 | int __libcpp_mutex_unlock(__libcpp_mutex_t *__m) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 188 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 189 | return pthread_mutex_unlock(__m); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 192 | int __libcpp_mutex_destroy(__libcpp_mutex_t *__m) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 193 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 194 | return pthread_mutex_destroy(__m); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 197 | // Condition Variable |
| 198 | int __libcpp_condvar_signal(__libcpp_condvar_t *__cv) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 199 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 200 | return pthread_cond_signal(__cv); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 203 | int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 204 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 205 | return pthread_cond_broadcast(__cv); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 208 | int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 209 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 210 | return pthread_cond_wait(__cv, __m); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 213 | int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m, |
| 214 | timespec *__ts) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 215 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 216 | return pthread_cond_timedwait(__cv, __m, __ts); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 219 | int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 220 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 221 | return pthread_cond_destroy(__cv); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 224 | // Returns non-zero if the thread ids are equal, otherwise 0 |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 225 | bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2) |
| 226 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 227 | return pthread_equal(t1, t2) != 0; |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // Returns non-zero if t1 < t2, otherwise 0 |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 231 | bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2) |
| 232 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 233 | return t1 < t2; |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // Thread |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 237 | int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *), |
| 238 | void *__arg) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 239 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 240 | return pthread_create(__t, 0, __func, __arg); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 243 | __libcpp_thread_id __libcpp_thread_get_current_id() |
| 244 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 245 | return pthread_self(); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 248 | __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 249 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 250 | return *__t; |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 253 | int __libcpp_thread_join(__libcpp_thread_t *__t) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 254 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 255 | return pthread_join(*__t, 0); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 258 | int __libcpp_thread_detach(__libcpp_thread_t *__t) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 259 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 260 | return pthread_detach(*__t); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 263 | void __libcpp_thread_yield() |
| 264 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 265 | sched_yield(); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // Thread local storage |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 269 | int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *)) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 270 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 271 | return pthread_key_create(__key, __at_exit); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 274 | void *__libcpp_tls_get(__libcpp_tls_key __key) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 275 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 276 | return pthread_getspecific(__key); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 279 | void __libcpp_tls_set(__libcpp_tls_key __key, void *__p) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 280 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 281 | pthread_setspecific(__key, __p); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 284 | #endif // _LIBCPP_HAS_THREAD_API_PTHREAD |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 285 | |
| 286 | _LIBCPP_END_NAMESPACE_STD |
| 287 | |
Asiri Rathnayake | 4f2c83f | 2016-10-14 13:56:58 +0000 | [diff] [blame] | 288 | #endif // !_LIBCPP_HAS_THREAD_API_EXTERNAL || !__libcpp_has_include(<__external_threading>) |
Asiri Rathnayake | 7250d33 | 2016-10-14 13:00:07 +0000 | [diff] [blame] | 289 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 290 | #endif // _LIBCPP_HAS_NO_THREADS |
| 291 | |
| 292 | #endif // _LIBCPP_THREADING_SUPPORT |