14684ddb6SLionel Sambuc// -*- C++ -*- 24684ddb6SLionel Sambuc//===--------------------------- mutex ------------------------------------===// 34684ddb6SLionel Sambuc// 44684ddb6SLionel Sambuc// The LLVM Compiler Infrastructure 54684ddb6SLionel Sambuc// 64684ddb6SLionel Sambuc// This file is dual licensed under the MIT and the University of Illinois Open 74684ddb6SLionel Sambuc// Source Licenses. See LICENSE.TXT for details. 84684ddb6SLionel Sambuc// 94684ddb6SLionel Sambuc//===----------------------------------------------------------------------===// 104684ddb6SLionel Sambuc 114684ddb6SLionel Sambuc#ifndef _LIBCPP_MUTEX 124684ddb6SLionel Sambuc#define _LIBCPP_MUTEX 134684ddb6SLionel Sambuc 144684ddb6SLionel Sambuc/* 154684ddb6SLionel Sambuc mutex synopsis 164684ddb6SLionel Sambuc 174684ddb6SLionel Sambucnamespace std 184684ddb6SLionel Sambuc{ 194684ddb6SLionel Sambuc 204684ddb6SLionel Sambucclass mutex 214684ddb6SLionel Sambuc{ 224684ddb6SLionel Sambucpublic: 234684ddb6SLionel Sambuc constexpr mutex() noexcept; 244684ddb6SLionel Sambuc ~mutex(); 254684ddb6SLionel Sambuc 264684ddb6SLionel Sambuc mutex(const mutex&) = delete; 274684ddb6SLionel Sambuc mutex& operator=(const mutex&) = delete; 284684ddb6SLionel Sambuc 294684ddb6SLionel Sambuc void lock(); 304684ddb6SLionel Sambuc bool try_lock(); 314684ddb6SLionel Sambuc void unlock(); 324684ddb6SLionel Sambuc 334684ddb6SLionel Sambuc typedef pthread_mutex_t* native_handle_type; 344684ddb6SLionel Sambuc native_handle_type native_handle(); 354684ddb6SLionel Sambuc}; 364684ddb6SLionel Sambuc 374684ddb6SLionel Sambucclass recursive_mutex 384684ddb6SLionel Sambuc{ 394684ddb6SLionel Sambucpublic: 404684ddb6SLionel Sambuc recursive_mutex(); 414684ddb6SLionel Sambuc ~recursive_mutex(); 424684ddb6SLionel Sambuc 434684ddb6SLionel Sambuc recursive_mutex(const recursive_mutex&) = delete; 444684ddb6SLionel Sambuc recursive_mutex& operator=(const recursive_mutex&) = delete; 454684ddb6SLionel Sambuc 464684ddb6SLionel Sambuc void lock(); 474684ddb6SLionel Sambuc bool try_lock() noexcept; 484684ddb6SLionel Sambuc void unlock(); 494684ddb6SLionel Sambuc 504684ddb6SLionel Sambuc typedef pthread_mutex_t* native_handle_type; 514684ddb6SLionel Sambuc native_handle_type native_handle(); 524684ddb6SLionel Sambuc}; 534684ddb6SLionel Sambuc 544684ddb6SLionel Sambucclass timed_mutex 554684ddb6SLionel Sambuc{ 564684ddb6SLionel Sambucpublic: 574684ddb6SLionel Sambuc timed_mutex(); 584684ddb6SLionel Sambuc ~timed_mutex(); 594684ddb6SLionel Sambuc 604684ddb6SLionel Sambuc timed_mutex(const timed_mutex&) = delete; 614684ddb6SLionel Sambuc timed_mutex& operator=(const timed_mutex&) = delete; 624684ddb6SLionel Sambuc 634684ddb6SLionel Sambuc void lock(); 644684ddb6SLionel Sambuc bool try_lock(); 654684ddb6SLionel Sambuc template <class Rep, class Period> 664684ddb6SLionel Sambuc bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); 674684ddb6SLionel Sambuc template <class Clock, class Duration> 684684ddb6SLionel Sambuc bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); 694684ddb6SLionel Sambuc void unlock(); 704684ddb6SLionel Sambuc}; 714684ddb6SLionel Sambuc 724684ddb6SLionel Sambucclass recursive_timed_mutex 734684ddb6SLionel Sambuc{ 744684ddb6SLionel Sambucpublic: 754684ddb6SLionel Sambuc recursive_timed_mutex(); 764684ddb6SLionel Sambuc ~recursive_timed_mutex(); 774684ddb6SLionel Sambuc 784684ddb6SLionel Sambuc recursive_timed_mutex(const recursive_timed_mutex&) = delete; 794684ddb6SLionel Sambuc recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete; 804684ddb6SLionel Sambuc 814684ddb6SLionel Sambuc void lock(); 824684ddb6SLionel Sambuc bool try_lock() noexcept; 834684ddb6SLionel Sambuc template <class Rep, class Period> 844684ddb6SLionel Sambuc bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); 854684ddb6SLionel Sambuc template <class Clock, class Duration> 864684ddb6SLionel Sambuc bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); 874684ddb6SLionel Sambuc void unlock(); 884684ddb6SLionel Sambuc}; 894684ddb6SLionel Sambuc 904684ddb6SLionel Sambucstruct defer_lock_t {}; 914684ddb6SLionel Sambucstruct try_to_lock_t {}; 924684ddb6SLionel Sambucstruct adopt_lock_t {}; 934684ddb6SLionel Sambuc 944684ddb6SLionel Sambucconstexpr defer_lock_t defer_lock{}; 954684ddb6SLionel Sambucconstexpr try_to_lock_t try_to_lock{}; 964684ddb6SLionel Sambucconstexpr adopt_lock_t adopt_lock{}; 974684ddb6SLionel Sambuc 984684ddb6SLionel Sambuctemplate <class Mutex> 994684ddb6SLionel Sambucclass lock_guard 1004684ddb6SLionel Sambuc{ 1014684ddb6SLionel Sambucpublic: 1024684ddb6SLionel Sambuc typedef Mutex mutex_type; 1034684ddb6SLionel Sambuc 1044684ddb6SLionel Sambuc explicit lock_guard(mutex_type& m); 1054684ddb6SLionel Sambuc lock_guard(mutex_type& m, adopt_lock_t); 1064684ddb6SLionel Sambuc ~lock_guard(); 1074684ddb6SLionel Sambuc 1084684ddb6SLionel Sambuc lock_guard(lock_guard const&) = delete; 1094684ddb6SLionel Sambuc lock_guard& operator=(lock_guard const&) = delete; 1104684ddb6SLionel Sambuc}; 1114684ddb6SLionel Sambuc 1124684ddb6SLionel Sambuctemplate <class Mutex> 1134684ddb6SLionel Sambucclass unique_lock 1144684ddb6SLionel Sambuc{ 1154684ddb6SLionel Sambucpublic: 1164684ddb6SLionel Sambuc typedef Mutex mutex_type; 1174684ddb6SLionel Sambuc unique_lock() noexcept; 1184684ddb6SLionel Sambuc explicit unique_lock(mutex_type& m); 1194684ddb6SLionel Sambuc unique_lock(mutex_type& m, defer_lock_t) noexcept; 1204684ddb6SLionel Sambuc unique_lock(mutex_type& m, try_to_lock_t); 1214684ddb6SLionel Sambuc unique_lock(mutex_type& m, adopt_lock_t); 1224684ddb6SLionel Sambuc template <class Clock, class Duration> 1234684ddb6SLionel Sambuc unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time); 1244684ddb6SLionel Sambuc template <class Rep, class Period> 1254684ddb6SLionel Sambuc unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time); 1264684ddb6SLionel Sambuc ~unique_lock(); 1274684ddb6SLionel Sambuc 1284684ddb6SLionel Sambuc unique_lock(unique_lock const&) = delete; 1294684ddb6SLionel Sambuc unique_lock& operator=(unique_lock const&) = delete; 1304684ddb6SLionel Sambuc 1314684ddb6SLionel Sambuc unique_lock(unique_lock&& u) noexcept; 1324684ddb6SLionel Sambuc unique_lock& operator=(unique_lock&& u) noexcept; 1334684ddb6SLionel Sambuc 1344684ddb6SLionel Sambuc void lock(); 1354684ddb6SLionel Sambuc bool try_lock(); 1364684ddb6SLionel Sambuc 1374684ddb6SLionel Sambuc template <class Rep, class Period> 1384684ddb6SLionel Sambuc bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); 1394684ddb6SLionel Sambuc template <class Clock, class Duration> 1404684ddb6SLionel Sambuc bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); 1414684ddb6SLionel Sambuc 1424684ddb6SLionel Sambuc void unlock(); 1434684ddb6SLionel Sambuc 1444684ddb6SLionel Sambuc void swap(unique_lock& u) noexcept; 1454684ddb6SLionel Sambuc mutex_type* release() noexcept; 1464684ddb6SLionel Sambuc 1474684ddb6SLionel Sambuc bool owns_lock() const noexcept; 1484684ddb6SLionel Sambuc explicit operator bool () const noexcept; 1494684ddb6SLionel Sambuc mutex_type* mutex() const noexcept; 1504684ddb6SLionel Sambuc}; 1514684ddb6SLionel Sambuc 1524684ddb6SLionel Sambuctemplate <class Mutex> 1534684ddb6SLionel Sambuc void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept; 1544684ddb6SLionel Sambuc 1554684ddb6SLionel Sambuctemplate <class L1, class L2, class... L3> 1564684ddb6SLionel Sambuc int try_lock(L1&, L2&, L3&...); 1574684ddb6SLionel Sambuctemplate <class L1, class L2, class... L3> 1584684ddb6SLionel Sambuc void lock(L1&, L2&, L3&...); 1594684ddb6SLionel Sambuc 1604684ddb6SLionel Sambucstruct once_flag 1614684ddb6SLionel Sambuc{ 1624684ddb6SLionel Sambuc constexpr once_flag() noexcept; 1634684ddb6SLionel Sambuc 1644684ddb6SLionel Sambuc once_flag(const once_flag&) = delete; 1654684ddb6SLionel Sambuc once_flag& operator=(const once_flag&) = delete; 1664684ddb6SLionel Sambuc}; 1674684ddb6SLionel Sambuc 1684684ddb6SLionel Sambuctemplate<class Callable, class ...Args> 1694684ddb6SLionel Sambuc void call_once(once_flag& flag, Callable&& func, Args&&... args); 1704684ddb6SLionel Sambuc 1714684ddb6SLionel Sambuc} // std 1724684ddb6SLionel Sambuc 1734684ddb6SLionel Sambuc*/ 1744684ddb6SLionel Sambuc 1754684ddb6SLionel Sambuc#include <__config> 176*0a6a1f1dSLionel Sambuc#if !defined(_LIBCPP_HAS_NO_THREADS) && defined(__minix) 1774684ddb6SLionel Sambuc#include <__mutex_base> 178*0a6a1f1dSLionel Sambuc#endif // !defined(_LIBCPP_HAS_NO_THREADS) && defined(__minix) 1794684ddb6SLionel Sambuc#include <functional> 180*0a6a1f1dSLionel Sambuc#include <memory> 1814684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_VARIADICS 1824684ddb6SLionel Sambuc#include <tuple> 1834684ddb6SLionel Sambuc#endif 184*0a6a1f1dSLionel Sambuc#include <sched.h> 1854684ddb6SLionel Sambuc 1864684ddb6SLionel Sambuc#include <__undef_min_max> 1874684ddb6SLionel Sambuc 1884684ddb6SLionel Sambuc#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 1894684ddb6SLionel Sambuc#pragma GCC system_header 1904684ddb6SLionel Sambuc#endif 1914684ddb6SLionel Sambuc 1924684ddb6SLionel Sambuc_LIBCPP_BEGIN_NAMESPACE_STD 1934684ddb6SLionel Sambuc 194*0a6a1f1dSLionel Sambuc#ifndef _LIBCPP_HAS_NO_THREADS 195*0a6a1f1dSLionel Sambuc 1964684ddb6SLionel Sambucclass _LIBCPP_TYPE_VIS recursive_mutex 1974684ddb6SLionel Sambuc{ 1984684ddb6SLionel Sambuc pthread_mutex_t __m_; 1994684ddb6SLionel Sambuc 2004684ddb6SLionel Sambucpublic: 2014684ddb6SLionel Sambuc recursive_mutex(); 2024684ddb6SLionel Sambuc ~recursive_mutex(); 2034684ddb6SLionel Sambuc 2044684ddb6SLionel Sambucprivate: 2054684ddb6SLionel Sambuc recursive_mutex(const recursive_mutex&); // = delete; 2064684ddb6SLionel Sambuc recursive_mutex& operator=(const recursive_mutex&); // = delete; 2074684ddb6SLionel Sambuc 2084684ddb6SLionel Sambucpublic: 2094684ddb6SLionel Sambuc void lock(); 2104684ddb6SLionel Sambuc bool try_lock() _NOEXCEPT; 2114684ddb6SLionel Sambuc void unlock() _NOEXCEPT; 2124684ddb6SLionel Sambuc 2134684ddb6SLionel Sambuc typedef pthread_mutex_t* native_handle_type; 2144684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 2154684ddb6SLionel Sambuc native_handle_type native_handle() {return &__m_;} 2164684ddb6SLionel Sambuc}; 2174684ddb6SLionel Sambuc 2184684ddb6SLionel Sambucclass _LIBCPP_TYPE_VIS timed_mutex 2194684ddb6SLionel Sambuc{ 2204684ddb6SLionel Sambuc mutex __m_; 2214684ddb6SLionel Sambuc condition_variable __cv_; 2224684ddb6SLionel Sambuc bool __locked_; 2234684ddb6SLionel Sambucpublic: 2244684ddb6SLionel Sambuc timed_mutex(); 2254684ddb6SLionel Sambuc ~timed_mutex(); 2264684ddb6SLionel Sambuc 2274684ddb6SLionel Sambucprivate: 2284684ddb6SLionel Sambuc timed_mutex(const timed_mutex&); // = delete; 2294684ddb6SLionel Sambuc timed_mutex& operator=(const timed_mutex&); // = delete; 2304684ddb6SLionel Sambuc 2314684ddb6SLionel Sambucpublic: 2324684ddb6SLionel Sambuc void lock(); 2334684ddb6SLionel Sambuc bool try_lock() _NOEXCEPT; 2344684ddb6SLionel Sambuc template <class _Rep, class _Period> 2354684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 2364684ddb6SLionel Sambuc bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) 2374684ddb6SLionel Sambuc {return try_lock_until(chrono::steady_clock::now() + __d);} 2384684ddb6SLionel Sambuc template <class _Clock, class _Duration> 2394684ddb6SLionel Sambuc bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); 2404684ddb6SLionel Sambuc void unlock() _NOEXCEPT; 2414684ddb6SLionel Sambuc}; 2424684ddb6SLionel Sambuc 2434684ddb6SLionel Sambuctemplate <class _Clock, class _Duration> 2444684ddb6SLionel Sambucbool 2454684ddb6SLionel Sambuctimed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) 2464684ddb6SLionel Sambuc{ 2474684ddb6SLionel Sambuc using namespace chrono; 2484684ddb6SLionel Sambuc unique_lock<mutex> __lk(__m_); 2494684ddb6SLionel Sambuc bool no_timeout = _Clock::now() < __t; 2504684ddb6SLionel Sambuc while (no_timeout && __locked_) 2514684ddb6SLionel Sambuc no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout; 2524684ddb6SLionel Sambuc if (!__locked_) 2534684ddb6SLionel Sambuc { 2544684ddb6SLionel Sambuc __locked_ = true; 2554684ddb6SLionel Sambuc return true; 2564684ddb6SLionel Sambuc } 2574684ddb6SLionel Sambuc return false; 2584684ddb6SLionel Sambuc} 2594684ddb6SLionel Sambuc 2604684ddb6SLionel Sambucclass _LIBCPP_TYPE_VIS recursive_timed_mutex 2614684ddb6SLionel Sambuc{ 2624684ddb6SLionel Sambuc mutex __m_; 2634684ddb6SLionel Sambuc condition_variable __cv_; 2644684ddb6SLionel Sambuc size_t __count_; 2654684ddb6SLionel Sambuc pthread_t __id_; 2664684ddb6SLionel Sambucpublic: 2674684ddb6SLionel Sambuc recursive_timed_mutex(); 2684684ddb6SLionel Sambuc ~recursive_timed_mutex(); 2694684ddb6SLionel Sambuc 2704684ddb6SLionel Sambucprivate: 2714684ddb6SLionel Sambuc recursive_timed_mutex(const recursive_timed_mutex&); // = delete; 2724684ddb6SLionel Sambuc recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete; 2734684ddb6SLionel Sambuc 2744684ddb6SLionel Sambucpublic: 2754684ddb6SLionel Sambuc void lock(); 2764684ddb6SLionel Sambuc bool try_lock() _NOEXCEPT; 2774684ddb6SLionel Sambuc template <class _Rep, class _Period> 2784684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 2794684ddb6SLionel Sambuc bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) 2804684ddb6SLionel Sambuc {return try_lock_until(chrono::steady_clock::now() + __d);} 2814684ddb6SLionel Sambuc template <class _Clock, class _Duration> 2824684ddb6SLionel Sambuc bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); 2834684ddb6SLionel Sambuc void unlock() _NOEXCEPT; 2844684ddb6SLionel Sambuc}; 2854684ddb6SLionel Sambuc 2864684ddb6SLionel Sambuctemplate <class _Clock, class _Duration> 2874684ddb6SLionel Sambucbool 2884684ddb6SLionel Sambucrecursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) 2894684ddb6SLionel Sambuc{ 2904684ddb6SLionel Sambuc using namespace chrono; 2914684ddb6SLionel Sambuc pthread_t __id = pthread_self(); 2924684ddb6SLionel Sambuc unique_lock<mutex> lk(__m_); 2934684ddb6SLionel Sambuc if (pthread_equal(__id, __id_)) 2944684ddb6SLionel Sambuc { 2954684ddb6SLionel Sambuc if (__count_ == numeric_limits<size_t>::max()) 2964684ddb6SLionel Sambuc return false; 2974684ddb6SLionel Sambuc ++__count_; 2984684ddb6SLionel Sambuc return true; 2994684ddb6SLionel Sambuc } 3004684ddb6SLionel Sambuc bool no_timeout = _Clock::now() < __t; 3014684ddb6SLionel Sambuc while (no_timeout && __count_ != 0) 3024684ddb6SLionel Sambuc no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout; 3034684ddb6SLionel Sambuc if (__count_ == 0) 3044684ddb6SLionel Sambuc { 3054684ddb6SLionel Sambuc __count_ = 1; 3064684ddb6SLionel Sambuc __id_ = __id; 3074684ddb6SLionel Sambuc return true; 3084684ddb6SLionel Sambuc } 3094684ddb6SLionel Sambuc return false; 3104684ddb6SLionel Sambuc} 3114684ddb6SLionel Sambuc 3124684ddb6SLionel Sambuctemplate <class _L0, class _L1> 3134684ddb6SLionel Sambucint 3144684ddb6SLionel Sambuctry_lock(_L0& __l0, _L1& __l1) 3154684ddb6SLionel Sambuc{ 3164684ddb6SLionel Sambuc unique_lock<_L0> __u0(__l0, try_to_lock); 3174684ddb6SLionel Sambuc if (__u0.owns_lock()) 3184684ddb6SLionel Sambuc { 3194684ddb6SLionel Sambuc if (__l1.try_lock()) 3204684ddb6SLionel Sambuc { 3214684ddb6SLionel Sambuc __u0.release(); 3224684ddb6SLionel Sambuc return -1; 3234684ddb6SLionel Sambuc } 3244684ddb6SLionel Sambuc else 3254684ddb6SLionel Sambuc return 1; 3264684ddb6SLionel Sambuc } 3274684ddb6SLionel Sambuc return 0; 3284684ddb6SLionel Sambuc} 3294684ddb6SLionel Sambuc 3304684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_VARIADICS 3314684ddb6SLionel Sambuc 3324684ddb6SLionel Sambuctemplate <class _L0, class _L1, class _L2, class... _L3> 3334684ddb6SLionel Sambucint 3344684ddb6SLionel Sambuctry_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) 3354684ddb6SLionel Sambuc{ 3364684ddb6SLionel Sambuc int __r = 0; 3374684ddb6SLionel Sambuc unique_lock<_L0> __u0(__l0, try_to_lock); 3384684ddb6SLionel Sambuc if (__u0.owns_lock()) 3394684ddb6SLionel Sambuc { 3404684ddb6SLionel Sambuc __r = try_lock(__l1, __l2, __l3...); 3414684ddb6SLionel Sambuc if (__r == -1) 3424684ddb6SLionel Sambuc __u0.release(); 3434684ddb6SLionel Sambuc else 3444684ddb6SLionel Sambuc ++__r; 3454684ddb6SLionel Sambuc } 3464684ddb6SLionel Sambuc return __r; 3474684ddb6SLionel Sambuc} 3484684ddb6SLionel Sambuc 3494684ddb6SLionel Sambuc#endif // _LIBCPP_HAS_NO_VARIADICS 3504684ddb6SLionel Sambuc 3514684ddb6SLionel Sambuctemplate <class _L0, class _L1> 3524684ddb6SLionel Sambucvoid 3534684ddb6SLionel Sambuclock(_L0& __l0, _L1& __l1) 3544684ddb6SLionel Sambuc{ 3554684ddb6SLionel Sambuc while (true) 3564684ddb6SLionel Sambuc { 3574684ddb6SLionel Sambuc { 3584684ddb6SLionel Sambuc unique_lock<_L0> __u0(__l0); 3594684ddb6SLionel Sambuc if (__l1.try_lock()) 3604684ddb6SLionel Sambuc { 3614684ddb6SLionel Sambuc __u0.release(); 3624684ddb6SLionel Sambuc break; 3634684ddb6SLionel Sambuc } 3644684ddb6SLionel Sambuc } 3654684ddb6SLionel Sambuc sched_yield(); 3664684ddb6SLionel Sambuc { 3674684ddb6SLionel Sambuc unique_lock<_L1> __u1(__l1); 3684684ddb6SLionel Sambuc if (__l0.try_lock()) 3694684ddb6SLionel Sambuc { 3704684ddb6SLionel Sambuc __u1.release(); 3714684ddb6SLionel Sambuc break; 3724684ddb6SLionel Sambuc } 3734684ddb6SLionel Sambuc } 3744684ddb6SLionel Sambuc sched_yield(); 3754684ddb6SLionel Sambuc } 3764684ddb6SLionel Sambuc} 3774684ddb6SLionel Sambuc 3784684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_VARIADICS 3794684ddb6SLionel Sambuc 3804684ddb6SLionel Sambuctemplate <class _L0, class _L1, class _L2, class ..._L3> 3814684ddb6SLionel Sambucvoid 3824684ddb6SLionel Sambuc__lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3) 3834684ddb6SLionel Sambuc{ 3844684ddb6SLionel Sambuc while (true) 3854684ddb6SLionel Sambuc { 3864684ddb6SLionel Sambuc switch (__i) 3874684ddb6SLionel Sambuc { 3884684ddb6SLionel Sambuc case 0: 3894684ddb6SLionel Sambuc { 3904684ddb6SLionel Sambuc unique_lock<_L0> __u0(__l0); 3914684ddb6SLionel Sambuc __i = try_lock(__l1, __l2, __l3...); 3924684ddb6SLionel Sambuc if (__i == -1) 3934684ddb6SLionel Sambuc { 3944684ddb6SLionel Sambuc __u0.release(); 3954684ddb6SLionel Sambuc return; 3964684ddb6SLionel Sambuc } 3974684ddb6SLionel Sambuc } 3984684ddb6SLionel Sambuc ++__i; 3994684ddb6SLionel Sambuc sched_yield(); 4004684ddb6SLionel Sambuc break; 4014684ddb6SLionel Sambuc case 1: 4024684ddb6SLionel Sambuc { 4034684ddb6SLionel Sambuc unique_lock<_L1> __u1(__l1); 4044684ddb6SLionel Sambuc __i = try_lock(__l2, __l3..., __l0); 4054684ddb6SLionel Sambuc if (__i == -1) 4064684ddb6SLionel Sambuc { 4074684ddb6SLionel Sambuc __u1.release(); 4084684ddb6SLionel Sambuc return; 4094684ddb6SLionel Sambuc } 4104684ddb6SLionel Sambuc } 4114684ddb6SLionel Sambuc if (__i == sizeof...(_L3) + 1) 4124684ddb6SLionel Sambuc __i = 0; 4134684ddb6SLionel Sambuc else 4144684ddb6SLionel Sambuc __i += 2; 4154684ddb6SLionel Sambuc sched_yield(); 4164684ddb6SLionel Sambuc break; 4174684ddb6SLionel Sambuc default: 4184684ddb6SLionel Sambuc __lock_first(__i - 2, __l2, __l3..., __l0, __l1); 4194684ddb6SLionel Sambuc return; 4204684ddb6SLionel Sambuc } 4214684ddb6SLionel Sambuc } 4224684ddb6SLionel Sambuc} 4234684ddb6SLionel Sambuc 4244684ddb6SLionel Sambuctemplate <class _L0, class _L1, class _L2, class ..._L3> 4254684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY 4264684ddb6SLionel Sambucvoid 4274684ddb6SLionel Sambuclock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3) 4284684ddb6SLionel Sambuc{ 4294684ddb6SLionel Sambuc __lock_first(0, __l0, __l1, __l2, __l3...); 4304684ddb6SLionel Sambuc} 4314684ddb6SLionel Sambuc 4324684ddb6SLionel Sambuc#endif // _LIBCPP_HAS_NO_VARIADICS 4334684ddb6SLionel Sambuc 434*0a6a1f1dSLionel Sambuc#endif // !_LIBCPP_HAS_NO_THREADS 435*0a6a1f1dSLionel Sambuc 436*0a6a1f1dSLionel Sambucstruct _LIBCPP_TYPE_VIS_ONLY once_flag; 4374684ddb6SLionel Sambuc 4384684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_VARIADICS 4394684ddb6SLionel Sambuc 4404684ddb6SLionel Sambuctemplate<class _Callable, class... _Args> 4414684ddb6SLionel Sambuc_LIBCPP_INLINE_VISIBILITY 4424684ddb6SLionel Sambucvoid call_once(once_flag&, _Callable&&, _Args&&...); 4434684ddb6SLionel Sambuc 4444684ddb6SLionel Sambuc#else // _LIBCPP_HAS_NO_VARIADICS 4454684ddb6SLionel Sambuc 4464684ddb6SLionel Sambuctemplate<class _Callable> 4474684ddb6SLionel Sambuc_LIBCPP_INLINE_VISIBILITY 448*0a6a1f1dSLionel Sambucvoid call_once(once_flag&, _Callable&); 449*0a6a1f1dSLionel Sambuc 450*0a6a1f1dSLionel Sambuctemplate<class _Callable> 451*0a6a1f1dSLionel Sambuc_LIBCPP_INLINE_VISIBILITY 452*0a6a1f1dSLionel Sambucvoid call_once(once_flag&, const _Callable&); 4534684ddb6SLionel Sambuc 4544684ddb6SLionel Sambuc#endif // _LIBCPP_HAS_NO_VARIADICS 4554684ddb6SLionel Sambuc 4564684ddb6SLionel Sambucstruct _LIBCPP_TYPE_VIS_ONLY once_flag 4574684ddb6SLionel Sambuc{ 4584684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 4594684ddb6SLionel Sambuc _LIBCPP_CONSTEXPR 4604684ddb6SLionel Sambuc once_flag() _NOEXCEPT : __state_(0) {} 4614684ddb6SLionel Sambuc 4624684ddb6SLionel Sambucprivate: 4634684ddb6SLionel Sambuc once_flag(const once_flag&); // = delete; 4644684ddb6SLionel Sambuc once_flag& operator=(const once_flag&); // = delete; 4654684ddb6SLionel Sambuc 4664684ddb6SLionel Sambuc unsigned long __state_; 4674684ddb6SLionel Sambuc 4684684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_VARIADICS 4694684ddb6SLionel Sambuc template<class _Callable, class... _Args> 4704684ddb6SLionel Sambuc friend 4714684ddb6SLionel Sambuc void call_once(once_flag&, _Callable&&, _Args&&...); 4724684ddb6SLionel Sambuc#else // _LIBCPP_HAS_NO_VARIADICS 4734684ddb6SLionel Sambuc template<class _Callable> 4744684ddb6SLionel Sambuc friend 475*0a6a1f1dSLionel Sambuc void call_once(once_flag&, _Callable&); 476*0a6a1f1dSLionel Sambuc 477*0a6a1f1dSLionel Sambuc template<class _Callable> 478*0a6a1f1dSLionel Sambuc friend 479*0a6a1f1dSLionel Sambuc void call_once(once_flag&, const _Callable&); 4804684ddb6SLionel Sambuc#endif // _LIBCPP_HAS_NO_VARIADICS 4814684ddb6SLionel Sambuc}; 4824684ddb6SLionel Sambuc 4834684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_VARIADICS 4844684ddb6SLionel Sambuc 4854684ddb6SLionel Sambuctemplate <class _Fp> 4864684ddb6SLionel Sambucclass __call_once_param 4874684ddb6SLionel Sambuc{ 488*0a6a1f1dSLionel Sambuc _Fp& __f_; 4894684ddb6SLionel Sambucpublic: 4904684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 491*0a6a1f1dSLionel Sambuc explicit __call_once_param(_Fp& __f) : __f_(__f) {} 4924684ddb6SLionel Sambuc 4934684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 4944684ddb6SLionel Sambuc void operator()() 4954684ddb6SLionel Sambuc { 4964684ddb6SLionel Sambuc typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index; 4974684ddb6SLionel Sambuc __execute(_Index()); 4984684ddb6SLionel Sambuc } 4994684ddb6SLionel Sambuc 5004684ddb6SLionel Sambucprivate: 5014684ddb6SLionel Sambuc template <size_t ..._Indices> 5024684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 5034684ddb6SLionel Sambuc void __execute(__tuple_indices<_Indices...>) 5044684ddb6SLionel Sambuc { 505*0a6a1f1dSLionel Sambuc __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...); 5064684ddb6SLionel Sambuc } 5074684ddb6SLionel Sambuc}; 5084684ddb6SLionel Sambuc 5094684ddb6SLionel Sambuc#else 5104684ddb6SLionel Sambuc 5114684ddb6SLionel Sambuctemplate <class _Fp> 5124684ddb6SLionel Sambucclass __call_once_param 5134684ddb6SLionel Sambuc{ 514*0a6a1f1dSLionel Sambuc _Fp& __f_; 5154684ddb6SLionel Sambucpublic: 5164684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 517*0a6a1f1dSLionel Sambuc explicit __call_once_param(_Fp& __f) : __f_(__f) {} 5184684ddb6SLionel Sambuc 5194684ddb6SLionel Sambuc _LIBCPP_INLINE_VISIBILITY 5204684ddb6SLionel Sambuc void operator()() 5214684ddb6SLionel Sambuc { 5224684ddb6SLionel Sambuc __f_(); 5234684ddb6SLionel Sambuc } 5244684ddb6SLionel Sambuc}; 5254684ddb6SLionel Sambuc 5264684ddb6SLionel Sambuc#endif 5274684ddb6SLionel Sambuc 5284684ddb6SLionel Sambuctemplate <class _Fp> 5294684ddb6SLionel Sambucvoid 5304684ddb6SLionel Sambuc__call_once_proxy(void* __vp) 5314684ddb6SLionel Sambuc{ 5324684ddb6SLionel Sambuc __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp); 5334684ddb6SLionel Sambuc (*__p)(); 5344684ddb6SLionel Sambuc} 5354684ddb6SLionel Sambuc 5364684ddb6SLionel Sambuc_LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*)); 5374684ddb6SLionel Sambuc 5384684ddb6SLionel Sambuc#ifndef _LIBCPP_HAS_NO_VARIADICS 5394684ddb6SLionel Sambuc 5404684ddb6SLionel Sambuctemplate<class _Callable, class... _Args> 5414684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY 5424684ddb6SLionel Sambucvoid 5434684ddb6SLionel Sambuccall_once(once_flag& __flag, _Callable&& __func, _Args&&... __args) 5444684ddb6SLionel Sambuc{ 545*0a6a1f1dSLionel Sambuc if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul) 5464684ddb6SLionel Sambuc { 547*0a6a1f1dSLionel Sambuc typedef tuple<_Callable&&, _Args&&...> _Gp; 548*0a6a1f1dSLionel Sambuc _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...); 549*0a6a1f1dSLionel Sambuc __call_once_param<_Gp> __p(__f); 5504684ddb6SLionel Sambuc __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>); 5514684ddb6SLionel Sambuc } 5524684ddb6SLionel Sambuc} 5534684ddb6SLionel Sambuc 5544684ddb6SLionel Sambuc#else // _LIBCPP_HAS_NO_VARIADICS 5554684ddb6SLionel Sambuc 5564684ddb6SLionel Sambuctemplate<class _Callable> 5574684ddb6SLionel Sambucinline _LIBCPP_INLINE_VISIBILITY 5584684ddb6SLionel Sambucvoid 559*0a6a1f1dSLionel Sambuccall_once(once_flag& __flag, _Callable& __func) 5604684ddb6SLionel Sambuc{ 561*0a6a1f1dSLionel Sambuc if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul) 5624684ddb6SLionel Sambuc { 5634684ddb6SLionel Sambuc __call_once_param<_Callable> __p(__func); 5644684ddb6SLionel Sambuc __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>); 5654684ddb6SLionel Sambuc } 5664684ddb6SLionel Sambuc} 5674684ddb6SLionel Sambuc 568*0a6a1f1dSLionel Sambuctemplate<class _Callable> 569*0a6a1f1dSLionel Sambucinline _LIBCPP_INLINE_VISIBILITY 570*0a6a1f1dSLionel Sambucvoid 571*0a6a1f1dSLionel Sambuccall_once(once_flag& __flag, const _Callable& __func) 572*0a6a1f1dSLionel Sambuc{ 573*0a6a1f1dSLionel Sambuc if (__flag.__state_ != ~0ul) 574*0a6a1f1dSLionel Sambuc { 575*0a6a1f1dSLionel Sambuc __call_once_param<const _Callable> __p(__func); 576*0a6a1f1dSLionel Sambuc __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>); 577*0a6a1f1dSLionel Sambuc } 578*0a6a1f1dSLionel Sambuc} 579*0a6a1f1dSLionel Sambuc 5804684ddb6SLionel Sambuc#endif // _LIBCPP_HAS_NO_VARIADICS 5814684ddb6SLionel Sambuc 5824684ddb6SLionel Sambuc_LIBCPP_END_NAMESPACE_STD 5834684ddb6SLionel Sambuc 5844684ddb6SLionel Sambuc#endif // _LIBCPP_MUTEX 585