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