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> |
Asiri Rathnayake | 1b93961 | 2017-02-09 09:31:41 +0000 | [diff] [blame] | 15 | #include <chrono> |
| 16 | #include <errno.h> |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 17 | |
| 18 | #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER |
| 19 | #pragma GCC system_header |
| 20 | #endif |
| 21 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 22 | #if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL) |
Eric Fiselier | 66134e8 | 2017-01-06 20:05:40 +0000 | [diff] [blame] | 23 | # include <__external_threading> |
| 24 | #elif !defined(_LIBCPP_HAS_NO_THREADS) |
| 25 | |
| 26 | #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) |
| 27 | # include <pthread.h> |
| 28 | # include <sched.h> |
| 29 | #endif |
| 30 | |
Eric Fiselier | 018a3d5 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 31 | _LIBCPP_PUSH_MACROS |
| 32 | #include <__undef_macros> |
| 33 | |
Peter Collingbourne | 61494b5 | 2018-02-21 21:36:18 +0000 | [diff] [blame] | 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 |
| 38 | #else |
| 39 | #define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY |
| 40 | #endif |
| 41 | |
Dimitry Andric | 362353a | 2017-01-26 18:37:18 +0000 | [diff] [blame] | 42 | #if defined(__FreeBSD__) && defined(__clang__) && __has_attribute(no_thread_safety_analysis) |
| 43 | #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis)) |
| 44 | #else |
| 45 | #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
| 46 | #endif |
| 47 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 48 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 49 | |
Peter Collingbourne | 61494b5 | 2018-02-21 21:36:18 +0000 | [diff] [blame] | 50 | #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 51 | // Mutex |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 52 | typedef pthread_mutex_t __libcpp_mutex_t; |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 53 | #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 54 | |
Saleem Abdulrasool | 3451a65 | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 55 | typedef pthread_mutex_t __libcpp_recursive_mutex_t; |
| 56 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 57 | // Condition Variable |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 58 | typedef pthread_cond_t __libcpp_condvar_t; |
| 59 | #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 60 | |
Asiri Rathnayake | e262e7b | 2017-01-03 12:59:50 +0000 | [diff] [blame] | 61 | // Execute once |
| 62 | typedef pthread_once_t __libcpp_exec_once_flag; |
| 63 | #define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT |
| 64 | |
| 65 | // Thread id |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 66 | typedef pthread_t __libcpp_thread_id; |
| 67 | |
| 68 | // Thread |
Asiri Rathnayake | a1d7d2f | 2017-01-16 12:19:54 +0000 | [diff] [blame] | 69 | #define _LIBCPP_NULL_THREAD 0U |
| 70 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 71 | typedef pthread_t __libcpp_thread_t; |
| 72 | |
Fangrui Song | 35a0c2c | 2018-09-17 07:40:42 +0000 | [diff] [blame] | 73 | // Thread Local Storage |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 74 | typedef pthread_key_t __libcpp_tls_key; |
Saleem Abdulrasool | 678eadd | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 75 | |
| 76 | #define _LIBCPP_TLS_DESTRUCTOR_CC |
| 77 | #else |
Peter Collingbourne | 59f2389 | 2018-01-23 01:59:43 +0000 | [diff] [blame] | 78 | // Mutex |
| 79 | typedef void* __libcpp_mutex_t; |
| 80 | #define _LIBCPP_MUTEX_INITIALIZER 0 |
| 81 | |
| 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]; |
| 86 | #else |
| 87 | # error Unsupported architecture |
| 88 | #endif |
Saleem Abdulrasool | 678eadd | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 89 | |
| 90 | // Condition Variable |
Peter Collingbourne | 59f2389 | 2018-01-23 01:59:43 +0000 | [diff] [blame] | 91 | typedef void* __libcpp_condvar_t; |
| 92 | #define _LIBCPP_CONDVAR_INITIALIZER 0 |
Saleem Abdulrasool | 678eadd | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 93 | |
| 94 | // Execute Once |
Peter Collingbourne | 59f2389 | 2018-01-23 01:59:43 +0000 | [diff] [blame] | 95 | typedef void* __libcpp_exec_once_flag; |
| 96 | #define _LIBCPP_EXEC_ONCE_INITIALIZER 0 |
Saleem Abdulrasool | 678eadd | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 97 | |
| 98 | // Thread ID |
Peter Collingbourne | 59f2389 | 2018-01-23 01:59:43 +0000 | [diff] [blame] | 99 | typedef long __libcpp_thread_id; |
Saleem Abdulrasool | 678eadd | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 100 | |
| 101 | // Thread |
Asiri Rathnayake | a1d7d2f | 2017-01-16 12:19:54 +0000 | [diff] [blame] | 102 | #define _LIBCPP_NULL_THREAD 0U |
| 103 | |
Peter Collingbourne | 59f2389 | 2018-01-23 01:59:43 +0000 | [diff] [blame] | 104 | typedef void* __libcpp_thread_t; |
Saleem Abdulrasool | 678eadd | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 105 | |
| 106 | // Thread Local Storage |
Peter Collingbourne | 59f2389 | 2018-01-23 01:59:43 +0000 | [diff] [blame] | 107 | typedef long __libcpp_tls_key; |
Saleem Abdulrasool | 678eadd | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 108 | |
Peter Collingbourne | 59f2389 | 2018-01-23 01:59:43 +0000 | [diff] [blame] | 109 | #define _LIBCPP_TLS_DESTRUCTOR_CC __stdcall |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 110 | #endif |
| 111 | |
| 112 | // Mutex |
| 113 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 3451a65 | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 114 | int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m); |
| 115 | |
Dimitry Andric | 362353a | 2017-01-26 18:37:18 +0000 | [diff] [blame] | 116 | _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
Saleem Abdulrasool | 3451a65 | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 117 | int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m); |
| 118 | |
Dimitry Andric | 362353a | 2017-01-26 18:37:18 +0000 | [diff] [blame] | 119 | _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
Eric Fiselier | 30d12e3 | 2017-01-14 10:27:12 +0000 | [diff] [blame] | 120 | bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m); |
Saleem Abdulrasool | 3451a65 | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 121 | |
Dimitry Andric | 362353a | 2017-01-26 18:37:18 +0000 | [diff] [blame] | 122 | _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
Saleem Abdulrasool | 3451a65 | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 123 | int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m); |
| 124 | |
| 125 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 126 | int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m); |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 127 | |
Dimitry Andric | 362353a | 2017-01-26 18:37:18 +0000 | [diff] [blame] | 128 | _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 129 | int __libcpp_mutex_lock(__libcpp_mutex_t *__m); |
| 130 | |
Dimitry Andric | 362353a | 2017-01-26 18:37:18 +0000 | [diff] [blame] | 131 | _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
Eric Fiselier | 30d12e3 | 2017-01-14 10:27:12 +0000 | [diff] [blame] | 132 | bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m); |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 133 | |
Dimitry Andric | 362353a | 2017-01-26 18:37:18 +0000 | [diff] [blame] | 134 | _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 135 | int __libcpp_mutex_unlock(__libcpp_mutex_t *__m); |
| 136 | |
| 137 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 138 | int __libcpp_mutex_destroy(__libcpp_mutex_t *__m); |
| 139 | |
| 140 | // Condition variable |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 141 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 142 | int __libcpp_condvar_signal(__libcpp_condvar_t* __cv); |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 143 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 144 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 145 | int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv); |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 146 | |
Dimitry Andric | 362353a | 2017-01-26 18:37:18 +0000 | [diff] [blame] | 147 | _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 148 | int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m); |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 149 | |
Dimitry Andric | 362353a | 2017-01-26 18:37:18 +0000 | [diff] [blame] | 150 | _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 151 | int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m, |
| 152 | timespec *__ts); |
| 153 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 154 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 155 | int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv); |
| 156 | |
Asiri Rathnayake | e262e7b | 2017-01-03 12:59:50 +0000 | [diff] [blame] | 157 | // Execute once |
| 158 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 159 | int __libcpp_execute_once(__libcpp_exec_once_flag *flag, |
| 160 | void (*init_routine)(void)); |
| 161 | |
| 162 | // Thread id |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 163 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 164 | 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] | 165 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 166 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 167 | bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2); |
| 168 | |
| 169 | // Thread |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 170 | _LIBCPP_THREAD_ABI_VISIBILITY |
Asiri Rathnayake | a1d7d2f | 2017-01-16 12:19:54 +0000 | [diff] [blame] | 171 | bool __libcpp_thread_isnull(const __libcpp_thread_t *__t); |
| 172 | |
| 173 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 174 | int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *), |
| 175 | void *__arg); |
| 176 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 177 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 178 | __libcpp_thread_id __libcpp_thread_get_current_id(); |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 179 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 180 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 181 | __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t); |
| 182 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 183 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 184 | int __libcpp_thread_join(__libcpp_thread_t *__t); |
| 185 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 186 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 187 | int __libcpp_thread_detach(__libcpp_thread_t *__t); |
| 188 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 189 | _LIBCPP_THREAD_ABI_VISIBILITY |
| 190 | void __libcpp_thread_yield(); |
| 191 | |
Asiri Rathnayake | 1b93961 | 2017-02-09 09:31:41 +0000 | [diff] [blame] | 192 | _LIBCPP_THREAD_ABI_VISIBILITY |
Joerg Sonnenberger | 5a0c9c3 | 2017-02-09 14:12:29 +0000 | [diff] [blame] | 193 | void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns); |
Asiri Rathnayake | 1b93961 | 2017-02-09 09:31:41 +0000 | [diff] [blame] | 194 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 195 | // Thread local storage |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 196 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 678eadd | 2017-01-07 03:07:45 +0000 | [diff] [blame] | 197 | int __libcpp_tls_create(__libcpp_tls_key* __key, |
| 198 | void(_LIBCPP_TLS_DESTRUCTOR_CC* __at_exit)(void*)); |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 199 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 200 | _LIBCPP_THREAD_ABI_VISIBILITY |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 201 | void *__libcpp_tls_get(__libcpp_tls_key __key); |
| 202 | |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 203 | _LIBCPP_THREAD_ABI_VISIBILITY |
Asiri Rathnayake | e262e7b | 2017-01-03 12:59:50 +0000 | [diff] [blame] | 204 | int __libcpp_tls_set(__libcpp_tls_key __key, void *__p); |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 205 | |
Peter Collingbourne | 59f2389 | 2018-01-23 01:59:43 +0000 | [diff] [blame] | 206 | #if (!defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \ |
| 207 | defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL)) && \ |
| 208 | defined(_LIBCPP_HAS_THREAD_API_PTHREAD) |
Asiri Rathnayake | 040945b | 2016-09-11 21:46:40 +0000 | [diff] [blame] | 209 | |
Saleem Abdulrasool | 3451a65 | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 210 | int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 211 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 212 | pthread_mutexattr_t attr; |
| 213 | int __ec = pthread_mutexattr_init(&attr); |
| 214 | if (__ec) |
| 215 | return __ec; |
| 216 | __ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 217 | if (__ec) { |
| 218 | pthread_mutexattr_destroy(&attr); |
| 219 | return __ec; |
| 220 | } |
| 221 | __ec = pthread_mutex_init(__m, &attr); |
| 222 | if (__ec) { |
| 223 | pthread_mutexattr_destroy(&attr); |
| 224 | return __ec; |
| 225 | } |
| 226 | __ec = pthread_mutexattr_destroy(&attr); |
| 227 | if (__ec) { |
| 228 | pthread_mutex_destroy(__m); |
| 229 | return __ec; |
| 230 | } |
| 231 | return 0; |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Saleem Abdulrasool | 3451a65 | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 234 | int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m) |
| 235 | { |
| 236 | return pthread_mutex_lock(__m); |
| 237 | } |
| 238 | |
Eric Fiselier | 30d12e3 | 2017-01-14 10:27:12 +0000 | [diff] [blame] | 239 | bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m) |
Saleem Abdulrasool | 3451a65 | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 240 | { |
Eric Fiselier | 30d12e3 | 2017-01-14 10:27:12 +0000 | [diff] [blame] | 241 | return pthread_mutex_trylock(__m) == 0; |
Saleem Abdulrasool | 3451a65 | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | int __libcpp_recursive_mutex_unlock(__libcpp_mutex_t *__m) |
| 245 | { |
| 246 | return pthread_mutex_unlock(__m); |
| 247 | } |
| 248 | |
| 249 | int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m) |
| 250 | { |
| 251 | return pthread_mutex_destroy(__m); |
| 252 | } |
| 253 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 254 | int __libcpp_mutex_lock(__libcpp_mutex_t *__m) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 255 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 256 | return pthread_mutex_lock(__m); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Eric Fiselier | 30d12e3 | 2017-01-14 10:27:12 +0000 | [diff] [blame] | 259 | bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 260 | { |
Eric Fiselier | 30d12e3 | 2017-01-14 10:27:12 +0000 | [diff] [blame] | 261 | return pthread_mutex_trylock(__m) == 0; |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 264 | int __libcpp_mutex_unlock(__libcpp_mutex_t *__m) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 265 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 266 | return pthread_mutex_unlock(__m); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 269 | int __libcpp_mutex_destroy(__libcpp_mutex_t *__m) |
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_mutex_destroy(__m); |
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 | // Condition Variable |
| 275 | int __libcpp_condvar_signal(__libcpp_condvar_t *__cv) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 276 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 277 | return pthread_cond_signal(__cv); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 280 | int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 281 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 282 | return pthread_cond_broadcast(__cv); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 285 | int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 286 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 287 | return pthread_cond_wait(__cv, __m); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 290 | int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m, |
| 291 | timespec *__ts) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 292 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 293 | return pthread_cond_timedwait(__cv, __m, __ts); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 296 | int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 297 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 298 | return pthread_cond_destroy(__cv); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Asiri Rathnayake | e262e7b | 2017-01-03 12:59:50 +0000 | [diff] [blame] | 301 | // Execute once |
| 302 | int __libcpp_execute_once(__libcpp_exec_once_flag *flag, |
| 303 | void (*init_routine)(void)) { |
| 304 | return pthread_once(flag, init_routine); |
| 305 | } |
| 306 | |
| 307 | // Thread id |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 308 | // Returns non-zero if the thread ids are equal, otherwise 0 |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 309 | bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2) |
| 310 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 311 | return pthread_equal(t1, t2) != 0; |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | // Returns non-zero if t1 < t2, otherwise 0 |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 315 | bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2) |
| 316 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 317 | return t1 < t2; |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // Thread |
Asiri Rathnayake | a1d7d2f | 2017-01-16 12:19:54 +0000 | [diff] [blame] | 321 | bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) { |
| 322 | return *__t == 0; |
| 323 | } |
| 324 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 325 | int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *), |
| 326 | void *__arg) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 327 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 328 | return pthread_create(__t, 0, __func, __arg); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 331 | __libcpp_thread_id __libcpp_thread_get_current_id() |
| 332 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 333 | return pthread_self(); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 336 | __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 337 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 338 | return *__t; |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 341 | int __libcpp_thread_join(__libcpp_thread_t *__t) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 342 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 343 | return pthread_join(*__t, 0); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 346 | int __libcpp_thread_detach(__libcpp_thread_t *__t) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 347 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 348 | return pthread_detach(*__t); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 351 | void __libcpp_thread_yield() |
| 352 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 353 | sched_yield(); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Asiri Rathnayake | 1b93961 | 2017-02-09 09:31:41 +0000 | [diff] [blame] | 356 | void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns) |
| 357 | { |
| 358 | using namespace chrono; |
| 359 | seconds __s = duration_cast<seconds>(__ns); |
| 360 | timespec __ts; |
| 361 | typedef decltype(__ts.tv_sec) ts_sec; |
| 362 | _LIBCPP_CONSTEXPR ts_sec __ts_sec_max = numeric_limits<ts_sec>::max(); |
| 363 | |
| 364 | if (__s.count() < __ts_sec_max) |
| 365 | { |
| 366 | __ts.tv_sec = static_cast<ts_sec>(__s.count()); |
| 367 | __ts.tv_nsec = static_cast<decltype(__ts.tv_nsec)>((__ns - __s).count()); |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | __ts.tv_sec = __ts_sec_max; |
| 372 | __ts.tv_nsec = 999999999; // (10^9 - 1) |
| 373 | } |
| 374 | |
| 375 | while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR); |
| 376 | } |
| 377 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 378 | // Thread local storage |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 379 | int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *)) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 380 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 381 | return pthread_key_create(__key, __at_exit); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 384 | void *__libcpp_tls_get(__libcpp_tls_key __key) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 385 | { |
Saleem Abdulrasool | 1d19237 | 2017-01-03 02:00:31 +0000 | [diff] [blame] | 386 | return pthread_getspecific(__key); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Asiri Rathnayake | e262e7b | 2017-01-03 12:59:50 +0000 | [diff] [blame] | 389 | int __libcpp_tls_set(__libcpp_tls_key __key, void *__p) |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 390 | { |
Asiri Rathnayake | e262e7b | 2017-01-03 12:59:50 +0000 | [diff] [blame] | 391 | return pthread_setspecific(__key, __p); |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Eric Fiselier | 66134e8 | 2017-01-06 20:05:40 +0000 | [diff] [blame] | 394 | #endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL |
| 395 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 396 | _LIBCPP_END_NAMESPACE_STD |
| 397 | |
Eric Fiselier | 018a3d5 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 398 | _LIBCPP_POP_MACROS |
| 399 | |
Eric Fiselier | 66134e8 | 2017-01-06 20:05:40 +0000 | [diff] [blame] | 400 | #endif // !_LIBCPP_HAS_NO_THREADS |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 401 | |
| 402 | #endif // _LIBCPP_THREADING_SUPPORT |