29 #ifndef _GLIBCXX_MUTEX
30 #define _GLIBCXX_MUTEX 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
45 #if ! _GTHREAD_USE_MUTEX_TIMEDLOCK
49 #ifndef _GLIBCXX_HAVE_TLS
53 namespace std _GLIBCXX_VISIBILITY(default)
55 _GLIBCXX_BEGIN_NAMESPACE_VERSION
62 #ifdef _GLIBCXX_HAS_GTHREADS
65 class __recursive_mutex_base
68 typedef __gthread_recursive_mutex_t __native_type;
70 __recursive_mutex_base(
const __recursive_mutex_base&) =
delete;
71 __recursive_mutex_base& operator=(
const __recursive_mutex_base&) =
delete;
73 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
74 __native_type _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
76 __recursive_mutex_base() =
default;
78 __native_type _M_mutex;
80 __recursive_mutex_base()
83 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
86 ~__recursive_mutex_base()
87 { __gthread_recursive_mutex_destroy(&_M_mutex); }
95 typedef __native_type* native_handle_type;
106 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
110 __throw_system_error(__e);
117 return !__gthread_recursive_mutex_trylock(&_M_mutex);
124 __gthread_recursive_mutex_unlock(&_M_mutex);
128 native_handle() noexcept
129 {
return &_M_mutex; }
132 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
133 template<
typename _Derived>
134 class __timed_mutex_impl
137 template<
typename _Rep,
typename _Period>
141 #if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
142 using __clock = chrono::steady_clock;
144 using __clock = chrono::system_clock;
150 return _M_try_lock_until(__clock::now() + __rt);
153 template<
typename _Duration>
155 _M_try_lock_until(
const chrono::time_point<chrono::system_clock,
161 __gthread_time_t __ts = {
162 static_cast<std::time_t
>(__s.time_since_epoch().count()),
163 static_cast<long>(__ns.count())
166 return static_cast<_Derived*
>(
this)->_M_timedlock(__ts);
169 #ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
170 template<
typename _Duration>
172 _M_try_lock_until(
const chrono::time_point<chrono::steady_clock,
178 __gthread_time_t __ts = {
179 static_cast<std::time_t
>(__s.time_since_epoch().count()),
180 static_cast<long>(__ns.count())
183 return static_cast<_Derived*
>(
this)->_M_clocklock(CLOCK_MONOTONIC,
188 template<
typename _Clock,
typename _Duration>
190 _M_try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
192 #if __cplusplus > 201703L
193 static_assert(chrono::is_clock_v<_Clock>);
198 auto __now = _Clock::now();
200 auto __rtime = __atime - __now;
201 if (_M_try_lock_for(__rtime))
203 __now = _Clock::now();
204 }
while (__atime > __now);
211 :
private __mutex_base,
public __timed_mutex_impl<timed_mutex>
214 typedef __native_type* native_handle_type;
216 timed_mutex() =
default;
217 ~timed_mutex() =
default;
219 timed_mutex(
const timed_mutex&) =
delete;
220 timed_mutex& operator=(
const timed_mutex&) =
delete;
225 int __e = __gthread_mutex_lock(&_M_mutex);
229 __throw_system_error(__e);
236 return !__gthread_mutex_trylock(&_M_mutex);
239 template <
class _Rep,
class _Period>
241 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
242 {
return _M_try_lock_for(__rtime); }
244 template <
class _Clock,
class _Duration>
246 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
247 {
return _M_try_lock_until(__atime); }
253 __gthread_mutex_unlock(&_M_mutex);
257 native_handle() noexcept
258 {
return &_M_mutex; }
261 friend class __timed_mutex_impl<timed_mutex>;
264 _M_timedlock(
const __gthread_time_t& __ts)
265 {
return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
267 #if _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
269 _M_clocklock(clockid_t clockid,
const __gthread_time_t& __ts)
270 {
return !pthread_mutex_clocklock(&_M_mutex, clockid, &__ts); }
275 class recursive_timed_mutex
276 :
private __recursive_mutex_base,
277 public __timed_mutex_impl<recursive_timed_mutex>
280 typedef __native_type* native_handle_type;
282 recursive_timed_mutex() =
default;
283 ~recursive_timed_mutex() =
default;
285 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
286 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
291 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
295 __throw_system_error(__e);
302 return !__gthread_recursive_mutex_trylock(&_M_mutex);
305 template <
class _Rep,
class _Period>
307 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
308 {
return _M_try_lock_for(__rtime); }
310 template <
class _Clock,
class _Duration>
312 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
313 {
return _M_try_lock_until(__atime); }
319 __gthread_recursive_mutex_unlock(&_M_mutex);
323 native_handle() noexcept
324 {
return &_M_mutex; }
327 friend class __timed_mutex_impl<recursive_timed_mutex>;
330 _M_timedlock(
const __gthread_time_t& __ts)
331 {
return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
333 #ifdef _GLIBCXX_USE_PTHREAD_MUTEX_CLOCKLOCK
335 _M_clocklock(clockid_t clockid,
const __gthread_time_t& __ts)
336 {
return !pthread_mutex_clocklock(&_M_mutex, clockid, &__ts); }
340 #else // !_GTHREAD_USE_MUTEX_TIMEDLOCK
347 bool _M_locked =
false;
361 _M_cv.wait(__lk, [&]{
return !_M_locked; });
375 template<
typename _Rep,
typename _Period>
380 if (!_M_cv.wait_for(__lk, __rtime, [&]{
return !_M_locked; }))
386 template<
typename _Clock,
typename _Duration>
391 if (!_M_cv.wait_until(__lk, __atime, [&]{
return !_M_locked; }))
400 lock_guard<mutex> __lk(_M_mut);
401 __glibcxx_assert( _M_locked );
413 unsigned _M_count = 0;
420 operator()()
const noexcept
421 {
return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
439 _Can_lock __can_lock{
this, __id};
441 _M_cv.wait(__lk, __can_lock);
443 __throw_system_error(EAGAIN);
452 _Can_lock __can_lock{
this, __id};
463 template<
typename _Rep,
typename _Period>
468 _Can_lock __can_lock{
this, __id};
470 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
479 template<
typename _Clock,
typename _Duration>
484 _Can_lock __can_lock{
this, __id};
486 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
500 __glibcxx_assert( _M_count > 0 );
510 #endif // _GLIBCXX_HAS_GTHREADS
513 template<
typename _Lock>
515 __try_to_lock(_Lock& __l)
518 template<
int _Idx,
bool _Continue = true>
519 struct __try_lock_impl
521 template<
typename... _Lock>
523 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
526 auto __lock = std::__try_to_lock(std::get<_Idx>(__locks));
527 if (__lock.owns_lock())
529 constexpr
bool __cont = _Idx + 2 <
sizeof...(_Lock);
530 using __try_locker = __try_lock_impl<_Idx + 1, __cont>;
531 __try_locker::__do_try_lock(__locks, __idx);
539 struct __try_lock_impl<_Idx, false>
541 template<
typename... _Lock>
543 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
546 auto __lock = std::__try_to_lock(std::get<_Idx>(__locks));
547 if (__lock.owns_lock())
566 template<
typename _Lock1,
typename _Lock2,
typename... _Lock3>
568 try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
571 auto __locks =
std::tie(__l1, __l2, __l3...);
572 __try_lock_impl<0>::__do_try_lock(__locks, __idx);
587 template<
typename _L1,
typename _L2,
typename... _L3>
589 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
593 using __try_locker = __try_lock_impl<0,
sizeof...(_L3) != 0>;
596 auto __locks =
std::tie(__l2, __l3...);
597 __try_locker::__do_try_lock(__locks, __idx);
606 #if __cplusplus >= 201703L
607 #define __cpp_lib_scoped_lock 201703
613 template<
typename... _MutexTypes>
617 explicit scoped_lock(_MutexTypes&... __m) : _M_devices(std::
tie(__m...))
620 explicit scoped_lock(adopt_lock_t, _MutexTypes&... __m) noexcept
625 { std::apply([](
auto&... __m) { (__m.unlock(), ...); }, _M_devices); }
627 scoped_lock(
const scoped_lock&) =
delete;
628 scoped_lock& operator=(
const scoped_lock&) =
delete;
631 tuple<_MutexTypes&...> _M_devices;
638 explicit scoped_lock() =
default;
639 explicit scoped_lock(adopt_lock_t) noexcept { }
640 ~scoped_lock() =
default;
642 scoped_lock(
const scoped_lock&) =
delete;
643 scoped_lock& operator=(
const scoped_lock&) =
delete;
646 template<
typename _Mutex>
647 class scoped_lock<_Mutex>
650 using mutex_type = _Mutex;
652 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
653 { _M_device.lock(); }
655 explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept
660 { _M_device.unlock(); }
662 scoped_lock(
const scoped_lock&) =
delete;
663 scoped_lock& operator=(
const scoped_lock&) =
delete;
666 mutex_type& _M_device;
670 #ifdef _GLIBCXX_HAS_GTHREADS
675 typedef __gthread_once_t __native_type;
676 __native_type _M_once = __GTHREAD_ONCE_INIT;
680 constexpr
once_flag() noexcept =
default;
687 template<
typename _Callable,
typename... _Args>
693 #ifdef _GLIBCXX_HAVE_TLS
694 extern __thread
void* __once_callable;
695 extern __thread void (*__once_call)();
697 extern function<void()> __once_functor;
706 extern "C" void __once_proxy(
void);
710 template<
typename _Callable,
typename... _Args>
716 auto __callable = [&] {
717 std::__invoke(std::forward<_Callable>(__f),
718 std::forward<_Args>(__args)...);
720 #ifdef _GLIBCXX_HAVE_TLS
722 __once_call = []{ (*(decltype(__callable)*)__once_callable)(); };
725 __once_functor = __callable;
726 __set_once_functor_lock_ptr(&__functor_lock);
729 int __e = __gthread_once(&__once._M_once, &__once_proxy);
731 #ifndef _GLIBCXX_HAVE_TLS
733 __set_once_functor_lock_ptr(0);
737 __throw_system_error(__e);
739 #endif // _GLIBCXX_HAS_GTHREADS
742 _GLIBCXX_END_NAMESPACE_VERSION
747 #endif // _GLIBCXX_MUTEX
once_flag & operator=(const once_flag &)=delete
Deleted assignment operator.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
A movable scoped lock type.
constexpr once_flag() noexcept=default
Constructor.
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic lock.
A simple scoped lock type.
constexpr try_to_lock_t try_to_lock
Tag used to prevent a scoped lock from blocking if a mutex is locked.
duration< int64_t, nano > nanoseconds
nanoseconds
Flag type used by std::call_once.
constexpr tuple< _Elements &...> tie(_Elements &...__args) noexcept
tie
int try_lock(_Lock1 &__l1, _Lock2 &__l2, _Lock3 &...__l3)
Generic try_lock.
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 __enable_if_is_duration< _ToDur > duration_cast(const duration< _Rep, _Period > &__d)
duration_cast
duration< int64_t > seconds
seconds
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
get_id
The standard recursive mutex type.
void call_once(once_flag &__once, _Callable &&__f, _Args &&...__args)
Invoke a callable and synchronize with other calls using the same flag.