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