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