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