30 #ifndef _GLIBCXX_MUTEX_H 31 #define _GLIBCXX_MUTEX_H 1 33 #pragma GCC system_header 35 #if __cplusplus < 201103L 41 #include <bits/gthr.h> 43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 #ifndef _GLIBCXX_THREAD_SAFETY_ANNOTATION 48 # ifdef _GLIBCXX_HAS_THREAD_SAFETY_ANNOTATIONS 49 # define _GLIBCXX_THREAD_SAFETY_ANNOTATION(x) __attribute__ ((x)) 51 # define _GLIBCXX_THREAD_SAFETY_ANNOTATION(x) 53 #endif // _GLIBCXX_THREAD_SAFETY_ANNOTATION 63 #ifdef _GLIBCXX_HAS_GTHREADS 68 typedef __gthread_mutex_t __native_type;
70 #ifdef __GTHREAD_MUTEX_INIT 71 __native_type _M_mutex = __GTHREAD_MUTEX_INIT;
73 constexpr __mutex_base() noexcept = default;
75 __native_type _M_mutex;
77 __mutex_base() noexcept
80 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
83 ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
86 __mutex_base(
const __mutex_base&) =
delete;
87 __mutex_base& operator=(
const __mutex_base&) =
delete;
91 class _GLIBCXX_THREAD_SAFETY_ANNOTATION(capability("
mutex"))
mutex 92 :
private __mutex_base
95 typedef __native_type* native_handle_type;
97 #ifdef __GTHREAD_MUTEX_INIT 100 mutex() noexcept =
default;
107 lock() _GLIBCXX_THREAD_SAFETY_ANNOTATION(acquire_capability())
109 int __e = __gthread_mutex_lock(&_M_mutex);
113 __throw_system_error(__e);
118 _GLIBCXX_THREAD_SAFETY_ANNOTATION(try_acquire_capability(
true))
121 return !__gthread_mutex_trylock(&_M_mutex);
125 unlock() _GLIBCXX_THREAD_SAFETY_ANNOTATION(release_capability())
128 __gthread_mutex_unlock(&_M_mutex);
132 native_handle() noexcept
133 {
return &_M_mutex; }
136 #endif // _GLIBCXX_HAS_GTHREADS 162 template<
typename _Mutex>
163 class _GLIBCXX_THREAD_SAFETY_ANNOTATION(scoped_lockable)
lock_guard 166 typedef _Mutex mutex_type;
169 _GLIBCXX_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
171 { _M_device.lock(); }
174 _GLIBCXX_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
178 ~
lock_guard() _GLIBCXX_THREAD_SAFETY_ANNOTATION(release_capability())
179 { _M_device.unlock(); }
185 mutex_type& _M_device;
189 _GLIBCXX_END_NAMESPACE_VERSION
192 #endif // _GLIBCXX_MUTEX_H Try to acquire ownership of the mutex without blocking.
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
constexpr try_to_lock_t try_to_lock
Tag used to prevent a scoped lock from blocking if a mutex is locked.
A simple scoped lock type.
Do not acquire ownership of the mutex.
Assume the calling thread has already obtained mutex ownership and manage it.
int try_lock(_Lock1 &__l1, _Lock2 &__l2, _Lock3 &... __l3)
Generic try_lock.
ISO C++ entities toplevel namespace is std.
constexpr adopt_lock_t adopt_lock
Tag used to make a scoped lock take ownership of a locked mutex.
constexpr defer_lock_t defer_lock
Tag used to prevent a scoped lock from acquiring ownership of a mutex.