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