30 #ifndef _GLIBCXX_MUTEX
31 #define _GLIBCXX_MUTEX 1
33 #pragma GCC system_header
35 #ifndef __GXX_EXPERIMENTAL_CXX0X__
47 #include <bits/gthr.h>
50 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
65 typedef __gthread_mutex_t __native_type;
66 __native_type _M_mutex;
69 typedef __native_type* native_handle_type;
74 #ifdef __GTHREAD_MUTEX_INIT
75 __native_type __tmp = __GTHREAD_MUTEX_INIT;
78 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
88 int __e = __gthread_mutex_lock(&_M_mutex);
92 __throw_system_error(__e);
99 return !__gthread_mutex_trylock(&_M_mutex);
106 __gthread_mutex_unlock(&_M_mutex);
111 {
return &_M_mutex; }
117 typedef __gthread_recursive_mutex_t __native_type;
118 __native_type _M_mutex;
121 typedef __native_type* native_handle_type;
126 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
127 __native_type __tmp = __GTHREAD_RECURSIVE_MUTEX_INIT;
130 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
140 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
144 __throw_system_error(__e);
151 return !__gthread_recursive_mutex_trylock(&_M_mutex);
158 __gthread_recursive_mutex_unlock(&_M_mutex);
163 {
return &_M_mutex; }
169 typedef __gthread_mutex_t __native_type;
171 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
177 __native_type _M_mutex;
180 typedef __native_type* native_handle_type;
184 #ifdef __GTHREAD_MUTEX_INIT
185 __native_type __tmp = __GTHREAD_MUTEX_INIT;
188 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
198 int __e = __gthread_mutex_lock(&_M_mutex);
202 __throw_system_error(__e);
209 return !__gthread_mutex_trylock(&_M_mutex);
212 template <
class _Rep,
class _Period>
215 {
return __try_lock_for_impl(__rtime); }
217 template <
class _Clock,
class _Duration>
227 __gthread_time_t __ts = {
228 static_cast<std::time_t
>(__s.time_since_epoch().count()),
229 static_cast<long>(__ns.count())
232 return !__gthread_mutex_timedlock(&_M_mutex, &__ts);
239 __gthread_mutex_unlock(&_M_mutex);
244 {
return &_M_mutex; }
247 template<
typename _Rep,
typename _Period>
255 return try_lock_until(__atime);
258 template <
typename _Rep,
typename _Period>
260 !ratio_less_equal<__clock_t::period, _Period>::value,
bool>::type
266 return try_lock_until(__atime);
273 typedef __gthread_recursive_mutex_t __native_type;
275 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
281 __native_type _M_mutex;
284 typedef __native_type* native_handle_type;
289 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
290 __native_type __tmp = __GTHREAD_RECURSIVE_MUTEX_INIT;
293 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
303 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
307 __throw_system_error(__e);
314 return !__gthread_recursive_mutex_trylock(&_M_mutex);
317 template <
class _Rep,
class _Period>
320 {
return __try_lock_for_impl(__rtime); }
322 template <
class _Clock,
class _Duration>
332 __gthread_time_t __ts = {
333 static_cast<std::time_t
>(__s.time_since_epoch().count()),
334 static_cast<long>(__ns.count())
337 return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts);
344 __gthread_recursive_mutex_unlock(&_M_mutex);
349 {
return &_M_mutex; }
352 template<
typename _Rep,
typename _Period>
360 return try_lock_until(__atime);
363 template <
typename _Rep,
typename _Period>
365 !ratio_less_equal<__clock_t::period, _Period>::value,
bool>::type
371 return try_lock_until(__atime);
398 what()
const throw();
404 template<
typename _Mutex>
408 typedef _Mutex mutex_type;
410 explicit lock_guard(mutex_type& __m) : _M_device(__m)
411 { _M_device.lock(); }
414 { _M_device.lock(); }
417 { _M_device.unlock(); }
423 mutex_type& _M_device;
427 template<
typename _Mutex>
431 typedef _Mutex mutex_type;
434 : _M_device(0), _M_owns(
false)
438 : _M_device(&__m), _M_owns(
false)
445 : _M_device(&__m), _M_owns(
false)
449 : _M_device(&__m), _M_owns(_M_device->try_lock())
453 : _M_device(&__m), _M_owns(
true)
458 template<
typename _Clock,
typename _Duration>
461 : _M_device(&__m), _M_owns(_M_device->try_lock_until(__atime))
464 template<
typename _Rep,
typename _Period>
467 : _M_device(&__m), _M_owns(_M_device->try_lock_for(__rtime))
480 : _M_device(__u._M_device), _M_owns(__u._M_owns)
503 __throw_system_error(
int(errc::operation_not_permitted));
505 __throw_system_error(
int(errc::resource_deadlock_would_occur));
517 __throw_system_error(
int(errc::operation_not_permitted));
519 __throw_system_error(
int(errc::resource_deadlock_would_occur));
522 _M_owns = _M_device->try_lock();
527 template<
typename _Clock,
typename _Duration>
532 __throw_system_error(
int(errc::operation_not_permitted));
534 __throw_system_error(
int(errc::resource_deadlock_would_occur));
537 _M_owns = _M_device->try_lock_until(__atime);
542 template<
typename _Rep,
typename _Period>
547 __throw_system_error(
int(errc::operation_not_permitted));
549 __throw_system_error(
int(errc::resource_deadlock_would_occur));
552 _M_owns = _M_device->try_lock_for(__rtime);
561 __throw_system_error(
int(errc::operation_not_permitted));
572 std::swap(_M_device, __u._M_device);
573 std::swap(_M_owns, __u._M_owns);
579 mutex_type* __ret = _M_device;
589 operator bool ()
const
590 {
return owns_lock(); }
594 {
return _M_device; }
597 mutex_type* _M_device;
601 template<
typename _Mutex>
606 template<
typename _Mutex>
608 swap(unique_lock<_Mutex>&& __x, unique_lock<_Mutex>& __y)
611 template<
typename _Mutex>
613 swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>&& __y)
619 template<
typename... _Lock>
621 __do_unlock(tuple<_Lock&...>& __locks)
623 std::get<_Idx>(__locks).unlock();
624 __unlock_impl<_Idx - 1>::__do_unlock(__locks);
629 struct __unlock_impl<-1>
631 template<
typename... _Lock>
633 __do_unlock(tuple<_Lock&...>&)
637 template<
int _Idx,
bool _Continue = true>
638 struct __try_lock_impl
640 template<
typename... _Lock>
642 __do_try_lock(tuple<_Lock&...>& __locks)
644 if(std::get<_Idx>(__locks).
try_lock())
646 return __try_lock_impl<_Idx + 1,
647 _Idx + 2 <
sizeof...(_Lock)>::__do_try_lock(__locks);
651 __unlock_impl<_Idx>::__do_unlock(__locks);
658 struct __try_lock_impl<_Idx, false>
660 template<
typename... _Lock>
662 __do_try_lock(tuple<_Lock&...>& __locks)
664 if(std::get<_Idx>(__locks).
try_lock())
668 __unlock_impl<_Idx>::__do_unlock(__locks);
684 template<
typename _Lock1,
typename _Lock2,
typename... _Lock3>
686 try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
688 tuple<_Lock1&, _Lock2&, _Lock3&...> __locks(__l1, __l2, __l3...);
689 return __try_lock_impl<0>::__do_try_lock(__locks);
693 template<
typename _L1,
typename _L2,
typename ..._L3>
695 lock(_L1&, _L2&, _L3&...);
701 typedef __gthread_once_t __native_type;
702 __native_type _M_once;
707 __native_type __tmp = __GTHREAD_ONCE_INIT;
714 template<
typename _Callable,
typename... _Args>
719 #ifdef _GLIBCXX_HAVE_TLS
720 extern __thread
void* __once_callable;
721 extern __thread void (*__once_call)();
723 template<
typename _Callable>
727 (*(_Callable*)__once_callable)();
730 extern function<void()> __once_functor;
733 __set_once_functor_lock_ptr(unique_lock<mutex>*);
739 extern "C" void __once_proxy();
742 template<
typename _Callable,
typename... _Args>
746 #ifdef _GLIBCXX_HAVE_TLS
747 auto __bound_functor =
bind(__f, __args...);
748 __once_callable = &__bound_functor;
749 __once_call = &__once_call_impl<decltype(__bound_functor)>;
752 __once_functor =
bind(__f, __args...);
753 __set_once_functor_lock_ptr(&__functor_lock);
756 int __e = __gthread_once(&(__once._M_once), &__once_proxy);
758 #ifndef _GLIBCXX_HAVE_TLS
760 __set_once_functor_lock_ptr(0);
764 __throw_system_error(__e);
770 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
772 #endif // __GXX_EXPERIMENTAL_CXX0X__
774 #endif // _GLIBCXX_MUTEX
void call_once(once_flag &__once, _Callable __f, _Args &&...__args)
call_once
Do not acquire ownership of the mutex.
_ToDuration duration_cast(const duration< _Rep, _Period > &__d)
duration_cast
Assume the calling thread has already obtained mutex ownership and manage it.
Try to acquire ownership of the mutex without blocking.
int try_lock(_Lock1 &__l1, _Lock2 &__l2, _Lock3 &...__l3)
Generic try_lock.
Thrown to indicate errors with lock operations.
_Bind< typename _Maybe_wrap_member_pointer< _Functor >::type(_ArgTypes...)> bind(_Functor __f, _ArgTypes...__args)
bind
void lock(_L1 &, _L2 &, _L3 &...)
lock
_OI move(_II __first, _II __last, _OI __result)
Moves the range [first,last) into result.
time_point< _Clock, _ToDuration > time_point_cast(const time_point< _Clock, _Duration > &__t)
time_point_cast
Base class for all library exceptions.