29 #ifndef _GLIBCXX_MUTEX
30 #define _GLIBCXX_MUTEX 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
45 #if ! _GTHREAD_USE_MUTEX_TIMEDLOCK
50 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
52 namespace std _GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
61 #ifdef _GLIBCXX_HAS_GTHREADS
64 class __recursive_mutex_base
67 typedef __gthread_recursive_mutex_t __native_type;
69 __recursive_mutex_base(
const __recursive_mutex_base&) =
delete;
70 __recursive_mutex_base& operator=(
const __recursive_mutex_base&) =
delete;
72 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
73 __native_type _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
75 __recursive_mutex_base() =
default;
77 __native_type _M_mutex;
79 __recursive_mutex_base()
82 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
85 ~__recursive_mutex_base()
86 { __gthread_recursive_mutex_destroy(&_M_mutex); }
94 typedef __native_type* native_handle_type;
105 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
109 __throw_system_error(__e);
116 return !__gthread_recursive_mutex_trylock(&_M_mutex);
123 __gthread_recursive_mutex_unlock(&_M_mutex);
128 {
return &_M_mutex; }
131 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
132 template<
typename _Derived>
133 class __timed_mutex_impl
136 typedef chrono::high_resolution_clock __clock_t;
138 template<
typename _Rep,
typename _Period>
142 using chrono::steady_clock;
144 if (ratio_greater<steady_clock::period, _Period>())
146 return _M_try_lock_until(steady_clock::now() + __rt);
149 template<
typename _Duration>
151 _M_try_lock_until(
const chrono::time_point<__clock_t,
157 __gthread_time_t __ts = {
158 static_cast<std::time_t
>(__s.time_since_epoch().count()),
159 static_cast<long>(__ns.count())
162 return static_cast<_Derived*
>(
this)->_M_timedlock(__ts);
165 template<
typename _Clock,
typename _Duration>
167 _M_try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
169 auto __rtime = __atime - _Clock::now();
170 return _M_try_lock_until(__clock_t::now() + __rtime);
176 :
private __mutex_base,
public __timed_mutex_impl<timed_mutex>
179 typedef __native_type* native_handle_type;
181 timed_mutex() =
default;
182 ~timed_mutex() =
default;
184 timed_mutex(
const timed_mutex&) =
delete;
185 timed_mutex& operator=(
const timed_mutex&) =
delete;
190 int __e = __gthread_mutex_lock(&_M_mutex);
194 __throw_system_error(__e);
201 return !__gthread_mutex_trylock(&_M_mutex);
204 template <
class _Rep,
class _Period>
206 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
207 {
return _M_try_lock_for(__rtime); }
209 template <
class _Clock,
class _Duration>
211 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
212 {
return _M_try_lock_until(__atime); }
218 __gthread_mutex_unlock(&_M_mutex);
223 {
return &_M_mutex; }
226 friend class __timed_mutex_impl<timed_mutex>;
229 _M_timedlock(
const __gthread_time_t& __ts)
230 {
return !__gthread_mutex_timedlock(&_M_mutex, &__ts); }
234 class recursive_timed_mutex
235 :
private __recursive_mutex_base,
236 public __timed_mutex_impl<recursive_timed_mutex>
239 typedef __native_type* native_handle_type;
241 recursive_timed_mutex() =
default;
242 ~recursive_timed_mutex() =
default;
244 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
245 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
250 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
254 __throw_system_error(__e);
261 return !__gthread_recursive_mutex_trylock(&_M_mutex);
264 template <
class _Rep,
class _Period>
266 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
267 {
return _M_try_lock_for(__rtime); }
269 template <
class _Clock,
class _Duration>
271 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
272 {
return _M_try_lock_until(__atime); }
278 __gthread_recursive_mutex_unlock(&_M_mutex);
283 {
return &_M_mutex; }
286 friend class __timed_mutex_impl<recursive_timed_mutex>;
289 _M_timedlock(
const __gthread_time_t& __ts)
290 {
return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); }
293 #else // !_GTHREAD_USE_MUTEX_TIMEDLOCK
300 bool _M_locked =
false;
314 _M_cv.wait(__lk, [&]{
return !_M_locked; });
328 template<
typename _Rep,
typename _Period>
333 if (!_M_cv.wait_for(__lk, __rtime, [&]{
return !_M_locked; }))
339 template<
typename _Clock,
typename _Duration>
344 if (!_M_cv.wait_until(__lk, __atime, [&]{
return !_M_locked; }))
353 lock_guard<mutex> __lk(_M_mut);
354 __glibcxx_assert( _M_locked );
366 unsigned _M_count = 0;
373 operator()()
const noexcept
374 {
return _M_mx->_M_count == 0 || _M_mx->_M_owner == _M_caller; }
392 _Can_lock __can_lock{
this, __id};
394 _M_cv.wait(__lk, __can_lock);
396 __throw_system_error(EAGAIN);
405 _Can_lock __can_lock{
this, __id};
416 template<
typename _Rep,
typename _Period>
421 _Can_lock __can_lock{
this, __id};
423 if (!_M_cv.wait_for(__lk, __rtime, __can_lock))
432 template<
typename _Clock,
typename _Duration>
437 _Can_lock __can_lock{
this, __id};
439 if (!_M_cv.wait_until(__lk, __atime, __can_lock))
453 __glibcxx_assert( _M_count > 0 );
463 #endif // _GLIBCXX_HAS_GTHREADS
465 template<
typename _Lock>
470 template<
int _Idx,
bool _Continue = true>
471 struct __try_lock_impl
473 template<
typename... _Lock>
475 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
479 if (__lock.owns_lock())
481 constexpr
bool __cont = _Idx + 2 <
sizeof...(_Lock);
482 using __try_locker = __try_lock_impl<_Idx + 1, __cont>;
483 __try_locker::__do_try_lock(__locks, __idx);
491 struct __try_lock_impl<_Idx, false>
493 template<
typename... _Lock>
495 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
499 if (__lock.owns_lock())
517 template<
typename _Lock1,
typename _Lock2,
typename... _Lock3>
519 try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
522 auto __locks =
std::tie(__l1, __l2, __l3...);
523 __try_lock_impl<0>::__do_try_lock(__locks, __idx);
538 template<
typename _L1,
typename _L2,
typename... _L3>
540 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
544 using __try_locker = __try_lock_impl<0,
sizeof...(_L3) != 0>;
547 auto __locks =
std::tie(__l2, __l3...);
548 __try_locker::__do_try_lock(__locks, __idx);
557 #ifdef _GLIBCXX_HAS_GTHREADS
562 typedef __gthread_once_t __native_type;
563 __native_type _M_once = __GTHREAD_ONCE_INIT;
567 constexpr
once_flag() noexcept =
default;
574 template<
typename _Callable,
typename... _Args>
579 #ifdef _GLIBCXX_HAVE_TLS
583 extern function<void()> __once_functor;
595 template<
typename _Callable,
typename... _Args>
601 auto __callable = [&] {
603 std::forward<_Args>(__args)...);
605 #ifdef _GLIBCXX_HAVE_TLS
607 __once_call = []{ (*(decltype(__callable)*)__once_callable)(); };
610 __once_functor = __callable;
611 __set_once_functor_lock_ptr(&__functor_lock);
614 int __e = __gthread_once(&__once._M_once, &
__once_proxy);
616 #ifndef _GLIBCXX_HAVE_TLS
618 __set_once_functor_lock_ptr(0);
622 __throw_system_error(__e);
624 #endif // _GLIBCXX_HAS_GTHREADS
627 _GLIBCXX_END_NAMESPACE_VERSION
629 #endif // _GLIBCXX_USE_C99_STDINT_TR1
633 #endif // _GLIBCXX_MUTEX
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
A simple scoped lock type.
int try_lock(_Lock1 &__l1, _Lock2 &__l2, _Lock3 &...__l3)
Generic try_lock.
A movable scoped lock type.
constexpr enable_if< __is_duration< _ToDur >::value, _ToDur >::type duration_cast(const duration< _Rep, _Period > &__d)
duration_cast
__thread void(* __once_call)()
Generic try_lock.
void __once_proxy(void)
Generic try_lock.
constexpr try_to_lock_t try_to_lock
Tag used to prevent a scoped lock from blocking if a mutex is locked.
The standard recursive mutex type.
__thread void * __once_callable
Generic try_lock.
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic 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
result_of< _Callable &&(_Args &&...)>::type __invoke(_Callable &&__fn, _Args &&...__args)
Invoke a callable object.
void call_once(once_flag &__once, _Callable &&__f, _Args &&...__args)
call_once
thread::id get_id() noexcept
get_id
duration< int64_t > seconds
seconds
duration< int64_t, nano > nanoseconds
nanoseconds
unique_lock< _Lock > __try_to_lock(_Lock &__l)
Generic try_lock.
constexpr tuple< _Elements &...> tie(_Elements &...__args) noexcept
tie