Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- mutex ------------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_MUTEX |
| 11 | #define _LIBCPP_MUTEX |
| 12 | |
| 13 | /* |
| 14 | mutex synopsis |
| 15 | |
| 16 | namespace std |
| 17 | { |
| 18 | |
| 19 | class mutex |
| 20 | { |
| 21 | public: |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 22 | constexpr mutex() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 | ~mutex(); |
| 24 | |
| 25 | mutex(const mutex&) = delete; |
| 26 | mutex& operator=(const mutex&) = delete; |
| 27 | |
| 28 | void lock(); |
| 29 | bool try_lock(); |
| 30 | void unlock(); |
| 31 | |
| 32 | typedef pthread_mutex_t* native_handle_type; |
| 33 | native_handle_type native_handle(); |
| 34 | }; |
| 35 | |
| 36 | class recursive_mutex |
| 37 | { |
| 38 | public: |
| 39 | recursive_mutex(); |
| 40 | ~recursive_mutex(); |
| 41 | |
| 42 | recursive_mutex(const recursive_mutex&) = delete; |
| 43 | recursive_mutex& operator=(const recursive_mutex&) = delete; |
| 44 | |
| 45 | void lock(); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 46 | bool try_lock() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 47 | void unlock(); |
| 48 | |
| 49 | typedef pthread_mutex_t* native_handle_type; |
| 50 | native_handle_type native_handle(); |
| 51 | }; |
| 52 | |
| 53 | class timed_mutex |
| 54 | { |
| 55 | public: |
| 56 | timed_mutex(); |
| 57 | ~timed_mutex(); |
| 58 | |
| 59 | timed_mutex(const timed_mutex&) = delete; |
| 60 | timed_mutex& operator=(const timed_mutex&) = delete; |
| 61 | |
| 62 | void lock(); |
| 63 | bool try_lock(); |
| 64 | template <class Rep, class Period> |
| 65 | bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); |
| 66 | template <class Clock, class Duration> |
| 67 | bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); |
| 68 | void unlock(); |
| 69 | }; |
| 70 | |
| 71 | class recursive_timed_mutex |
| 72 | { |
| 73 | public: |
| 74 | recursive_timed_mutex(); |
| 75 | ~recursive_timed_mutex(); |
| 76 | |
| 77 | recursive_timed_mutex(const recursive_timed_mutex&) = delete; |
| 78 | recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete; |
| 79 | |
| 80 | void lock(); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 81 | bool try_lock() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 82 | template <class Rep, class Period> |
| 83 | bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); |
| 84 | template <class Clock, class Duration> |
| 85 | bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); |
| 86 | void unlock(); |
| 87 | }; |
| 88 | |
Louis Dionne | a7a2beb | 2019-09-26 14:51:10 +0000 | [diff] [blame] | 89 | struct defer_lock_t { explicit defer_lock_t() = default; }; |
| 90 | struct try_to_lock_t { explicit try_to_lock_t() = default; }; |
| 91 | struct adopt_lock_t { explicit adopt_lock_t() = default; }; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 92 | |
Marshall Clow | f1bf62f | 2018-01-02 17:17:01 +0000 | [diff] [blame] | 93 | inline constexpr defer_lock_t defer_lock{}; |
| 94 | inline constexpr try_to_lock_t try_to_lock{}; |
| 95 | inline constexpr adopt_lock_t adopt_lock{}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 96 | |
| 97 | template <class Mutex> |
| 98 | class lock_guard |
| 99 | { |
| 100 | public: |
| 101 | typedef Mutex mutex_type; |
| 102 | |
| 103 | explicit lock_guard(mutex_type& m); |
| 104 | lock_guard(mutex_type& m, adopt_lock_t); |
| 105 | ~lock_guard(); |
| 106 | |
| 107 | lock_guard(lock_guard const&) = delete; |
| 108 | lock_guard& operator=(lock_guard const&) = delete; |
| 109 | }; |
| 110 | |
Marshall Clow | 580b6e6 | 2017-03-24 03:40:36 +0000 | [diff] [blame] | 111 | template <class... MutexTypes> |
| 112 | class scoped_lock // C++17 |
Eric Fiselier | 68143d4 | 2016-06-14 03:48:09 +0000 | [diff] [blame] | 113 | { |
| 114 | public: |
Marshall Clow | 580b6e6 | 2017-03-24 03:40:36 +0000 | [diff] [blame] | 115 | using mutex_type = Mutex; // If MutexTypes... consists of the single type Mutex |
| 116 | |
| 117 | explicit scoped_lock(MutexTypes&... m); |
Marshall Clow | f68c692 | 2017-07-27 17:44:03 +0000 | [diff] [blame] | 118 | scoped_lock(adopt_lock_t, MutexTypes&... m); |
Marshall Clow | 580b6e6 | 2017-03-24 03:40:36 +0000 | [diff] [blame] | 119 | ~scoped_lock(); |
| 120 | scoped_lock(scoped_lock const&) = delete; |
| 121 | scoped_lock& operator=(scoped_lock const&) = delete; |
Eric Fiselier | 68143d4 | 2016-06-14 03:48:09 +0000 | [diff] [blame] | 122 | private: |
| 123 | tuple<MutexTypes&...> pm; // exposition only |
| 124 | }; |
| 125 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 126 | template <class Mutex> |
| 127 | class unique_lock |
| 128 | { |
| 129 | public: |
| 130 | typedef Mutex mutex_type; |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 131 | unique_lock() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 132 | explicit unique_lock(mutex_type& m); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 133 | unique_lock(mutex_type& m, defer_lock_t) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 134 | unique_lock(mutex_type& m, try_to_lock_t); |
| 135 | unique_lock(mutex_type& m, adopt_lock_t); |
| 136 | template <class Clock, class Duration> |
| 137 | unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time); |
| 138 | template <class Rep, class Period> |
| 139 | unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time); |
| 140 | ~unique_lock(); |
| 141 | |
| 142 | unique_lock(unique_lock const&) = delete; |
| 143 | unique_lock& operator=(unique_lock const&) = delete; |
| 144 | |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 145 | unique_lock(unique_lock&& u) noexcept; |
| 146 | unique_lock& operator=(unique_lock&& u) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 147 | |
| 148 | void lock(); |
| 149 | bool try_lock(); |
| 150 | |
| 151 | template <class Rep, class Period> |
| 152 | bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); |
| 153 | template <class Clock, class Duration> |
| 154 | bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); |
| 155 | |
| 156 | void unlock(); |
| 157 | |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 158 | void swap(unique_lock& u) noexcept; |
| 159 | mutex_type* release() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 160 | |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 161 | bool owns_lock() const noexcept; |
| 162 | explicit operator bool () const noexcept; |
| 163 | mutex_type* mutex() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | template <class Mutex> |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 167 | void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 168 | |
| 169 | template <class L1, class L2, class... L3> |
| 170 | int try_lock(L1&, L2&, L3&...); |
| 171 | template <class L1, class L2, class... L3> |
| 172 | void lock(L1&, L2&, L3&...); |
| 173 | |
| 174 | struct once_flag |
| 175 | { |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 176 | constexpr once_flag() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 177 | |
| 178 | once_flag(const once_flag&) = delete; |
| 179 | once_flag& operator=(const once_flag&) = delete; |
| 180 | }; |
| 181 | |
| 182 | template<class Callable, class ...Args> |
| 183 | void call_once(once_flag& flag, Callable&& func, Args&&... args); |
| 184 | |
| 185 | } // std |
| 186 | |
| 187 | */ |
| 188 | |
| 189 | #include <__config> |
| 190 | #include <__mutex_base> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 191 | #include <__threading_support> |
Christopher Di Bella | 41f26e8 | 2021-06-05 02:47:47 +0000 | [diff] [blame] | 192 | #include <__utility/forward.h> |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 193 | #include <cstdint> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 194 | #include <functional> |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 195 | #include <memory> |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 196 | #ifndef _LIBCPP_CXX03_LANG |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 197 | # include <tuple> |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 198 | #endif |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 199 | #include <version> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 200 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 201 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 202 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 203 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 204 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 205 | _LIBCPP_PUSH_MACROS |
| 206 | #include <__undef_macros> |
| 207 | |
| 208 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 209 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 210 | |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 211 | #ifndef _LIBCPP_HAS_NO_THREADS |
| 212 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 213 | class _LIBCPP_TYPE_VIS recursive_mutex |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 214 | { |
Saleem Abdulrasool | 0bc37ca | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 215 | __libcpp_recursive_mutex_t __m_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 216 | |
| 217 | public: |
| 218 | recursive_mutex(); |
| 219 | ~recursive_mutex(); |
| 220 | |
| 221 | private: |
| 222 | recursive_mutex(const recursive_mutex&); // = delete; |
| 223 | recursive_mutex& operator=(const recursive_mutex&); // = delete; |
| 224 | |
| 225 | public: |
| 226 | void lock(); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 227 | bool try_lock() _NOEXCEPT; |
| 228 | void unlock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 229 | |
Saleem Abdulrasool | 0bc37ca | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 230 | typedef __libcpp_recursive_mutex_t* native_handle_type; |
| 231 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 232 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 233 | native_handle_type native_handle() {return &__m_;} |
| 234 | }; |
| 235 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 236 | class _LIBCPP_TYPE_VIS timed_mutex |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 237 | { |
| 238 | mutex __m_; |
| 239 | condition_variable __cv_; |
| 240 | bool __locked_; |
| 241 | public: |
| 242 | timed_mutex(); |
| 243 | ~timed_mutex(); |
| 244 | |
| 245 | private: |
| 246 | timed_mutex(const timed_mutex&); // = delete; |
| 247 | timed_mutex& operator=(const timed_mutex&); // = delete; |
| 248 | |
| 249 | public: |
| 250 | void lock(); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 251 | bool try_lock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 252 | template <class _Rep, class _Period> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 253 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 254 | bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 255 | {return try_lock_until(chrono::steady_clock::now() + __d);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 256 | template <class _Clock, class _Duration> |
Shoaib Meenai | 55f3a46 | 2017-03-02 03:22:18 +0000 | [diff] [blame] | 257 | _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 258 | bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 259 | void unlock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 260 | }; |
| 261 | |
| 262 | template <class _Clock, class _Duration> |
| 263 | bool |
| 264 | timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) |
| 265 | { |
| 266 | using namespace chrono; |
| 267 | unique_lock<mutex> __lk(__m_); |
| 268 | bool no_timeout = _Clock::now() < __t; |
| 269 | while (no_timeout && __locked_) |
| 270 | no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout; |
| 271 | if (!__locked_) |
| 272 | { |
| 273 | __locked_ = true; |
| 274 | return true; |
| 275 | } |
| 276 | return false; |
| 277 | } |
| 278 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 279 | class _LIBCPP_TYPE_VIS recursive_timed_mutex |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 280 | { |
| 281 | mutex __m_; |
| 282 | condition_variable __cv_; |
| 283 | size_t __count_; |
Marshall Clow | 5865536 | 2019-08-14 16:21:27 +0000 | [diff] [blame] | 284 | __thread_id __id_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 285 | public: |
| 286 | recursive_timed_mutex(); |
| 287 | ~recursive_timed_mutex(); |
| 288 | |
| 289 | private: |
| 290 | recursive_timed_mutex(const recursive_timed_mutex&); // = delete; |
| 291 | recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete; |
| 292 | |
| 293 | public: |
| 294 | void lock(); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 295 | bool try_lock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 296 | template <class _Rep, class _Period> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 297 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 298 | bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 299 | {return try_lock_until(chrono::steady_clock::now() + __d);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 300 | template <class _Clock, class _Duration> |
Shoaib Meenai | 55f3a46 | 2017-03-02 03:22:18 +0000 | [diff] [blame] | 301 | _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 302 | bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 303 | void unlock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | template <class _Clock, class _Duration> |
| 307 | bool |
| 308 | recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) |
| 309 | { |
| 310 | using namespace chrono; |
Marshall Clow | 5865536 | 2019-08-14 16:21:27 +0000 | [diff] [blame] | 311 | __thread_id __id = this_thread::get_id(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 312 | unique_lock<mutex> lk(__m_); |
Marshall Clow | 5865536 | 2019-08-14 16:21:27 +0000 | [diff] [blame] | 313 | if (__id == __id_) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 314 | { |
| 315 | if (__count_ == numeric_limits<size_t>::max()) |
| 316 | return false; |
| 317 | ++__count_; |
| 318 | return true; |
| 319 | } |
| 320 | bool no_timeout = _Clock::now() < __t; |
| 321 | while (no_timeout && __count_ != 0) |
| 322 | no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout; |
| 323 | if (__count_ == 0) |
| 324 | { |
| 325 | __count_ = 1; |
| 326 | __id_ = __id; |
| 327 | return true; |
| 328 | } |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | template <class _L0, class _L1> |
| 333 | int |
| 334 | try_lock(_L0& __l0, _L1& __l1) |
| 335 | { |
| 336 | unique_lock<_L0> __u0(__l0, try_to_lock); |
| 337 | if (__u0.owns_lock()) |
| 338 | { |
| 339 | if (__l1.try_lock()) |
| 340 | { |
| 341 | __u0.release(); |
| 342 | return -1; |
| 343 | } |
| 344 | else |
| 345 | return 1; |
| 346 | } |
| 347 | return 0; |
| 348 | } |
| 349 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 350 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 351 | |
| 352 | template <class _L0, class _L1, class _L2, class... _L3> |
| 353 | int |
| 354 | try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) |
| 355 | { |
| 356 | int __r = 0; |
| 357 | unique_lock<_L0> __u0(__l0, try_to_lock); |
| 358 | if (__u0.owns_lock()) |
| 359 | { |
| 360 | __r = try_lock(__l1, __l2, __l3...); |
| 361 | if (__r == -1) |
| 362 | __u0.release(); |
| 363 | else |
| 364 | ++__r; |
| 365 | } |
| 366 | return __r; |
| 367 | } |
| 368 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 369 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 370 | |
| 371 | template <class _L0, class _L1> |
| 372 | void |
| 373 | lock(_L0& __l0, _L1& __l1) |
| 374 | { |
| 375 | while (true) |
| 376 | { |
| 377 | { |
| 378 | unique_lock<_L0> __u0(__l0); |
| 379 | if (__l1.try_lock()) |
| 380 | { |
| 381 | __u0.release(); |
| 382 | break; |
| 383 | } |
| 384 | } |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 385 | __libcpp_thread_yield(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 386 | { |
| 387 | unique_lock<_L1> __u1(__l1); |
| 388 | if (__l0.try_lock()) |
| 389 | { |
| 390 | __u1.release(); |
| 391 | break; |
| 392 | } |
| 393 | } |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 394 | __libcpp_thread_yield(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 398 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 399 | |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 400 | template <class _L0, class _L1, class _L2, class ..._L3> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 401 | void |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 402 | __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 403 | { |
| 404 | while (true) |
| 405 | { |
| 406 | switch (__i) |
| 407 | { |
| 408 | case 0: |
| 409 | { |
| 410 | unique_lock<_L0> __u0(__l0); |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 411 | __i = try_lock(__l1, __l2, __l3...); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 412 | if (__i == -1) |
| 413 | { |
| 414 | __u0.release(); |
| 415 | return; |
| 416 | } |
| 417 | } |
| 418 | ++__i; |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 419 | __libcpp_thread_yield(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 420 | break; |
| 421 | case 1: |
| 422 | { |
| 423 | unique_lock<_L1> __u1(__l1); |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 424 | __i = try_lock(__l2, __l3..., __l0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 425 | if (__i == -1) |
| 426 | { |
| 427 | __u1.release(); |
| 428 | return; |
| 429 | } |
| 430 | } |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 431 | if (__i == sizeof...(_L3) + 1) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 432 | __i = 0; |
| 433 | else |
| 434 | __i += 2; |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 435 | __libcpp_thread_yield(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 436 | break; |
| 437 | default: |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 438 | __lock_first(__i - 2, __l2, __l3..., __l0, __l1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 439 | return; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 444 | template <class _L0, class _L1, class _L2, class ..._L3> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 445 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 446 | void |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 447 | lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 448 | { |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 449 | __lock_first(0, __l0, __l1, __l2, __l3...); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Eric Fiselier | 68143d4 | 2016-06-14 03:48:09 +0000 | [diff] [blame] | 452 | template <class _L0> |
| 453 | inline _LIBCPP_INLINE_VISIBILITY |
| 454 | void __unlock(_L0& __l0) { |
| 455 | __l0.unlock(); |
| 456 | } |
| 457 | |
| 458 | template <class _L0, class _L1> |
| 459 | inline _LIBCPP_INLINE_VISIBILITY |
| 460 | void __unlock(_L0& __l0, _L1& __l1) { |
| 461 | __l0.unlock(); |
| 462 | __l1.unlock(); |
| 463 | } |
| 464 | |
| 465 | template <class _L0, class _L1, class _L2, class ..._L3> |
| 466 | inline _LIBCPP_INLINE_VISIBILITY |
| 467 | void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) { |
| 468 | __l0.unlock(); |
| 469 | __l1.unlock(); |
| 470 | _VSTD::__unlock(__l2, __l3...); |
| 471 | } |
| 472 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 473 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 474 | |
Marshall Clow | 2a7ae6f | 2017-03-24 05:19:15 +0000 | [diff] [blame] | 475 | #if _LIBCPP_STD_VER > 14 |
| 476 | template <class ..._Mutexes> |
| 477 | class _LIBCPP_TEMPLATE_VIS scoped_lock; |
| 478 | |
| 479 | template <> |
| 480 | class _LIBCPP_TEMPLATE_VIS scoped_lock<> { |
| 481 | public: |
| 482 | explicit scoped_lock() {} |
| 483 | ~scoped_lock() = default; |
| 484 | |
| 485 | _LIBCPP_INLINE_VISIBILITY |
| 486 | explicit scoped_lock(adopt_lock_t) {} |
| 487 | |
| 488 | scoped_lock(scoped_lock const&) = delete; |
| 489 | scoped_lock& operator=(scoped_lock const&) = delete; |
| 490 | }; |
| 491 | |
| 492 | template <class _Mutex> |
Aaron Puchert | e1560fb | 2018-10-09 23:42:29 +0000 | [diff] [blame] | 493 | class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) scoped_lock<_Mutex> { |
Marshall Clow | 2a7ae6f | 2017-03-24 05:19:15 +0000 | [diff] [blame] | 494 | public: |
| 495 | typedef _Mutex mutex_type; |
| 496 | private: |
| 497 | mutex_type& __m_; |
| 498 | public: |
| 499 | explicit scoped_lock(mutex_type & __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m)) |
| 500 | : __m_(__m) {__m_.lock();} |
| 501 | |
| 502 | ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();} |
| 503 | |
| 504 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | f68c692 | 2017-07-27 17:44:03 +0000 | [diff] [blame] | 505 | explicit scoped_lock(adopt_lock_t, mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m)) |
Marshall Clow | 2a7ae6f | 2017-03-24 05:19:15 +0000 | [diff] [blame] | 506 | : __m_(__m) {} |
Marshall Clow | 2a7ae6f | 2017-03-24 05:19:15 +0000 | [diff] [blame] | 507 | |
| 508 | scoped_lock(scoped_lock const&) = delete; |
| 509 | scoped_lock& operator=(scoped_lock const&) = delete; |
| 510 | }; |
| 511 | |
| 512 | template <class ..._MArgs> |
| 513 | class _LIBCPP_TEMPLATE_VIS scoped_lock |
| 514 | { |
| 515 | static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required"); |
| 516 | typedef tuple<_MArgs&...> _MutexTuple; |
| 517 | |
| 518 | public: |
| 519 | _LIBCPP_INLINE_VISIBILITY |
| 520 | explicit scoped_lock(_MArgs&... __margs) |
| 521 | : __t_(__margs...) |
| 522 | { |
| 523 | _VSTD::lock(__margs...); |
| 524 | } |
| 525 | |
| 526 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | f68c692 | 2017-07-27 17:44:03 +0000 | [diff] [blame] | 527 | scoped_lock(adopt_lock_t, _MArgs&... __margs) |
Marshall Clow | 2a7ae6f | 2017-03-24 05:19:15 +0000 | [diff] [blame] | 528 | : __t_(__margs...) |
| 529 | { |
| 530 | } |
| 531 | |
| 532 | _LIBCPP_INLINE_VISIBILITY |
| 533 | ~scoped_lock() { |
| 534 | typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices; |
| 535 | __unlock_unpack(_Indices{}, __t_); |
| 536 | } |
| 537 | |
| 538 | scoped_lock(scoped_lock const&) = delete; |
| 539 | scoped_lock& operator=(scoped_lock const&) = delete; |
| 540 | |
| 541 | private: |
| 542 | template <size_t ..._Indx> |
| 543 | _LIBCPP_INLINE_VISIBILITY |
| 544 | static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) { |
| 545 | _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...); |
| 546 | } |
| 547 | |
| 548 | _MutexTuple __t_; |
| 549 | }; |
| 550 | |
| 551 | #endif // _LIBCPP_STD_VER > 14 |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 552 | #endif // !_LIBCPP_HAS_NO_THREADS |
| 553 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 554 | struct _LIBCPP_TEMPLATE_VIS once_flag; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 555 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 556 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 557 | |
| 558 | template<class _Callable, class... _Args> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 559 | _LIBCPP_INLINE_VISIBILITY |
| 560 | void call_once(once_flag&, _Callable&&, _Args&&...); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 561 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 562 | #else // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 563 | |
| 564 | template<class _Callable> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 565 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 566 | void call_once(once_flag&, _Callable&); |
| 567 | |
| 568 | template<class _Callable> |
| 569 | _LIBCPP_INLINE_VISIBILITY |
| 570 | void call_once(once_flag&, const _Callable&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 571 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 572 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 573 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 574 | struct _LIBCPP_TEMPLATE_VIS once_flag |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 575 | { |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 576 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 577 | _LIBCPP_CONSTEXPR |
| 578 | once_flag() _NOEXCEPT : __state_(0) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 579 | |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 580 | #if defined(_LIBCPP_ABI_MICROSOFT) |
| 581 | typedef uintptr_t _State_type; |
| 582 | #else |
| 583 | typedef unsigned long _State_type; |
| 584 | #endif |
| 585 | |
| 586 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 587 | private: |
| 588 | once_flag(const once_flag&); // = delete; |
| 589 | once_flag& operator=(const once_flag&); // = delete; |
| 590 | |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 591 | _State_type __state_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 592 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 593 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 594 | template<class _Callable, class... _Args> |
| 595 | friend |
| 596 | void call_once(once_flag&, _Callable&&, _Args&&...); |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 597 | #else // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 598 | template<class _Callable> |
| 599 | friend |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 600 | void call_once(once_flag&, _Callable&); |
| 601 | |
| 602 | template<class _Callable> |
| 603 | friend |
| 604 | void call_once(once_flag&, const _Callable&); |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 605 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 606 | }; |
| 607 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 608 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 609 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 610 | template <class _Fp> |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 611 | class __call_once_param |
| 612 | { |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 613 | _Fp& __f_; |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 614 | public: |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 615 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 616 | explicit __call_once_param(_Fp& __f) : __f_(__f) {} |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 617 | |
| 618 | _LIBCPP_INLINE_VISIBILITY |
| 619 | void operator()() |
| 620 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 621 | typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index; |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 622 | __execute(_Index()); |
| 623 | } |
| 624 | |
| 625 | private: |
| 626 | template <size_t ..._Indices> |
| 627 | _LIBCPP_INLINE_VISIBILITY |
| 628 | void __execute(__tuple_indices<_Indices...>) |
| 629 | { |
Arthur O'Dwyer | 34465da | 2020-12-15 19:32:29 -0500 | [diff] [blame] | 630 | _VSTD::__invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...); |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 631 | } |
| 632 | }; |
| 633 | |
| 634 | #else |
| 635 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 636 | template <class _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 637 | class __call_once_param |
| 638 | { |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 639 | _Fp& __f_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 640 | public: |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 641 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 642 | explicit __call_once_param(_Fp& __f) : __f_(__f) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 643 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 644 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 645 | void operator()() |
| 646 | { |
| 647 | __f_(); |
| 648 | } |
| 649 | }; |
| 650 | |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 651 | #endif |
| 652 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 653 | template <class _Fp> |
Louis Dionne | 530e240 | 2019-11-11 10:21:57 -0500 | [diff] [blame] | 654 | void _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 655 | __call_once_proxy(void* __vp) |
| 656 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 657 | __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 658 | (*__p)(); |
| 659 | } |
| 660 | |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 661 | _LIBCPP_FUNC_VIS void __call_once(volatile once_flag::_State_type&, void*, |
| 662 | void (*)(void*)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 663 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 664 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 665 | |
| 666 | template<class _Callable, class... _Args> |
| 667 | inline _LIBCPP_INLINE_VISIBILITY |
| 668 | void |
| 669 | call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args) |
| 670 | { |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 671 | if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 672 | { |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 673 | typedef tuple<_Callable&&, _Args&&...> _Gp; |
| 674 | _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...); |
| 675 | __call_once_param<_Gp> __p(__f); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 676 | __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 680 | #else // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 681 | |
| 682 | template<class _Callable> |
| 683 | inline _LIBCPP_INLINE_VISIBILITY |
| 684 | void |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 685 | call_once(once_flag& __flag, _Callable& __func) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 686 | { |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 687 | if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 688 | { |
| 689 | __call_once_param<_Callable> __p(__func); |
| 690 | __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>); |
| 691 | } |
| 692 | } |
| 693 | |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 694 | template<class _Callable> |
| 695 | inline _LIBCPP_INLINE_VISIBILITY |
| 696 | void |
| 697 | call_once(once_flag& __flag, const _Callable& __func) |
| 698 | { |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 699 | if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0)) |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 700 | { |
| 701 | __call_once_param<const _Callable> __p(__func); |
| 702 | __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>); |
| 703 | } |
| 704 | } |
| 705 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 706 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 707 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 708 | _LIBCPP_END_NAMESPACE_STD |
| 709 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 710 | _LIBCPP_POP_MACROS |
| 711 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 712 | #endif // _LIBCPP_MUTEX |