30#define _GLIBCXX_MUTEX 1
32#pragma GCC system_header
36#if __cplusplus < 201103L
49#if ! _GTHREAD_USE_MUTEX_TIMEDLOCK
55#if defined _GLIBCXX_HAS_GTHREADS && ! defined _GLIBCXX_HAVE_TLS
59#define __glibcxx_want_scoped_lock
62namespace std _GLIBCXX_VISIBILITY(default)
64_GLIBCXX_BEGIN_NAMESPACE_VERSION
71#ifdef _GLIBCXX_HAS_GTHREADS
75 class __recursive_mutex_base
78 typedef __gthread_recursive_mutex_t __native_type;
80 __recursive_mutex_base(
const __recursive_mutex_base&) =
delete;
81 __recursive_mutex_base& operator=(
const __recursive_mutex_base&) =
delete;
83#ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
84 __native_type _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
86 __recursive_mutex_base() =
default;
88 __native_type _M_mutex;
90 __recursive_mutex_base()
93 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
96 ~__recursive_mutex_base()
97 { __gthread_recursive_mutex_destroy(&_M_mutex); }
114 typedef __native_type* native_handle_type;
125 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
129 __throw_system_error(__e);
137 return !__gthread_recursive_mutex_trylock(&_M_mutex);
144 __gthread_recursive_mutex_unlock(&_M_mutex);
148 native_handle()
noexcept
149 {
return &_M_mutex; }
152#if _GTHREAD_USE_MUTEX_TIMEDLOCK
155 template<
typename _Derived>
156 class __timed_mutex_impl
159 template<
typename _Rep,
typename _Period>
163#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
169 auto __rt = chrono::duration_cast<__clock::duration>(__rtime);
172 return _M_try_lock_until(__clock::now() + __rt);
175 template<
typename _Duration>
177 _M_try_lock_until(
const chrono::time_point<chrono::system_clock,
180 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
181 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
183 __gthread_time_t __ts = {
184 static_cast<std::time_t
>(__s.time_since_epoch().count()),
185 static_cast<long>(__ns.count())
188 return static_cast<_Derived*
>(
this)->_M_timedlock(__ts);
191#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
192 template<
typename _Duration>
194 _M_try_lock_until(
const chrono::time_point<chrono::steady_clock,
197 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
198 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
200 __gthread_time_t __ts = {
201 static_cast<std::time_t
>(__s.time_since_epoch().count()),
202 static_cast<long>(__ns.count())
205 return static_cast<_Derived*
>(
this)->_M_clocklock(CLOCK_MONOTONIC,
210 template<
typename _Clock,
typename _Duration>
212 _M_try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
214#if __cplusplus > 201703L
215 static_assert(chrono::is_clock_v<_Clock>);
220 auto __now = _Clock::now();
222 auto __rtime = __atime - __now;
223 if (_M_try_lock_for(__rtime))
225 __now = _Clock::now();
226 }
while (__atime > __now);
241 :
private __mutex_base,
public __timed_mutex_impl<timed_mutex>
244 typedef __native_type* native_handle_type;
255 int __e = __gthread_mutex_lock(&_M_mutex);
259 __throw_system_error(__e);
267 return !__gthread_mutex_trylock(&_M_mutex);
270 template <
class _Rep,
class _Period>
274 {
return _M_try_lock_for(__rtime); }
276 template <
class _Clock,
class _Duration>
280 {
return _M_try_lock_until(__atime); }
286 __gthread_mutex_unlock(&_M_mutex);
290 native_handle()
noexcept
291 {
return &_M_mutex; }
297 _M_timedlock(
const __gthread_time_t& __ts)
298 {
return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
300#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
302 _M_clocklock(clockid_t __clockid,
const __gthread_time_t& __ts)
303 {
return !pthread_mutex_clocklock(&_M_mutex, __clockid, &__ts); }
318 :
private __recursive_mutex_base,
319 public __timed_mutex_impl<recursive_timed_mutex>
322 typedef __native_type* native_handle_type;
333 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
337 __throw_system_error(__e);
345 return !__gthread_recursive_mutex_trylock(&_M_mutex);
348 template <
class _Rep,
class _Period>
352 {
return _M_try_lock_for(__rtime); }
354 template <
class _Clock,
class _Duration>
358 {
return _M_try_lock_until(__atime); }
364 __gthread_recursive_mutex_unlock(&_M_mutex);
368 native_handle()
noexcept
369 {
return &_M_mutex; }
375 _M_timedlock(
const __gthread_time_t& __ts)
376 {
return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
378#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
380 _M_clocklock(clockid_t __clockid,
const __gthread_time_t& __ts)
381 {
return !pthread_mutex_clocklock(&_M_mutex, __clockid, &__ts); }
392 bool _M_locked =
false;
399 timed_mutex(
const timed_mutex&) =
delete;
400 timed_mutex& operator=(
const timed_mutex&) =
delete;
405 unique_lock<mutex> __lk(_M_mut);
406 _M_cv.wait(__lk, [&]{
return !_M_locked; });
414 lock_guard<mutex> __lk(_M_mut);
421 template<
typename _Rep,
typename _Period>
424 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
426 unique_lock<mutex> __lk(_M_mut);
427 if (!_M_cv.wait_for(__lk, __rtime, [&]{ return !_M_locked; }))
433 template<
typename _Clock,
typename _Duration>
436 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
438 unique_lock<mutex> __lk(_M_mut);
439 if (!_M_cv.wait_until(__lk, __atime, [&]{ return !_M_locked; }))
448 lock_guard<mutex> __lk(_M_mut);
449 __glibcxx_assert( _M_locked );
456 class recursive_timed_mutex
459 condition_variable _M_cv;
461 unsigned _M_count = 0;
468 operator()() const noexcept
469 {
return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
471 const recursive_timed_mutex* _M_mx;
472 thread::id _M_caller;
477 recursive_timed_mutex() =
default;
478 ~recursive_timed_mutex() { __glibcxx_assert( _M_count == 0 ); }
480 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
481 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
487 _Can_lock __can_lock{
this, __id};
488 unique_lock<mutex> __lk(_M_mut);
489 _M_cv.wait(__lk, __can_lock);
491 __throw_system_error(EAGAIN);
501 _Can_lock __can_lock{
this, __id};
502 lock_guard<mutex> __lk(_M_mut);
512 template<
typename _Rep,
typename _Period>
515 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
518 _Can_lock __can_lock{
this, __id};
519 unique_lock<mutex> __lk(_M_mut);
520 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
529 template<
typename _Clock,
typename _Duration>
532 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
535 _Can_lock __can_lock{
this, __id};
536 unique_lock<mutex> __lk(_M_mut);
537 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
549 lock_guard<mutex> __lk(_M_mut);
551 __glibcxx_assert( _M_count > 0 );
567 template<
typename _Lockable>
569 __try_lock_impl(_Lockable& __l)
571 if (unique_lock<_Lockable> __lock{__l,
try_to_lock})
582 template<
typename _L0,
typename... _Lockables>
584 __try_lock_impl(_L0& __l0, _Lockables&... __lockables)
586#if __cplusplus >= 201703L
587 if constexpr ((is_same_v<_L0, _Lockables> && ...))
589 constexpr int _Np = 1 +
sizeof...(_Lockables);
590 unique_lock<_L0> __locks[_Np] = {
593 for (
int __i = 0; __i < _Np; ++__i)
597 const int __failed = __i;
599 __locks[__i].unlock();
603 for (
auto& __l : __locks)
611 int __idx = __detail::__try_lock_impl(__lockables...);
636 template<
typename _L1,
typename _L2,
typename... _L3>
641 return __detail::__try_lock_impl(__l1, __l2, __l3...);
652 template<
typename _L0,
typename... _L1>
654 __lock_impl(
int& __i,
int __depth, _L0& __l0, _L1&... __l1)
656 while (__i >= __depth)
662 unique_lock<_L0> __first(__l0);
663 __failed += __detail::__try_lock_impl(__l1...);
671#if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
674 constexpr auto __n = 1 +
sizeof...(_L1);
675 __i = (__depth + __failed) % __n;
678 __detail::__lock_impl(__i, __depth + 1, __l1..., __l0);
696 template<
typename _L1,
typename _L2,
typename... _L3>
698 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
700#if __cplusplus >= 201703L
701 if constexpr (is_same_v<_L1, _L2> && (is_same_v<_L1, _L3> && ...))
703 constexpr int _Np = 2 +
sizeof...(_L3);
709 __locks[__first].lock();
710 for (
int __j = 1; __j < _Np; ++__j)
712 const int __idx = (__first + __j) % _Np;
715 for (
int __k = __j; __k != 0; --__k)
716 __locks[(__first + __k - 1) % _Np].unlock();
721 }
while (!__locks[__first].owns_lock());
723 for (
auto& __l : __locks)
730 __detail::__lock_impl(__i, 0, __l1, __l2, __l3...);
734#ifdef __cpp_lib_scoped_lock
743 template<
typename... _MutexTypes>
749 explicit scoped_lock(_MutexTypes&... __m) : _M_devices(
std::tie(__m...))
753 explicit scoped_lock(adopt_lock_t, _MutexTypes&... __m) noexcept
758 { std::apply([](
auto&... __m) { (__m.unlock(), ...); }, _M_devices); }
760 scoped_lock(
const scoped_lock&) =
delete;
761 scoped_lock& operator=(
const scoped_lock&) =
delete;
764 tuple<_MutexTypes&...> _M_devices;
771 explicit scoped_lock() =
default;
772 explicit scoped_lock(adopt_lock_t)
noexcept { }
773 ~scoped_lock() =
default;
775 scoped_lock(
const scoped_lock&) =
delete;
776 scoped_lock& operator=(
const scoped_lock&) =
delete;
779 template<
typename _Mutex>
780 class scoped_lock<_Mutex>
783 using mutex_type = _Mutex;
786 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
787 { _M_device.lock(); }
790 explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept
795 { _M_device.unlock(); }
797 scoped_lock(
const scoped_lock&) =
delete;
798 scoped_lock& operator=(
const scoped_lock&) =
delete;
801 mutex_type& _M_device;
805#ifdef _GLIBCXX_HAS_GTHREADS
809 constexpr once_flag()
noexcept =
default;
819 __gthread_once_t _M_once = __GTHREAD_ONCE_INIT;
821 struct _Prepare_execution;
823 template<
typename _Callable,
typename... _Args>
829# ifdef _GLIBCXX_HAVE_TLS
832 extern __thread
void* __once_callable;
833 extern __thread void (*__once_call)();
836 struct once_flag::_Prepare_execution
838 template<
typename _Callable>
840 _Prepare_execution(_Callable& __c)
845 __once_call = [] { (*
static_cast<_Callable*
>(__once_callable))(); };
848 ~_Prepare_execution()
851 __once_callable =
nullptr;
852 __once_call =
nullptr;
855 _Prepare_execution(
const _Prepare_execution&) =
delete;
856 _Prepare_execution& operator=(
const _Prepare_execution&) =
delete;
862 extern function<void()> __once_functor;
865 __set_once_functor_lock_ptr(unique_lock<mutex>*);
871 struct once_flag::_Prepare_execution
873 template<
typename _Callable>
875 _Prepare_execution(_Callable& __c)
878 __once_functor = __c;
879 __set_once_functor_lock_ptr(&_M_functor_lock);
882 ~_Prepare_execution()
885 __set_once_functor_lock_ptr(
nullptr);
890 unique_lock<mutex> _M_functor_lock{__get_once_mutex()};
892 _Prepare_execution(
const _Prepare_execution&) =
delete;
893 _Prepare_execution& operator=(
const _Prepare_execution&) =
delete;
900 extern "C" void __once_proxy(
void);
903 template<
typename _Callable,
typename... _Args>
908 auto __callable = [&] {
910 std::forward<_Args>(__args)...);
913 once_flag::_Prepare_execution __exec(__callable);
916 if (
int __e = __gthread_once(&__once._M_once, &__once_proxy))
917 __throw_system_error(__e);
925 constexpr once_flag() noexcept = default;
939 enum _Bits :
int { _Init = 0, _Active = 1, _Done = 2 };
941 int _M_once = _Bits::_Init;
945 _M_passive() const noexcept;
953 void _M_finish(
bool __returning) noexcept;
956 struct _Active_execution
958 explicit _Active_execution(once_flag& __flag) : _M_flag(__flag) { }
960 ~_Active_execution() { _M_flag._M_finish(_M_returning); }
962 _Active_execution(
const _Active_execution&) =
delete;
963 _Active_execution&
operator=(
const _Active_execution&) =
delete;
966 bool _M_returning =
false;
969 template<
typename _Callable,
typename... _Args>
971 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
977 once_flag::_M_passive() const noexcept
978 {
return _M_once == _Bits::_Done; }
981 once_flag::_M_activate()
983 if (_M_once == _Bits::_Init) [[__likely__]]
985 _M_once = _Bits::_Active;
988 else if (_M_passive())
991 __throw_system_error(EDEADLK);
995 once_flag::_M_finish(
bool __returning)
noexcept
996 { _M_once = __returning ? _Bits::_Done : _Bits::_Init; }
999 template<
typename _Callable,
typename... _Args>
1001 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
1003 if (__once._M_passive())
1005 else if (__once._M_activate())
1007 once_flag::_Active_execution __exec(__once);
1012 std::forward<_Args>(__args)...);
1015 __exec._M_returning =
true;
1021_GLIBCXX_END_NAMESPACE_VERSION
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
constexpr tuple< _Elements &... > tie(_Elements &... __args) noexcept
Return a tuple of lvalue references bound to the arguments.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
constexpr try_to_lock_t try_to_lock
Tag used to prevent a scoped lock from blocking if a mutex is locked.
int try_lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic try_lock.
constexpr defer_lock_t defer_lock
Tag used to prevent a scoped lock from acquiring ownership of a mutex.
void call_once(once_flag &__once, _Callable &&__f, _Args &&... __args)
Invoke a callable and synchronize with other calls using the same flag.
ISO C++ entities toplevel namespace is std.
thread::id get_id() noexcept
The unique identifier of the current thread.
Flag type used by std::call_once.
friend void call_once(once_flag &__once, _Callable &&__f, _Args &&... __args)
Invoke a callable and synchronize with other calls using the same flag.
once_flag(const once_flag &)=delete
Deleted copy constructor.
once_flag & operator=(const once_flag &)=delete
Deleted assignment operator.
chrono::duration represents a distance between two points in time
chrono::time_point represents a point in time as measured by a clock
A movable scoped lock type.