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);
129 {
return &_M_mutex; }
132 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
133 template<
typename _Derived>
134 class __timed_mutex_impl
137 typedef chrono::high_resolution_clock __clock_t;
139 template<
typename _Rep,
typename _Period>
143 using chrono::steady_clock;
145 if (ratio_greater<steady_clock::period, _Period>())
147 return _M_try_lock_until(steady_clock::now() + __rt);
150 template<
typename _Duration>
152 _M_try_lock_until(
const chrono::time_point<__clock_t,
158 __gthread_time_t __ts = {
159 static_cast<std::time_t
>(__s.time_since_epoch().count()),
160 static_cast<long>(__ns.count())
163 return static_cast<_Derived*
>(
this)->_M_timedlock(__ts);
166 template<
typename _Clock,
typename _Duration>
168 _M_try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
170 auto __rtime = __atime - _Clock::now();
171 return _M_try_lock_until(__clock_t::now() + __rtime);
177 :
private __mutex_base,
public __timed_mutex_impl<timed_mutex>
180 typedef __native_type* native_handle_type;
182 timed_mutex() =
default;
183 ~timed_mutex() =
default;
185 timed_mutex(
const timed_mutex&) =
delete;
186 timed_mutex& operator=(
const timed_mutex&) =
delete;
191 int __e = __gthread_mutex_lock(&_M_mutex);
195 __throw_system_error(__e);
202 return !__gthread_mutex_trylock(&_M_mutex);
205 template <
class _Rep,
class _Period>
207 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
208 {
return _M_try_lock_for(__rtime); }
210 template <
class _Clock,
class _Duration>
212 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
213 {
return _M_try_lock_until(__atime); }
219 __gthread_mutex_unlock(&_M_mutex);
224 {
return &_M_mutex; }
227 friend class __timed_mutex_impl<timed_mutex>;
230 _M_timedlock(
const __gthread_time_t& __ts)
231 {
return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
235 class recursive_timed_mutex
236 :
private __recursive_mutex_base,
237 public __timed_mutex_impl<recursive_timed_mutex>
240 typedef __native_type* native_handle_type;
242 recursive_timed_mutex() =
default;
243 ~recursive_timed_mutex() =
default;
245 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
246 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
251 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
255 __throw_system_error(__e);
262 return !__gthread_recursive_mutex_trylock(&_M_mutex);
265 template <
class _Rep,
class _Period>
267 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
268 {
return _M_try_lock_for(__rtime); }
270 template <
class _Clock,
class _Duration>
272 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
273 {
return _M_try_lock_until(__atime); }
279 __gthread_recursive_mutex_unlock(&_M_mutex);
284 {
return &_M_mutex; }
287 friend class __timed_mutex_impl<recursive_timed_mutex>;
290 _M_timedlock(
const __gthread_time_t& __ts)
291 {
return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
294 #else // !_GTHREAD_USE_MUTEX_TIMEDLOCK
301 bool _M_locked =
false;
315 _M_cv.wait(__lk, [&]{
return !_M_locked; });
329 template<
typename _Rep,
typename _Period>
334 if (!_M_cv.wait_for(__lk, __rtime, [&]{
return !_M_locked; }))
340 template<
typename _Clock,
typename _Duration>
345 if (!_M_cv.wait_until(__lk, __atime, [&]{
return !_M_locked; }))
354 lock_guard<mutex> __lk(_M_mut);
355 __glibcxx_assert( _M_locked );
367 unsigned _M_count = 0;
375 {
return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
393 _Can_lock __can_lock{
this, __id};
395 _M_cv.wait(__lk, __can_lock);
397 __throw_system_error(EAGAIN);
406 _Can_lock __can_lock{
this, __id};
417 template<
typename _Rep,
typename _Period>
422 _Can_lock __can_lock{
this, __id};
424 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
433 template<
typename _Clock,
typename _Duration>
438 _Can_lock __can_lock{
this, __id};
440 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
454 __glibcxx_assert( _M_count > 0 );
464 #endif // _GLIBCXX_HAS_GTHREADS
466 template<
typename _Lock>
471 template<
int _Idx,
bool _Continue = true>
472 struct __try_lock_impl
474 template<
typename... _Lock>
476 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
480 if (__lock.owns_lock())
482 constexpr
bool __cont = _Idx + 2 <
sizeof...(_Lock);
483 using __try_locker = __try_lock_impl<_Idx + 1, __cont>;
484 __try_locker::__do_try_lock(__locks, __idx);
492 struct __try_lock_impl<_Idx, false>
494 template<
typename... _Lock>
496 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
500 if (__lock.owns_lock())
518 template<
typename _Lock1,
typename _Lock2,
typename... _Lock3>
520 try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
523 auto __locks =
std::tie(__l1, __l2, __l3...);
524 __try_lock_impl<0>::__do_try_lock(__locks, __idx);
539 template<
typename _L1,
typename _L2,
typename... _L3>
541 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
545 using __try_locker = __try_lock_impl<0,
sizeof...(_L3) != 0>;
548 auto __locks =
std::tie(__l2, __l3...);
549 __try_locker::__do_try_lock(__locks, __idx);
558 #if __cplusplus >= 201703L
559 #define __cpp_lib_scoped_lock 201703
565 template<
typename... _MutexTypes>
569 explicit scoped_lock(_MutexTypes&... __m) : _M_devices(std::
tie(__m...))
572 explicit scoped_lock(adopt_lock_t, _MutexTypes&... __m)
noexcept
578 std::apply([](_MutexTypes&... __m) {
579 char __i[] __attribute__((__unused__)) = { (__m.unlock(), 0)... };
583 scoped_lock(
const scoped_lock&) =
delete;
584 scoped_lock& operator=(
const scoped_lock&) =
delete;
587 tuple<_MutexTypes&...> _M_devices;
594 explicit scoped_lock() =
default;
595 explicit scoped_lock(adopt_lock_t)
noexcept { }
596 ~scoped_lock() =
default;
598 scoped_lock(
const scoped_lock&) =
delete;
599 scoped_lock& operator=(
const scoped_lock&) =
delete;
602 template<
typename _Mutex>
603 class scoped_lock<_Mutex>
606 using mutex_type = _Mutex;
608 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
609 { _M_device.lock(); }
611 explicit scoped_lock(adopt_lock_t, mutex_type& __m)
noexcept
616 { _M_device.unlock(); }
618 scoped_lock(
const scoped_lock&) =
delete;
619 scoped_lock& operator=(
const scoped_lock&) =
delete;
622 mutex_type& _M_device;
626 #ifdef _GLIBCXX_HAS_GTHREADS
631 typedef __gthread_once_t __native_type;
632 __native_type _M_once = __GTHREAD_ONCE_INIT;
643 template<
typename _Callable,
typename... _Args>
648 #ifdef _GLIBCXX_HAVE_TLS
652 extern function<void()> __once_functor;
664 template<
typename _Callable,
typename... _Args>
670 auto __callable = [&] {
671 std::__invoke(std::forward<_Callable>(__f),
672 std::forward<_Args>(__args)...);
674 #ifdef _GLIBCXX_HAVE_TLS
679 __once_functor = __callable;
680 __set_once_functor_lock_ptr(&__functor_lock);
683 int __e = __gthread_once(&__once._M_once, &
__once_proxy);
685 #ifndef _GLIBCXX_HAVE_TLS
687 __set_once_functor_lock_ptr(0);
690 #ifdef __clang_analyzer__
697 __throw_system_error(__e);
699 #endif // _GLIBCXX_HAS_GTHREADS
702 _GLIBCXX_END_NAMESPACE_VERSION
707 #endif // _GLIBCXX_MUTEX
thread::id get_id() noexcept
get_id
__thread void(* __once_call)()
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 tuple< _Elements &...> tie(_Elements &...__args) noexcept
tie
int try_lock(_Lock1 &__l1, _Lock2 &__l2, _Lock3 &...__l3)
Generic try_lock.
void __once_proxy(void)
Generic try_lock.
__thread void * __once_callable
Generic try_lock.
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic lock.
A simple scoped lock type.
enable_if< ::__array_traits< _Tp, _Nm >::_Is_swappable::value >::type noexcept(noexcept(__one.swap(__two)))
swap
A movable scoped lock type.
duration< int64_t, nano > nanoseconds
nanoseconds
unique_lock< _Lock > __try_to_lock(_Lock &__l)
Generic try_lock.
duration< int64_t > seconds
seconds
constexpr __enable_if_is_duration< _ToDur > duration_cast(const duration< _Rep, _Period > &__d)
duration_cast
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
The standard recursive mutex type.
_GLIBCXX17_INLINE constexpr try_to_lock_t try_to_lock
Tag used to prevent a scoped lock from blocking if a mutex is locked.
void call_once(once_flag &__once, _Callable &&__f, _Args &&...__args)
call_once