29 #ifndef _GLIBCXX_MUTEX
30 #define _GLIBCXX_MUTEX 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
45 #include <bits/gthr.h>
48 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
50 namespace std _GLIBCXX_VISIBILITY(default)
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
54 #ifdef _GLIBCXX_HAS_GTHREADS
59 typedef __gthread_mutex_t __native_type;
61 #ifdef __GTHREAD_MUTEX_INIT
62 __native_type _M_mutex = __GTHREAD_MUTEX_INIT;
64 constexpr __mutex_base() noexcept = default;
66 __native_type _M_mutex;
68 __mutex_base() noexcept
71 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
74 ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
77 __mutex_base(
const __mutex_base&) =
delete;
78 __mutex_base& operator=(
const __mutex_base&) =
delete;
82 class __recursive_mutex_base
85 typedef __gthread_recursive_mutex_t __native_type;
87 __recursive_mutex_base(
const __recursive_mutex_base&) =
delete;
88 __recursive_mutex_base& operator=(
const __recursive_mutex_base&) =
delete;
90 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
91 __native_type _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
93 __recursive_mutex_base() =
default;
95 __native_type _M_mutex;
97 __recursive_mutex_base()
100 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
103 ~__recursive_mutex_base()
104 { __gthread_recursive_mutex_destroy(&_M_mutex); }
120 typedef __native_type* native_handle_type;
122 #ifdef __GTHREAD_MUTEX_INIT
125 mutex() noexcept =
default;
134 int __e = __gthread_mutex_lock(&_M_mutex);
138 __throw_system_error(__e);
145 return !__gthread_mutex_trylock(&_M_mutex);
152 __gthread_mutex_unlock(&_M_mutex);
157 {
return &_M_mutex; }
164 typedef __native_type* native_handle_type;
175 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
179 __throw_system_error(__e);
186 return !__gthread_recursive_mutex_trylock(&_M_mutex);
193 __gthread_recursive_mutex_unlock(&_M_mutex);
198 {
return &_M_mutex; }
201 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
202 template<
typename _Derived>
203 class __timed_mutex_impl
206 typedef chrono::high_resolution_clock __clock_t;
208 template<
typename _Rep,
typename _Period>
212 using chrono::steady_clock;
214 if (ratio_greater<steady_clock::period, _Period>())
216 return _M_try_lock_until(steady_clock::now() + __rt);
219 template<
typename _Duration>
221 _M_try_lock_until(
const chrono::time_point<__clock_t,
227 __gthread_time_t __ts = {
228 static_cast<std::time_t
>(__s.time_since_epoch().count()),
229 static_cast<long>(__ns.count())
232 auto __mutex =
static_cast<_Derived*
>(
this)->native_handle();
233 return !__gthread_mutex_timedlock(__mutex, &__ts);
236 template<
typename _Clock,
typename _Duration>
238 _M_try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
240 auto __rtime = __atime - _Clock::now();
241 return _M_try_lock_until(__clock_t::now() + __rtime);
247 :
private __mutex_base,
public __timed_mutex_impl<timed_mutex>
250 typedef __native_type* native_handle_type;
252 timed_mutex() =
default;
253 ~timed_mutex() =
default;
255 timed_mutex(
const timed_mutex&) =
delete;
256 timed_mutex& operator=(
const timed_mutex&) =
delete;
261 int __e = __gthread_mutex_lock(&_M_mutex);
265 __throw_system_error(__e);
272 return !__gthread_mutex_trylock(&_M_mutex);
275 template <
class _Rep,
class _Period>
277 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
278 {
return _M_try_lock_for(__rtime); }
280 template <
class _Clock,
class _Duration>
282 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
283 {
return _M_try_lock_until(__atime); }
289 __gthread_mutex_unlock(&_M_mutex);
294 {
return &_M_mutex; }
298 class recursive_timed_mutex
299 :
private __recursive_mutex_base,
300 public __timed_mutex_impl<recursive_timed_mutex>
303 typedef __native_type* native_handle_type;
305 recursive_timed_mutex() =
default;
306 ~recursive_timed_mutex() =
default;
308 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
309 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
314 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
318 __throw_system_error(__e);
325 return !__gthread_recursive_mutex_trylock(&_M_mutex);
328 template <
class _Rep,
class _Period>
330 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
331 {
return _M_try_lock_for(__rtime); }
333 template <
class _Clock,
class _Duration>
335 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
336 {
return _M_try_lock_until(__atime); }
342 __gthread_recursive_mutex_unlock(&_M_mutex);
347 {
return &_M_mutex; }
350 #endif // _GLIBCXX_HAS_GTHREADS
363 constexpr try_to_lock_t try_to_lock { };
364 constexpr adopt_lock_t adopt_lock { };
369 template<
typename _Mutex>
373 typedef _Mutex mutex_type;
375 explicit lock_guard(mutex_type& __m) : _M_device(__m)
376 { _M_device.lock(); }
382 { _M_device.unlock(); }
388 mutex_type& _M_device;
392 template<
typename _Mutex>
396 typedef _Mutex mutex_type;
399 : _M_device(0), _M_owns(
false)
403 : _M_device(&__m), _M_owns(
false)
410 : _M_device(&__m), _M_owns(
false)
414 : _M_device(&__m), _M_owns(_M_device->try_lock())
418 : _M_device(&__m), _M_owns(
true)
423 template<
typename _Clock,
typename _Duration>
426 : _M_device(&__m), _M_owns(_M_device->try_lock_until(__atime))
429 template<
typename _Rep,
typename _Period>
432 : _M_device(&__m), _M_owns(_M_device->try_lock_for(__rtime))
445 : _M_device(__u._M_device), _M_owns(__u._M_owns)
468 __throw_system_error(
int(errc::operation_not_permitted));
470 __throw_system_error(
int(errc::resource_deadlock_would_occur));
482 __throw_system_error(
int(errc::operation_not_permitted));
484 __throw_system_error(
int(errc::resource_deadlock_would_occur));
487 _M_owns = _M_device->try_lock();
492 template<
typename _Clock,
typename _Duration>
497 __throw_system_error(
int(errc::operation_not_permitted));
499 __throw_system_error(
int(errc::resource_deadlock_would_occur));
502 _M_owns = _M_device->try_lock_until(__atime);
507 template<
typename _Rep,
typename _Period>
512 __throw_system_error(
int(errc::operation_not_permitted));
514 __throw_system_error(
int(errc::resource_deadlock_would_occur));
517 _M_owns = _M_device->try_lock_for(__rtime);
526 __throw_system_error(
int(errc::operation_not_permitted));
544 mutex_type* __ret = _M_device;
551 owns_lock()
const noexcept
554 explicit operator bool()
const noexcept
555 {
return owns_lock(); }
558 mutex()
const noexcept
559 {
return _M_device; }
562 mutex_type* _M_device;
567 template<
typename _Mutex>
575 template<
typename... _Lock>
577 __do_unlock(tuple<_Lock&...>& __locks)
579 std::get<_Idx>(__locks).unlock();
580 __unlock_impl<_Idx - 1>::__do_unlock(__locks);
585 struct __unlock_impl<-1>
587 template<
typename... _Lock>
589 __do_unlock(tuple<_Lock&...>&)
593 template<
typename _Lock>
595 __try_to_lock(_Lock& __l)
596 {
return unique_lock<_Lock>(__l, try_to_lock); }
598 template<
int _Idx,
bool _Continue = true>
599 struct __try_lock_impl
601 template<
typename... _Lock>
603 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
606 auto __lock = __try_to_lock(std::get<_Idx>(__locks));
607 if (__lock.owns_lock())
609 __try_lock_impl<_Idx + 1, _Idx + 2 <
sizeof...(_Lock)>::
610 __do_try_lock(__locks, __idx);
618 struct __try_lock_impl<_Idx, false>
620 template<
typename... _Lock>
622 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
625 auto __lock = __try_to_lock(std::get<_Idx>(__locks));
626 if (__lock.owns_lock())
644 template<
typename _Lock1,
typename _Lock2,
typename... _Lock3>
646 try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
649 auto __locks =
std::tie(__l1, __l2, __l3...);
651 { __try_lock_impl<0>::__do_try_lock(__locks, __idx); }
668 template<
typename _L1,
typename _L2,
typename ..._L3>
670 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
676 auto __locks =
std::tie(__l2, __l3...);
677 __try_lock_impl<0,
sizeof...(_L3)>::__do_try_lock(__locks, __idx);
686 #ifdef _GLIBCXX_HAS_GTHREADS
691 typedef __gthread_once_t __native_type;
692 __native_type _M_once = __GTHREAD_ONCE_INIT;
696 constexpr
once_flag() noexcept =
default;
703 template<
typename _Callable,
typename... _Args>
708 #ifdef _GLIBCXX_HAVE_TLS
709 extern __thread
void* __once_callable;
710 extern __thread void (*__once_call)();
712 template<
typename _Callable>
716 (*(_Callable*)__once_callable)();
719 extern function<void()> __once_functor;
722 __set_once_functor_lock_ptr(unique_lock<mutex>*);
728 extern "C" void __once_proxy(
void);
731 template<
typename _Callable,
typename... _Args>
735 #ifdef _GLIBCXX_HAVE_TLS
736 auto __bound_functor = std::__bind_simple(std::forward<_Callable>(__f),
737 std::forward<_Args>(__args)...);
738 __once_callable = &__bound_functor;
739 __once_call = &__once_call_impl<decltype(__bound_functor)>;
742 auto __callable = std::__bind_simple(std::forward<_Callable>(__f),
743 std::forward<_Args>(__args)...);
744 __once_functor = [&]() { __callable(); };
745 __set_once_functor_lock_ptr(&__functor_lock);
748 int __e = __gthread_once(&__once._M_once, &__once_proxy);
750 #ifndef _GLIBCXX_HAVE_TLS
752 __set_once_functor_lock_ptr(0);
756 __throw_system_error(__e);
758 #endif // _GLIBCXX_HAS_GTHREADS
761 _GLIBCXX_END_NAMESPACE_VERSION
763 #endif // _GLIBCXX_USE_C99_STDINT_TR1
767 #endif // _GLIBCXX_MUTEX
duration< int64_t, nano > nanoseconds
nanoseconds
Assume the calling thread has already obtained mutex ownership and manage it.
duration< int64_t > seconds
seconds
Do not acquire ownership of the mutex.
constexpr enable_if< __is_duration< _ToDur >::value, time_point< _Clock, _ToDur > >::type time_point_cast(const time_point< _Clock, _Dur > &__t)
time_point_cast
Try to acquire ownership of the mutex without blocking.
void call_once(once_flag &__once, _Callable &&__f, _Args &&...__args)
call_once
void lock(_L1 &__l1, _L2 &__l2, _L3 &...__l3)
Generic lock.
tuple< _Elements &...> tie(_Elements &...__args) noexcept
tie
void swap(function< _Res(_Args...)> &__x, function< _Res(_Args...)> &__y)
Swap the targets of two polymorphic function object wrappers.
int try_lock(_Lock1 &__l1, _Lock2 &__l2, _Lock3 &...__l3)
Generic try_lock.
constexpr enable_if< __is_duration< _ToDur >::value, _ToDur >::type duration_cast(const duration< _Rep, _Period > &__d)
duration_cast