Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- mutex ------------------------------------===// |
| 3 | // |
Howard Hinnant | c566dc3 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | ee11c31 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_MUTEX |
| 12 | #define _LIBCPP_MUTEX |
| 13 | |
| 14 | /* |
| 15 | mutex synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
| 19 | |
| 20 | class mutex |
| 21 | { |
| 22 | public: |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 23 | constexpr mutex() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 24 | ~mutex(); |
| 25 | |
| 26 | mutex(const mutex&) = delete; |
| 27 | mutex& operator=(const mutex&) = delete; |
| 28 | |
| 29 | void lock(); |
| 30 | bool try_lock(); |
| 31 | void unlock(); |
| 32 | |
| 33 | typedef pthread_mutex_t* native_handle_type; |
| 34 | native_handle_type native_handle(); |
| 35 | }; |
| 36 | |
| 37 | class recursive_mutex |
| 38 | { |
| 39 | public: |
| 40 | recursive_mutex(); |
| 41 | ~recursive_mutex(); |
| 42 | |
| 43 | recursive_mutex(const recursive_mutex&) = delete; |
| 44 | recursive_mutex& operator=(const recursive_mutex&) = delete; |
| 45 | |
| 46 | void lock(); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 47 | bool try_lock() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 48 | void unlock(); |
| 49 | |
| 50 | typedef pthread_mutex_t* native_handle_type; |
| 51 | native_handle_type native_handle(); |
| 52 | }; |
| 53 | |
| 54 | class timed_mutex |
| 55 | { |
| 56 | public: |
| 57 | timed_mutex(); |
| 58 | ~timed_mutex(); |
| 59 | |
| 60 | timed_mutex(const timed_mutex&) = delete; |
| 61 | timed_mutex& operator=(const timed_mutex&) = delete; |
| 62 | |
| 63 | void lock(); |
| 64 | bool try_lock(); |
| 65 | template <class Rep, class Period> |
| 66 | bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); |
| 67 | template <class Clock, class Duration> |
| 68 | bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); |
| 69 | void unlock(); |
| 70 | }; |
| 71 | |
| 72 | class recursive_timed_mutex |
| 73 | { |
| 74 | public: |
| 75 | recursive_timed_mutex(); |
| 76 | ~recursive_timed_mutex(); |
| 77 | |
| 78 | recursive_timed_mutex(const recursive_timed_mutex&) = delete; |
| 79 | recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete; |
| 80 | |
| 81 | void lock(); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 82 | bool try_lock() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 83 | template <class Rep, class Period> |
| 84 | bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); |
| 85 | template <class Clock, class Duration> |
| 86 | bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); |
| 87 | void unlock(); |
| 88 | }; |
| 89 | |
| 90 | struct defer_lock_t {}; |
| 91 | struct try_to_lock_t {}; |
| 92 | struct adopt_lock_t {}; |
| 93 | |
| 94 | constexpr defer_lock_t defer_lock{}; |
| 95 | constexpr try_to_lock_t try_to_lock{}; |
| 96 | constexpr adopt_lock_t adopt_lock{}; |
| 97 | |
| 98 | template <class Mutex> |
| 99 | class lock_guard |
| 100 | { |
| 101 | public: |
| 102 | typedef Mutex mutex_type; |
| 103 | |
| 104 | explicit lock_guard(mutex_type& m); |
| 105 | lock_guard(mutex_type& m, adopt_lock_t); |
| 106 | ~lock_guard(); |
| 107 | |
| 108 | lock_guard(lock_guard const&) = delete; |
| 109 | lock_guard& operator=(lock_guard const&) = delete; |
| 110 | }; |
| 111 | |
| 112 | template <class Mutex> |
| 113 | class unique_lock |
| 114 | { |
| 115 | public: |
| 116 | typedef Mutex mutex_type; |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 117 | unique_lock() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 118 | explicit unique_lock(mutex_type& m); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 119 | unique_lock(mutex_type& m, defer_lock_t) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 120 | unique_lock(mutex_type& m, try_to_lock_t); |
| 121 | unique_lock(mutex_type& m, adopt_lock_t); |
| 122 | template <class Clock, class Duration> |
| 123 | unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time); |
| 124 | template <class Rep, class Period> |
| 125 | unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time); |
| 126 | ~unique_lock(); |
| 127 | |
| 128 | unique_lock(unique_lock const&) = delete; |
| 129 | unique_lock& operator=(unique_lock const&) = delete; |
| 130 | |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 131 | unique_lock(unique_lock&& u) noexcept; |
| 132 | unique_lock& operator=(unique_lock&& u) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 133 | |
| 134 | void lock(); |
| 135 | bool try_lock(); |
| 136 | |
| 137 | template <class Rep, class Period> |
| 138 | bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); |
| 139 | template <class Clock, class Duration> |
| 140 | bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); |
| 141 | |
| 142 | void unlock(); |
| 143 | |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 144 | void swap(unique_lock& u) noexcept; |
| 145 | mutex_type* release() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 146 | |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 147 | bool owns_lock() const noexcept; |
| 148 | explicit operator bool () const noexcept; |
| 149 | mutex_type* mutex() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | template <class Mutex> |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 153 | void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 154 | |
| 155 | template <class L1, class L2, class... L3> |
| 156 | int try_lock(L1&, L2&, L3&...); |
| 157 | template <class L1, class L2, class... L3> |
| 158 | void lock(L1&, L2&, L3&...); |
| 159 | |
| 160 | struct once_flag |
| 161 | { |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 162 | constexpr once_flag() noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 163 | |
| 164 | once_flag(const once_flag&) = delete; |
| 165 | once_flag& operator=(const once_flag&) = delete; |
| 166 | }; |
| 167 | |
| 168 | template<class Callable, class ...Args> |
| 169 | void call_once(once_flag& flag, Callable&& func, Args&&... args); |
| 170 | |
| 171 | } // std |
| 172 | |
| 173 | */ |
| 174 | |
| 175 | #include <__config> |
| 176 | #include <__mutex_base> |
| 177 | #include <functional> |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 178 | #include <memory> |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 179 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 180 | #include <tuple> |
| 181 | #endif |
Jonathan Roelofs | 72b390d | 2015-08-27 17:47:34 +0000 | [diff] [blame] | 182 | #ifndef _LIBCPP_HAS_NO_THREADS |
Sergey Dmitrouk | 7dfca3d | 2014-12-08 14:50:21 +0000 | [diff] [blame] | 183 | #include <sched.h> |
Jonathan Roelofs | 72b390d | 2015-08-27 17:47:34 +0000 | [diff] [blame] | 184 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 185 | |
Howard Hinnant | c5a5fbd | 2011-11-29 16:45:27 +0000 | [diff] [blame] | 186 | #include <__undef_min_max> |
| 187 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 188 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 189 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 190 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 191 | |
| 192 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 193 | |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 194 | #ifndef _LIBCPP_HAS_NO_THREADS |
| 195 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 196 | class _LIBCPP_TYPE_VIS recursive_mutex |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 197 | { |
| 198 | pthread_mutex_t __m_; |
| 199 | |
| 200 | public: |
| 201 | recursive_mutex(); |
| 202 | ~recursive_mutex(); |
| 203 | |
| 204 | private: |
| 205 | recursive_mutex(const recursive_mutex&); // = delete; |
| 206 | recursive_mutex& operator=(const recursive_mutex&); // = delete; |
| 207 | |
| 208 | public: |
| 209 | void lock(); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 210 | bool try_lock() _NOEXCEPT; |
| 211 | void unlock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 212 | |
| 213 | typedef pthread_mutex_t* native_handle_type; |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 214 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 215 | native_handle_type native_handle() {return &__m_;} |
| 216 | }; |
| 217 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 218 | class _LIBCPP_TYPE_VIS timed_mutex |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 219 | { |
| 220 | mutex __m_; |
| 221 | condition_variable __cv_; |
| 222 | bool __locked_; |
| 223 | public: |
| 224 | timed_mutex(); |
| 225 | ~timed_mutex(); |
| 226 | |
| 227 | private: |
| 228 | timed_mutex(const timed_mutex&); // = delete; |
| 229 | timed_mutex& operator=(const timed_mutex&); // = delete; |
| 230 | |
| 231 | public: |
| 232 | void lock(); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 233 | bool try_lock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 234 | template <class _Rep, class _Period> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 235 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 236 | bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 237 | {return try_lock_until(chrono::steady_clock::now() + __d);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 238 | template <class _Clock, class _Duration> |
| 239 | bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 240 | void unlock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | template <class _Clock, class _Duration> |
| 244 | bool |
| 245 | timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) |
| 246 | { |
| 247 | using namespace chrono; |
| 248 | unique_lock<mutex> __lk(__m_); |
| 249 | bool no_timeout = _Clock::now() < __t; |
| 250 | while (no_timeout && __locked_) |
| 251 | no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout; |
| 252 | if (!__locked_) |
| 253 | { |
| 254 | __locked_ = true; |
| 255 | return true; |
| 256 | } |
| 257 | return false; |
| 258 | } |
| 259 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 260 | class _LIBCPP_TYPE_VIS recursive_timed_mutex |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 261 | { |
| 262 | mutex __m_; |
| 263 | condition_variable __cv_; |
| 264 | size_t __count_; |
| 265 | pthread_t __id_; |
| 266 | public: |
| 267 | recursive_timed_mutex(); |
| 268 | ~recursive_timed_mutex(); |
| 269 | |
| 270 | private: |
| 271 | recursive_timed_mutex(const recursive_timed_mutex&); // = delete; |
| 272 | recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete; |
| 273 | |
| 274 | public: |
| 275 | void lock(); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 276 | bool try_lock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 277 | template <class _Rep, class _Period> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 278 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 279 | bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) |
Howard Hinnant | c8dbd22 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 280 | {return try_lock_until(chrono::steady_clock::now() + __d);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 281 | template <class _Clock, class _Duration> |
| 282 | bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 283 | void unlock() _NOEXCEPT; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 284 | }; |
| 285 | |
| 286 | template <class _Clock, class _Duration> |
| 287 | bool |
| 288 | recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) |
| 289 | { |
| 290 | using namespace chrono; |
| 291 | pthread_t __id = pthread_self(); |
| 292 | unique_lock<mutex> lk(__m_); |
| 293 | if (pthread_equal(__id, __id_)) |
| 294 | { |
| 295 | if (__count_ == numeric_limits<size_t>::max()) |
| 296 | return false; |
| 297 | ++__count_; |
| 298 | return true; |
| 299 | } |
| 300 | bool no_timeout = _Clock::now() < __t; |
| 301 | while (no_timeout && __count_ != 0) |
| 302 | no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout; |
| 303 | if (__count_ == 0) |
| 304 | { |
| 305 | __count_ = 1; |
| 306 | __id_ = __id; |
| 307 | return true; |
| 308 | } |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | template <class _L0, class _L1> |
| 313 | int |
| 314 | try_lock(_L0& __l0, _L1& __l1) |
| 315 | { |
| 316 | unique_lock<_L0> __u0(__l0, try_to_lock); |
| 317 | if (__u0.owns_lock()) |
| 318 | { |
| 319 | if (__l1.try_lock()) |
| 320 | { |
| 321 | __u0.release(); |
| 322 | return -1; |
| 323 | } |
| 324 | else |
| 325 | return 1; |
| 326 | } |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 331 | |
| 332 | template <class _L0, class _L1, class _L2, class... _L3> |
| 333 | int |
| 334 | try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) |
| 335 | { |
| 336 | int __r = 0; |
| 337 | unique_lock<_L0> __u0(__l0, try_to_lock); |
| 338 | if (__u0.owns_lock()) |
| 339 | { |
| 340 | __r = try_lock(__l1, __l2, __l3...); |
| 341 | if (__r == -1) |
| 342 | __u0.release(); |
| 343 | else |
| 344 | ++__r; |
| 345 | } |
| 346 | return __r; |
| 347 | } |
| 348 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 349 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 350 | |
| 351 | template <class _L0, class _L1> |
| 352 | void |
| 353 | lock(_L0& __l0, _L1& __l1) |
| 354 | { |
| 355 | while (true) |
| 356 | { |
| 357 | { |
| 358 | unique_lock<_L0> __u0(__l0); |
| 359 | if (__l1.try_lock()) |
| 360 | { |
| 361 | __u0.release(); |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | sched_yield(); |
| 366 | { |
| 367 | unique_lock<_L1> __u1(__l1); |
| 368 | if (__l0.try_lock()) |
| 369 | { |
| 370 | __u1.release(); |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | sched_yield(); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 379 | |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 380 | template <class _L0, class _L1, class _L2, class ..._L3> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 381 | void |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 382 | __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 383 | { |
| 384 | while (true) |
| 385 | { |
| 386 | switch (__i) |
| 387 | { |
| 388 | case 0: |
| 389 | { |
| 390 | unique_lock<_L0> __u0(__l0); |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 391 | __i = try_lock(__l1, __l2, __l3...); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 392 | if (__i == -1) |
| 393 | { |
| 394 | __u0.release(); |
| 395 | return; |
| 396 | } |
| 397 | } |
| 398 | ++__i; |
| 399 | sched_yield(); |
| 400 | break; |
| 401 | case 1: |
| 402 | { |
| 403 | unique_lock<_L1> __u1(__l1); |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 404 | __i = try_lock(__l2, __l3..., __l0); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 405 | if (__i == -1) |
| 406 | { |
| 407 | __u1.release(); |
| 408 | return; |
| 409 | } |
| 410 | } |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 411 | if (__i == sizeof...(_L3) + 1) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 412 | __i = 0; |
| 413 | else |
| 414 | __i += 2; |
| 415 | sched_yield(); |
| 416 | break; |
| 417 | default: |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 418 | __lock_first(__i - 2, __l2, __l3..., __l0, __l1); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 424 | template <class _L0, class _L1, class _L2, class ..._L3> |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 425 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 426 | void |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 427 | lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 428 | { |
Howard Hinnant | f848532 | 2011-01-12 22:56:59 +0000 | [diff] [blame] | 429 | __lock_first(0, __l0, __l1, __l2, __l3...); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 432 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 433 | |
Jonathan Roelofs | 39cb6bf | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 434 | #endif // !_LIBCPP_HAS_NO_THREADS |
| 435 | |
Marshall Clow | 7ed4234 | 2014-07-29 21:05:31 +0000 | [diff] [blame] | 436 | struct _LIBCPP_TYPE_VIS_ONLY once_flag; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 437 | |
| 438 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 439 | |
| 440 | template<class _Callable, class... _Args> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 441 | _LIBCPP_INLINE_VISIBILITY |
| 442 | void call_once(once_flag&, _Callable&&, _Args&&...); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 443 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 444 | #else // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 445 | |
| 446 | template<class _Callable> |
Howard Hinnant | a54386e | 2012-09-14 00:39:16 +0000 | [diff] [blame] | 447 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 448 | void call_once(once_flag&, _Callable&); |
| 449 | |
| 450 | template<class _Callable> |
| 451 | _LIBCPP_INLINE_VISIBILITY |
| 452 | void call_once(once_flag&, const _Callable&); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 453 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 454 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 455 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 456 | struct _LIBCPP_TYPE_VIS_ONLY once_flag |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 457 | { |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 458 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bd05d6a | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 459 | _LIBCPP_CONSTEXPR |
| 460 | once_flag() _NOEXCEPT : __state_(0) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 461 | |
| 462 | private: |
| 463 | once_flag(const once_flag&); // = delete; |
| 464 | once_flag& operator=(const once_flag&); // = delete; |
| 465 | |
| 466 | unsigned long __state_; |
| 467 | |
| 468 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 469 | template<class _Callable, class... _Args> |
| 470 | friend |
| 471 | void call_once(once_flag&, _Callable&&, _Args&&...); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 472 | #else // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 473 | template<class _Callable> |
| 474 | friend |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 475 | void call_once(once_flag&, _Callable&); |
| 476 | |
| 477 | template<class _Callable> |
| 478 | friend |
| 479 | void call_once(once_flag&, const _Callable&); |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 480 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 481 | }; |
| 482 | |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 483 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 484 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 485 | template <class _Fp> |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 486 | class __call_once_param |
| 487 | { |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 488 | _Fp& __f_; |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 489 | public: |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 490 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 491 | explicit __call_once_param(_Fp& __f) : __f_(__f) {} |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 492 | |
| 493 | _LIBCPP_INLINE_VISIBILITY |
| 494 | void operator()() |
| 495 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 496 | typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index; |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 497 | __execute(_Index()); |
| 498 | } |
| 499 | |
| 500 | private: |
| 501 | template <size_t ..._Indices> |
| 502 | _LIBCPP_INLINE_VISIBILITY |
| 503 | void __execute(__tuple_indices<_Indices...>) |
| 504 | { |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 505 | __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] | 506 | } |
| 507 | }; |
| 508 | |
| 509 | #else |
| 510 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 511 | template <class _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 512 | class __call_once_param |
| 513 | { |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 514 | _Fp& __f_; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 515 | public: |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 516 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 517 | explicit __call_once_param(_Fp& __f) : __f_(__f) {} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 518 | |
Howard Hinnant | f5f9999 | 2010-09-22 18:02:38 +0000 | [diff] [blame] | 519 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 520 | void operator()() |
| 521 | { |
| 522 | __f_(); |
| 523 | } |
| 524 | }; |
| 525 | |
Howard Hinnant | 51d62e0 | 2011-05-16 19:05:11 +0000 | [diff] [blame] | 526 | #endif |
| 527 | |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 528 | template <class _Fp> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 529 | void |
| 530 | __call_once_proxy(void* __vp) |
| 531 | { |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 532 | __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 533 | (*__p)(); |
| 534 | } |
| 535 | |
Howard Hinnant | a37d3cf | 2013-08-12 18:38:34 +0000 | [diff] [blame] | 536 | _LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*)); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 537 | |
| 538 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
| 539 | |
| 540 | template<class _Callable, class... _Args> |
| 541 | inline _LIBCPP_INLINE_VISIBILITY |
| 542 | void |
| 543 | call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args) |
| 544 | { |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 545 | if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 546 | { |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 547 | typedef tuple<_Callable&&, _Args&&...> _Gp; |
| 548 | _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...); |
| 549 | __call_once_param<_Gp> __p(__f); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 550 | __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 554 | #else // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 555 | |
| 556 | template<class _Callable> |
| 557 | inline _LIBCPP_INLINE_VISIBILITY |
| 558 | void |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 559 | call_once(once_flag& __flag, _Callable& __func) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 560 | { |
Eric Fiselier | 89659d1 | 2015-07-07 00:27:16 +0000 | [diff] [blame] | 561 | if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 562 | { |
| 563 | __call_once_param<_Callable> __p(__func); |
| 564 | __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>); |
| 565 | } |
| 566 | } |
| 567 | |
Eric Fiselier | 1e88fdc | 2015-06-13 02:23:00 +0000 | [diff] [blame] | 568 | template<class _Callable> |
| 569 | inline _LIBCPP_INLINE_VISIBILITY |
| 570 | void |
| 571 | call_once(once_flag& __flag, const _Callable& __func) |
| 572 | { |
| 573 | if (__flag.__state_ != ~0ul) |
| 574 | { |
| 575 | __call_once_param<const _Callable> __p(__func); |
| 576 | __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>); |
| 577 | } |
| 578 | } |
| 579 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 580 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 581 | |
| 582 | _LIBCPP_END_NAMESPACE_STD |
| 583 | |
| 584 | #endif // _LIBCPP_MUTEX |