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
203 class timed_mutex :
private __mutex_base
205 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
206 typedef chrono::steady_clock __clock_t;
208 typedef chrono::high_resolution_clock __clock_t;
212 typedef __native_type* native_handle_type;
214 timed_mutex() =
default;
215 ~timed_mutex() =
default;
217 timed_mutex(
const timed_mutex&) =
delete;
218 timed_mutex& operator=(
const timed_mutex&) =
delete;
223 int __e = __gthread_mutex_lock(&_M_mutex);
227 __throw_system_error(__e);
234 return !__gthread_mutex_trylock(&_M_mutex);
237 template <
class _Rep,
class _Period>
239 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
240 {
return __try_lock_for_impl(__rtime); }
242 template <
class _Clock,
class _Duration>
244 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
246 chrono::time_point<_Clock, chrono::seconds> __s =
252 __gthread_time_t __ts = {
253 static_cast<std::time_t
>(__s.time_since_epoch().count()),
254 static_cast<long>(__ns.count())
257 return !__gthread_mutex_timedlock(&_M_mutex, &__ts);
264 __gthread_mutex_unlock(&_M_mutex);
269 {
return &_M_mutex; }
272 template<
typename _Rep,
typename _Period>
274 ratio_less_equal<__clock_t::period, _Period>::value,
bool>::type
275 __try_lock_for_impl(
const chrono::duration<_Rep, _Period>& __rtime)
277 __clock_t::time_point __atime = __clock_t::now()
280 return try_lock_until(__atime);
283 template <
typename _Rep,
typename _Period>
285 !ratio_less_equal<__clock_t::period, _Period>::value,
bool>::type
286 __try_lock_for_impl(
const chrono::duration<_Rep, _Period>& __rtime)
288 __clock_t::time_point __atime = __clock_t::now()
291 return try_lock_until(__atime);
296 class recursive_timed_mutex :
private __recursive_mutex_base
298 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
299 typedef chrono::steady_clock __clock_t;
301 typedef chrono::high_resolution_clock __clock_t;
305 typedef __native_type* native_handle_type;
307 recursive_timed_mutex() =
default;
308 ~recursive_timed_mutex() =
default;
310 recursive_timed_mutex(
const recursive_timed_mutex&) =
delete;
311 recursive_timed_mutex& operator=(
const recursive_timed_mutex&) =
delete;
316 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
320 __throw_system_error(__e);
327 return !__gthread_recursive_mutex_trylock(&_M_mutex);
330 template <
class _Rep,
class _Period>
332 try_lock_for(
const chrono::duration<_Rep, _Period>& __rtime)
333 {
return __try_lock_for_impl(__rtime); }
335 template <
class _Clock,
class _Duration>
337 try_lock_until(
const chrono::time_point<_Clock, _Duration>& __atime)
339 chrono::time_point<_Clock, chrono::seconds> __s =
345 __gthread_time_t __ts = {
346 static_cast<std::time_t
>(__s.time_since_epoch().count()),
347 static_cast<long>(__ns.count())
350 return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts);
357 __gthread_recursive_mutex_unlock(&_M_mutex);
362 {
return &_M_mutex; }
365 template<
typename _Rep,
typename _Period>
367 ratio_less_equal<__clock_t::period, _Period>::value,
bool>::type
368 __try_lock_for_impl(
const chrono::duration<_Rep, _Period>& __rtime)
370 __clock_t::time_point __atime = __clock_t::now()
373 return try_lock_until(__atime);
376 template <
typename _Rep,
typename _Period>
378 !ratio_less_equal<__clock_t::period, _Period>::value,
bool>::type
379 __try_lock_for_impl(
const chrono::duration<_Rep, _Period>& __rtime)
381 __clock_t::time_point __atime = __clock_t::now()
384 return try_lock_until(__atime);
388 #endif // _GLIBCXX_HAS_GTHREADS
401 constexpr try_to_lock_t try_to_lock { };
402 constexpr adopt_lock_t adopt_lock { };
407 template<
typename _Mutex>
411 typedef _Mutex mutex_type;
413 explicit lock_guard(mutex_type& __m) : _M_device(__m)
414 { _M_device.lock(); }
420 { _M_device.unlock(); }
426 mutex_type& _M_device;
430 template<
typename _Mutex>
434 typedef _Mutex mutex_type;
437 : _M_device(0), _M_owns(
false)
441 : _M_device(&__m), _M_owns(
false)
448 : _M_device(&__m), _M_owns(
false)
452 : _M_device(&__m), _M_owns(_M_device->try_lock())
456 : _M_device(&__m), _M_owns(
true)
461 template<
typename _Clock,
typename _Duration>
464 : _M_device(&__m), _M_owns(_M_device->try_lock_until(__atime))
467 template<
typename _Rep,
typename _Period>
470 : _M_device(&__m), _M_owns(_M_device->try_lock_for(__rtime))
483 : _M_device(__u._M_device), _M_owns(__u._M_owns)
506 __throw_system_error(
int(errc::operation_not_permitted));
508 __throw_system_error(
int(errc::resource_deadlock_would_occur));
520 __throw_system_error(
int(errc::operation_not_permitted));
522 __throw_system_error(
int(errc::resource_deadlock_would_occur));
525 _M_owns = _M_device->try_lock();
530 template<
typename _Clock,
typename _Duration>
535 __throw_system_error(
int(errc::operation_not_permitted));
537 __throw_system_error(
int(errc::resource_deadlock_would_occur));
540 _M_owns = _M_device->try_lock_until(__atime);
545 template<
typename _Rep,
typename _Period>
550 __throw_system_error(
int(errc::operation_not_permitted));
552 __throw_system_error(
int(errc::resource_deadlock_would_occur));
555 _M_owns = _M_device->try_lock_for(__rtime);
564 __throw_system_error(
int(errc::operation_not_permitted));
575 std::swap(_M_device, __u._M_device);
576 std::swap(_M_owns, __u._M_owns);
582 mutex_type* __ret = _M_device;
589 owns_lock()
const noexcept
592 explicit operator bool()
const noexcept
593 {
return owns_lock(); }
596 mutex()
const noexcept
597 {
return _M_device; }
600 mutex_type* _M_device;
605 template<
typename _Mutex>
613 template<
typename... _Lock>
615 __do_unlock(tuple<_Lock&...>& __locks)
617 std::get<_Idx>(__locks).unlock();
618 __unlock_impl<_Idx - 1>::__do_unlock(__locks);
623 struct __unlock_impl<-1>
625 template<
typename... _Lock>
627 __do_unlock(tuple<_Lock&...>&)
631 template<
typename _Lock>
633 __try_to_lock(_Lock& __l)
634 {
return unique_lock<_Lock>(__l, try_to_lock); }
636 template<
int _Idx,
bool _Continue = true>
637 struct __try_lock_impl
639 template<
typename... _Lock>
641 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
644 auto __lock = __try_to_lock(std::get<_Idx>(__locks));
645 if (__lock.owns_lock())
647 __try_lock_impl<_Idx + 1, _Idx + 2 <
sizeof...(_Lock)>::
648 __do_try_lock(__locks, __idx);
656 struct __try_lock_impl<_Idx, false>
658 template<
typename... _Lock>
660 __do_try_lock(tuple<_Lock&...>& __locks,
int& __idx)
663 auto __lock = __try_to_lock(std::get<_Idx>(__locks));
664 if (__lock.owns_lock())
682 template<
typename _Lock1,
typename _Lock2,
typename... _Lock3>
684 try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
687 auto __locks =
std::tie(__l1, __l2, __l3...);
689 { __try_lock_impl<0>::__do_try_lock(__locks, __idx); }
706 template<
typename _L1,
typename _L2,
typename ..._L3>
708 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
714 auto __locks =
std::tie(__l2, __l3...);
715 __try_lock_impl<0,
sizeof...(_L3)>::__do_try_lock(__locks, __idx);
724 #ifdef _GLIBCXX_HAS_GTHREADS
729 typedef __gthread_once_t __native_type;
730 __native_type _M_once = __GTHREAD_ONCE_INIT;
734 constexpr
once_flag() noexcept =
default;
741 template<
typename _Callable,
typename... _Args>
746 #ifdef _GLIBCXX_HAVE_TLS
747 extern __thread
void* __once_callable;
748 extern __thread void (*__once_call)();
750 template<
typename _Callable>
754 (*(_Callable*)__once_callable)();
757 extern function<void()> __once_functor;
760 __set_once_functor_lock_ptr(unique_lock<mutex>*);
766 extern "C" void __once_proxy(
void);
769 template<
typename _Callable,
typename... _Args>
773 #ifdef _GLIBCXX_HAVE_TLS
774 auto __bound_functor = std::__bind_simple(std::forward<_Callable>(__f),
775 std::forward<_Args>(__args)...);
776 __once_callable = &__bound_functor;
777 __once_call = &__once_call_impl<decltype(__bound_functor)>;
780 auto __callable = std::__bind_simple(std::forward<_Callable>(__f),
781 std::forward<_Args>(__args)...);
782 __once_functor = [&]() { __callable(); };
783 __set_once_functor_lock_ptr(&__functor_lock);
786 int __e = __gthread_once(&(__once._M_once), &__once_proxy);
788 #ifndef _GLIBCXX_HAVE_TLS
790 __set_once_functor_lock_ptr(0);
794 __throw_system_error(__e);
796 #endif // _GLIBCXX_HAS_GTHREADS
799 _GLIBCXX_END_NAMESPACE_VERSION
801 #endif // _GLIBCXX_USE_C99_STDINT_TR1
805 #endif // _GLIBCXX_MUTEX