29 #ifndef _GLIBCXX_MUTEX
30 #define _GLIBCXX_MUTEX 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
44 #if ! _GTHREAD_USE_MUTEX_TIMEDLOCK
48 #ifndef _GLIBCXX_HAVE_TLS
52 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
54 namespace std _GLIBCXX_VISIBILITY(default)
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
63 #ifdef _GLIBCXX_HAS_GTHREADS
66 class __recursive_mutex_base
69 typedef __gthread_recursive_mutex_t __native_type;
71 __recursive_mutex_base(
const __recursive_mutex_base&) =
delete;
72 __recursive_mutex_base& operator=(
const __recursive_mutex_base&) =
delete;
74 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
75 __native_type _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
77 __recursive_mutex_base() =
default;
79 __native_type _M_mutex;
81 __recursive_mutex_base()
84 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
87 ~__recursive_mutex_base()
88 { __gthread_recursive_mutex_destroy(&_M_mutex); }
96 typedef __native_type* native_handle_type;
107 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
111 __throw_system_error(__e);
118 return !__gthread_recursive_mutex_trylock(&_M_mutex);
125 __gthread_recursive_mutex_unlock(&_M_mutex);
130 {
return &_M_mutex; }
133 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
134 template<
typename _Derived>
135 class __timed_mutex_impl
138 typedef chrono::high_resolution_clock __clock_t;
140 template<
typename _Rep,
typename _Period>
144 using chrono::steady_clock;
146 if (ratio_greater<steady_clock::period, _Period>())
148 return _M_try_lock_until(steady_clock::now() + __rt);
151 template<
typename _Duration>
153 _M_try_lock_until(
const chrono::time_point<__clock_t,
159 __gthread_time_t __ts = {
160 static_cast<std::time_t
>(__s.time_since_epoch().count()),
161 static_cast<long>(__ns.count())
164 return static_cast<_Derived*
>(
this)->_M_timedlock(__ts);
167 template<
typename _Clock,
typename _Duration>
169 _M_try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
171 auto __rtime = __atime - _Clock::now();
172 return _M_try_lock_until(__clock_t::now() + __rtime);
178 :
private __mutex_base,
public __timed_mutex_impl<timed_mutex>
181 typedef __native_type* native_handle_type;
183 timed_mutex() =
default;
184 ~timed_mutex() =
default;
186 timed_mutex(
const timed_mutex&) =
delete;
187 timed_mutex& operator=(
const timed_mutex&) =
delete;
192 int __e = __gthread_mutex_lock(&_M_mutex);
196 __throw_system_error(__e);
203 return !__gthread_mutex_trylock(&_M_mutex);
206 template <
class _Rep,
class _Period>
208 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
209 {
return _M_try_lock_for(__rtime); }
211 template <
class _Clock,
class _Duration>
213 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
214 {
return _M_try_lock_until(__atime); }
220 __gthread_mutex_unlock(&_M_mutex);
225 {
return &_M_mutex; }
228 friend class __timed_mutex_impl<timed_mutex>;
231 _M_timedlock(
const __gthread_time_t& __ts)
232 {
return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
236 class recursive_timed_mutex
237 :
private __recursive_mutex_base,
238 public __timed_mutex_impl<recursive_timed_mutex>
241 typedef __native_type* native_handle_type;
243 recursive_timed_mutex() =
default;
244 ~recursive_timed_mutex() =
default;
246 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
247 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
252 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
256 __throw_system_error(__e);
263 return !__gthread_recursive_mutex_trylock(&_M_mutex);
266 template <
class _Rep,
class _Period>
268 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
269 {
return _M_try_lock_for(__rtime); }
271 template <
class _Clock,
class _Duration>
273 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
274 {
return _M_try_lock_until(__atime); }
280 __gthread_recursive_mutex_unlock(&_M_mutex);
285 {
return &_M_mutex; }
288 friend class __timed_mutex_impl<recursive_timed_mutex>;
291 _M_timedlock(
const __gthread_time_t& __ts)
292 {
return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
295 #else // !_GTHREAD_USE_MUTEX_TIMEDLOCK
302 bool _M_locked =
false;
316 _M_cv.wait(__lk, [&]{
return !_M_locked; });
330 template<
typename _Rep,
typename _Period>
335 if (!_M_cv.wait_for(__lk, __rtime, [&]{
return !_M_locked; }))
341 template<
typename _Clock,
typename _Duration>
346 if (!_M_cv.wait_until(__lk, __atime, [&]{
return !_M_locked; }))
355 lock_guard<mutex> __lk(_M_mut);
356 __glibcxx_assert( _M_locked );
368 unsigned _M_count = 0;
376 {
return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
394 _Can_lock __can_lock{
this, __id};
396 _M_cv.wait(__lk, __can_lock);
398 __throw_system_error(EAGAIN);
407 _Can_lock __can_lock{
this, __id};
418 template<
typename _Rep,
typename _Period>
423 _Can_lock __can_lock{
this, __id};
425 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
434 template<
typename _Clock,
typename _Duration>
439 _Can_lock __can_lock{
this, __id};
441 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
455 __glibcxx_assert( _M_count > 0 );
465 #endif // _GLIBCXX_HAS_GTHREADS
467 template<
typename _Lock>
472 template<
int _Idx,
bool _Continue = true>
473 struct __try_lock_impl
475 template<
typename... _Lock>
477 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
481 if (__lock.owns_lock())
483 constexpr
bool __cont = _Idx + 2 <
sizeof...(_Lock);
484 using __try_locker = __try_lock_impl<_Idx + 1, __cont>;
485 __try_locker::__do_try_lock(__locks, __idx);
493 struct __try_lock_impl<_Idx, false>
495 template<
typename... _Lock>
497 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
501 if (__lock.owns_lock())
519 template<
typename _Lock1,
typename _Lock2,
typename... _Lock3>
521 try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
524 auto __locks =
std::tie(__l1, __l2, __l3...);
525 __try_lock_impl<0>::__do_try_lock(__locks, __idx);
540 template<
typename _L1,
typename _L2,
typename... _L3>
542 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
546 using __try_locker = __try_lock_impl<0,
sizeof...(_L3) != 0>;
549 auto __locks =
std::tie(__l2, __l3...);
550 __try_locker::__do_try_lock(__locks, __idx);
559 #if __cplusplus >= 201703L
560 #define __cpp_lib_scoped_lock 201703
566 template<
typename... _MutexTypes>
570 explicit scoped_lock(_MutexTypes&... __m) : _M_devices(std::
tie(__m...))
573 explicit scoped_lock(adopt_lock_t, _MutexTypes&... __m)
noexcept
579 std::apply([](_MutexTypes&... __m) {
580 char __i[] __attribute__((__unused__)) = { (__m.unlock(), 0)... };
584 scoped_lock(
const scoped_lock&) =
delete;
585 scoped_lock& operator=(
const scoped_lock&) =
delete;
588 tuple<_MutexTypes&...> _M_devices;
595 explicit scoped_lock() =
default;
596 explicit scoped_lock(adopt_lock_t)
noexcept { }
597 ~scoped_lock() =
default;
599 scoped_lock(
const scoped_lock&) =
delete;
600 scoped_lock& operator=(
const scoped_lock&) =
delete;
603 template<
typename _Mutex>
604 class scoped_lock<_Mutex>
607 using mutex_type = _Mutex;
609 explicit scoped_lock(mutex_type& __m) : _M_device(__m)
610 { _M_device.lock(); }
612 explicit scoped_lock(adopt_lock_t, mutex_type& __m)
noexcept
617 { _M_device.unlock(); }
619 scoped_lock(
const scoped_lock&) =
delete;
620 scoped_lock& operator=(
const scoped_lock&) =
delete;
623 mutex_type& _M_device;
627 #ifdef _GLIBCXX_HAS_GTHREADS
632 typedef __gthread_once_t __native_type;
633 __native_type _M_once = __GTHREAD_ONCE_INIT;
644 template<
typename _Callable,
typename... _Args>
649 #ifdef _GLIBCXX_HAVE_TLS
653 extern function<void()> __once_functor;
665 template<
typename _Callable,
typename... _Args>
671 auto __callable = [&] {
672 std::__invoke(std::forward<_Callable>(__f),
673 std::forward<_Args>(__args)...);
675 #ifdef _GLIBCXX_HAVE_TLS
680 __once_functor = __callable;
681 __set_once_functor_lock_ptr(&__functor_lock);
684 int __e = __gthread_once(&__once._M_once, &
__once_proxy);
686 #ifndef _GLIBCXX_HAVE_TLS
688 __set_once_functor_lock_ptr(0);
691 #ifdef __clang_analyzer__
698 __throw_system_error(__e);
700 #endif // _GLIBCXX_HAS_GTHREADS
703 _GLIBCXX_END_NAMESPACE_VERSION
705 #endif // _GLIBCXX_USE_C99_STDINT_TR1
709 #endif // _GLIBCXX_MUTEX
void __once_proxy(void)
Generic try_lock.
_GLIBCXX17_INLINE constexpr try_to_lock_t try_to_lock
Tag used to prevent a scoped lock from blocking if a mutex is locked.
constexpr tuple< _Elements &...> tie(_Elements &...__args) noexcept
tie
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic lock.
enable_if< ::__array_traits< _Tp, _Nm >::_Is_swappable::value >::type noexcept(noexcept(__one.swap(__two)))
swap
A simple scoped lock type.
A movable scoped lock type.
int try_lock(_Lock1 &__l1, _Lock2 &__l2, _Lock3 &...__l3)
Generic try_lock.
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
__thread void * __once_callable
Generic try_lock.
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.
void call_once(once_flag &__once, _Callable &&__f, _Args &&...__args)
call_once
duration< int64_t > seconds
seconds
duration< int64_t, nano > nanoseconds
nanoseconds
The standard recursive mutex type.
unique_lock< _Lock > __try_to_lock(_Lock &__l)
Generic try_lock.