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);
131 return !__gthread_recursive_mutex_trylock(&_M_mutex);
138 __gthread_recursive_mutex_unlock(&_M_mutex);
142 native_handle()
noexcept
143 {
return &_M_mutex; }
146#if _GTHREAD_USE_MUTEX_TIMEDLOCK
149 template<
typename _Derived>
150 class __timed_mutex_impl
153 template<
typename _Rep,
typename _Period>
157#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
163 auto __rt = chrono::duration_cast<__clock::duration>(__rtime);
166 return _M_try_lock_until(__clock::now() + __rt);
169 template<
typename _Duration>
171 _M_try_lock_until(
const chrono::time_point<chrono::system_clock,
174 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
175 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
177 __gthread_time_t __ts = {
178 static_cast<std::time_t
>(__s.time_since_epoch().count()),
179 static_cast<long>(__ns.count())
182 return static_cast<_Derived*
>(
this)->_M_timedlock(__ts);
185#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
186 template<
typename _Duration>
188 _M_try_lock_until(
const chrono::time_point<chrono::steady_clock,
191 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
192 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
194 __gthread_time_t __ts = {
195 static_cast<std::time_t
>(__s.time_since_epoch().count()),
196 static_cast<long>(__ns.count())
199 return static_cast<_Derived*
>(
this)->_M_clocklock(CLOCK_MONOTONIC,
204 template<
typename _Clock,
typename _Duration>
206 _M_try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
208#if __cplusplus > 201703L
209 static_assert(chrono::is_clock_v<_Clock>);
214 auto __now = _Clock::now();
216 auto __rtime = __atime - __now;
217 if (_M_try_lock_for(__rtime))
219 __now = _Clock::now();
220 }
while (__atime > __now);
235 :
private __mutex_base,
public __timed_mutex_impl<timed_mutex>
238 typedef __native_type* native_handle_type;
249 int __e = __gthread_mutex_lock(&_M_mutex);
253 __throw_system_error(__e);
260 return !__gthread_mutex_trylock(&_M_mutex);
263 template <
class _Rep,
class _Period>
266 {
return _M_try_lock_for(__rtime); }
268 template <
class _Clock,
class _Duration>
271 {
return _M_try_lock_until(__atime); }
277 __gthread_mutex_unlock(&_M_mutex);
281 native_handle()
noexcept
282 {
return &_M_mutex; }
288 _M_timedlock(
const __gthread_time_t& __ts)
289 {
return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
291#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
293 _M_clocklock(clockid_t clockid,
const __gthread_time_t& __ts)
294 {
return !pthread_mutex_clocklock(&_M_mutex, clockid, &__ts); }
309 :
private __recursive_mutex_base,
310 public __timed_mutex_impl<recursive_timed_mutex>
313 typedef __native_type* native_handle_type;
324 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
328 __throw_system_error(__e);
335 return !__gthread_recursive_mutex_trylock(&_M_mutex);
338 template <
class _Rep,
class _Period>
341 {
return _M_try_lock_for(__rtime); }
343 template <
class _Clock,
class _Duration>
346 {
return _M_try_lock_until(__atime); }
352 __gthread_recursive_mutex_unlock(&_M_mutex);
356 native_handle()
noexcept
357 {
return &_M_mutex; }
363 _M_timedlock(
const __gthread_time_t& __ts)
364 {
return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
366#ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
368 _M_clocklock(clockid_t clockid,
const __gthread_time_t& __ts)
369 {
return !pthread_mutex_clocklock(&_M_mutex, clockid, &__ts); }
380 bool _M_locked =
false;
387 timed_mutex(
const timed_mutex&) =
delete;
388 timed_mutex& operator=(
const timed_mutex&) =
delete;
393 unique_lock<mutex> __lk(_M_mut);
394 _M_cv.wait(__lk, [&]{
return !_M_locked; });
401 lock_guard<mutex> __lk(_M_mut);
408 template<
typename _Rep,
typename _Period>
410 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
412 unique_lock<mutex> __lk(_M_mut);
413 if (!_M_cv.wait_for(__lk, __rtime, [&]{ return !_M_locked; }))
419 template<
typename _Clock,
typename _Duration>
421 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
423 unique_lock<mutex> __lk(_M_mut);
424 if (!_M_cv.wait_until(__lk, __atime, [&]{ return !_M_locked; }))
433 lock_guard<mutex> __lk(_M_mut);
434 __glibcxx_assert( _M_locked );
441 class recursive_timed_mutex
444 condition_variable _M_cv;
446 unsigned _M_count = 0;
453 operator()() const noexcept
454 {
return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
456 const recursive_timed_mutex* _M_mx;
457 thread::id _M_caller;
462 recursive_timed_mutex() =
default;
463 ~recursive_timed_mutex() { __glibcxx_assert( _M_count == 0 ); }
465 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
466 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
472 _Can_lock __can_lock{
this, __id};
473 unique_lock<mutex> __lk(_M_mut);
474 _M_cv.wait(__lk, __can_lock);
476 __throw_system_error(EAGAIN);
485 _Can_lock __can_lock{
this, __id};
486 lock_guard<mutex> __lk(_M_mut);
496 template<
typename _Rep,
typename _Period>
498 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
501 _Can_lock __can_lock{
this, __id};
502 unique_lock<mutex> __lk(_M_mut);
503 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
512 template<
typename _Clock,
typename _Duration>
514 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
517 _Can_lock __can_lock{
this, __id};
518 unique_lock<mutex> __lk(_M_mut);
519 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
531 lock_guard<mutex> __lk(_M_mut);
533 __glibcxx_assert( _M_count > 0 );
549 template<
typename _Lockable>
551 __try_lock_impl(_Lockable& __l)
553 if (unique_lock<_Lockable> __lock{__l,
try_to_lock})
564 template<
typename _L0,
typename... _Lockables>
566 __try_lock_impl(_L0& __l0, _Lockables&... __lockables)
568#if __cplusplus >= 201703L
569 if constexpr ((is_same_v<_L0, _Lockables> && ...))
571 constexpr int _Np = 1 +
sizeof...(_Lockables);
572 unique_lock<_L0> __locks[_Np] = {
575 for (
int __i = 0; __i < _Np; ++__i)
579 const int __failed = __i;
581 __locks[__i].unlock();
585 for (
auto& __l : __locks)
593 int __idx = __detail::__try_lock_impl(__lockables...);
618 template<
typename _L1,
typename _L2,
typename... _L3>
622 return __detail::__try_lock_impl(__l1, __l2, __l3...);
633 template<
typename _L0,
typename... _L1>
635 __lock_impl(
int& __i,
int __depth, _L0& __l0, _L1&... __l1)
637 while (__i >= __depth)
643 unique_lock<_L0> __first(__l0);
644 __failed += __detail::__try_lock_impl(__l1...);
652#if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
655 constexpr auto __n = 1 +
sizeof...(_L1);
656 __i = (__depth + __failed) % __n;
659 __detail::__lock_impl(__i, __depth + 1, __l1..., __l0);
677 template<
typename _L1,
typename _L2,
typename... _L3>
679 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
681#if __cplusplus >= 201703L
682 if constexpr (is_same_v<_L1, _L2> && (is_same_v<_L1, _L3> && ...))
684 constexpr int _Np = 2 +
sizeof...(_L3);
690 __locks[__first].lock();
691 for (
int __j = 1; __j < _Np; ++__j)
693 const int __idx = (__first + __j) % _Np;
696 for (
int __k = __j; __k != 0; --__k)
697 __locks[(__first + __k - 1) % _Np].unlock();
702 }
while (!__locks[__first].owns_lock());
704 for (
auto& __l : __locks)
711 __detail::__lock_impl(__i, 0, __l1, __l2, __l3...);
715#if __cplusplus >= 201703L
716#define __cpp_lib_scoped_lock 201703L
725 template<
typename... _MutexTypes>
737 { std::apply([](
auto&... __m) { (__m.unlock(), ...); }, _M_devices); }
743 tuple<_MutexTypes&...> _M_devices;
752 ~scoped_lock() =
default;
754 scoped_lock(
const scoped_lock&) =
delete;
755 scoped_lock& operator=(
const scoped_lock&) =
delete;
758 template<
typename _Mutex>
759 class scoped_lock<_Mutex>
762 using mutex_type = _Mutex;
764 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
765 { _M_device.lock(); }
767 explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept
772 { _M_device.unlock(); }
774 scoped_lock(
const scoped_lock&) =
delete;
775 scoped_lock& operator=(
const scoped_lock&) =
delete;
778 mutex_type& _M_device;
782#ifdef _GLIBCXX_HAS_GTHREADS
786 constexpr once_flag()
noexcept =
default;
796 __gthread_once_t _M_once = __GTHREAD_ONCE_INIT;
798 struct _Prepare_execution;
800 template<
typename _Callable,
typename... _Args>
806# ifdef _GLIBCXX_HAVE_TLS
809 extern __thread
void* __once_callable;
810 extern __thread void (*__once_call)();
813 struct once_flag::_Prepare_execution
815 template<
typename _Callable>
817 _Prepare_execution(_Callable& __c)
822 __once_call = [] { (*
static_cast<_Callable*
>(__once_callable))(); };
825 ~_Prepare_execution()
828 __once_callable =
nullptr;
829 __once_call =
nullptr;
832 _Prepare_execution(
const _Prepare_execution&) =
delete;
833 _Prepare_execution&
operator=(
const _Prepare_execution&) =
delete;
839 extern function<void()> __once_functor;
842 __set_once_functor_lock_ptr(unique_lock<mutex>*);
848 struct once_flag::_Prepare_execution
850 template<
typename _Callable>
852 _Prepare_execution(_Callable& __c)
855 __once_functor = __c;
856 __set_once_functor_lock_ptr(&_M_functor_lock);
859 ~_Prepare_execution()
862 __set_once_functor_lock_ptr(
nullptr);
867 unique_lock<mutex> _M_functor_lock{__get_once_mutex()};
869 _Prepare_execution(
const _Prepare_execution&) =
delete;
870 _Prepare_execution&
operator=(
const _Prepare_execution&) =
delete;
877 extern "C" void __once_proxy(
void);
880 template<
typename _Callable,
typename... _Args>
885 auto __callable = [&] {
887 std::forward<_Args>(__args)...);
890 once_flag::_Prepare_execution __exec(__callable);
893 if (
int __e = __gthread_once(&__once._M_once, &__once_proxy))
894 __throw_system_error(__e);
902 constexpr once_flag() noexcept = default;
916 enum _Bits :
int { _Init = 0, _Active = 1, _Done = 2 };
918 int _M_once = _Bits::_Init;
922 _M_passive() const noexcept;
930 void _M_finish(
bool __returning) noexcept;
933 struct _Active_execution
935 explicit _Active_execution(once_flag& __flag) : _M_flag(__flag) { }
937 ~_Active_execution() { _M_flag._M_finish(_M_returning); }
939 _Active_execution(
const _Active_execution&) =
delete;
940 _Active_execution&
operator=(
const _Active_execution&) =
delete;
943 bool _M_returning =
false;
946 template<
typename _Callable,
typename... _Args>
948 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
954 once_flag::_M_passive() const noexcept
955 {
return _M_once == _Bits::_Done; }
958 once_flag::_M_activate()
960 if (_M_once == _Bits::_Init) [[__likely__]]
962 _M_once = _Bits::_Active;
965 else if (_M_passive())
968 __throw_system_error(EDEADLK);
972 once_flag::_M_finish(
bool __returning)
noexcept
973 { _M_once = __returning ? _Bits::_Done : _Bits::_Init; }
976 template<
typename _Callable,
typename... _Args>
978 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
980 if (__once._M_passive())
982 else if (__once._M_activate())
984 once_flag::_Active_execution __exec(__once);
989 std::forward<_Args>(__args)...);
992 __exec._M_returning =
true;
998_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
tie
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.