| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#ifndef _GLIBCXX_MUTEX_H |
| 31 |
#define _GLIBCXX_MUTEX_H 1 |
| 32 |
|
| 33 |
#ifdef _GLIBCXX_SYSHDR |
| 34 |
#pragma GCC system_header |
| 35 |
#endif |
| 36 |
|
| 37 |
#if __cplusplus < 201103L |
| 38 |
# include <bits/c++0x_warning.h> |
| 39 |
#else |
| 40 |
|
| 41 |
#include <errno.h> |
| 42 |
#include <bits/functexcept.h> |
| 43 |
#include <bits/gthr.h> |
| 44 |
|
| 45 |
namespace std _GLIBCXX_VISIBILITY(default) |
| 46 |
{ |
| 47 |
_GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
#ifdef _GLIBCXX_HAS_GTHREADS |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
class __mutex_base |
| 62 |
{ |
| 63 |
protected: |
| 64 |
typedef __gthread_mutex_t __native_type; |
| 65 |
|
| 66 |
#ifdef __GTHREAD_MUTEX_INIT |
| 67 |
__native_type _M_mutex = __GTHREAD_MUTEX_INIT; |
| 68 |
|
| 69 |
constexpr __mutex_base() noexcept = default; |
| 70 |
#else |
| 71 |
__native_type _M_mutex; |
| 72 |
|
| 73 |
__mutex_base() noexcept |
| 74 |
{ |
| 75 |
|
| 76 |
__GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex); |
| 77 |
} |
| 78 |
|
| 79 |
~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); } |
| 80 |
#endif |
| 81 |
|
| 82 |
__mutex_base(const __mutex_base&) = delete; |
| 83 |
__mutex_base& operator=(const __mutex_base&) = delete; |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
class mutex : private __mutex_base |
| 99 |
{ |
| 100 |
public: |
| 101 |
typedef __native_type* native_handle_type; |
| 102 |
|
| 103 |
#ifdef __GTHREAD_MUTEX_INIT |
| 104 |
constexpr |
| 105 |
#endif |
| 106 |
mutex() noexcept = default; |
| 107 |
~mutex() = default; |
| 108 |
|
| 109 |
mutex(const mutex&) = delete; |
| 110 |
mutex& operator=(const mutex&) = delete; |
| 111 |
|
| 112 |
void |
| 113 |
lock() |
| 114 |
{ |
| 115 |
int __e = __gthread_mutex_lock(&_M_mutex); |
| 116 |
|
| 117 |
|
| 118 |
if (__e) |
| 119 |
__throw_system_error(__e); |
| 120 |
} |
| 121 |
|
| 122 |
_GLIBCXX_NODISCARD |
| 123 |
bool |
| 124 |
try_lock() noexcept |
| 125 |
{ |
| 126 |
|
| 127 |
return !__gthread_mutex_trylock(&_M_mutex); |
| 128 |
} |
| 129 |
|
| 130 |
void |
| 131 |
unlock() |
| 132 |
{ |
| 133 |
|
| 134 |
__gthread_mutex_unlock(&_M_mutex); |
| 135 |
} |
| 136 |
|
| 137 |
native_handle_type |
| 138 |
native_handle() noexcept |
| 139 |
{ return &_M_mutex; } |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
class __condvar |
| 146 |
{ |
| 147 |
using timespec = __gthread_time_t; |
| 148 |
|
| 149 |
public: |
| 150 |
__condvar() noexcept |
| 151 |
{ |
| 152 |
#ifndef __GTHREAD_COND_INIT |
| 153 |
__GTHREAD_COND_INIT_FUNCTION(&_M_cond); |
| 154 |
#endif |
| 155 |
} |
| 156 |
|
| 157 |
~__condvar() |
| 158 |
{ |
| 159 |
int __e __attribute__((__unused__)) = __gthread_cond_destroy(&_M_cond); |
| 160 |
__glibcxx_assert(__e != EBUSY); |
| 161 |
} |
| 162 |
|
| 163 |
__condvar(const __condvar&) = delete; |
| 164 |
__condvar& operator=(const __condvar&) = delete; |
| 165 |
|
| 166 |
__gthread_cond_t* native_handle() noexcept { return &_M_cond; } |
| 167 |
|
| 168 |
|
| 169 |
void |
| 170 |
wait(mutex& __m) |
| 171 |
{ |
| 172 |
int __e __attribute__((__unused__)) |
| 173 |
= __gthread_cond_wait(&_M_cond, __m.native_handle()); |
| 174 |
__glibcxx_assert(__e == 0); |
| 175 |
} |
| 176 |
|
| 177 |
void |
| 178 |
wait_until(mutex& __m, timespec& __abs_time) |
| 179 |
{ |
| 180 |
__gthread_cond_timedwait(&_M_cond, __m.native_handle(), &__abs_time); |
| 181 |
} |
| 182 |
|
| 183 |
#ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT |
| 184 |
void |
| 185 |
wait_until(mutex& __m, clockid_t __clock, timespec& __abs_time) |
| 186 |
{ |
| 187 |
pthread_cond_clockwait(&_M_cond, __m.native_handle(), __clock, |
| 188 |
&__abs_time); |
| 189 |
} |
| 190 |
#endif |
| 191 |
|
| 192 |
void |
| 193 |
notify_one() noexcept |
| 194 |
{ |
| 195 |
int __e __attribute__((__unused__)) = __gthread_cond_signal(&_M_cond); |
| 196 |
__glibcxx_assert(__e == 0); |
| 197 |
} |
| 198 |
|
| 199 |
void |
| 200 |
notify_all() noexcept |
| 201 |
{ |
| 202 |
int __e __attribute__((__unused__)) = __gthread_cond_broadcast(&_M_cond); |
| 203 |
__glibcxx_assert(__e == 0); |
| 204 |
} |
| 205 |
|
| 206 |
protected: |
| 207 |
#ifdef __GTHREAD_COND_INIT |
| 208 |
__gthread_cond_t _M_cond = __GTHREAD_COND_INIT; |
| 209 |
#else |
| 210 |
__gthread_cond_t _M_cond; |
| 211 |
#endif |
| 212 |
}; |
| 213 |
|
| 214 |
|
| 215 |
#endif |
| 216 |
|
| 217 |
|
| 218 |
struct defer_lock_t { explicit defer_lock_t() = default; }; |
| 219 |
|
| 220 |
|
| 221 |
struct try_to_lock_t { explicit try_to_lock_t() = default; }; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
struct adopt_lock_t { explicit adopt_lock_t() = default; }; |
| 226 |
|
| 227 |
|
| 228 |
_GLIBCXX17_INLINE constexpr defer_lock_t defer_lock { }; |
| 229 |
|
| 230 |
|
| 231 |
_GLIBCXX17_INLINE constexpr try_to_lock_t try_to_lock { }; |
| 232 |
|
| 233 |
|
| 234 |
_GLIBCXX17_INLINE constexpr adopt_lock_t adopt_lock { }; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
template<typename _Mutex> |
| 245 |
class lock_guard |
| 246 |
{ |
| 247 |
public: |
| 248 |
typedef _Mutex mutex_type; |
| 249 |
|
| 250 |
[[__nodiscard__]] |
| 251 |
explicit lock_guard(mutex_type& __m) : _M_device(__m) |
| 252 |
{ _M_device.lock(); } |
| 253 |
|
| 254 |
[[__nodiscard__]] |
| 255 |
lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m) |
| 256 |
{ } |
| 257 |
|
| 258 |
~lock_guard() |
| 259 |
{ _M_device.unlock(); } |
| 260 |
|
| 261 |
lock_guard(const lock_guard&) = delete; |
| 262 |
lock_guard& operator=(const lock_guard&) = delete; |
| 263 |
|
| 264 |
private: |
| 265 |
mutex_type& _M_device; |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
_GLIBCXX_END_NAMESPACE_VERSION |
| 270 |
} |
| 271 |
#endif |
| 272 |
#endif |