29 #ifndef _GLIBCXX_MUTEX
30 #define _GLIBCXX_MUTEX 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
45 #if ! _GTHREAD_USE_MUTEX_TIMEDLOCK
51 #if defined _GLIBCXX_HAS_GTHREADS && ! defined _GLIBCXX_HAVE_TLS
55 namespace std _GLIBCXX_VISIBILITY(default)
57 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 #ifdef _GLIBCXX_HAS_GTHREADS
67 class __recursive_mutex_base
70 typedef __gthread_recursive_mutex_t __native_type;
72 __recursive_mutex_base(
const __recursive_mutex_base&) =
delete;
73 __recursive_mutex_base& operator=(
const __recursive_mutex_base&) =
delete;
75 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
76 __native_type _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
78 __recursive_mutex_base() =
default;
80 __native_type _M_mutex;
82 __recursive_mutex_base()
85 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
88 ~__recursive_mutex_base()
89 { __gthread_recursive_mutex_destroy(&_M_mutex); }
97 typedef __native_type* native_handle_type;
108 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
112 __throw_system_error(__e);
119 return !__gthread_recursive_mutex_trylock(&_M_mutex);
126 __gthread_recursive_mutex_unlock(&_M_mutex);
130 native_handle() noexcept
131 {
return &_M_mutex; }
134 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
135 template<
typename _Derived>
136 class __timed_mutex_impl
139 template<
typename _Rep,
typename _Period>
143 #if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
149 auto __rt = chrono::duration_cast<__clock::duration>(__rtime);
152 return _M_try_lock_until(__clock::now() + __rt);
155 template<
typename _Duration>
157 _M_try_lock_until(
const chrono::time_point<chrono::system_clock,
160 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
161 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
163 __gthread_time_t __ts = {
164 static_cast<std::time_t
>(__s.time_since_epoch().count()),
165 static_cast<long>(__ns.count())
168 return static_cast<_Derived*
>(
this)->_M_timedlock(__ts);
171 #ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
172 template<
typename _Duration>
174 _M_try_lock_until(
const chrono::time_point<chrono::steady_clock,
177 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
178 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
180 __gthread_time_t __ts = {
181 static_cast<std::time_t
>(__s.time_since_epoch().count()),
182 static_cast<long>(__ns.count())
185 return static_cast<_Derived*
>(
this)->_M_clocklock(CLOCK_MONOTONIC,
190 template<
typename _Clock,
typename _Duration>
192 _M_try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
194 #if __cplusplus > 201703L
195 static_assert(chrono::is_clock_v<_Clock>);
200 auto __now = _Clock::now();
202 auto __rtime = __atime - __now;
203 if (_M_try_lock_for(__rtime))
205 __now = _Clock::now();
206 }
while (__atime > __now);
213 :
private __mutex_base,
public __timed_mutex_impl<timed_mutex>
216 typedef __native_type* native_handle_type;
218 timed_mutex() =
default;
219 ~timed_mutex() =
default;
221 timed_mutex(
const timed_mutex&) =
delete;
222 timed_mutex& operator=(
const timed_mutex&) =
delete;
227 int __e = __gthread_mutex_lock(&_M_mutex);
231 __throw_system_error(__e);
238 return !__gthread_mutex_trylock(&_M_mutex);
241 template <
class _Rep,
class _Period>
243 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
244 {
return _M_try_lock_for(__rtime); }
246 template <
class _Clock,
class _Duration>
248 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
249 {
return _M_try_lock_until(__atime); }
255 __gthread_mutex_unlock(&_M_mutex);
259 native_handle() noexcept
260 {
return &_M_mutex; }
263 friend class __timed_mutex_impl<timed_mutex>;
266 _M_timedlock(
const __gthread_time_t& __ts)
267 {
return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
269 #if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
271 _M_clocklock(clockid_t clockid,
const __gthread_time_t& __ts)
272 {
return !pthread_mutex_clocklock(&_M_mutex, clockid, &__ts); }
277 class recursive_timed_mutex
278 :
private __recursive_mutex_base,
279 public __timed_mutex_impl<recursive_timed_mutex>
282 typedef __native_type* native_handle_type;
284 recursive_timed_mutex() =
default;
285 ~recursive_timed_mutex() =
default;
287 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
288 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
293 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
297 __throw_system_error(__e);
304 return !__gthread_recursive_mutex_trylock(&_M_mutex);
307 template <
class _Rep,
class _Period>
309 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
310 {
return _M_try_lock_for(__rtime); }
312 template <
class _Clock,
class _Duration>
314 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
315 {
return _M_try_lock_until(__atime); }
321 __gthread_recursive_mutex_unlock(&_M_mutex);
325 native_handle() noexcept
326 {
return &_M_mutex; }
329 friend class __timed_mutex_impl<recursive_timed_mutex>;
332 _M_timedlock(
const __gthread_time_t& __ts)
333 {
return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
335 #ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
337 _M_clocklock(clockid_t clockid,
const __gthread_time_t& __ts)
338 {
return !pthread_mutex_clocklock(&_M_mutex, clockid, &__ts); }
349 bool _M_locked =
false;
363 _M_cv.wait(__lk, [&]{
return !_M_locked; });
377 template<
typename _Rep,
typename _Period>
382 if (!_M_cv.wait_for(__lk, __rtime, [&]{ return !_M_locked; }))
388 template<
typename _Clock,
typename _Duration>
393 if (!_M_cv.wait_until(__lk, __atime, [&]{ return !_M_locked; }))
403 __glibcxx_assert( _M_locked );
415 unsigned _M_count = 0;
422 operator()()
const noexcept
423 {
return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
441 _Can_lock __can_lock{
this, __id};
443 _M_cv.wait(__lk, __can_lock);
445 __throw_system_error(EAGAIN);
454 _Can_lock __can_lock{
this, __id};
465 template<
typename _Rep,
typename _Period>
470 _Can_lock __can_lock{
this, __id};
472 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
481 template<
typename _Clock,
typename _Duration>
486 _Can_lock __can_lock{
this, __id};
488 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
502 __glibcxx_assert( _M_count > 0 );
518 template<
typename _Lockable>
520 __try_lock_impl(_Lockable& __l)
533 template<
typename _L0,
typename... _Lockables>
535 __try_lock_impl(_L0& __l0, _Lockables&... __lockables)
537 #if __cplusplus >= 201703L
538 if constexpr ((is_same_v<_L0, _Lockables> && ...))
540 constexpr
int _Np = 1 +
sizeof...(_Lockables);
541 unique_lock<_L0> __locks[_Np] = {
544 for (
int __i = 0; __i < _Np; ++__i)
548 const int __failed = __i;
550 __locks[__i].unlock();
554 for (
auto& __l : __locks)
562 int __idx = __detail::__try_lock_impl(__lockables...);
587 template<
typename _L1,
typename _L2,
typename... _L3>
591 return __detail::__try_lock_impl(__l1, __l2, __l3...);
602 template<
typename _L0,
typename... _L1>
604 __lock_impl(
int& __i,
int __depth, _L0& __l0, _L1&... __l1)
606 while (__i >= __depth)
612 unique_lock<_L0> __first(__l0);
613 __failed += __detail::__try_lock_impl(__l1...);
621 #if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
624 constexpr
auto __n = 1 +
sizeof...(_L1);
625 __i = (__depth + __failed) % __n;
628 __detail::__lock_impl(__i, __depth + 1, __l1..., __l0);
646 template<
typename _L1,
typename _L2,
typename... _L3>
648 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
650 #if __cplusplus >= 201703L
651 if constexpr (is_same_v<_L1, _L2> && (is_same_v<_L1, _L3> && ...))
653 constexpr
int _Np = 2 +
sizeof...(_L3);
659 __locks[__first].lock();
660 for (
int __j = 1; __j < _Np; ++__j)
662 const int __idx = (__first + __j) % _Np;
665 for (
int __k = __j; __k != 0; --__k)
666 __locks[(__first + __k - 1) % _Np].unlock();
671 }
while (!__locks[__first].owns_lock());
673 for (
auto& __l : __locks)
680 __detail::__lock_impl(__i, 0, __l1, __l2, __l3...);
684 #if __cplusplus >= 201703L
685 #define __cpp_lib_scoped_lock 201703L
691 template<
typename... _MutexTypes>
703 { std::apply([](
auto&... __m) { (__m.unlock(), ...); }, _M_devices); }
709 tuple<_MutexTypes&...> _M_devices;
718 ~scoped_lock() =
default;
720 scoped_lock(
const scoped_lock&) =
delete;
721 scoped_lock& operator=(
const scoped_lock&) =
delete;
724 template<
typename _Mutex>
725 class scoped_lock<_Mutex>
728 using mutex_type = _Mutex;
730 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
731 { _M_device.lock(); }
733 explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept
738 { _M_device.unlock(); }
740 scoped_lock(
const scoped_lock&) =
delete;
741 scoped_lock& operator=(
const scoped_lock&) =
delete;
744 mutex_type& _M_device;
748 #ifdef _GLIBCXX_HAS_GTHREADS
752 constexpr
once_flag() noexcept =
default;
762 __gthread_once_t _M_once = __GTHREAD_ONCE_INIT;
764 struct _Prepare_execution;
766 template<
typename _Callable,
typename... _Args>
772 # ifdef _GLIBCXX_HAVE_TLS
775 extern __thread
void* __once_callable;
776 extern __thread void (*__once_call)();
779 struct once_flag::_Prepare_execution
781 template<
typename _Callable>
783 _Prepare_execution(_Callable& __c)
788 __once_call = [] { (*
static_cast<_Callable*
>(__once_callable))(); };
791 ~_Prepare_execution()
794 __once_callable =
nullptr;
795 __once_call =
nullptr;
798 _Prepare_execution(
const _Prepare_execution&) =
delete;
799 _Prepare_execution&
operator=(
const _Prepare_execution&) =
delete;
805 extern function<void()> __once_functor;
808 __set_once_functor_lock_ptr(unique_lock<mutex>*);
814 struct once_flag::_Prepare_execution
816 template<
typename _Callable>
818 _Prepare_execution(_Callable& __c)
821 __once_functor = __c;
822 __set_once_functor_lock_ptr(&_M_functor_lock);
825 ~_Prepare_execution()
828 __set_once_functor_lock_ptr(
nullptr);
833 unique_lock<mutex> _M_functor_lock{__get_once_mutex()};
835 _Prepare_execution(
const _Prepare_execution&) =
delete;
836 _Prepare_execution&
operator=(
const _Prepare_execution&) =
delete;
843 extern "C" void __once_proxy(
void);
846 template<
typename _Callable,
typename... _Args>
851 auto __callable = [&] {
853 std::forward<_Args>(__args)...);
856 once_flag::_Prepare_execution __exec(__callable);
859 if (
int __e = __gthread_once(&__once._M_once, &__once_proxy))
860 __throw_system_error(__e);
868 constexpr
once_flag() noexcept = default;
882 enum _Bits :
int { _Init = 0, _Active = 1, _Done = 2 };
884 int _M_once = _Bits::_Init;
888 _M_passive() const noexcept;
896 void _M_finish(
bool __returning) noexcept;
899 struct _Active_execution
901 explicit _Active_execution(once_flag& __flag) : _M_flag(__flag) { }
903 ~_Active_execution() { _M_flag._M_finish(_M_returning); }
905 _Active_execution(
const _Active_execution&) =
delete;
906 _Active_execution&
operator=(
const _Active_execution&) =
delete;
909 bool _M_returning =
false;
912 template<
typename _Callable,
typename... _Args>
914 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
920 once_flag::_M_passive() const noexcept
921 {
return _M_once == _Bits::_Done; }
924 once_flag::_M_activate()
926 if (_M_once == _Bits::_Init) [[__likely__]]
928 _M_once = _Bits::_Active;
931 else if (_M_passive())
934 __throw_system_error(EDEADLK);
938 once_flag::_M_finish(
bool __returning) noexcept
939 { _M_once = __returning ? _Bits::_Done : _Bits::_Init; }
942 template<
typename _Callable,
typename... _Args>
944 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
946 if (__once._M_passive())
948 else if (__once._M_activate())
950 once_flag::_Active_execution __exec(__once);
955 std::forward<_Args>(__args)...);
958 __exec._M_returning =
true;
964 _GLIBCXX_END_NAMESPACE_VERSION
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
constexpr tuple< _Elements &... > tie(_Elements &... __args) noexcept
tie
constexpr __invoke_result< _Callable, _Args... >::type __invoke(_Callable &&__fn, _Args &&... __args) noexcept(__is_nothrow_invocable< _Callable, _Args... >::value)
Invoke a callable object.
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
this_thread::get_id
The standard recursive mutex type.
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 simple scoped lock type.
A movable scoped lock type.