29 #ifndef _GLIBCXX_MUTEX
30 #define _GLIBCXX_MUTEX 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
45 #include <bits/gthr.h>
49 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
51 namespace std _GLIBCXX_VISIBILITY(default)
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 #ifdef _GLIBCXX_HAS_GTHREADS
60 typedef __gthread_mutex_t __native_type;
62 #ifdef __GTHREAD_MUTEX_INIT
63 __native_type _M_mutex = __GTHREAD_MUTEX_INIT;
65 constexpr __mutex_base() noexcept = default;
67 __native_type _M_mutex;
69 __mutex_base() noexcept
72 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
75 ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
78 __mutex_base(
const __mutex_base&) =
delete;
79 __mutex_base& operator=(
const __mutex_base&) =
delete;
83 class __recursive_mutex_base
86 typedef __gthread_recursive_mutex_t __native_type;
88 __recursive_mutex_base(
const __recursive_mutex_base&) =
delete;
89 __recursive_mutex_base& operator=(
const __recursive_mutex_base&) =
delete;
91 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
92 __native_type _M_mutex = __GTHREAD_RECURSIVE_MUTEX_INIT;
94 __recursive_mutex_base() =
default;
96 __native_type _M_mutex;
98 __recursive_mutex_base()
101 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
104 ~__recursive_mutex_base()
105 { __gthread_recursive_mutex_destroy(&_M_mutex); }
121 typedef __native_type* native_handle_type;
123 #ifdef __GTHREAD_MUTEX_INIT
126 mutex() noexcept =
default;
135 int __e = __gthread_mutex_lock(&_M_mutex);
139 __throw_system_error(__e);
146 return !__gthread_mutex_trylock(&_M_mutex);
153 __gthread_mutex_unlock(&_M_mutex);
158 {
return &_M_mutex; }
165 typedef __native_type* native_handle_type;
176 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
180 __throw_system_error(__e);
187 return !__gthread_recursive_mutex_trylock(&_M_mutex);
194 __gthread_recursive_mutex_unlock(&_M_mutex);
199 {
return &_M_mutex; }
202 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
203 template<
typename _Derived>
204 class __timed_mutex_impl
207 typedef chrono::high_resolution_clock __clock_t;
209 template<
typename _Rep,
typename _Period>
213 using chrono::steady_clock;
215 if (ratio_greater<steady_clock::period, _Period>())
217 return _M_try_lock_until(steady_clock::now() + __rt);
220 template<
typename _Duration>
222 _M_try_lock_until(
const chrono::time_point<__clock_t,
228 __gthread_time_t __ts = {
229 static_cast<std::time_t
>(__s.time_since_epoch().count()),
230 static_cast<long>(__ns.count())
233 auto __mutex =
static_cast<_Derived*
>(
this)->native_handle();
234 return !__gthread_mutex_timedlock(__mutex, &__ts);
237 template<
typename _Clock,
typename _Duration>
239 _M_try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
241 auto __rtime = __atime - _Clock::now();
242 return _M_try_lock_until(__clock_t::now() + __rtime);
248 :
private __mutex_base,
public __timed_mutex_impl<timed_mutex>
251 typedef __native_type* native_handle_type;
253 timed_mutex() =
default;
254 ~timed_mutex() =
default;
256 timed_mutex(
const timed_mutex&) =
delete;
257 timed_mutex& operator=(
const timed_mutex&) =
delete;
262 int __e = __gthread_mutex_lock(&_M_mutex);
266 __throw_system_error(__e);
273 return !__gthread_mutex_trylock(&_M_mutex);
276 template <
class _Rep,
class _Period>
278 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
279 {
return _M_try_lock_for(__rtime); }
281 template <
class _Clock,
class _Duration>
283 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
284 {
return _M_try_lock_until(__atime); }
290 __gthread_mutex_unlock(&_M_mutex);
295 {
return &_M_mutex; }
299 class recursive_timed_mutex
300 :
private __recursive_mutex_base,
301 public __timed_mutex_impl<recursive_timed_mutex>
304 typedef __native_type* native_handle_type;
306 recursive_timed_mutex() =
default;
307 ~recursive_timed_mutex() =
default;
309 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
310 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
315 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
319 __throw_system_error(__e);
326 return !__gthread_recursive_mutex_trylock(&_M_mutex);
329 template <
class _Rep,
class _Period>
331 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
332 {
return _M_try_lock_for(__rtime); }
334 template <
class _Clock,
class _Duration>
336 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
337 {
return _M_try_lock_until(__atime); }
343 __gthread_recursive_mutex_unlock(&_M_mutex);
348 {
return &_M_mutex; }
351 #endif // _GLIBCXX_HAS_GTHREADS
364 constexpr try_to_lock_t try_to_lock { };
365 constexpr adopt_lock_t adopt_lock { };
370 template<
typename _Mutex>
374 typedef _Mutex mutex_type;
376 explicit lock_guard(mutex_type& __m) : _M_device(__m)
377 { _M_device.lock(); }
383 { _M_device.unlock(); }
389 mutex_type& _M_device;
393 template<
typename _Mutex>
397 typedef _Mutex mutex_type;
400 : _M_device(0), _M_owns(
false)
404 : _M_device(&__m), _M_owns(
false)
411 : _M_device(&__m), _M_owns(
false)
415 : _M_device(&__m), _M_owns(_M_device->try_lock())
419 : _M_device(&__m), _M_owns(
true)
424 template<
typename _Clock,
typename _Duration>
427 : _M_device(&__m), _M_owns(_M_device->try_lock_until(__atime))
430 template<
typename _Rep,
typename _Period>
433 : _M_device(&__m), _M_owns(_M_device->try_lock_for(__rtime))
446 : _M_device(__u._M_device), _M_owns(__u._M_owns)
469 __throw_system_error(
int(errc::operation_not_permitted));
471 __throw_system_error(
int(errc::resource_deadlock_would_occur));
483 __throw_system_error(
int(errc::operation_not_permitted));
485 __throw_system_error(
int(errc::resource_deadlock_would_occur));
488 _M_owns = _M_device->try_lock();
493 template<
typename _Clock,
typename _Duration>
498 __throw_system_error(
int(errc::operation_not_permitted));
500 __throw_system_error(
int(errc::resource_deadlock_would_occur));
503 _M_owns = _M_device->try_lock_until(__atime);
508 template<
typename _Rep,
typename _Period>
513 __throw_system_error(
int(errc::operation_not_permitted));
515 __throw_system_error(
int(errc::resource_deadlock_would_occur));
518 _M_owns = _M_device->try_lock_for(__rtime);
527 __throw_system_error(
int(errc::operation_not_permitted));
545 mutex_type* __ret = _M_device;
552 owns_lock()
const noexcept
555 explicit operator bool()
const noexcept
556 {
return owns_lock(); }
559 mutex()
const noexcept
560 {
return _M_device; }
563 mutex_type* _M_device;
568 template<
typename _Mutex>
576 template<
typename... _Lock>
578 __do_unlock(tuple<_Lock&...>& __locks)
580 std::get<_Idx>(__locks).unlock();
581 __unlock_impl<_Idx - 1>::__do_unlock(__locks);
586 struct __unlock_impl<-1>
588 template<
typename... _Lock>
590 __do_unlock(tuple<_Lock&...>&)
594 template<
typename _Lock>
596 __try_to_lock(_Lock& __l)
597 {
return unique_lock<_Lock>(__l, try_to_lock); }
599 template<
int _Idx,
bool _Continue = true>
600 struct __try_lock_impl
602 template<
typename... _Lock>
604 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
607 auto __lock = __try_to_lock(std::get<_Idx>(__locks));
608 if (__lock.owns_lock())
610 __try_lock_impl<_Idx + 1, _Idx + 2 <
sizeof...(_Lock)>::
611 __do_try_lock(__locks, __idx);
619 struct __try_lock_impl<_Idx, false>
621 template<
typename... _Lock>
623 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
626 auto __lock = __try_to_lock(std::get<_Idx>(__locks));
627 if (__lock.owns_lock())
645 template<
typename _Lock1,
typename _Lock2,
typename... _Lock3>
647 try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
650 auto __locks =
std::tie(__l1, __l2, __l3...);
651 __try_lock_impl<0>::__do_try_lock(__locks, __idx);
666 template<
typename _L1,
typename _L2,
typename ..._L3>
668 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
674 auto __locks =
std::tie(__l2, __l3...);
675 __try_lock_impl<0,
sizeof...(_L3)>::__do_try_lock(__locks, __idx);
684 #ifdef _GLIBCXX_HAS_GTHREADS
689 typedef __gthread_once_t __native_type;
690 __native_type _M_once = __GTHREAD_ONCE_INIT;
694 constexpr
once_flag() noexcept =
default;
701 template<
typename _Callable,
typename... _Args>
706 #ifdef _GLIBCXX_HAVE_TLS
707 extern __thread
void* __once_callable;
708 extern __thread void (*__once_call)();
710 template<
typename _Callable>
714 (*(_Callable*)__once_callable)();
717 extern function<void()> __once_functor;
720 __set_once_functor_lock_ptr(unique_lock<mutex>*);
726 extern "C" void __once_proxy(
void);
729 template<
typename _Callable,
typename... _Args>
733 #ifdef _GLIBCXX_HAVE_TLS
734 auto __bound_functor = std::__bind_simple(std::forward<_Callable>(__f),
735 std::forward<_Args>(__args)...);
736 __once_callable = &__bound_functor;
737 __once_call = &__once_call_impl<decltype(__bound_functor)>;
740 auto __callable = std::__bind_simple(std::forward<_Callable>(__f),
741 std::forward<_Args>(__args)...);
742 __once_functor = [&]() { __callable(); };
743 __set_once_functor_lock_ptr(&__functor_lock);
746 int __e = __gthread_once(&__once._M_once, &__once_proxy);
748 #ifndef _GLIBCXX_HAVE_TLS
750 __set_once_functor_lock_ptr(0);
754 __throw_system_error(__e);
756 #endif // _GLIBCXX_HAS_GTHREADS
759 _GLIBCXX_END_NAMESPACE_VERSION
761 #endif // _GLIBCXX_USE_C99_STDINT_TR1
765 #endif // _GLIBCXX_MUTEX
duration< int64_t, nano > nanoseconds
nanoseconds
duration< int64_t > seconds
seconds
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
Do not acquire ownership of the mutex.
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
Assume the calling thread has already obtained mutex ownership and manage it.