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