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