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); }
113 class recursive_mutex :
private __recursive_mutex_base
116 typedef __native_type* native_handle_type;
118 recursive_mutex() =
default;
119 ~recursive_mutex() =
default;
121 recursive_mutex(
const recursive_mutex&) =
delete;
122 recursive_mutex& operator=(
const recursive_mutex&) =
delete;
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
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 __gthread_time_t __ts = chrono::__to_timeout_gthread_time_t(__atime);
183 return static_cast<_Derived*
>(
this)->_M_timedlock(__ts);
186#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
187 template<
typename _Duration>
189 _M_try_lock_until(
const chrono::time_point<chrono::steady_clock,
192 __gthread_time_t __ts = chrono::__to_timeout_gthread_time_t(__atime);
193 return static_cast<_Derived*
>(
this)->_M_clocklock(CLOCK_MONOTONIC,
198 template<
typename _Clock,
typename _Duration>
200 _M_try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
202#if __cplusplus > 201703L
203 static_assert(chrono::is_clock_v<_Clock>);
208 auto __now = _Clock::now();
210 auto __rtime = __atime - __now;
211 if (_M_try_lock_for(__rtime))
213 __now = _Clock::now();
214 }
while (__atime > __now);
229 :
private __mutex_base,
public __timed_mutex_impl<timed_mutex>
232 typedef __native_type* native_handle_type;
234 timed_mutex() =
default;
235 ~timed_mutex() =
default;
237 timed_mutex(
const timed_mutex&) =
delete;
238 timed_mutex& operator=(
const timed_mutex&) =
delete;
243 int __e = __gthread_mutex_lock(&_M_mutex);
247 __throw_system_error(__e);
255 return !__gthread_mutex_trylock(&_M_mutex);
258 template <
class _Rep,
class _Period>
262 {
return _M_try_lock_for(__rtime); }
264 template <
class _Clock,
class _Duration>
268 {
return _M_try_lock_until(__atime); }
274 __gthread_mutex_unlock(&_M_mutex);
278 native_handle()
noexcept
279 {
return &_M_mutex; }
282 friend class __timed_mutex_impl<timed_mutex>;
285 _M_timedlock(
const __gthread_time_t& __ts)
286 {
return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
288#if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
290 _M_clocklock(clockid_t __clockid,
const __gthread_time_t& __ts)
291 {
return !pthread_mutex_clocklock(&_M_mutex, __clockid, &__ts); }
305 class recursive_timed_mutex
306 :
private __recursive_mutex_base,
307 public __timed_mutex_impl<recursive_timed_mutex>
310 typedef __native_type* native_handle_type;
312 recursive_timed_mutex() =
default;
313 ~recursive_timed_mutex() =
default;
315 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
316 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
321 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
325 __throw_system_error(__e);
333 return !__gthread_recursive_mutex_trylock(&_M_mutex);
336 template <
class _Rep,
class _Period>
340 {
return _M_try_lock_for(__rtime); }
342 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; }
360 friend class __timed_mutex_impl<recursive_timed_mutex>;
363 _M_timedlock(
const __gthread_time_t& __ts)
364 {
return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
366#if _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; });
402 lock_guard<mutex> __lk(_M_mut);
409 template<
typename _Rep,
typename _Period>
412 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
414 unique_lock<mutex> __lk(_M_mut);
415 if (!_M_cv.wait_for(__lk, __rtime, [&]{ return !_M_locked; }))
421 template<
typename _Clock,
typename _Duration>
424 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
426 unique_lock<mutex> __lk(_M_mut);
427 if (!_M_cv.wait_until(__lk, __atime, [&]{ return !_M_locked; }))
436 lock_guard<mutex> __lk(_M_mut);
437 __glibcxx_assert( _M_locked );
447 condition_variable _M_cv;
449 unsigned _M_count = 0;
456 operator()() const noexcept
457 {
return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
459 const recursive_timed_mutex* _M_mx;
460 thread::id _M_caller;
465 recursive_timed_mutex() =
default;
466 ~recursive_timed_mutex() { __glibcxx_assert( _M_count == 0 ); }
468 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
469 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
475 _Can_lock __can_lock{
this, __id};
476 unique_lock<mutex> __lk(_M_mut);
477 _M_cv.wait(__lk, __can_lock);
479 __throw_system_error(EAGAIN);
489 _Can_lock __can_lock{
this, __id};
490 lock_guard<mutex> __lk(_M_mut);
500 template<
typename _Rep,
typename _Period>
503 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
506 _Can_lock __can_lock{
this, __id};
507 unique_lock<mutex> __lk(_M_mut);
508 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
517 template<
typename _Clock,
typename _Duration>
520 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
523 _Can_lock __can_lock{
this, __id};
524 unique_lock<mutex> __lk(_M_mut);
525 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
537 lock_guard<mutex> __lk(_M_mut);
539 __glibcxx_assert( _M_count > 0 );
555 template<
typename _Lockable>
557 __try_lock_impl(_Lockable& __l)
559 if (unique_lock<_Lockable> __lock{__l,
try_to_lock})
570 template<
typename _L0,
typename... _Lockables>
572 __try_lock_impl(_L0& __l0, _Lockables&... __lockables)
574#if __cplusplus >= 201703L
575 if constexpr ((is_same_v<_L0, _Lockables> && ...))
577 constexpr int _Np = 1 +
sizeof...(_Lockables);
578 unique_lock<_L0> __locks[_Np] = {
581 for (
int __i = 0; __i < _Np; ++__i)
585 const int __failed = __i;
587 __locks[__i].unlock();
591 for (
auto& __l : __locks)
599 int __idx = __detail::__try_lock_impl(__lockables...);
624 template<
typename _L1,
typename _L2,
typename... _L3>
629 return __detail::__try_lock_impl(__l1, __l2, __l3...);
640 template<
typename _L0,
typename... _L1>
642 __lock_impl(
int& __i,
int __depth, _L0& __l0, _L1&... __l1)
644 while (__i >= __depth)
650 unique_lock<_L0> __first(__l0);
651 __failed += __detail::__try_lock_impl(__l1...);
659#if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
662 constexpr auto __n = 1 +
sizeof...(_L1);
663 __i = (__depth + __failed) % __n;
666 __detail::__lock_impl(__i, __depth + 1, __l1..., __l0);
684 template<
typename _L1,
typename _L2,
typename... _L3>
686 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
688#if __cplusplus >= 201703L
689 if constexpr (is_same_v<_L1, _L2> && (is_same_v<_L1, _L3> && ...))
691 constexpr int _Np = 2 +
sizeof...(_L3);
697 __locks[__first].lock();
698 for (
int __j = 1; __j < _Np; ++__j)
700 const int __idx = (__first + __j) % _Np;
703 for (
int __k = __j; __k != 0; --__k)
704 __locks[(__first + __k - 1) % _Np].unlock();
709 }
while (!__locks[__first].owns_lock());
711 for (
auto& __l : __locks)
718 __detail::__lock_impl(__i, 0, __l1, __l2, __l3...);
722#ifdef __cpp_lib_scoped_lock
731 template<
typename... _MutexTypes>
737 explicit scoped_lock(_MutexTypes&... __m) : _M_devices(
std::tie(__m...))
741 explicit scoped_lock(adopt_lock_t, _MutexTypes&... __m) noexcept
746 { std::apply([](
auto&... __m) { (__m.unlock(), ...); }, _M_devices); }
748 scoped_lock(
const scoped_lock&) =
delete;
749 scoped_lock& operator=(
const scoped_lock&) =
delete;
752 tuple<_MutexTypes&...> _M_devices;
759 explicit scoped_lock() =
default;
760 explicit scoped_lock(adopt_lock_t)
noexcept { }
761 ~scoped_lock() =
default;
763 scoped_lock(
const scoped_lock&) =
delete;
764 scoped_lock& operator=(
const scoped_lock&) =
delete;
767 template<
typename _Mutex>
768 class scoped_lock<_Mutex>
771 using mutex_type = _Mutex;
774 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
775 { _M_device.lock(); }
778 explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept
783 { _M_device.unlock(); }
785 scoped_lock(
const scoped_lock&) =
delete;
786 scoped_lock& operator=(
const scoped_lock&) =
delete;
789 mutex_type& _M_device;
793#ifdef _GLIBCXX_HAS_GTHREADS
797 constexpr once_flag()
noexcept =
default;
807 __gthread_once_t _M_once = __GTHREAD_ONCE_INIT;
809 struct _Prepare_execution;
811 template<
typename _Callable,
typename... _Args>
813 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
817# ifdef _GLIBCXX_HAVE_TLS
820 extern __thread
void* __once_callable;
821 extern __thread void (*__once_call)();
824 struct once_flag::_Prepare_execution
826 template<
typename _Callable>
828 _Prepare_execution(_Callable& __c)
833 __once_call = [] { (*
static_cast<_Callable*
>(__once_callable))(); };
836 ~_Prepare_execution()
839 __once_callable =
nullptr;
840 __once_call =
nullptr;
843 _Prepare_execution(
const _Prepare_execution&) =
delete;
844 _Prepare_execution& operator=(
const _Prepare_execution&) =
delete;
850 extern function<void()> __once_functor;
859 struct once_flag::_Prepare_execution
861 template<
typename _Callable>
863 _Prepare_execution(_Callable& __c)
866 __once_functor = __c;
867 __set_once_functor_lock_ptr(&_M_functor_lock);
870 ~_Prepare_execution()
873 __set_once_functor_lock_ptr(
nullptr);
878 unique_lock<mutex> _M_functor_lock{__get_once_mutex()};
880 _Prepare_execution(
const _Prepare_execution&) =
delete;
881 _Prepare_execution& operator=(
const _Prepare_execution&) =
delete;
888 extern "C" void __once_proxy(
void);
891 template<
typename _Callable,
typename... _Args>
893 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
896 auto __callable = [&] {
901 once_flag::_Prepare_execution __exec(__callable);
904 if (
int __e = __gthread_once(&__once._M_once, &__once_proxy))
905 __throw_system_error(__e);
913 constexpr once_flag() noexcept = default;
927 enum _Bits :
int { _Init = 0, _Active = 1, _Done = 2 };
929 int _M_once = _Bits::_Init;
933 _M_passive() const noexcept;
941 void _M_finish(
bool __returning) noexcept;
944 struct _Active_execution
946 explicit _Active_execution(once_flag& __flag) : _M_flag(__flag) { }
948 ~_Active_execution() { _M_flag._M_finish(_M_returning); }
950 _Active_execution(
const _Active_execution&) =
delete;
951 _Active_execution&
operator=(
const _Active_execution&) =
delete;
954 bool _M_returning =
false;
957 template<
typename _Callable,
typename... _Args>
959 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
965 once_flag::_M_passive() const noexcept
966 {
return _M_once == _Bits::_Done; }
969 once_flag::_M_activate()
971 if (_M_once == _Bits::_Init) [[__likely__]]
973 _M_once = _Bits::_Active;
976 else if (_M_passive())
979 __throw_system_error(EDEADLK);
983 once_flag::_M_finish(
bool __returning)
noexcept
984 { _M_once = __returning ? _Bits::_Done : _Bits::_Init; }
987 template<
typename _Callable,
typename... _Args>
991 if (__once._M_passive())
993 else if (__once._M_activate())
995 once_flag::_Active_execution __exec(__once);
1003 __exec._M_returning =
true;
1009_GLIBCXX_END_NAMESPACE_VERSION
constexpr __enable_if_is_duration< _ToDur > duration_cast(const duration< _Rep, _Period > &__d)
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.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
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.
Implementation details not part of the namespace std interface.
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.