Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP___MUTEX_BASE |
| 12 | #define _LIBCPP___MUTEX_BASE |
| 13 | |
| 14 | #include <__config> |
| 15 | #include <chrono> |
| 16 | #include <system_error> |
| 17 | #include <pthread.h> |
| 18 | |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 19 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 20 | #pragma GCC system_header |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 21 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 22 | |
Howard Hinnant | ac417fa | 2010-11-28 19:41:07 +0000 | [diff] [blame] | 23 | #ifdef _LIBCPP_SHARED_LOCK |
| 24 | |
| 25 | namespace ting { |
Howard Hinnant | 2b1b2d4 | 2011-06-14 19:58:17 +0000 | [diff] [blame] | 26 | template <class _Mutex> class shared_lock; |
| 27 | template <class _Mutex> class upgrade_lock; |
Howard Hinnant | ac417fa | 2010-11-28 19:41:07 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | #endif // _LIBCPP_SHARED_LOCK |
| 31 | |
| 32 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 34 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 35 | class _LIBCPP_TYPE_VIS mutex |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 36 | { |
| 37 | pthread_mutex_t __m_; |
| 38 | |
| 39 | public: |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 40 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 384608e | 2012-07-07 20:01:52 +0000 | [diff] [blame] | 41 | #ifndef _LIBCPP_HAS_NO_CONSTEXPR |
Howard Hinnant | 5c90cba | 2012-09-11 16:10:20 +0000 | [diff] [blame] | 42 | constexpr mutex() _NOEXCEPT : __m_(PTHREAD_MUTEX_INITIALIZER) {} |
Howard Hinnant | 384608e | 2012-07-07 20:01:52 +0000 | [diff] [blame] | 43 | #else |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 44 | mutex() _NOEXCEPT {__m_ = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;} |
Howard Hinnant | 384608e | 2012-07-07 20:01:52 +0000 | [diff] [blame] | 45 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 46 | ~mutex(); |
| 47 | |
| 48 | private: |
| 49 | mutex(const mutex&);// = delete; |
| 50 | mutex& operator=(const mutex&);// = delete; |
| 51 | |
| 52 | public: |
| 53 | void lock(); |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 54 | bool try_lock() _NOEXCEPT; |
| 55 | void unlock() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 56 | |
| 57 | typedef pthread_mutex_t* native_handle_type; |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 58 | _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 61 | struct _LIBCPP_TYPE_VIS defer_lock_t {}; |
| 62 | struct _LIBCPP_TYPE_VIS try_to_lock_t {}; |
| 63 | struct _LIBCPP_TYPE_VIS adopt_lock_t {}; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 64 | |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 65 | #if defined(_LIBCPP_HAS_NO_CONSTEXPR) || defined(_LIBCPP_BUILDING_MUTEX) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 66 | |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 67 | extern const defer_lock_t defer_lock; |
| 68 | extern const try_to_lock_t try_to_lock; |
| 69 | extern const adopt_lock_t adopt_lock; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 70 | |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 71 | #else |
| 72 | |
| 73 | constexpr defer_lock_t defer_lock = defer_lock_t(); |
| 74 | constexpr try_to_lock_t try_to_lock = try_to_lock_t(); |
| 75 | constexpr adopt_lock_t adopt_lock = adopt_lock_t(); |
| 76 | |
| 77 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 78 | |
| 79 | template <class _Mutex> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 80 | class _LIBCPP_TYPE_VIS lock_guard |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 81 | { |
| 82 | public: |
| 83 | typedef _Mutex mutex_type; |
| 84 | |
| 85 | private: |
| 86 | mutex_type& __m_; |
| 87 | public: |
| 88 | |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 89 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 90 | explicit lock_guard(mutex_type& __m) |
| 91 | : __m_(__m) {__m_.lock();} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 92 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 93 | lock_guard(mutex_type& __m, adopt_lock_t) |
| 94 | : __m_(__m) {} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 95 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 96 | ~lock_guard() {__m_.unlock();} |
| 97 | |
| 98 | private: |
| 99 | lock_guard(lock_guard const&);// = delete; |
| 100 | lock_guard& operator=(lock_guard const&);// = delete; |
| 101 | }; |
| 102 | |
| 103 | template <class _Mutex> |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 104 | class _LIBCPP_TYPE_VIS unique_lock |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 105 | { |
| 106 | public: |
| 107 | typedef _Mutex mutex_type; |
| 108 | |
| 109 | private: |
| 110 | mutex_type* __m_; |
| 111 | bool __owns_; |
| 112 | |
| 113 | public: |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 114 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 115 | unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 116 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 117 | explicit unique_lock(mutex_type& __m) |
| 118 | : __m_(&__m), __owns_(true) {__m_->lock();} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 119 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 120 | unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 121 | : __m_(&__m), __owns_(false) {} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 122 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 123 | unique_lock(mutex_type& __m, try_to_lock_t) |
| 124 | : __m_(&__m), __owns_(__m.try_lock()) {} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 125 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 126 | unique_lock(mutex_type& __m, adopt_lock_t) |
| 127 | : __m_(&__m), __owns_(true) {} |
| 128 | template <class _Clock, class _Duration> |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 129 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 130 | unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t) |
| 131 | : __m_(&__m), __owns_(__m.try_lock_until(__t)) {} |
| 132 | template <class _Rep, class _Period> |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 133 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 134 | unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d) |
| 135 | : __m_(&__m), __owns_(__m.try_lock_for(__d)) {} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 136 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 137 | ~unique_lock() |
| 138 | { |
| 139 | if (__owns_) |
| 140 | __m_->unlock(); |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | unique_lock(unique_lock const&); // = delete; |
| 145 | unique_lock& operator=(unique_lock const&); // = delete; |
| 146 | |
| 147 | public: |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 148 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 149 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 150 | unique_lock(unique_lock&& __u) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 151 | : __m_(__u.__m_), __owns_(__u.__owns_) |
| 152 | {__u.__m_ = nullptr; __u.__owns_ = false;} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 153 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 154 | unique_lock& operator=(unique_lock&& __u) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 155 | { |
| 156 | if (__owns_) |
| 157 | __m_->unlock(); |
| 158 | __m_ = __u.__m_; |
| 159 | __owns_ = __u.__owns_; |
| 160 | __u.__m_ = nullptr; |
| 161 | __u.__owns_ = false; |
| 162 | return *this; |
| 163 | } |
Howard Hinnant | ac417fa | 2010-11-28 19:41:07 +0000 | [diff] [blame] | 164 | |
| 165 | #ifdef _LIBCPP_SHARED_LOCK |
| 166 | |
| 167 | unique_lock(ting::shared_lock<mutex_type>&&, try_to_lock_t); |
| 168 | template <class _Clock, class _Duration> |
| 169 | unique_lock(ting::shared_lock<mutex_type>&&, |
| 170 | const chrono::time_point<_Clock, _Duration>&); |
| 171 | template <class _Rep, class _Period> |
| 172 | unique_lock(ting::shared_lock<mutex_type>&&, |
| 173 | const chrono::duration<_Rep, _Period>&); |
| 174 | |
| 175 | explicit unique_lock(ting::upgrade_lock<mutex_type>&&); |
| 176 | unique_lock(ting::upgrade_lock<mutex_type>&&, try_to_lock_t); |
| 177 | template <class _Clock, class _Duration> |
| 178 | unique_lock(ting::upgrade_lock<mutex_type>&&, |
| 179 | const chrono::time_point<_Clock, _Duration>&); |
| 180 | template <class _Rep, class _Period> |
| 181 | unique_lock(ting::upgrade_lock<mutex_type>&&, |
| 182 | const chrono::duration<_Rep, _Period>&); |
| 183 | |
| 184 | #endif // _LIBCPP_SHARED_LOCK |
| 185 | |
Howard Hinnant | 73d21a4 | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 186 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 187 | |
| 188 | void lock(); |
| 189 | bool try_lock(); |
| 190 | |
| 191 | template <class _Rep, class _Period> |
| 192 | bool try_lock_for(const chrono::duration<_Rep, _Period>& __d); |
| 193 | template <class _Clock, class _Duration> |
| 194 | bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); |
| 195 | |
| 196 | void unlock(); |
| 197 | |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 198 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 199 | void swap(unique_lock& __u) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 200 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 201 | _VSTD::swap(__m_, __u.__m_); |
| 202 | _VSTD::swap(__owns_, __u.__owns_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 203 | } |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 204 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 205 | mutex_type* release() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 206 | { |
| 207 | mutex_type* __m = __m_; |
| 208 | __m_ = nullptr; |
| 209 | __owns_ = false; |
| 210 | return __m; |
| 211 | } |
| 212 | |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 213 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 214 | bool owns_lock() const _NOEXCEPT {return __owns_;} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 215 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7786188 | 2012-02-21 21:46:43 +0000 | [diff] [blame] | 216 | _LIBCPP_EXPLICIT |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 217 | operator bool () const _NOEXCEPT {return __owns_;} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 218 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 219 | mutex_type* mutex() const _NOEXCEPT {return __m_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | template <class _Mutex> |
| 223 | void |
| 224 | unique_lock<_Mutex>::lock() |
| 225 | { |
| 226 | if (__m_ == nullptr) |
| 227 | __throw_system_error(EPERM, "unique_lock::lock: references null mutex"); |
| 228 | if (__owns_) |
| 229 | __throw_system_error(EDEADLK, "unique_lock::lock: already locked"); |
| 230 | __m_->lock(); |
| 231 | __owns_ = true; |
| 232 | } |
| 233 | |
| 234 | template <class _Mutex> |
| 235 | bool |
| 236 | unique_lock<_Mutex>::try_lock() |
| 237 | { |
| 238 | if (__m_ == nullptr) |
| 239 | __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex"); |
| 240 | if (__owns_) |
| 241 | __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked"); |
| 242 | __owns_ = __m_->try_lock(); |
| 243 | return __owns_; |
| 244 | } |
| 245 | |
| 246 | template <class _Mutex> |
| 247 | template <class _Rep, class _Period> |
| 248 | bool |
| 249 | unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) |
| 250 | { |
| 251 | if (__m_ == nullptr) |
| 252 | __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex"); |
| 253 | if (__owns_) |
| 254 | __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked"); |
| 255 | __owns_ = __m_->try_lock_for(__d); |
| 256 | return __owns_; |
| 257 | } |
| 258 | |
| 259 | template <class _Mutex> |
| 260 | template <class _Clock, class _Duration> |
| 261 | bool |
| 262 | unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) |
| 263 | { |
| 264 | if (__m_ == nullptr) |
| 265 | __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex"); |
| 266 | if (__owns_) |
| 267 | __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked"); |
| 268 | __owns_ = __m_->try_lock_until(__t); |
| 269 | return __owns_; |
| 270 | } |
| 271 | |
| 272 | template <class _Mutex> |
| 273 | void |
| 274 | unique_lock<_Mutex>::unlock() |
| 275 | { |
| 276 | if (!__owns_) |
| 277 | __throw_system_error(EPERM, "unique_lock::unlock: not locked"); |
| 278 | __m_->unlock(); |
| 279 | __owns_ = false; |
| 280 | } |
| 281 | |
| 282 | template <class _Mutex> |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 283 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 284 | void |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 +0000 | [diff] [blame] | 285 | swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT |
| 286 | {__x.swap(__y);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 287 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 288 | struct _LIBCPP_TYPE_VIS cv_status |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 289 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 290 | enum __lx { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 291 | no_timeout, |
| 292 | timeout |
| 293 | }; |
| 294 | |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 295 | __lx __v_; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 296 | |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 297 | _LIBCPP_INLINE_VISIBILITY cv_status(__lx __v) : __v_(__v) {} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 298 | _LIBCPP_INLINE_VISIBILITY operator int() const {return __v_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 299 | |
| 300 | }; |
| 301 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame^] | 302 | class _LIBCPP_TYPE_VIS condition_variable |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 303 | { |
| 304 | pthread_cond_t __cv_; |
| 305 | public: |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 306 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 384608e | 2012-07-07 20:01:52 +0000 | [diff] [blame] | 307 | #ifndef _LIBCPP_HAS_NO_CONSTEXPR |
Howard Hinnant | 5c90cba | 2012-09-11 16:10:20 +0000 | [diff] [blame] | 308 | constexpr condition_variable() : __cv_(PTHREAD_COND_INITIALIZER) {} |
Howard Hinnant | 384608e | 2012-07-07 20:01:52 +0000 | [diff] [blame] | 309 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 310 | condition_variable() {__cv_ = (pthread_cond_t)PTHREAD_COND_INITIALIZER;} |
Howard Hinnant | 384608e | 2012-07-07 20:01:52 +0000 | [diff] [blame] | 311 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 312 | ~condition_variable(); |
| 313 | |
| 314 | private: |
| 315 | condition_variable(const condition_variable&); // = delete; |
| 316 | condition_variable& operator=(const condition_variable&); // = delete; |
| 317 | |
| 318 | public: |
Howard Hinnant | c8f7413 | 2012-07-21 16:32:53 +0000 | [diff] [blame] | 319 | void notify_one() _NOEXCEPT; |
| 320 | void notify_all() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 321 | |
| 322 | void wait(unique_lock<mutex>& __lk); |
| 323 | template <class _Predicate> |
| 324 | void wait(unique_lock<mutex>& __lk, _Predicate __pred); |
| 325 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 326 | template <class _Clock, class _Duration> |
| 327 | cv_status |
| 328 | wait_until(unique_lock<mutex>& __lk, |
| 329 | const chrono::time_point<_Clock, _Duration>& __t); |
| 330 | |
| 331 | template <class _Clock, class _Duration, class _Predicate> |
| 332 | bool |
| 333 | wait_until(unique_lock<mutex>& __lk, |
| 334 | const chrono::time_point<_Clock, _Duration>& __t, |
| 335 | _Predicate __pred); |
| 336 | |
| 337 | template <class _Rep, class _Period> |
| 338 | cv_status |
| 339 | wait_for(unique_lock<mutex>& __lk, |
| 340 | const chrono::duration<_Rep, _Period>& __d); |
| 341 | |
| 342 | template <class _Rep, class _Period, class _Predicate> |
| 343 | bool |
| 344 | wait_for(unique_lock<mutex>& __lk, |
| 345 | const chrono::duration<_Rep, _Period>& __d, |
| 346 | _Predicate __pred); |
| 347 | |
| 348 | typedef pthread_cond_t* native_handle_type; |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 349 | _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 350 | |
| 351 | private: |
| 352 | void __do_timed_wait(unique_lock<mutex>& __lk, |
| 353 | chrono::time_point<chrono::system_clock, chrono::nanoseconds>); |
| 354 | }; |
| 355 | |
| 356 | template <class _To, class _Rep, class _Period> |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 357 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 358 | typename enable_if |
| 359 | < |
| 360 | chrono::__is_duration<_To>::value, |
| 361 | _To |
| 362 | >::type |
| 363 | __ceil(chrono::duration<_Rep, _Period> __d) |
| 364 | { |
| 365 | using namespace chrono; |
| 366 | _To __r = duration_cast<_To>(__d); |
| 367 | if (__r < __d) |
| 368 | ++__r; |
| 369 | return __r; |
| 370 | } |
| 371 | |
| 372 | template <class _Predicate> |
| 373 | void |
| 374 | condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred) |
| 375 | { |
| 376 | while (!__pred()) |
| 377 | wait(__lk); |
| 378 | } |
| 379 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 380 | template <class _Clock, class _Duration> |
| 381 | cv_status |
| 382 | condition_variable::wait_until(unique_lock<mutex>& __lk, |
| 383 | const chrono::time_point<_Clock, _Duration>& __t) |
| 384 | { |
| 385 | using namespace chrono; |
Howard Hinnant | cf115d2 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 386 | wait_for(__lk, __t - _Clock::now()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 387 | return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout; |
| 388 | } |
| 389 | |
| 390 | template <class _Clock, class _Duration, class _Predicate> |
| 391 | bool |
| 392 | condition_variable::wait_until(unique_lock<mutex>& __lk, |
| 393 | const chrono::time_point<_Clock, _Duration>& __t, |
| 394 | _Predicate __pred) |
| 395 | { |
| 396 | while (!__pred()) |
| 397 | { |
| 398 | if (wait_until(__lk, __t) == cv_status::timeout) |
| 399 | return __pred(); |
| 400 | } |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | template <class _Rep, class _Period> |
| 405 | cv_status |
| 406 | condition_variable::wait_for(unique_lock<mutex>& __lk, |
| 407 | const chrono::duration<_Rep, _Period>& __d) |
| 408 | { |
| 409 | using namespace chrono; |
Howard Hinnant | cf115d2 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 410 | if (__d <= __d.zero()) |
| 411 | return cv_status::timeout; |
| 412 | typedef time_point<system_clock, duration<long double, nano> > __sys_tpf; |
| 413 | typedef time_point<system_clock, nanoseconds> __sys_tpi; |
| 414 | __sys_tpf _Max = __sys_tpi::max(); |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 415 | system_clock::time_point __s_now = system_clock::now(); |
| 416 | steady_clock::time_point __c_now = steady_clock::now(); |
Howard Hinnant | cf115d2 | 2012-08-30 19:14:33 +0000 | [diff] [blame] | 417 | if (_Max - __d > __s_now) |
| 418 | __do_timed_wait(__lk, __s_now + __ceil<nanoseconds>(__d)); |
| 419 | else |
| 420 | __do_timed_wait(__lk, __sys_tpi::max()); |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 421 | return steady_clock::now() - __c_now < __d ? cv_status::no_timeout : |
| 422 | cv_status::timeout; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | template <class _Rep, class _Period, class _Predicate> |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 426 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 427 | bool |
| 428 | condition_variable::wait_for(unique_lock<mutex>& __lk, |
| 429 | const chrono::duration<_Rep, _Period>& __d, |
| 430 | _Predicate __pred) |
| 431 | { |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 432 | return wait_until(__lk, chrono::steady_clock::now() + __d, |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 433 | _VSTD::move(__pred)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | _LIBCPP_END_NAMESPACE_STD |
| 437 | |
| 438 | #endif // _LIBCPP___MUTEX_BASE |