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
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 mutex :
private __mutex_base
86 typedef __native_type* native_handle_type;
88 #ifdef __GTHREAD_MUTEX_INIT 91 mutex() noexcept =
default;
100 int __e = __gthread_mutex_lock(&_M_mutex);
104 __throw_system_error(__e);
111 return !__gthread_mutex_trylock(&_M_mutex);
118 __gthread_mutex_unlock(&_M_mutex);
122 native_handle() noexcept
123 {
return &_M_mutex; }
129 using timespec = __gthread_time_t;
134 #ifndef __GTHREAD_COND_INIT 135 __GTHREAD_COND_INIT_FUNCTION(&_M_cond);
141 int __e __attribute__((__unused__)) = __gthread_cond_destroy(&_M_cond);
142 __glibcxx_assert(__e != EBUSY);
145 __condvar(
const __condvar&) =
delete;
146 __condvar& operator=(
const __condvar&) =
delete;
148 __gthread_cond_t* native_handle() noexcept {
return &_M_cond; }
152 wait(mutex& __m) noexcept
154 int __e __attribute__((__unused__))
155 = __gthread_cond_wait(&_M_cond, __m.native_handle());
156 __glibcxx_assert(__e == 0);
160 wait_until(mutex& __m, timespec& __abs_time) noexcept
162 __gthread_cond_timedwait(&_M_cond, __m.native_handle(), &__abs_time);
165 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT 167 wait_until(mutex& __m, clockid_t __clock, timespec& __abs_time) noexcept
169 pthread_cond_clockwait(&_M_cond, __m.native_handle(), __clock,
175 notify_one() noexcept
177 int __e __attribute__((__unused__)) = __gthread_cond_signal(&_M_cond);
178 __glibcxx_assert(__e == 0);
182 notify_all() noexcept
184 int __e __attribute__((__unused__)) = __gthread_cond_broadcast(&_M_cond);
185 __glibcxx_assert(__e == 0);
189 #ifdef __GTHREAD_COND_INIT 190 __gthread_cond_t _M_cond = __GTHREAD_COND_INIT;
192 __gthread_cond_t _M_cond;
196 #endif // _GLIBCXX_HAS_GTHREADS 222 template<
typename _Mutex>
226 typedef _Mutex mutex_type;
228 explicit lock_guard(mutex_type& __m) : _M_device(__m)
229 { _M_device.lock(); }
235 { _M_device.unlock(); }
241 mutex_type& _M_device;
245 _GLIBCXX_END_NAMESPACE_VERSION
248 #endif // _GLIBCXX_MUTEX_H Do not acquire ownership of the mutex.
Assume the calling thread has already obtained mutex ownership and manage it.
Try to acquire ownership of the mutex without blocking.
constexpr adopt_lock_t adopt_lock
Tag used to make a scoped lock take ownership of a locked mutex.
constexpr try_to_lock_t try_to_lock
Tag used to prevent a scoped lock from blocking if a mutex is locked.
constexpr defer_lock_t defer_lock
Tag used to prevent a scoped lock from acquiring ownership of a mutex.
ISO C++ entities toplevel namespace is std.
A simple scoped lock type.