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
144 using __clock = chrono::steady_clock;
146 using __clock = chrono::system_clock;
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,
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,
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); }
342 #else // !_GTHREAD_USE_MUTEX_TIMEDLOCK
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; }))
402 lock_guard<mutex> __lk(_M_mut);
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 );
512 #endif // _GLIBCXX_HAS_GTHREADS
515 template<
typename _Lock>
517 __try_to_lock(_Lock& __l)
520 template<
int _Idx,
bool _Continue = true>
521 struct __try_lock_impl
523 template<
typename... _Lock>
525 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
528 auto __lock = std::__try_to_lock(std::get<_Idx>(__locks));
529 if (__lock.owns_lock())
531 constexpr
bool __cont = _Idx + 2 <
sizeof...(_Lock);
532 using __try_locker = __try_lock_impl<_Idx + 1, __cont>;
533 __try_locker::__do_try_lock(__locks, __idx);
541 struct __try_lock_impl<_Idx, false>
543 template<
typename... _Lock>
545 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
548 auto __lock = std::__try_to_lock(std::get<_Idx>(__locks));
549 if (__lock.owns_lock())
568 template<
typename _Lock1,
typename _Lock2,
typename... _Lock3>
570 try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
573 auto __locks =
std::tie(__l1, __l2, __l3...);
574 __try_lock_impl<0>::__do_try_lock(__locks, __idx);
589 template<
typename _L1,
typename _L2,
typename... _L3>
591 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
595 using __try_locker = __try_lock_impl<0,
sizeof...(_L3) != 0>;
598 auto __locks =
std::tie(__l2, __l3...);
599 __try_locker::__do_try_lock(__locks, __idx);
608 #if __cplusplus >= 201703L
609 #define __cpp_lib_scoped_lock 201703
615 template<
typename... _MutexTypes>
627 { std::apply([](
auto&... __m) { (__m.unlock(), ...); }, _M_devices); }
633 tuple<_MutexTypes&...> _M_devices;
642 ~scoped_lock() =
default;
644 scoped_lock(
const scoped_lock&) =
delete;
645 scoped_lock& operator=(
const scoped_lock&) =
delete;
648 template<
typename _Mutex>
649 class scoped_lock<_Mutex>
652 using mutex_type = _Mutex;
654 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
655 { _M_device.lock(); }
657 explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept
662 { _M_device.unlock(); }
664 scoped_lock(
const scoped_lock&) =
delete;
665 scoped_lock& operator=(
const scoped_lock&) =
delete;
668 mutex_type& _M_device;
672 #ifdef _GLIBCXX_HAS_GTHREADS
676 constexpr
once_flag() noexcept =
default;
686 __gthread_once_t _M_once = __GTHREAD_ONCE_INIT;
688 struct _Prepare_execution;
690 template<
typename _Callable,
typename... _Args>
696 # ifdef _GLIBCXX_HAVE_TLS
699 extern __thread
void* __once_callable;
700 extern __thread void (*__once_call)();
703 struct once_flag::_Prepare_execution
705 template<
typename _Callable>
707 _Prepare_execution(_Callable& __c)
712 __once_call = [] { (*
static_cast<_Callable*
>(__once_callable))(); };
715 ~_Prepare_execution()
718 __once_callable =
nullptr;
719 __once_call =
nullptr;
722 _Prepare_execution(
const _Prepare_execution&) =
delete;
723 _Prepare_execution&
operator=(
const _Prepare_execution&) =
delete;
729 extern function<void()> __once_functor;
732 __set_once_functor_lock_ptr(unique_lock<mutex>*);
738 struct once_flag::_Prepare_execution
740 template<
typename _Callable>
742 _Prepare_execution(_Callable& __c)
745 __once_functor = __c;
746 __set_once_functor_lock_ptr(&_M_functor_lock);
749 ~_Prepare_execution()
752 __set_once_functor_lock_ptr(
nullptr);
757 unique_lock<mutex> _M_functor_lock{__get_once_mutex()};
759 _Prepare_execution(
const _Prepare_execution&) =
delete;
760 _Prepare_execution&
operator=(
const _Prepare_execution&) =
delete;
767 extern "C" void __once_proxy(
void);
770 template<
typename _Callable,
typename... _Args>
775 auto __callable = [&] {
776 std::__invoke(std::forward<_Callable>(__f),
777 std::forward<_Args>(__args)...);
780 once_flag::_Prepare_execution __exec(__callable);
783 if (
int __e = __gthread_once(&__once._M_once, &__once_proxy))
784 __throw_system_error(__e);
787 #else // _GLIBCXX_HAS_GTHREADS
792 constexpr once_flag() noexcept = default;
795 once_flag(const once_flag&) = delete;
797 once_flag& operator=(const once_flag&) = delete;
806 enum _Bits :
int { _Init = 0, _Active = 1, _Done = 2 };
808 int _M_once = _Bits::_Init;
812 _M_passive() const noexcept;
820 void _M_finish(
bool __returning) noexcept;
823 struct _Active_execution
825 explicit _Active_execution(once_flag& __flag) : _M_flag(__flag) { }
827 ~_Active_execution() { _M_flag._M_finish(_M_returning); }
829 _Active_execution(
const _Active_execution&) =
delete;
830 _Active_execution&
operator=(
const _Active_execution&) =
delete;
833 bool _M_returning =
false;
836 template<
typename _Callable,
typename... _Args>
838 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
844 once_flag::_M_passive() const noexcept
845 {
return _M_once == _Bits::_Done; }
848 once_flag::_M_activate()
850 if (_M_once == _Bits::_Init) [[__likely__]]
852 _M_once = _Bits::_Active;
855 else if (_M_passive())
858 __throw_system_error(EDEADLK);
862 once_flag::_M_finish(
bool __returning) noexcept
863 { _M_once = __returning ? _Bits::_Done : _Bits::_Init; }
866 template<
typename _Callable,
typename... _Args>
868 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
870 if (__once._M_passive())
872 else if (__once._M_activate())
874 once_flag::_Active_execution __exec(__once);
878 std::__invoke(std::forward<_Callable>(__f),
879 std::forward<_Args>(__args)...);
882 __exec._M_returning =
true;
885 #endif // _GLIBCXX_HAS_GTHREADS
888 _GLIBCXX_END_NAMESPACE_VERSION
893 #endif // _GLIBCXX_MUTEX
The standard recursive mutex type.
constexpr try_to_lock_t try_to_lock
Tag used to prevent a scoped lock from blocking if a mutex is locked.
constexpr enable_if< __is_duration< _ToDur >::value, time_point< _Clock, _ToDur > >::type time_point_cast(const time_point< _Clock, _Dur > &__t)
time_point_cast
constexpr tuple< _Elements &...> tie(_Elements &...__args) noexcept
tie
int try_lock(_Lock1 &__l1, _Lock2 &__l2, _Lock3 &...__l3)
Generic try_lock.
A scoped lock type for multiple lockable objects.
Flag type used by std::call_once.
once_flag & operator=(const once_flag &)=delete
Deleted assignment operator.
A simple scoped lock type.
duration< int64_t, nano > nanoseconds
nanoseconds
friend void call_once(once_flag &__once, _Callable &&__f, _Args &&...__args)
Invoke a callable and synchronize with other calls using the same flag.
thread::id get_id() noexcept
this_thread::get_id
A movable scoped lock type.
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic lock.
duration< int64_t > seconds
seconds
constexpr __enable_if_is_duration< _ToDur > duration_cast(const duration< _Rep, _Period > &__d)
duration_cast
Assume the calling thread has already obtained mutex ownership and manage it.
Primary class template, tuple.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
void call_once(once_flag &__once, _Callable &&__f, _Args &&...__args)
Invoke a callable and synchronize with other calls using the same flag.
auto_ptr & operator=(auto_ptr &__a)
auto_ptr assignment operator.