Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===------------------------- mutex.cpp ----------------------------------===// |
| 2 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "mutex" |
| 10 | #include "limits" |
| 11 | #include "system_error" |
Eric Fiselier | 1366d26 | 2015-08-18 21:08:54 +0000 | [diff] [blame] | 12 | #include "include/atomic_support.h" |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 13 | #include "__undef_macros" |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 14 | |
Petr Hosek | 99575aa | 2019-05-30 01:34:41 +0000 | [diff] [blame] | 15 | #ifndef _LIBCPP_HAS_NO_THREADS |
Petr Hosek | 10ed428 | 2019-05-30 04:40:21 +0000 | [diff] [blame] | 16 | #if defined(__unix__) && defined(__ELF__) && defined(_LIBCPP_HAS_COMMENT_LIB_PRAGMA) |
Petr Hosek | 99575aa | 2019-05-30 01:34:41 +0000 | [diff] [blame] | 17 | #pragma comment(lib, "pthread") |
| 18 | #endif |
| 19 | #endif |
| 20 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | _LIBCPP_BEGIN_NAMESPACE_STD |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 22 | #ifndef _LIBCPP_HAS_NO_THREADS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 | |
| 24 | const defer_lock_t defer_lock = {}; |
| 25 | const try_to_lock_t try_to_lock = {}; |
| 26 | const adopt_lock_t adopt_lock = {}; |
| 27 | |
Chandler Carruth | f38b826 | 2019-04-26 05:04:33 +0000 | [diff] [blame] | 28 | mutex::~mutex() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 29 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 30 | __libcpp_mutex_destroy(&__m_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void |
| 34 | mutex::lock() |
| 35 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 36 | int ec = __libcpp_mutex_lock(&__m_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 37 | if (ec) |
| 38 | __throw_system_error(ec, "mutex lock failed"); |
| 39 | } |
| 40 | |
| 41 | bool |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 42 | mutex::try_lock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 43 | { |
Eric Fiselier | 0ec1447 | 2017-01-14 10:27:12 +0000 | [diff] [blame] | 44 | return __libcpp_mutex_trylock(&__m_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 48 | mutex::unlock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 49 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 50 | int ec = __libcpp_mutex_unlock(&__m_); |
Howard Hinnant | 9ad56f3 | 2013-09-21 21:26:37 +0000 | [diff] [blame] | 51 | (void)ec; |
Eric Fiselier | 59cd98b | 2017-02-04 23:22:28 +0000 | [diff] [blame] | 52 | _LIBCPP_ASSERT(ec == 0, "call to mutex::unlock failed"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // recursive_mutex |
| 56 | |
| 57 | recursive_mutex::recursive_mutex() |
| 58 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 59 | int ec = __libcpp_recursive_mutex_init(&__m_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 60 | if (ec) |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 61 | __throw_system_error(ec, "recursive_mutex constructor failed"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | recursive_mutex::~recursive_mutex() |
| 65 | { |
Saleem Abdulrasool | 0bc37ca | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 66 | int e = __libcpp_recursive_mutex_destroy(&__m_); |
Howard Hinnant | 9ad56f3 | 2013-09-21 21:26:37 +0000 | [diff] [blame] | 67 | (void)e; |
Eric Fiselier | 59cd98b | 2017-02-04 23:22:28 +0000 | [diff] [blame] | 68 | _LIBCPP_ASSERT(e == 0, "call to ~recursive_mutex() failed"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void |
| 72 | recursive_mutex::lock() |
| 73 | { |
Saleem Abdulrasool | 0bc37ca | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 74 | int ec = __libcpp_recursive_mutex_lock(&__m_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 75 | if (ec) |
| 76 | __throw_system_error(ec, "recursive_mutex lock failed"); |
| 77 | } |
| 78 | |
| 79 | void |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 80 | recursive_mutex::unlock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 81 | { |
Saleem Abdulrasool | 0bc37ca | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 82 | int e = __libcpp_recursive_mutex_unlock(&__m_); |
Howard Hinnant | 9ad56f3 | 2013-09-21 21:26:37 +0000 | [diff] [blame] | 83 | (void)e; |
Eric Fiselier | 59cd98b | 2017-02-04 23:22:28 +0000 | [diff] [blame] | 84 | _LIBCPP_ASSERT(e == 0, "call to recursive_mutex::unlock() failed"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | bool |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 88 | recursive_mutex::try_lock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 89 | { |
Eric Fiselier | 0ec1447 | 2017-01-14 10:27:12 +0000 | [diff] [blame] | 90 | return __libcpp_recursive_mutex_trylock(&__m_); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // timed_mutex |
| 94 | |
| 95 | timed_mutex::timed_mutex() |
| 96 | : __locked_(false) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | timed_mutex::~timed_mutex() |
| 101 | { |
| 102 | lock_guard<mutex> _(__m_); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | timed_mutex::lock() |
| 107 | { |
| 108 | unique_lock<mutex> lk(__m_); |
| 109 | while (__locked_) |
| 110 | __cv_.wait(lk); |
| 111 | __locked_ = true; |
| 112 | } |
| 113 | |
| 114 | bool |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 115 | timed_mutex::try_lock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 116 | { |
| 117 | unique_lock<mutex> lk(__m_, try_to_lock); |
| 118 | if (lk.owns_lock() && !__locked_) |
| 119 | { |
| 120 | __locked_ = true; |
| 121 | return true; |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | void |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 127 | timed_mutex::unlock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 128 | { |
| 129 | lock_guard<mutex> _(__m_); |
| 130 | __locked_ = false; |
| 131 | __cv_.notify_one(); |
| 132 | } |
| 133 | |
| 134 | // recursive_timed_mutex |
| 135 | |
| 136 | recursive_timed_mutex::recursive_timed_mutex() |
| 137 | : __count_(0), |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 138 | __id_(0) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 139 | { |
| 140 | } |
| 141 | |
| 142 | recursive_timed_mutex::~recursive_timed_mutex() |
| 143 | { |
| 144 | lock_guard<mutex> _(__m_); |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | recursive_timed_mutex::lock() |
| 149 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 150 | __libcpp_thread_id id = __libcpp_thread_get_current_id(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 151 | unique_lock<mutex> lk(__m_); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 152 | if (__libcpp_thread_id_equal(id, __id_)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 153 | { |
| 154 | if (__count_ == numeric_limits<size_t>::max()) |
| 155 | __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached"); |
| 156 | ++__count_; |
| 157 | return; |
| 158 | } |
| 159 | while (__count_ != 0) |
| 160 | __cv_.wait(lk); |
| 161 | __count_ = 1; |
| 162 | __id_ = id; |
| 163 | } |
| 164 | |
| 165 | bool |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 166 | recursive_timed_mutex::try_lock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 167 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 168 | __libcpp_thread_id id = __libcpp_thread_get_current_id(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 169 | unique_lock<mutex> lk(__m_, try_to_lock); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 170 | if (lk.owns_lock() && (__count_ == 0 || __libcpp_thread_id_equal(id, __id_))) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 171 | { |
| 172 | if (__count_ == numeric_limits<size_t>::max()) |
| 173 | return false; |
| 174 | ++__count_; |
| 175 | __id_ = id; |
| 176 | return true; |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | void |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 182 | recursive_timed_mutex::unlock() _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 183 | { |
| 184 | unique_lock<mutex> lk(__m_); |
| 185 | if (--__count_ == 0) |
| 186 | { |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 187 | __id_ = 0; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 188 | lk.unlock(); |
| 189 | __cv_.notify_one(); |
| 190 | } |
| 191 | } |
| 192 | |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 193 | #endif // !_LIBCPP_HAS_NO_THREADS |
| 194 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 195 | // If dispatch_once_f ever handles C++ exceptions, and if one can get to it |
| 196 | // without illegal macros (unexpected macros not beginning with _UpperCase or |
| 197 | // __lowercase), and if it stops spinning waiting threads, then call_once should |
| 198 | // call into dispatch_once_f instead of here. Relevant radar this code needs to |
| 199 | // keep in sync with: 7741191. |
| 200 | |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 201 | #ifndef _LIBCPP_HAS_NO_THREADS |
Eric Fiselier | 637dd95 | 2016-09-28 22:08:13 +0000 | [diff] [blame] | 202 | _LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER; |
| 203 | _LIBCPP_SAFE_STATIC static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER; |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 204 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 205 | |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 206 | void __call_once(volatile once_flag::_State_type& flag, void* arg, |
| 207 | void (*func)(void*)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 208 | { |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 209 | #if defined(_LIBCPP_HAS_NO_THREADS) |
| 210 | if (flag == 0) |
| 211 | { |
| 212 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 213 | try |
| 214 | { |
| 215 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 216 | flag = 1; |
| 217 | func(arg); |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 218 | flag = ~once_flag::_State_type(0); |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 219 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 220 | } |
| 221 | catch (...) |
| 222 | { |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 223 | flag = 0; |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 224 | throw; |
| 225 | } |
| 226 | #endif // _LIBCPP_NO_EXCEPTIONS |
| 227 | } |
| 228 | #else // !_LIBCPP_HAS_NO_THREADS |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 229 | __libcpp_mutex_lock(&mut); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 230 | while (flag == 1) |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 231 | __libcpp_condvar_wait(&cv, &mut); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 232 | if (flag == 0) |
| 233 | { |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 234 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 235 | try |
| 236 | { |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 237 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 238 | __libcpp_relaxed_store(&flag, once_flag::_State_type(1)); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 239 | __libcpp_mutex_unlock(&mut); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 240 | func(arg); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 241 | __libcpp_mutex_lock(&mut); |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 242 | __libcpp_atomic_store(&flag, ~once_flag::_State_type(0), |
| 243 | _AO_Release); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 244 | __libcpp_mutex_unlock(&mut); |
| 245 | __libcpp_condvar_broadcast(&cv); |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 246 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 247 | } |
| 248 | catch (...) |
| 249 | { |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 250 | __libcpp_mutex_lock(&mut); |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 251 | __libcpp_relaxed_store(&flag, once_flag::_State_type(0)); |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 252 | __libcpp_mutex_unlock(&mut); |
| 253 | __libcpp_condvar_broadcast(&cv); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 254 | throw; |
| 255 | } |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 256 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 257 | } |
| 258 | else |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 259 | __libcpp_mutex_unlock(&mut); |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 260 | #endif // !_LIBCPP_HAS_NO_THREADS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | _LIBCPP_END_NAMESPACE_STD |