Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
Louis Dionne | 9bd9388 | 2021-11-17 16:25:01 -0500 | [diff] [blame] | 2 | //===----------------------------------------------------------------------===// |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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: |
Joe Loser | addc36f | 2021-10-26 13:45:52 -0400 | [diff] [blame] | 115 | using mutex_type = Mutex; // Only if sizeof...(MutexTypes) == 1 |
Marshall Clow | 580b6e6 | 2017-03-24 03:40:36 +0000 | [diff] [blame] | 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) |
Arthur O'Dwyer | 6eeaa00 | 2022-02-01 20:16:40 -0500 | [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: |
Nikolas Klauser | 0708864 | 2021-12-08 10:57:12 +0100 | [diff] [blame] | 218 | recursive_mutex(); |
| 219 | ~recursive_mutex(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 220 | |
Nikolas Klauser | 0708864 | 2021-12-08 10:57:12 +0100 | [diff] [blame] | 221 | recursive_mutex(const recursive_mutex&) = delete; |
| 222 | recursive_mutex& operator=(const recursive_mutex&) = delete; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 223 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 224 | void lock(); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 225 | bool try_lock() _NOEXCEPT; |
| 226 | void unlock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 227 | |
Saleem Abdulrasool | 0bc37ca | 2017-01-05 17:54:45 +0000 | [diff] [blame] | 228 | typedef __libcpp_recursive_mutex_t* native_handle_type; |
| 229 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 230 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 231 | native_handle_type native_handle() {return &__m_;} |
| 232 | }; |
| 233 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 234 | class _LIBCPP_TYPE_VIS timed_mutex |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 235 | { |
| 236 | mutex __m_; |
| 237 | condition_variable __cv_; |
| 238 | bool __locked_; |
| 239 | public: |
| 240 | timed_mutex(); |
| 241 | ~timed_mutex(); |
| 242 | |
Nikolas Klauser | 0708864 | 2021-12-08 10:57:12 +0100 | [diff] [blame] | 243 | timed_mutex(const timed_mutex&) = delete; |
| 244 | timed_mutex& operator=(const timed_mutex&) = delete; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 245 | |
| 246 | public: |
| 247 | void lock(); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 248 | bool try_lock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 249 | template <class _Rep, class _Period> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 250 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 251 | bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 252 | {return try_lock_until(chrono::steady_clock::now() + __d);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 253 | template <class _Clock, class _Duration> |
Shoaib Meenai | 55f3a46 | 2017-03-02 03:22:18 +0000 | [diff] [blame] | 254 | _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 255 | bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 256 | void unlock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | template <class _Clock, class _Duration> |
| 260 | bool |
| 261 | timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) |
| 262 | { |
| 263 | using namespace chrono; |
| 264 | unique_lock<mutex> __lk(__m_); |
| 265 | bool no_timeout = _Clock::now() < __t; |
| 266 | while (no_timeout && __locked_) |
| 267 | no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout; |
| 268 | if (!__locked_) |
| 269 | { |
| 270 | __locked_ = true; |
| 271 | return true; |
| 272 | } |
| 273 | return false; |
| 274 | } |
| 275 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 276 | class _LIBCPP_TYPE_VIS recursive_timed_mutex |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 277 | { |
| 278 | mutex __m_; |
| 279 | condition_variable __cv_; |
| 280 | size_t __count_; |
Marshall Clow | 5865536 | 2019-08-14 16:21:27 +0000 | [diff] [blame] | 281 | __thread_id __id_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 282 | public: |
Nikolas Klauser | 0708864 | 2021-12-08 10:57:12 +0100 | [diff] [blame] | 283 | recursive_timed_mutex(); |
| 284 | ~recursive_timed_mutex(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 285 | |
Nikolas Klauser | 0708864 | 2021-12-08 10:57:12 +0100 | [diff] [blame] | 286 | recursive_timed_mutex(const recursive_timed_mutex&) = delete; |
| 287 | recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 288 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 289 | void lock(); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 290 | bool try_lock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 291 | template <class _Rep, class _Period> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 292 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 293 | bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 294 | {return try_lock_until(chrono::steady_clock::now() + __d);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 295 | template <class _Clock, class _Duration> |
Shoaib Meenai | 55f3a46 | 2017-03-02 03:22:18 +0000 | [diff] [blame] | 296 | _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 297 | bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 298 | void unlock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
| 301 | template <class _Clock, class _Duration> |
| 302 | bool |
| 303 | recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) |
| 304 | { |
| 305 | using namespace chrono; |
Marshall Clow | 5865536 | 2019-08-14 16:21:27 +0000 | [diff] [blame] | 306 | __thread_id __id = this_thread::get_id(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 307 | unique_lock<mutex> lk(__m_); |
Marshall Clow | 5865536 | 2019-08-14 16:21:27 +0000 | [diff] [blame] | 308 | if (__id == __id_) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 309 | { |
| 310 | if (__count_ == numeric_limits<size_t>::max()) |
| 311 | return false; |
| 312 | ++__count_; |
| 313 | return true; |
| 314 | } |
| 315 | bool no_timeout = _Clock::now() < __t; |
| 316 | while (no_timeout && __count_ != 0) |
| 317 | no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout; |
| 318 | if (__count_ == 0) |
| 319 | { |
| 320 | __count_ = 1; |
| 321 | __id_ = __id; |
| 322 | return true; |
| 323 | } |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | template <class _L0, class _L1> |
| 328 | int |
| 329 | try_lock(_L0& __l0, _L1& __l1) |
| 330 | { |
| 331 | unique_lock<_L0> __u0(__l0, try_to_lock); |
| 332 | if (__u0.owns_lock()) |
| 333 | { |
| 334 | if (__l1.try_lock()) |
| 335 | { |
| 336 | __u0.release(); |
| 337 | return -1; |
| 338 | } |
| 339 | else |
| 340 | return 1; |
| 341 | } |
| 342 | return 0; |
| 343 | } |
| 344 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 345 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 346 | |
| 347 | template <class _L0, class _L1, class _L2, class... _L3> |
| 348 | int |
| 349 | try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) |
| 350 | { |
| 351 | int __r = 0; |
| 352 | unique_lock<_L0> __u0(__l0, try_to_lock); |
| 353 | if (__u0.owns_lock()) |
| 354 | { |
| 355 | __r = try_lock(__l1, __l2, __l3...); |
| 356 | if (__r == -1) |
| 357 | __u0.release(); |
| 358 | else |
| 359 | ++__r; |
| 360 | } |
| 361 | return __r; |
| 362 | } |
| 363 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 364 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 365 | |
| 366 | template <class _L0, class _L1> |
| 367 | void |
| 368 | lock(_L0& __l0, _L1& __l1) |
| 369 | { |
| 370 | while (true) |
| 371 | { |
| 372 | { |
| 373 | unique_lock<_L0> __u0(__l0); |
| 374 | if (__l1.try_lock()) |
| 375 | { |
| 376 | __u0.release(); |
| 377 | break; |
| 378 | } |
| 379 | } |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 380 | __libcpp_thread_yield(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 381 | { |
| 382 | unique_lock<_L1> __u1(__l1); |
| 383 | if (__l0.try_lock()) |
| 384 | { |
| 385 | __u1.release(); |
| 386 | break; |
| 387 | } |
| 388 | } |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 389 | __libcpp_thread_yield(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 393 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 394 | |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 395 | template <class _L0, class _L1, class _L2, class ..._L3> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 396 | void |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 397 | __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 398 | { |
| 399 | while (true) |
| 400 | { |
| 401 | switch (__i) |
| 402 | { |
| 403 | case 0: |
| 404 | { |
| 405 | unique_lock<_L0> __u0(__l0); |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 406 | __i = try_lock(__l1, __l2, __l3...); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 407 | if (__i == -1) |
| 408 | { |
| 409 | __u0.release(); |
| 410 | return; |
| 411 | } |
| 412 | } |
| 413 | ++__i; |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 414 | __libcpp_thread_yield(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 415 | break; |
| 416 | case 1: |
| 417 | { |
| 418 | unique_lock<_L1> __u1(__l1); |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 419 | __i = try_lock(__l2, __l3..., __l0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 420 | if (__i == -1) |
| 421 | { |
| 422 | __u1.release(); |
| 423 | return; |
| 424 | } |
| 425 | } |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 426 | if (__i == sizeof...(_L3) + 1) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 427 | __i = 0; |
| 428 | else |
| 429 | __i += 2; |
Asiri Rathnayake | fa2e203 | 2016-05-06 14:06:29 +0000 | [diff] [blame] | 430 | __libcpp_thread_yield(); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 431 | break; |
| 432 | default: |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 433 | __lock_first(__i - 2, __l2, __l3..., __l0, __l1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 439 | template <class _L0, class _L1, class _L2, class ..._L3> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 440 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 441 | void |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 442 | lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 443 | { |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 444 | __lock_first(0, __l0, __l1, __l2, __l3...); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Eric Fiselier | 68143d4 | 2016-06-14 03:48:09 +0000 | [diff] [blame] | 447 | template <class _L0> |
| 448 | inline _LIBCPP_INLINE_VISIBILITY |
| 449 | void __unlock(_L0& __l0) { |
| 450 | __l0.unlock(); |
| 451 | } |
| 452 | |
| 453 | template <class _L0, class _L1> |
| 454 | inline _LIBCPP_INLINE_VISIBILITY |
| 455 | void __unlock(_L0& __l0, _L1& __l1) { |
| 456 | __l0.unlock(); |
| 457 | __l1.unlock(); |
| 458 | } |
| 459 | |
| 460 | template <class _L0, class _L1, class _L2, class ..._L3> |
| 461 | inline _LIBCPP_INLINE_VISIBILITY |
| 462 | void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) { |
| 463 | __l0.unlock(); |
| 464 | __l1.unlock(); |
| 465 | _VSTD::__unlock(__l2, __l3...); |
| 466 | } |
| 467 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 468 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 469 | |
Marshall Clow | 2a7ae6f | 2017-03-24 05:19:15 +0000 | [diff] [blame] | 470 | #if _LIBCPP_STD_VER > 14 |
| 471 | template <class ..._Mutexes> |
| 472 | class _LIBCPP_TEMPLATE_VIS scoped_lock; |
| 473 | |
| 474 | template <> |
| 475 | class _LIBCPP_TEMPLATE_VIS scoped_lock<> { |
| 476 | public: |
| 477 | explicit scoped_lock() {} |
| 478 | ~scoped_lock() = default; |
| 479 | |
| 480 | _LIBCPP_INLINE_VISIBILITY |
| 481 | explicit scoped_lock(adopt_lock_t) {} |
| 482 | |
| 483 | scoped_lock(scoped_lock const&) = delete; |
| 484 | scoped_lock& operator=(scoped_lock const&) = delete; |
| 485 | }; |
| 486 | |
| 487 | template <class _Mutex> |
Aaron Puchert | e1560fb | 2018-10-09 23:42:29 +0000 | [diff] [blame] | 488 | 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] | 489 | public: |
| 490 | typedef _Mutex mutex_type; |
| 491 | private: |
| 492 | mutex_type& __m_; |
| 493 | public: |
| 494 | explicit scoped_lock(mutex_type & __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m)) |
| 495 | : __m_(__m) {__m_.lock();} |
| 496 | |
| 497 | ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();} |
| 498 | |
| 499 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | f68c692 | 2017-07-27 17:44:03 +0000 | [diff] [blame] | 500 | 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] | 501 | : __m_(__m) {} |
Marshall Clow | 2a7ae6f | 2017-03-24 05:19:15 +0000 | [diff] [blame] | 502 | |
| 503 | scoped_lock(scoped_lock const&) = delete; |
| 504 | scoped_lock& operator=(scoped_lock const&) = delete; |
| 505 | }; |
| 506 | |
| 507 | template <class ..._MArgs> |
| 508 | class _LIBCPP_TEMPLATE_VIS scoped_lock |
| 509 | { |
| 510 | static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required"); |
| 511 | typedef tuple<_MArgs&...> _MutexTuple; |
| 512 | |
| 513 | public: |
| 514 | _LIBCPP_INLINE_VISIBILITY |
| 515 | explicit scoped_lock(_MArgs&... __margs) |
| 516 | : __t_(__margs...) |
| 517 | { |
| 518 | _VSTD::lock(__margs...); |
| 519 | } |
| 520 | |
| 521 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | f68c692 | 2017-07-27 17:44:03 +0000 | [diff] [blame] | 522 | scoped_lock(adopt_lock_t, _MArgs&... __margs) |
Marshall Clow | 2a7ae6f | 2017-03-24 05:19:15 +0000 | [diff] [blame] | 523 | : __t_(__margs...) |
| 524 | { |
| 525 | } |
| 526 | |
| 527 | _LIBCPP_INLINE_VISIBILITY |
| 528 | ~scoped_lock() { |
| 529 | typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices; |
| 530 | __unlock_unpack(_Indices{}, __t_); |
| 531 | } |
| 532 | |
| 533 | scoped_lock(scoped_lock const&) = delete; |
| 534 | scoped_lock& operator=(scoped_lock const&) = delete; |
| 535 | |
| 536 | private: |
| 537 | template <size_t ..._Indx> |
| 538 | _LIBCPP_INLINE_VISIBILITY |
| 539 | static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) { |
| 540 | _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...); |
| 541 | } |
| 542 | |
| 543 | _MutexTuple __t_; |
| 544 | }; |
| 545 | |
| 546 | #endif // _LIBCPP_STD_VER > 14 |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 547 | #endif // !_LIBCPP_HAS_NO_THREADS |
| 548 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 549 | struct _LIBCPP_TEMPLATE_VIS once_flag; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 550 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 551 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 552 | |
| 553 | template<class _Callable, class... _Args> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 554 | _LIBCPP_INLINE_VISIBILITY |
| 555 | void call_once(once_flag&, _Callable&&, _Args&&...); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 556 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 557 | #else // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 558 | |
| 559 | template<class _Callable> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 560 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 561 | void call_once(once_flag&, _Callable&); |
| 562 | |
| 563 | template<class _Callable> |
| 564 | _LIBCPP_INLINE_VISIBILITY |
| 565 | void call_once(once_flag&, const _Callable&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 566 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 567 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 568 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 569 | struct _LIBCPP_TEMPLATE_VIS once_flag |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 570 | { |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 571 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 572 | _LIBCPP_CONSTEXPR |
| 573 | once_flag() _NOEXCEPT : __state_(0) {} |
Nikolas Klauser | 0708864 | 2021-12-08 10:57:12 +0100 | [diff] [blame] | 574 | once_flag(const once_flag&) = delete; |
| 575 | once_flag& operator=(const once_flag&) = delete; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 576 | |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 577 | #if defined(_LIBCPP_ABI_MICROSOFT) |
| 578 | typedef uintptr_t _State_type; |
| 579 | #else |
| 580 | typedef unsigned long _State_type; |
| 581 | #endif |
| 582 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 583 | private: |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 584 | _State_type __state_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 585 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 586 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 587 | template<class _Callable, class... _Args> |
| 588 | friend |
| 589 | void call_once(once_flag&, _Callable&&, _Args&&...); |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 590 | #else // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 591 | template<class _Callable> |
| 592 | friend |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 593 | void call_once(once_flag&, _Callable&); |
| 594 | |
| 595 | template<class _Callable> |
| 596 | friend |
| 597 | void call_once(once_flag&, const _Callable&); |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 598 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 599 | }; |
| 600 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 601 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 602 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 603 | template <class _Fp> |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 604 | class __call_once_param |
| 605 | { |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 606 | _Fp& __f_; |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 607 | public: |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 608 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 609 | explicit __call_once_param(_Fp& __f) : __f_(__f) {} |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 610 | |
| 611 | _LIBCPP_INLINE_VISIBILITY |
| 612 | void operator()() |
| 613 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 614 | typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index; |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 615 | __execute(_Index()); |
| 616 | } |
| 617 | |
| 618 | private: |
| 619 | template <size_t ..._Indices> |
| 620 | _LIBCPP_INLINE_VISIBILITY |
| 621 | void __execute(__tuple_indices<_Indices...>) |
| 622 | { |
Arthur O'Dwyer | 34465da | 2020-12-15 19:32:29 -0500 | [diff] [blame] | 623 | _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] | 624 | } |
| 625 | }; |
| 626 | |
| 627 | #else |
| 628 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 629 | template <class _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 630 | class __call_once_param |
| 631 | { |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 632 | _Fp& __f_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 633 | public: |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 634 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 635 | explicit __call_once_param(_Fp& __f) : __f_(__f) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 636 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 637 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 638 | void operator()() |
| 639 | { |
| 640 | __f_(); |
| 641 | } |
| 642 | }; |
| 643 | |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 644 | #endif |
| 645 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 646 | template <class _Fp> |
Louis Dionne | 530e240 | 2019-11-11 10:21:57 -0500 | [diff] [blame] | 647 | void _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 648 | __call_once_proxy(void* __vp) |
| 649 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 650 | __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 651 | (*__p)(); |
| 652 | } |
| 653 | |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 654 | _LIBCPP_FUNC_VIS void __call_once(volatile once_flag::_State_type&, void*, |
| 655 | void (*)(void*)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 656 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 657 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 658 | |
| 659 | template<class _Callable, class... _Args> |
| 660 | inline _LIBCPP_INLINE_VISIBILITY |
| 661 | void |
| 662 | call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args) |
| 663 | { |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 664 | if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 665 | { |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 666 | typedef tuple<_Callable&&, _Args&&...> _Gp; |
| 667 | _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...); |
| 668 | __call_once_param<_Gp> __p(__f); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 669 | __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
Eric Fiselier | b615975 | 2017-04-18 23:05:08 +0000 | [diff] [blame] | 673 | #else // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 674 | |
| 675 | template<class _Callable> |
| 676 | inline _LIBCPP_INLINE_VISIBILITY |
| 677 | void |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 678 | call_once(once_flag& __flag, _Callable& __func) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 679 | { |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 680 | if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0)) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 681 | { |
| 682 | __call_once_param<_Callable> __p(__func); |
| 683 | __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>); |
| 684 | } |
| 685 | } |
| 686 | |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 687 | template<class _Callable> |
| 688 | inline _LIBCPP_INLINE_VISIBILITY |
| 689 | void |
| 690 | call_once(once_flag& __flag, const _Callable& __func) |
| 691 | { |
Nico Weber | 03776e7 | 2019-03-20 22:55:03 +0000 | [diff] [blame] | 692 | if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0)) |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 693 | { |
| 694 | __call_once_param<const _Callable> __p(__func); |
| 695 | __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>); |
| 696 | } |
| 697 | } |
| 698 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 699 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 700 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 701 | _LIBCPP_END_NAMESPACE_STD |
| 702 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 703 | _LIBCPP_POP_MACROS |
| 704 | |
Louis Dionne | 2b1ceaa | 2021-04-20 12:03:32 -0400 | [diff] [blame] | 705 | #endif // _LIBCPP_MUTEX |