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