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