30#define _GLIBCXX_MUTEX 1
33#pragma GCC system_header
38#if __cplusplus < 201103L
51#if ! _GTHREAD_USE_MUTEX_TIMEDLOCK
57#if defined _GLIBCXX_HAS_GTHREADS && ! defined _GLIBCXX_HAVE_TLS
61#define __glibcxx_want_scoped_lock
64namespace std _GLIBCXX_VISIBILITY(default)
66_GLIBCXX_BEGIN_NAMESPACE_VERSION
73#ifdef _GLIBCXX_HAS_GTHREADS
77 class __recursive_mutex_base
80 typedef __gthread_recursive_mutex_t __native_type;
82 __recursive_mutex_base(
const __recursive_mutex_base&) =
delete;
83 __recursive_mutex_base& operator=(
const __recursive_mutex_base&) =
delete;
85#ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
86 __native_type _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
88 __recursive_mutex_base() =
default;
90 __native_type _M_mutex;
92 __recursive_mutex_base()
95 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
98 ~__recursive_mutex_base()
99 { __gthread_recursive_mutex_destroy(&_M_mutex); }
116 typedef __native_type* native_handle_type;
127 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
131 __throw_system_error(__e);
139 return !__gthread_recursive_mutex_trylock(&_M_mutex);
146 __gthread_recursive_mutex_unlock(&_M_mutex);
150 native_handle()
noexcept
151 {
return &_M_mutex; }
154#if _GTHREAD_USE_MUTEX_TIMEDLOCK
157 template<
typename _Derived>
158 class __timed_mutex_impl
161 template<
typename _Rep,
typename _Period>
165#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
171 auto __rt = chrono::duration_cast<__clock::duration>(__rtime);
174 return _M_try_lock_until(__clock::now() + __rt);
177 template<
typename _Duration>
179 _M_try_lock_until(
const chrono::time_point<chrono::system_clock,
182 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
183 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
185 __gthread_time_t __ts = {
186 static_cast<std::time_t
>(__s.time_since_epoch().count()),
187 static_cast<long>(__ns.count())
190 return static_cast<_Derived*
>(
this)->_M_timedlock(__ts);
193#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
194 template<
typename _Duration>
196 _M_try_lock_until(
const chrono::time_point<chrono::steady_clock,
199 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
200 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
202 __gthread_time_t __ts = {
203 static_cast<std::time_t
>(__s.time_since_epoch().count()),
204 static_cast<long>(__ns.count())
207 return static_cast<_Derived*
>(
this)->_M_clocklock(CLOCK_MONOTONIC,
212 template<
typename _Clock,
typename _Duration>
214 _M_try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
216#if __cplusplus > 201703L
217 static_assert(chrono::is_clock_v<_Clock>);
222 auto __now = _Clock::now();
224 auto __rtime = __atime - __now;
225 if (_M_try_lock_for(__rtime))
227 __now = _Clock::now();
228 }
while (__atime > __now);
243 :
private __mutex_base,
public __timed_mutex_impl<timed_mutex>
246 typedef __native_type* native_handle_type;
257 int __e = __gthread_mutex_lock(&_M_mutex);
261 __throw_system_error(__e);
269 return !__gthread_mutex_trylock(&_M_mutex);
272 template <
class _Rep,
class _Period>
276 {
return _M_try_lock_for(__rtime); }
278 template <
class _Clock,
class _Duration>
282 {
return _M_try_lock_until(__atime); }
288 __gthread_mutex_unlock(&_M_mutex);
292 native_handle()
noexcept
293 {
return &_M_mutex; }
299 _M_timedlock(
const __gthread_time_t& __ts)
300 {
return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
302#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
304 _M_clocklock(clockid_t __clockid,
const __gthread_time_t& __ts)
305 {
return !pthread_mutex_clocklock(&_M_mutex, __clockid, &__ts); }
320 :
private __recursive_mutex_base,
321 public __timed_mutex_impl<recursive_timed_mutex>
324 typedef __native_type* native_handle_type;
335 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
339 __throw_system_error(__e);
347 return !__gthread_recursive_mutex_trylock(&_M_mutex);
350 template <
class _Rep,
class _Period>
354 {
return _M_try_lock_for(__rtime); }
356 template <
class _Clock,
class _Duration>
360 {
return _M_try_lock_until(__atime); }
366 __gthread_recursive_mutex_unlock(&_M_mutex);
370 native_handle()
noexcept
371 {
return &_M_mutex; }
377 _M_timedlock(
const __gthread_time_t& __ts)
378 {
return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
380#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
382 _M_clocklock(clockid_t __clockid,
const __gthread_time_t& __ts)
383 {
return !pthread_mutex_clocklock(&_M_mutex, __clockid, &__ts); }
394 bool _M_locked =
false;
401 timed_mutex(
const timed_mutex&) =
delete;
402 timed_mutex& operator=(
const timed_mutex&) =
delete;
407 unique_lock<mutex> __lk(_M_mut);
408 _M_cv.wait(__lk, [&]{
return !_M_locked; });
416 lock_guard<mutex> __lk(_M_mut);
423 template<
typename _Rep,
typename _Period>
426 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
428 unique_lock<mutex> __lk(_M_mut);
429 if (!_M_cv.wait_for(__lk, __rtime, [&]{ return !_M_locked; }))
435 template<
typename _Clock,
typename _Duration>
438 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
440 unique_lock<mutex> __lk(_M_mut);
441 if (!_M_cv.wait_until(__lk, __atime, [&]{ return !_M_locked; }))
450 lock_guard<mutex> __lk(_M_mut);
451 __glibcxx_assert( _M_locked );
458 class recursive_timed_mutex
461 condition_variable _M_cv;
463 unsigned _M_count = 0;
470 operator()() const noexcept
471 {
return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
473 const recursive_timed_mutex* _M_mx;
474 thread::id _M_caller;
479 recursive_timed_mutex() =
default;
480 ~recursive_timed_mutex() { __glibcxx_assert( _M_count == 0 ); }
482 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
483 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
489 _Can_lock __can_lock{
this, __id};
490 unique_lock<mutex> __lk(_M_mut);
491 _M_cv.wait(__lk, __can_lock);
493 __throw_system_error(EAGAIN);
503 _Can_lock __can_lock{
this, __id};
504 lock_guard<mutex> __lk(_M_mut);
514 template<
typename _Rep,
typename _Period>
517 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
520 _Can_lock __can_lock{
this, __id};
521 unique_lock<mutex> __lk(_M_mut);
522 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
531 template<
typename _Clock,
typename _Duration>
534 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
537 _Can_lock __can_lock{
this, __id};
538 unique_lock<mutex> __lk(_M_mut);
539 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
551 lock_guard<mutex> __lk(_M_mut);
553 __glibcxx_assert( _M_count > 0 );
569 template<
typename _Lockable>
571 __try_lock_impl(_Lockable& __l)
573 if (unique_lock<_Lockable> __lock{__l,
try_to_lock})
584 template<
typename _L0,
typename... _Lockables>
586 __try_lock_impl(_L0& __l0, _Lockables&... __lockables)
588#if __cplusplus >= 201703L
589 if constexpr ((is_same_v<_L0, _Lockables> && ...))
591 constexpr int _Np = 1 +
sizeof...(_Lockables);
592 unique_lock<_L0> __locks[_Np] = {
595 for (
int __i = 0; __i < _Np; ++__i)
599 const int __failed = __i;
601 __locks[__i].unlock();
605 for (
auto& __l : __locks)
613 int __idx = __detail::__try_lock_impl(__lockables...);
638 template<
typename _L1,
typename _L2,
typename... _L3>
643 return __detail::__try_lock_impl(__l1, __l2, __l3...);
654 template<
typename _L0,
typename... _L1>
656 __lock_impl(
int& __i,
int __depth, _L0& __l0, _L1&... __l1)
658 while (__i >= __depth)
664 unique_lock<_L0> __first(__l0);
665 __failed += __detail::__try_lock_impl(__l1...);
673#if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
676 constexpr auto __n = 1 +
sizeof...(_L1);
677 __i = (__depth + __failed) % __n;
680 __detail::__lock_impl(__i, __depth + 1, __l1..., __l0);
698 template<
typename _L1,
typename _L2,
typename... _L3>
700 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
702#if __cplusplus >= 201703L
703 if constexpr (is_same_v<_L1, _L2> && (is_same_v<_L1, _L3> && ...))
705 constexpr int _Np = 2 +
sizeof...(_L3);
711 __locks[__first].lock();
712 for (
int __j = 1; __j < _Np; ++__j)
714 const int __idx = (__first + __j) % _Np;
717 for (
int __k = __j; __k != 0; --__k)
718 __locks[(__first + __k - 1) % _Np].unlock();
723 }
while (!__locks[__first].owns_lock());
725 for (
auto& __l : __locks)
732 __detail::__lock_impl(__i, 0, __l1, __l2, __l3...);
736#ifdef __cpp_lib_scoped_lock
745 template<
typename... _MutexTypes>
751 explicit scoped_lock(_MutexTypes&... __m) : _M_devices(
std::tie(__m...))
755 explicit scoped_lock(adopt_lock_t, _MutexTypes&... __m) noexcept
760 { std::apply([](
auto&... __m) { (__m.unlock(), ...); }, _M_devices); }
762 scoped_lock(
const scoped_lock&) =
delete;
763 scoped_lock& operator=(
const scoped_lock&) =
delete;
766 tuple<_MutexTypes&...> _M_devices;
773 explicit scoped_lock() =
default;
774 explicit scoped_lock(adopt_lock_t)
noexcept { }
775 ~scoped_lock() =
default;
777 scoped_lock(
const scoped_lock&) =
delete;
778 scoped_lock& operator=(
const scoped_lock&) =
delete;
781 template<
typename _Mutex>
782 class scoped_lock<_Mutex>
785 using mutex_type = _Mutex;
788 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
789 { _M_device.lock(); }
792 explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept
797 { _M_device.unlock(); }
799 scoped_lock(
const scoped_lock&) =
delete;
800 scoped_lock& operator=(
const scoped_lock&) =
delete;
803 mutex_type& _M_device;
807#ifdef _GLIBCXX_HAS_GTHREADS
811 constexpr once_flag()
noexcept =
default;
821 __gthread_once_t _M_once = __GTHREAD_ONCE_INIT;
823 struct _Prepare_execution;
825 template<
typename _Callable,
typename... _Args>
831# ifdef _GLIBCXX_HAVE_TLS
834 extern __thread
void* __once_callable;
835 extern __thread void (*__once_call)();
838 struct once_flag::_Prepare_execution
840 template<
typename _Callable>
842 _Prepare_execution(_Callable& __c)
847 __once_call = [] { (*
static_cast<_Callable*
>(__once_callable))(); };
850 ~_Prepare_execution()
853 __once_callable =
nullptr;
854 __once_call =
nullptr;
857 _Prepare_execution(
const _Prepare_execution&) =
delete;
858 _Prepare_execution& operator=(
const _Prepare_execution&) =
delete;
864 extern function<void()> __once_functor;
867 __set_once_functor_lock_ptr(unique_lock<mutex>*);
873 struct once_flag::_Prepare_execution
875 template<
typename _Callable>
877 _Prepare_execution(_Callable& __c)
880 __once_functor = __c;
881 __set_once_functor_lock_ptr(&_M_functor_lock);
884 ~_Prepare_execution()
887 __set_once_functor_lock_ptr(
nullptr);
892 unique_lock<mutex> _M_functor_lock{__get_once_mutex()};
894 _Prepare_execution(
const _Prepare_execution&) =
delete;
895 _Prepare_execution& operator=(
const _Prepare_execution&) =
delete;
902 extern "C" void __once_proxy(
void);
905 template<
typename _Callable,
typename... _Args>
910 auto __callable = [&] {
912 std::forward<_Args>(__args)...);
915 once_flag::_Prepare_execution __exec(__callable);
918 if (
int __e = __gthread_once(&__once._M_once, &__once_proxy))
919 __throw_system_error(__e);
927 constexpr once_flag() noexcept = default;
941 enum _Bits :
int { _Init = 0, _Active = 1, _Done = 2 };
943 int _M_once = _Bits::_Init;
947 _M_passive() const noexcept;
955 void _M_finish(
bool __returning) noexcept;
958 struct _Active_execution
960 explicit _Active_execution(once_flag& __flag) : _M_flag(__flag) { }
962 ~_Active_execution() { _M_flag._M_finish(_M_returning); }
964 _Active_execution(
const _Active_execution&) =
delete;
965 _Active_execution&
operator=(
const _Active_execution&) =
delete;
968 bool _M_returning =
false;
971 template<
typename _Callable,
typename... _Args>
973 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
979 once_flag::_M_passive() const noexcept
980 {
return _M_once == _Bits::_Done; }
983 once_flag::_M_activate()
985 if (_M_once == _Bits::_Init) [[__likely__]]
987 _M_once = _Bits::_Active;
990 else if (_M_passive())
993 __throw_system_error(EDEADLK);
997 once_flag::_M_finish(
bool __returning)
noexcept
998 { _M_once = __returning ? _Bits::_Done : _Bits::_Init; }
1001 template<
typename _Callable,
typename... _Args>
1003 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
1005 if (__once._M_passive())
1007 else if (__once._M_activate())
1009 once_flag::_Active_execution __exec(__once);
1014 std::forward<_Args>(__args)...);
1017 __exec._M_returning =
true;
1023_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.