Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
Chandler Carruth | 7c3769d | 2019-01-19 10:56:40 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP___MUTEX_BASE |
| 11 | #define _LIBCPP___MUTEX_BASE |
| 12 | |
| 13 | #include <__config> |
| 14 | #include <chrono> |
| 15 | #include <system_error> |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 | [diff] [blame] | 16 | #include <__threading_support> |
Eric Fiselier | 018a3d5 | 2017-05-31 22:07:49 | [diff] [blame] | 17 | |
Dan Albert | a4ccec7 | 2019-09-18 18:13:32 | [diff] [blame^] | 18 | #include <time.h> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 19 | |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 | [diff] [blame] | 20 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 21 | #pragma GCC system_header |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 | [diff] [blame] | 22 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 23 | |
Eric Fiselier | 018a3d5 | 2017-05-31 22:07:49 | [diff] [blame] | 24 | _LIBCPP_PUSH_MACROS |
| 25 | #include <__undef_macros> |
| 26 | |
| 27 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 28 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 29 | |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 | [diff] [blame] | 30 | #ifndef _LIBCPP_HAS_NO_THREADS |
| 31 | |
Eric Fiselier | a15e8c3 | 2016-03-16 02:30:06 | [diff] [blame] | 32 | #ifndef _LIBCPP_THREAD_SAFETY_ANNOTATION |
| 33 | # ifdef _LIBCPP_HAS_THREAD_SAFETY_ANNOTATIONS |
| 34 | # define _LIBCPP_THREAD_SAFETY_ANNOTATION(x) __attribute__((x)) |
| 35 | # else |
| 36 | # define _LIBCPP_THREAD_SAFETY_ANNOTATION(x) |
| 37 | # endif |
| 38 | #endif // _LIBCPP_THREAD_SAFETY_ANNOTATION |
| 39 | |
Eric Fiselier | 47e6bb8 | 2019-07-07 01:20:54 | [diff] [blame] | 40 | |
Eric Fiselier | a15e8c3 | 2016-03-16 02:30:06 | [diff] [blame] | 41 | class _LIBCPP_TYPE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(capability("mutex")) mutex |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 42 | { |
Marshall Clow | 65eb1e9 | 2016-07-18 17:23:06 | [diff] [blame] | 43 | __libcpp_mutex_t __m_ = _LIBCPP_MUTEX_INITIALIZER; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 44 | |
| 45 | public: |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 46 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 47e6bb8 | 2019-07-07 01:20:54 | [diff] [blame] | 47 | _LIBCPP_CONSTEXPR mutex() = default; |
| 48 | |
| 49 | mutex(const mutex&) = delete; |
| 50 | mutex& operator=(const mutex&) = delete; |
| 51 | |
| 52 | #if defined(_LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION) |
| 53 | ~mutex() = default; |
Howard Hinnant | 384608e | 2012-07-07 20:01:52 | [diff] [blame] | 54 | #else |
Richard Smith | 8a590a4 | 2019-04-25 20:00:06 | [diff] [blame] | 55 | ~mutex() _NOEXCEPT; |
Eric Fiselier | 47e6bb8 | 2019-07-07 01:20:54 | [diff] [blame] | 56 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 57 | |
Eric Fiselier | a15e8c3 | 2016-03-16 02:30:06 | [diff] [blame] | 58 | void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability()); |
| 59 | bool try_lock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(try_acquire_capability(true)); |
| 60 | void unlock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 61 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 | [diff] [blame] | 62 | typedef __libcpp_mutex_t* native_handle_type; |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 63 | _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 64 | }; |
| 65 | |
Eric Fiselier | 276a69c | 2017-06-07 20:47:42 | [diff] [blame] | 66 | static_assert(is_nothrow_default_constructible<mutex>::value, |
| 67 | "the default constructor for std::mutex must be nothrow"); |
| 68 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 | [diff] [blame] | 69 | struct _LIBCPP_TYPE_VIS defer_lock_t {}; |
| 70 | struct _LIBCPP_TYPE_VIS try_to_lock_t {}; |
| 71 | struct _LIBCPP_TYPE_VIS adopt_lock_t {}; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 72 | |
Louis Dionne | 6952d14 | 2018-08-01 02:08:59 | [diff] [blame] | 73 | #if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 74 | |
Louis Dionne | 07f95bd | 2018-10-25 12:13:43 | [diff] [blame] | 75 | extern _LIBCPP_EXPORTED_FROM_ABI const defer_lock_t defer_lock; |
| 76 | extern _LIBCPP_EXPORTED_FROM_ABI const try_to_lock_t try_to_lock; |
| 77 | extern _LIBCPP_EXPORTED_FROM_ABI const adopt_lock_t adopt_lock; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 78 | |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 79 | #else |
| 80 | |
Marshall Clow | 97adf8a | 2018-01-02 19:23:30 | [diff] [blame] | 81 | /* _LIBCPP_INLINE_VAR */ constexpr defer_lock_t defer_lock = defer_lock_t(); |
| 82 | /* _LIBCPP_INLINE_VAR */ constexpr try_to_lock_t try_to_lock = try_to_lock_t(); |
| 83 | /* _LIBCPP_INLINE_VAR */ constexpr adopt_lock_t adopt_lock = adopt_lock_t(); |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 84 | |
| 85 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 86 | |
| 87 | template <class _Mutex> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 | [diff] [blame] | 88 | class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) |
Eric Fiselier | 10b52a0 | 2016-06-14 03:48:09 | [diff] [blame] | 89 | lock_guard |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 90 | { |
| 91 | public: |
| 92 | typedef _Mutex mutex_type; |
| 93 | |
| 94 | private: |
| 95 | mutex_type& __m_; |
| 96 | public: |
| 97 | |
Louis Dionne | 22eff1a | 2019-08-13 11:12:28 | [diff] [blame] | 98 | _LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | a15e8c3 | 2016-03-16 02:30:06 | [diff] [blame] | 99 | explicit lock_guard(mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m)) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 100 | : __m_(__m) {__m_.lock();} |
Louis Dionne | 22eff1a | 2019-08-13 11:12:28 | [diff] [blame] | 101 | |
| 102 | _LIBCPP_NODISCARD_EXT _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | a15e8c3 | 2016-03-16 02:30:06 | [diff] [blame] | 103 | 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] | 104 | : __m_(__m) {} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 105 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | a15e8c3 | 2016-03-16 02:30:06 | [diff] [blame] | 106 | ~lock_guard() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 107 | |
| 108 | private: |
Eric Fiselier | 10b52a0 | 2016-06-14 03:48:09 | [diff] [blame] | 109 | lock_guard(lock_guard const&) _LIBCPP_EQUAL_DELETE; |
| 110 | lock_guard& operator=(lock_guard const&) _LIBCPP_EQUAL_DELETE; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | template <class _Mutex> |
Eric Fiselier | c3589a8 | 2017-01-04 23:56:00 | [diff] [blame] | 114 | class _LIBCPP_TEMPLATE_VIS unique_lock |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 115 | { |
| 116 | public: |
| 117 | typedef _Mutex mutex_type; |
| 118 | |
| 119 | private: |
| 120 | mutex_type* __m_; |
| 121 | bool __owns_; |
| 122 | |
| 123 | public: |
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() _NOEXCEPT : __m_(nullptr), __owns_(false) {} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 126 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 127 | explicit unique_lock(mutex_type& __m) |
Marshall Clow | 8165249 | 2016-04-13 17:02:23 | [diff] [blame] | 128 | : __m_(_VSTD::addressof(__m)), __owns_(true) {__m_->lock();} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 129 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 130 | unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT |
Marshall Clow | 8165249 | 2016-04-13 17:02:23 | [diff] [blame] | 131 | : __m_(_VSTD::addressof(__m)), __owns_(false) {} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 132 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 133 | unique_lock(mutex_type& __m, try_to_lock_t) |
Marshall Clow | 8165249 | 2016-04-13 17:02:23 | [diff] [blame] | 134 | : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock()) {} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 135 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 136 | unique_lock(mutex_type& __m, adopt_lock_t) |
Marshall Clow | 8165249 | 2016-04-13 17:02:23 | [diff] [blame] | 137 | : __m_(_VSTD::addressof(__m)), __owns_(true) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 138 | template <class _Clock, class _Duration> |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 139 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 140 | unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t) |
Marshall Clow | 8165249 | 2016-04-13 17:02:23 | [diff] [blame] | 141 | : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock_until(__t)) {} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 142 | template <class _Rep, class _Period> |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 143 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 144 | unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d) |
Marshall Clow | 8165249 | 2016-04-13 17:02:23 | [diff] [blame] | 145 | : __m_(_VSTD::addressof(__m)), __owns_(__m.try_lock_for(__d)) {} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 146 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 147 | ~unique_lock() |
| 148 | { |
| 149 | if (__owns_) |
| 150 | __m_->unlock(); |
| 151 | } |
| 152 | |
| 153 | private: |
| 154 | unique_lock(unique_lock const&); // = delete; |
| 155 | unique_lock& operator=(unique_lock const&); // = delete; |
| 156 | |
| 157 | public: |
Eric Fiselier | 690d999 | 2017-04-18 23:05:08 | [diff] [blame] | 158 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 159 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 160 | unique_lock(unique_lock&& __u) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 161 | : __m_(__u.__m_), __owns_(__u.__owns_) |
| 162 | {__u.__m_ = nullptr; __u.__owns_ = false;} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 163 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 164 | unique_lock& operator=(unique_lock&& __u) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 165 | { |
| 166 | if (__owns_) |
| 167 | __m_->unlock(); |
| 168 | __m_ = __u.__m_; |
| 169 | __owns_ = __u.__owns_; |
| 170 | __u.__m_ = nullptr; |
| 171 | __u.__owns_ = false; |
| 172 | return *this; |
| 173 | } |
Howard Hinnant | ac417fa | 2010-11-28 19:41:07 | [diff] [blame] | 174 | |
Eric Fiselier | 690d999 | 2017-04-18 23:05:08 | [diff] [blame] | 175 | #endif // _LIBCPP_CXX03_LANG |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 176 | |
| 177 | void lock(); |
| 178 | bool try_lock(); |
| 179 | |
| 180 | template <class _Rep, class _Period> |
| 181 | bool try_lock_for(const chrono::duration<_Rep, _Period>& __d); |
| 182 | template <class _Clock, class _Duration> |
| 183 | bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t); |
| 184 | |
| 185 | void unlock(); |
| 186 | |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 187 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 188 | void swap(unique_lock& __u) _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 189 | { |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 190 | _VSTD::swap(__m_, __u.__m_); |
| 191 | _VSTD::swap(__owns_, __u.__owns_); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 192 | } |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 193 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 194 | mutex_type* release() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 195 | { |
| 196 | mutex_type* __m = __m_; |
| 197 | __m_ = nullptr; |
| 198 | __owns_ = false; |
| 199 | return __m; |
| 200 | } |
| 201 | |
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 | bool owns_lock() const _NOEXCEPT {return __owns_;} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 204 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 7786188 | 2012-02-21 21:46:43 | [diff] [blame] | 205 | _LIBCPP_EXPLICIT |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 206 | operator bool () const _NOEXCEPT {return __owns_;} |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 207 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 208 | mutex_type* mutex() const _NOEXCEPT {return __m_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | template <class _Mutex> |
| 212 | void |
| 213 | unique_lock<_Mutex>::lock() |
| 214 | { |
| 215 | if (__m_ == nullptr) |
| 216 | __throw_system_error(EPERM, "unique_lock::lock: references null mutex"); |
| 217 | if (__owns_) |
| 218 | __throw_system_error(EDEADLK, "unique_lock::lock: already locked"); |
| 219 | __m_->lock(); |
| 220 | __owns_ = true; |
| 221 | } |
| 222 | |
| 223 | template <class _Mutex> |
| 224 | bool |
| 225 | unique_lock<_Mutex>::try_lock() |
| 226 | { |
| 227 | if (__m_ == nullptr) |
| 228 | __throw_system_error(EPERM, "unique_lock::try_lock: references null mutex"); |
| 229 | if (__owns_) |
| 230 | __throw_system_error(EDEADLK, "unique_lock::try_lock: already locked"); |
| 231 | __owns_ = __m_->try_lock(); |
| 232 | return __owns_; |
| 233 | } |
| 234 | |
| 235 | template <class _Mutex> |
| 236 | template <class _Rep, class _Period> |
| 237 | bool |
| 238 | unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) |
| 239 | { |
| 240 | if (__m_ == nullptr) |
| 241 | __throw_system_error(EPERM, "unique_lock::try_lock_for: references null mutex"); |
| 242 | if (__owns_) |
| 243 | __throw_system_error(EDEADLK, "unique_lock::try_lock_for: already locked"); |
| 244 | __owns_ = __m_->try_lock_for(__d); |
| 245 | return __owns_; |
| 246 | } |
| 247 | |
| 248 | template <class _Mutex> |
| 249 | template <class _Clock, class _Duration> |
| 250 | bool |
| 251 | unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) |
| 252 | { |
| 253 | if (__m_ == nullptr) |
| 254 | __throw_system_error(EPERM, "unique_lock::try_lock_until: references null mutex"); |
| 255 | if (__owns_) |
| 256 | __throw_system_error(EDEADLK, "unique_lock::try_lock_until: already locked"); |
| 257 | __owns_ = __m_->try_lock_until(__t); |
| 258 | return __owns_; |
| 259 | } |
| 260 | |
| 261 | template <class _Mutex> |
| 262 | void |
| 263 | unique_lock<_Mutex>::unlock() |
| 264 | { |
| 265 | if (!__owns_) |
| 266 | __throw_system_error(EPERM, "unique_lock::unlock: not locked"); |
| 267 | __m_->unlock(); |
| 268 | __owns_ = false; |
| 269 | } |
| 270 | |
| 271 | template <class _Mutex> |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 272 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 273 | void |
Howard Hinnant | 499c61f | 2012-07-21 16:13:09 | [diff] [blame] | 274 | swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT |
| 275 | {__x.swap(__y);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 276 | |
Marshall Clow | 239bc42 | 2013-12-23 22:14:27 | [diff] [blame] | 277 | //enum class cv_status |
| 278 | _LIBCPP_DECLARE_STRONG_ENUM(cv_status) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 279 | { |
Marshall Clow | 239bc42 | 2013-12-23 22:14:27 | [diff] [blame] | 280 | no_timeout, |
| 281 | timeout |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 282 | }; |
Marshall Clow | 239bc42 | 2013-12-23 22:14:27 | [diff] [blame] | 283 | _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(cv_status) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 284 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 | [diff] [blame] | 285 | class _LIBCPP_TYPE_VIS condition_variable |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 286 | { |
Marshall Clow | 65eb1e9 | 2016-07-18 17:23:06 | [diff] [blame] | 287 | __libcpp_condvar_t __cv_ = _LIBCPP_CONDVAR_INITIALIZER; |
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 |
Eric Fiselier | a386369 | 2019-07-07 17:24:03 | [diff] [blame] | 290 | _LIBCPP_CONSTEXPR condition_variable() _NOEXCEPT = default; |
| 291 | |
| 292 | #ifdef _LIBCPP_HAS_TRIVIAL_CONDVAR_DESTRUCTION |
| 293 | ~condition_variable() = default; |
Howard Hinnant | 384608e | 2012-07-07 20:01:52 | [diff] [blame] | 294 | #else |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 295 | ~condition_variable(); |
Eric Fiselier | a386369 | 2019-07-07 17:24:03 | [diff] [blame] | 296 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 297 | |
Eric Fiselier | a386369 | 2019-07-07 17:24:03 | [diff] [blame] | 298 | condition_variable(const condition_variable&) = delete; |
| 299 | condition_variable& operator=(const condition_variable&) = delete; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 300 | |
Howard Hinnant | c8f7413 | 2012-07-21 16:32:53 | [diff] [blame] | 301 | void notify_one() _NOEXCEPT; |
| 302 | void notify_all() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 303 | |
Marshall Clow | b076785 | 2014-03-26 02:45:04 | [diff] [blame] | 304 | void wait(unique_lock<mutex>& __lk) _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 305 | template <class _Predicate> |
Shoaib Meenai | 6b73492 | 2017-03-02 03:22:18 | [diff] [blame] | 306 | _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 307 | void wait(unique_lock<mutex>& __lk, _Predicate __pred); |
| 308 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 309 | template <class _Clock, class _Duration> |
Shoaib Meenai | 6b73492 | 2017-03-02 03:22:18 | [diff] [blame] | 310 | _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 311 | cv_status |
| 312 | wait_until(unique_lock<mutex>& __lk, |
| 313 | const chrono::time_point<_Clock, _Duration>& __t); |
| 314 | |
| 315 | template <class _Clock, class _Duration, class _Predicate> |
Shoaib Meenai | 6b73492 | 2017-03-02 03:22:18 | [diff] [blame] | 316 | _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 317 | bool |
| 318 | wait_until(unique_lock<mutex>& __lk, |
| 319 | const chrono::time_point<_Clock, _Duration>& __t, |
| 320 | _Predicate __pred); |
| 321 | |
| 322 | template <class _Rep, class _Period> |
Shoaib Meenai | 6b73492 | 2017-03-02 03:22:18 | [diff] [blame] | 323 | _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 324 | cv_status |
| 325 | wait_for(unique_lock<mutex>& __lk, |
| 326 | const chrono::duration<_Rep, _Period>& __d); |
| 327 | |
| 328 | template <class _Rep, class _Period, class _Predicate> |
| 329 | bool |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 | [diff] [blame] | 330 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 331 | wait_for(unique_lock<mutex>& __lk, |
| 332 | const chrono::duration<_Rep, _Period>& __d, |
| 333 | _Predicate __pred); |
| 334 | |
Asiri Rathnayake | 35ff03b | 2016-05-06 14:06:29 | [diff] [blame] | 335 | typedef __libcpp_condvar_t* native_handle_type; |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 336 | _LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__cv_;} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 337 | |
| 338 | private: |
| 339 | void __do_timed_wait(unique_lock<mutex>& __lk, |
Marshall Clow | b076785 | 2014-03-26 02:45:04 | [diff] [blame] | 340 | chrono::time_point<chrono::system_clock, chrono::nanoseconds>) _NOEXCEPT; |
Dan Albert | a4ccec7 | 2019-09-18 18:13:32 | [diff] [blame^] | 341 | #if defined(_LIBCPP_HAS_COND_CLOCKWAIT) |
| 342 | void __do_timed_wait(unique_lock<mutex>& __lk, |
| 343 | chrono::time_point<chrono::steady_clock, chrono::nanoseconds>) _NOEXCEPT; |
| 344 | #endif |
| 345 | template <class _Clock> |
| 346 | void __do_timed_wait(unique_lock<mutex>& __lk, |
| 347 | chrono::time_point<_Clock, chrono::nanoseconds>) _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 348 | }; |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 | [diff] [blame] | 349 | #endif // !_LIBCPP_HAS_NO_THREADS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 350 | |
Dan Albert | a4ccec7 | 2019-09-18 18:13:32 | [diff] [blame^] | 351 | template <class _Rep, class _Period> |
Howard Hinnant | 333f50d | 2010-09-21 20:16:37 | [diff] [blame] | 352 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 353 | typename enable_if |
| 354 | < |
Dan Albert | a4ccec7 | 2019-09-18 18:13:32 | [diff] [blame^] | 355 | is_floating_point<_Rep>::value, |
| 356 | chrono::nanoseconds |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 357 | >::type |
Dan Albert | a4ccec7 | 2019-09-18 18:13:32 | [diff] [blame^] | 358 | __safe_nanosecond_cast(chrono::duration<_Rep, _Period> __d) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 359 | { |
| 360 | using namespace chrono; |
Dan Albert | a4ccec7 | 2019-09-18 18:13:32 | [diff] [blame^] | 361 | using __ratio = ratio_divide<_Period, nano>; |
| 362 | using __ns_rep = nanoseconds::rep; |
| 363 | _Rep __result_float = __d.count() * __ratio::num / __ratio::den; |
| 364 | |
| 365 | _Rep __result_max = numeric_limits<__ns_rep>::max(); |
| 366 | if (__result_float >= __result_max) { |
| 367 | return nanoseconds::max(); |
| 368 | } |
| 369 | |
| 370 | _Rep __result_min = numeric_limits<__ns_rep>::min(); |
| 371 | if (__result_float <= __result_min) { |
| 372 | return nanoseconds::min(); |
| 373 | } |
| 374 | |
| 375 | return nanoseconds(static_cast<__ns_rep>(__result_float)); |
| 376 | } |
| 377 | |
| 378 | template <class _Rep, class _Period> |
| 379 | inline _LIBCPP_INLINE_VISIBILITY |
| 380 | typename enable_if |
| 381 | < |
| 382 | !is_floating_point<_Rep>::value, |
| 383 | chrono::nanoseconds |
| 384 | >::type |
| 385 | __safe_nanosecond_cast(chrono::duration<_Rep, _Period> __d) |
| 386 | { |
| 387 | using namespace chrono; |
| 388 | if (__d.count() == 0) { |
| 389 | return nanoseconds(0); |
| 390 | } |
| 391 | |
| 392 | using __ratio = ratio_divide<_Period, nano>; |
| 393 | using __ns_rep = nanoseconds::rep; |
| 394 | __ns_rep __result_max = std::numeric_limits<__ns_rep>::max(); |
| 395 | if (__d.count() > 0 && __d.count() > __result_max / __ratio::num) { |
| 396 | return nanoseconds::max(); |
| 397 | } |
| 398 | |
| 399 | __ns_rep __result_min = std::numeric_limits<__ns_rep>::min(); |
| 400 | if (__d.count() < 0 && __d.count() < __result_min / __ratio::num) { |
| 401 | return nanoseconds::min(); |
| 402 | } |
| 403 | |
| 404 | __ns_rep __result = __d.count() * __ratio::num / __ratio::den; |
| 405 | if (__result == 0) { |
| 406 | return nanoseconds(1); |
| 407 | } |
| 408 | |
| 409 | return nanoseconds(__result); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 410 | } |
| 411 | |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 | [diff] [blame] | 412 | #ifndef _LIBCPP_HAS_NO_THREADS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 413 | template <class _Predicate> |
| 414 | void |
| 415 | condition_variable::wait(unique_lock<mutex>& __lk, _Predicate __pred) |
| 416 | { |
| 417 | while (!__pred()) |
| 418 | wait(__lk); |
| 419 | } |
| 420 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 421 | template <class _Clock, class _Duration> |
| 422 | cv_status |
| 423 | condition_variable::wait_until(unique_lock<mutex>& __lk, |
| 424 | const chrono::time_point<_Clock, _Duration>& __t) |
| 425 | { |
| 426 | using namespace chrono; |
Dan Albert | a4ccec7 | 2019-09-18 18:13:32 | [diff] [blame^] | 427 | using __clock_tp_ns = time_point<_Clock, nanoseconds>; |
| 428 | |
| 429 | typename _Clock::time_point __now = _Clock::now(); |
| 430 | if (__t <= __now) |
| 431 | return cv_status::timeout; |
| 432 | |
| 433 | __clock_tp_ns __t_ns = __clock_tp_ns(__safe_nanosecond_cast(__t.time_since_epoch())); |
| 434 | |
| 435 | __do_timed_wait(__lk, __t_ns); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 436 | return _Clock::now() < __t ? cv_status::no_timeout : cv_status::timeout; |
| 437 | } |
| 438 | |
| 439 | template <class _Clock, class _Duration, class _Predicate> |
| 440 | bool |
| 441 | condition_variable::wait_until(unique_lock<mutex>& __lk, |
| 442 | const chrono::time_point<_Clock, _Duration>& __t, |
| 443 | _Predicate __pred) |
| 444 | { |
| 445 | while (!__pred()) |
| 446 | { |
| 447 | if (wait_until(__lk, __t) == cv_status::timeout) |
| 448 | return __pred(); |
| 449 | } |
| 450 | return true; |
| 451 | } |
| 452 | |
| 453 | template <class _Rep, class _Period> |
| 454 | cv_status |
| 455 | condition_variable::wait_for(unique_lock<mutex>& __lk, |
| 456 | const chrono::duration<_Rep, _Period>& __d) |
| 457 | { |
| 458 | using namespace chrono; |
Howard Hinnant | cf115d2 | 2012-08-30 19:14:33 | [diff] [blame] | 459 | if (__d <= __d.zero()) |
| 460 | return cv_status::timeout; |
Dan Albert | a4ccec7 | 2019-09-18 18:13:32 | [diff] [blame^] | 461 | using __ns_rep = nanoseconds::rep; |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 | [diff] [blame] | 462 | steady_clock::time_point __c_now = steady_clock::now(); |
Dan Albert | a4ccec7 | 2019-09-18 18:13:32 | [diff] [blame^] | 463 | |
| 464 | #if defined(_LIBCPP_HAS_COND_CLOCKWAIT) |
| 465 | using __clock_tp_ns = time_point<steady_clock, nanoseconds>; |
| 466 | __ns_rep __now_count_ns = __safe_nanosecond_cast(__c_now.time_since_epoch()).count(); |
| 467 | #else |
| 468 | using __clock_tp_ns = time_point<system_clock, nanoseconds>; |
| 469 | __ns_rep __now_count_ns = __safe_nanosecond_cast(system_clock::now().time_since_epoch()).count(); |
| 470 | #endif |
| 471 | |
| 472 | __ns_rep __d_ns_count = __safe_nanosecond_cast(__d).count(); |
| 473 | |
| 474 | if (__now_count_ns > numeric_limits<__ns_rep>::max() - __d_ns_count) { |
| 475 | __do_timed_wait(__lk, __clock_tp_ns::max()); |
| 476 | } else { |
| 477 | __do_timed_wait(__lk, __clock_tp_ns(nanoseconds(__now_count_ns + __d_ns_count))); |
| 478 | } |
| 479 | |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 | [diff] [blame] | 480 | return steady_clock::now() - __c_now < __d ? cv_status::no_timeout : |
| 481 | cv_status::timeout; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | template <class _Rep, class _Period, class _Predicate> |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 | [diff] [blame] | 485 | inline |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 486 | bool |
| 487 | condition_variable::wait_for(unique_lock<mutex>& __lk, |
| 488 | const chrono::duration<_Rep, _Period>& __d, |
| 489 | _Predicate __pred) |
| 490 | { |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 | [diff] [blame] | 491 | return wait_until(__lk, chrono::steady_clock::now() + __d, |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 | [diff] [blame] | 492 | _VSTD::move(__pred)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 493 | } |
| 494 | |
Dan Albert | a4ccec7 | 2019-09-18 18:13:32 | [diff] [blame^] | 495 | #if defined(_LIBCPP_HAS_COND_CLOCKWAIT) |
| 496 | inline |
| 497 | void |
| 498 | condition_variable::__do_timed_wait(unique_lock<mutex>& __lk, |
| 499 | chrono::time_point<chrono::steady_clock, chrono::nanoseconds> __tp) _NOEXCEPT |
| 500 | { |
| 501 | using namespace chrono; |
| 502 | if (!__lk.owns_lock()) |
| 503 | __throw_system_error(EPERM, |
| 504 | "condition_variable::timed wait: mutex not locked"); |
| 505 | nanoseconds __d = __tp.time_since_epoch(); |
| 506 | timespec __ts; |
| 507 | seconds __s = duration_cast<seconds>(__d); |
| 508 | using __ts_sec = decltype(__ts.tv_sec); |
| 509 | const __ts_sec __ts_sec_max = numeric_limits<__ts_sec>::max(); |
| 510 | if (__s.count() < __ts_sec_max) |
| 511 | { |
| 512 | __ts.tv_sec = static_cast<__ts_sec>(__s.count()); |
| 513 | __ts.tv_nsec = (__d - __s).count(); |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | __ts.tv_sec = __ts_sec_max; |
| 518 | __ts.tv_nsec = giga::num - 1; |
| 519 | } |
| 520 | int __ec = pthread_cond_clockwait(&__cv_, __lk.mutex()->native_handle(), CLOCK_MONOTONIC, &__ts); |
| 521 | if (__ec != 0 && __ec != ETIMEDOUT) |
| 522 | __throw_system_error(__ec, "condition_variable timed_wait failed"); |
| 523 | } |
| 524 | #endif // _LIBCPP_HAS_COND_CLOCKWAIT |
| 525 | |
| 526 | template <class _Clock> |
| 527 | inline |
| 528 | void |
| 529 | condition_variable::__do_timed_wait(unique_lock<mutex>& __lk, |
| 530 | chrono::time_point<_Clock, chrono::nanoseconds> __tp) _NOEXCEPT |
| 531 | { |
| 532 | wait_for(__lk, __tp - _Clock::now()); |
| 533 | } |
| 534 | |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 | [diff] [blame] | 535 | #endif // !_LIBCPP_HAS_NO_THREADS |
| 536 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 537 | _LIBCPP_END_NAMESPACE_STD |
| 538 | |
Eric Fiselier | 018a3d5 | 2017-05-31 22:07:49 | [diff] [blame] | 539 | _LIBCPP_POP_MACROS |
| 540 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 | [diff] [blame] | 541 | #endif // _LIBCPP___MUTEX_BASE |