30#define _GLIBCXX_MUTEX 1
32#pragma GCC system_header
36#if __cplusplus < 201103L
47#if ! _GTHREAD_USE_MUTEX_TIMEDLOCK
53#if defined _GLIBCXX_HAS_GTHREADS && ! defined _GLIBCXX_HAVE_TLS
57namespace std _GLIBCXX_VISIBILITY(default)
59_GLIBCXX_BEGIN_NAMESPACE_VERSION
66#ifdef _GLIBCXX_HAS_GTHREADS
70 class __recursive_mutex_base
73 typedef __gthread_recursive_mutex_t __native_type;
75 __recursive_mutex_base(
const __recursive_mutex_base&) =
delete;
76 __recursive_mutex_base& operator=(
const __recursive_mutex_base&) =
delete;
78#ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
79 __native_type _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
81 __recursive_mutex_base() =
default;
83 __native_type _M_mutex;
85 __recursive_mutex_base()
88 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
91 ~__recursive_mutex_base()
92 { __gthread_recursive_mutex_destroy(&_M_mutex); }
109 typedef __native_type* native_handle_type;
120 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
124 __throw_system_error(__e);
132 return !__gthread_recursive_mutex_trylock(&_M_mutex);
139 __gthread_recursive_mutex_unlock(&_M_mutex);
143 native_handle()
noexcept
144 {
return &_M_mutex; }
147#if _GTHREAD_USE_MUTEX_TIMEDLOCK
150 template<
typename _Derived>
151 class __timed_mutex_impl
154 template<
typename _Rep,
typename _Period>
158#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
164 auto __rt = chrono::duration_cast<__clock::duration>(__rtime);
167 return _M_try_lock_until(__clock::now() + __rt);
170 template<
typename _Duration>
172 _M_try_lock_until(
const chrono::time_point<chrono::system_clock,
175 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
176 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
178 __gthread_time_t __ts = {
179 static_cast<std::time_t
>(__s.time_since_epoch().count()),
180 static_cast<long>(__ns.count())
183 return static_cast<_Derived*
>(
this)->_M_timedlock(__ts);
186#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
187 template<
typename _Duration>
189 _M_try_lock_until(
const chrono::time_point<chrono::steady_clock,
192 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
193 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
195 __gthread_time_t __ts = {
196 static_cast<std::time_t
>(__s.time_since_epoch().count()),
197 static_cast<long>(__ns.count())
200 return static_cast<_Derived*
>(
this)->_M_clocklock(CLOCK_MONOTONIC,
205 template<
typename _Clock,
typename _Duration>
207 _M_try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
209#if __cplusplus > 201703L
210 static_assert(chrono::is_clock_v<_Clock>);
215 auto __now = _Clock::now();
217 auto __rtime = __atime - __now;
218 if (_M_try_lock_for(__rtime))
220 __now = _Clock::now();
221 }
while (__atime > __now);
236 :
private __mutex_base,
public __timed_mutex_impl<timed_mutex>
239 typedef __native_type* native_handle_type;
250 int __e = __gthread_mutex_lock(&_M_mutex);
254 __throw_system_error(__e);
262 return !__gthread_mutex_trylock(&_M_mutex);
265 template <
class _Rep,
class _Period>
269 {
return _M_try_lock_for(__rtime); }
271 template <
class _Clock,
class _Duration>
275 {
return _M_try_lock_until(__atime); }
281 __gthread_mutex_unlock(&_M_mutex);
285 native_handle()
noexcept
286 {
return &_M_mutex; }
292 _M_timedlock(
const __gthread_time_t& __ts)
293 {
return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
295#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
297 _M_clocklock(clockid_t clockid,
const __gthread_time_t& __ts)
298 {
return !pthread_mutex_clocklock(&_M_mutex, clockid, &__ts); }
313 :
private __recursive_mutex_base,
314 public __timed_mutex_impl<recursive_timed_mutex>
317 typedef __native_type* native_handle_type;
328 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
332 __throw_system_error(__e);
340 return !__gthread_recursive_mutex_trylock(&_M_mutex);
343 template <
class _Rep,
class _Period>
347 {
return _M_try_lock_for(__rtime); }
349 template <
class _Clock,
class _Duration>
353 {
return _M_try_lock_until(__atime); }
359 __gthread_recursive_mutex_unlock(&_M_mutex);
363 native_handle()
noexcept
364 {
return &_M_mutex; }
370 _M_timedlock(
const __gthread_time_t& __ts)
371 {
return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
373#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
375 _M_clocklock(clockid_t clockid,
const __gthread_time_t& __ts)
376 {
return !pthread_mutex_clocklock(&_M_mutex, clockid, &__ts); }
387 bool _M_locked =
false;
394 timed_mutex(
const timed_mutex&) =
delete;
395 timed_mutex& operator=(
const timed_mutex&) =
delete;
400 unique_lock<mutex> __lk(_M_mut);
401 _M_cv.wait(__lk, [&]{
return !_M_locked; });
409 lock_guard<mutex> __lk(_M_mut);
416 template<
typename _Rep,
typename _Period>
419 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
421 unique_lock<mutex> __lk(_M_mut);
422 if (!_M_cv.wait_for(__lk, __rtime, [&]{ return !_M_locked; }))
428 template<
typename _Clock,
typename _Duration>
431 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
433 unique_lock<mutex> __lk(_M_mut);
434 if (!_M_cv.wait_until(__lk, __atime, [&]{ return !_M_locked; }))
443 lock_guard<mutex> __lk(_M_mut);
444 __glibcxx_assert( _M_locked );
451 class recursive_timed_mutex
454 condition_variable _M_cv;
456 unsigned _M_count = 0;
463 operator()() const noexcept
464 {
return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
466 const recursive_timed_mutex* _M_mx;
467 thread::id _M_caller;
472 recursive_timed_mutex() =
default;
473 ~recursive_timed_mutex() { __glibcxx_assert( _M_count == 0 ); }
475 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
476 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
482 _Can_lock __can_lock{
this, __id};
483 unique_lock<mutex> __lk(_M_mut);
484 _M_cv.wait(__lk, __can_lock);
486 __throw_system_error(EAGAIN);
496 _Can_lock __can_lock{
this, __id};
497 lock_guard<mutex> __lk(_M_mut);
507 template<
typename _Rep,
typename _Period>
510 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
513 _Can_lock __can_lock{
this, __id};
514 unique_lock<mutex> __lk(_M_mut);
515 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
524 template<
typename _Clock,
typename _Duration>
527 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
530 _Can_lock __can_lock{
this, __id};
531 unique_lock<mutex> __lk(_M_mut);
532 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
544 lock_guard<mutex> __lk(_M_mut);
546 __glibcxx_assert( _M_count > 0 );
562 template<
typename _Lockable>
564 __try_lock_impl(_Lockable& __l)
566 if (unique_lock<_Lockable> __lock{__l,
try_to_lock})
577 template<
typename _L0,
typename... _Lockables>
579 __try_lock_impl(_L0& __l0, _Lockables&... __lockables)
581#if __cplusplus >= 201703L
582 if constexpr ((is_same_v<_L0, _Lockables> && ...))
584 constexpr int _Np = 1 +
sizeof...(_Lockables);
585 unique_lock<_L0> __locks[_Np] = {
588 for (
int __i = 0; __i < _Np; ++__i)
592 const int __failed = __i;
594 __locks[__i].unlock();
598 for (
auto& __l : __locks)
606 int __idx = __detail::__try_lock_impl(__lockables...);
631 template<
typename _L1,
typename _L2,
typename... _L3>
636 return __detail::__try_lock_impl(__l1, __l2, __l3...);
647 template<
typename _L0,
typename... _L1>
649 __lock_impl(
int& __i,
int __depth, _L0& __l0, _L1&... __l1)
651 while (__i >= __depth)
657 unique_lock<_L0> __first(__l0);
658 __failed += __detail::__try_lock_impl(__l1...);
666#if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
669 constexpr auto __n = 1 +
sizeof...(_L1);
670 __i = (__depth + __failed) % __n;
673 __detail::__lock_impl(__i, __depth + 1, __l1..., __l0);
691 template<
typename _L1,
typename _L2,
typename... _L3>
693 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
695#if __cplusplus >= 201703L
696 if constexpr (is_same_v<_L1, _L2> && (is_same_v<_L1, _L3> && ...))
698 constexpr int _Np = 2 +
sizeof...(_L3);
704 __locks[__first].lock();
705 for (
int __j = 1; __j < _Np; ++__j)
707 const int __idx = (__first + __j) % _Np;
710 for (
int __k = __j; __k != 0; --__k)
711 __locks[(__first + __k - 1) % _Np].unlock();
716 }
while (!__locks[__first].owns_lock());
718 for (
auto& __l : __locks)
725 __detail::__lock_impl(__i, 0, __l1, __l2, __l3...);
729#if __cplusplus >= 201703L
730#define __cpp_lib_scoped_lock 201703L
739 template<
typename... _MutexTypes>
751 { std::apply([](
auto&... __m) { (__m.unlock(), ...); }, _M_devices); }
757 tuple<_MutexTypes&...> _M_devices;
766 ~scoped_lock() =
default;
768 scoped_lock(
const scoped_lock&) =
delete;
769 scoped_lock& operator=(
const scoped_lock&) =
delete;
772 template<
typename _Mutex>
773 class scoped_lock<_Mutex>
776 using mutex_type = _Mutex;
778 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
779 { _M_device.lock(); }
781 explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept
786 { _M_device.unlock(); }
788 scoped_lock(
const scoped_lock&) =
delete;
789 scoped_lock& operator=(
const scoped_lock&) =
delete;
792 mutex_type& _M_device;
796#ifdef _GLIBCXX_HAS_GTHREADS
800 constexpr once_flag()
noexcept =
default;
810 __gthread_once_t _M_once = __GTHREAD_ONCE_INIT;
812 struct _Prepare_execution;
814 template<
typename _Callable,
typename... _Args>
820# ifdef _GLIBCXX_HAVE_TLS
823 extern __thread
void* __once_callable;
824 extern __thread void (*__once_call)();
827 struct once_flag::_Prepare_execution
829 template<
typename _Callable>
831 _Prepare_execution(_Callable& __c)
836 __once_call = [] { (*
static_cast<_Callable*
>(__once_callable))(); };
839 ~_Prepare_execution()
842 __once_callable =
nullptr;
843 __once_call =
nullptr;
846 _Prepare_execution(
const _Prepare_execution&) =
delete;
847 _Prepare_execution&
operator=(
const _Prepare_execution&) =
delete;
853 extern function<void()> __once_functor;
856 __set_once_functor_lock_ptr(unique_lock<mutex>*);
862 struct once_flag::_Prepare_execution
864 template<
typename _Callable>
866 _Prepare_execution(_Callable& __c)
869 __once_functor = __c;
870 __set_once_functor_lock_ptr(&_M_functor_lock);
873 ~_Prepare_execution()
876 __set_once_functor_lock_ptr(
nullptr);
881 unique_lock<mutex> _M_functor_lock{__get_once_mutex()};
883 _Prepare_execution(
const _Prepare_execution&) =
delete;
884 _Prepare_execution&
operator=(
const _Prepare_execution&) =
delete;
891 extern "C" void __once_proxy(
void);
894 template<
typename _Callable,
typename... _Args>
899 auto __callable = [&] {
901 std::forward<_Args>(__args)...);
904 once_flag::_Prepare_execution __exec(__callable);
907 if (
int __e = __gthread_once(&__once._M_once, &__once_proxy))
908 __throw_system_error(__e);
916 constexpr once_flag() noexcept = default;
930 enum _Bits :
int { _Init = 0, _Active = 1, _Done = 2 };
932 int _M_once = _Bits::_Init;
936 _M_passive() const noexcept;
944 void _M_finish(
bool __returning) noexcept;
947 struct _Active_execution
949 explicit _Active_execution(once_flag& __flag) : _M_flag(__flag) { }
951 ~_Active_execution() { _M_flag._M_finish(_M_returning); }
953 _Active_execution(
const _Active_execution&) =
delete;
954 _Active_execution&
operator=(
const _Active_execution&) =
delete;
957 bool _M_returning =
false;
960 template<
typename _Callable,
typename... _Args>
962 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
968 once_flag::_M_passive() const noexcept
969 {
return _M_once == _Bits::_Done; }
972 once_flag::_M_activate()
974 if (_M_once == _Bits::_Init) [[__likely__]]
976 _M_once = _Bits::_Active;
979 else if (_M_passive())
982 __throw_system_error(EDEADLK);
986 once_flag::_M_finish(
bool __returning)
noexcept
987 { _M_once = __returning ? _Bits::_Done : _Bits::_Init; }
990 template<
typename _Callable,
typename... _Args>
992 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
994 if (__once._M_passive())
996 else if (__once._M_activate())
998 once_flag::_Active_execution __exec(__once);
1003 std::forward<_Args>(__args)...);
1006 __exec._M_returning =
true;
1012_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.
A scoped lock type for multiple lockable objects.
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.
Primary class template, tuple.
chrono::duration represents a distance between two points in time
chrono::time_point represents a point in time as measured by a clock
Assume the calling thread has already obtained mutex ownership and manage it.
A movable scoped lock type.