Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===---------------------- condition_variable ----------------------------===// |
| 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_CONDITION_VARIABLE |
| 12 | #define _LIBCPP_CONDITION_VARIABLE |
| 13 | |
| 14 | /* |
| 15 | condition_variable synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
| 19 | |
| 20 | enum class cv_status { no_timeout, timeout }; |
| 21 | |
| 22 | class condition_variable |
| 23 | { |
| 24 | public: |
| 25 | condition_variable(); |
| 26 | ~condition_variable(); |
| 27 | |
| 28 | condition_variable(const condition_variable&) = delete; |
| 29 | condition_variable& operator=(const condition_variable&) = delete; |
| 30 | |
Howard Hinnant | c8f7413 | 2012-07-21 16:32:53 +0000 | [diff] [blame] | 31 | void notify_one() noexcept; |
| 32 | void notify_all() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | |
| 34 | void wait(unique_lock<mutex>& lock); |
| 35 | template <class Predicate> |
| 36 | void wait(unique_lock<mutex>& lock, Predicate pred); |
| 37 | |
| 38 | template <class Clock, class Duration> |
| 39 | cv_status |
| 40 | wait_until(unique_lock<mutex>& lock, |
| 41 | const chrono::time_point<Clock, Duration>& abs_time); |
| 42 | |
| 43 | template <class Clock, class Duration, class Predicate> |
| 44 | bool |
| 45 | wait_until(unique_lock<mutex>& lock, |
| 46 | const chrono::time_point<Clock, Duration>& abs_time, |
| 47 | Predicate pred); |
| 48 | |
| 49 | template <class Rep, class Period> |
| 50 | cv_status |
| 51 | wait_for(unique_lock<mutex>& lock, |
| 52 | const chrono::duration<Rep, Period>& rel_time); |
| 53 | |
| 54 | template <class Rep, class Period, class Predicate> |
| 55 | bool |
| 56 | wait_for(unique_lock<mutex>& lock, |
| 57 | const chrono::duration<Rep, Period>& rel_time, |
| 58 | Predicate pred); |
| 59 | |
| 60 | typedef pthread_cond_t* native_handle_type; |
| 61 | native_handle_type native_handle(); |
| 62 | }; |
| 63 | |
Howard Hinnant | 47499b1 | 2010-08-27 20:10:19 +0000 | [diff] [blame] | 64 | void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk); |
| 65 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 66 | class condition_variable_any |
| 67 | { |
| 68 | public: |
| 69 | condition_variable_any(); |
| 70 | ~condition_variable_any(); |
| 71 | |
| 72 | condition_variable_any(const condition_variable_any&) = delete; |
| 73 | condition_variable_any& operator=(const condition_variable_any&) = delete; |
| 74 | |
Howard Hinnant | c8f7413 | 2012-07-21 16:32:53 +0000 | [diff] [blame] | 75 | void notify_one() noexcept; |
| 76 | void notify_all() noexcept; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 77 | |
| 78 | template <class Lock> |
| 79 | void wait(Lock& lock); |
| 80 | template <class Lock, class Predicate> |
| 81 | void wait(Lock& lock, Predicate pred); |
| 82 | |
| 83 | template <class Lock, class Clock, class Duration> |
| 84 | cv_status |
| 85 | wait_until(Lock& lock, |
| 86 | const chrono::time_point<Clock, Duration>& abs_time); |
| 87 | |
| 88 | template <class Lock, class Clock, class Duration, class Predicate> |
| 89 | bool |
| 90 | wait_until(Lock& lock, |
| 91 | const chrono::time_point<Clock, Duration>& abs_time, |
| 92 | Predicate pred); |
| 93 | |
| 94 | template <class Lock, class Rep, class Period> |
| 95 | cv_status |
| 96 | wait_for(Lock& lock, |
| 97 | const chrono::duration<Rep, Period>& rel_time); |
| 98 | |
| 99 | template <class Lock, class Rep, class Period, class Predicate> |
| 100 | bool |
| 101 | wait_for(Lock& lock, |
| 102 | const chrono::duration<Rep, Period>& rel_time, |
| 103 | Predicate pred); |
| 104 | }; |
| 105 | |
| 106 | } // std |
| 107 | |
| 108 | */ |
| 109 | |
| 110 | #include <__config> |
| 111 | #include <__mutex_base> |
| 112 | #include <memory> |
| 113 | |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 114 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 115 | #pragma GCC system_header |
Howard Hinnant | 08e1747 | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 116 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 117 | |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 118 | #ifndef _LIBCPP_HAS_NO_THREADS |
| 119 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 120 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 121 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 122 | class _LIBCPP_TYPE_VIS condition_variable_any |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 123 | { |
| 124 | condition_variable __cv_; |
| 125 | shared_ptr<mutex> __mut_; |
| 126 | public: |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 127 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 128 | condition_variable_any(); |
| 129 | |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 130 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c8f7413 | 2012-07-21 16:32:53 +0000 | [diff] [blame] | 131 | void notify_one() _NOEXCEPT; |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 132 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c8f7413 | 2012-07-21 16:32:53 +0000 | [diff] [blame] | 133 | void notify_all() _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 134 | |
| 135 | template <class _Lock> |
| 136 | void wait(_Lock& __lock); |
| 137 | template <class _Lock, class _Predicate> |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 138 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 139 | void wait(_Lock& __lock, _Predicate __pred); |
| 140 | |
| 141 | template <class _Lock, class _Clock, class _Duration> |
| 142 | cv_status |
| 143 | wait_until(_Lock& __lock, |
| 144 | const chrono::time_point<_Clock, _Duration>& __t); |
| 145 | |
| 146 | template <class _Lock, class _Clock, class _Duration, class _Predicate> |
| 147 | bool |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 148 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 149 | wait_until(_Lock& __lock, |
| 150 | const chrono::time_point<_Clock, _Duration>& __t, |
| 151 | _Predicate __pred); |
| 152 | |
| 153 | template <class _Lock, class _Rep, class _Period> |
| 154 | cv_status |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 155 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 156 | wait_for(_Lock& __lock, |
| 157 | const chrono::duration<_Rep, _Period>& __d); |
| 158 | |
| 159 | template <class _Lock, class _Rep, class _Period, class _Predicate> |
| 160 | bool |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 161 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 162 | wait_for(_Lock& __lock, |
| 163 | const chrono::duration<_Rep, _Period>& __d, |
| 164 | _Predicate __pred); |
| 165 | }; |
| 166 | |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 167 | inline |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 168 | condition_variable_any::condition_variable_any() |
| 169 | : __mut_(make_shared<mutex>()) {} |
| 170 | |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 171 | inline |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 172 | void |
Howard Hinnant | c8f7413 | 2012-07-21 16:32:53 +0000 | [diff] [blame] | 173 | condition_variable_any::notify_one() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 174 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 175 | {lock_guard<mutex> __lx(*__mut_);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 176 | __cv_.notify_one(); |
| 177 | } |
| 178 | |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 179 | inline |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 180 | void |
Howard Hinnant | c8f7413 | 2012-07-21 16:32:53 +0000 | [diff] [blame] | 181 | condition_variable_any::notify_all() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 182 | { |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 183 | {lock_guard<mutex> __lx(*__mut_);} |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 184 | __cv_.notify_all(); |
| 185 | } |
| 186 | |
| 187 | struct __lock_external |
| 188 | { |
| 189 | template <class _Lock> |
| 190 | void operator()(_Lock* __m) {__m->lock();} |
| 191 | }; |
| 192 | |
| 193 | template <class _Lock> |
| 194 | void |
| 195 | condition_variable_any::wait(_Lock& __lock) |
| 196 | { |
| 197 | shared_ptr<mutex> __mut = __mut_; |
| 198 | unique_lock<mutex> __lk(*__mut); |
| 199 | __lock.unlock(); |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 200 | unique_ptr<_Lock, __lock_external> __lxx(&__lock); |
| 201 | lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 202 | __cv_.wait(__lk); |
| 203 | } // __mut_.unlock(), __lock.lock() |
| 204 | |
| 205 | template <class _Lock, class _Predicate> |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 206 | inline |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 207 | void |
| 208 | condition_variable_any::wait(_Lock& __lock, _Predicate __pred) |
| 209 | { |
| 210 | while (!__pred()) |
| 211 | wait(__lock); |
| 212 | } |
| 213 | |
| 214 | template <class _Lock, class _Clock, class _Duration> |
| 215 | cv_status |
| 216 | condition_variable_any::wait_until(_Lock& __lock, |
| 217 | const chrono::time_point<_Clock, _Duration>& __t) |
| 218 | { |
| 219 | shared_ptr<mutex> __mut = __mut_; |
| 220 | unique_lock<mutex> __lk(*__mut); |
| 221 | __lock.unlock(); |
Howard Hinnant | 9c0df14 | 2012-10-30 19:06:59 +0000 | [diff] [blame] | 222 | unique_ptr<_Lock, __lock_external> __lxx(&__lock); |
| 223 | lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 224 | return __cv_.wait_until(__lk, __t); |
| 225 | } // __mut_.unlock(), __lock.lock() |
| 226 | |
| 227 | template <class _Lock, class _Clock, class _Duration, class _Predicate> |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 228 | inline |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 229 | bool |
| 230 | condition_variable_any::wait_until(_Lock& __lock, |
| 231 | const chrono::time_point<_Clock, _Duration>& __t, |
| 232 | _Predicate __pred) |
| 233 | { |
| 234 | while (!__pred()) |
| 235 | if (wait_until(__lock, __t) == cv_status::timeout) |
| 236 | return __pred(); |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | template <class _Lock, class _Rep, class _Period> |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 241 | inline |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 242 | cv_status |
| 243 | condition_variable_any::wait_for(_Lock& __lock, |
| 244 | const chrono::duration<_Rep, _Period>& __d) |
| 245 | { |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 246 | return wait_until(__lock, chrono::steady_clock::now() + __d); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | template <class _Lock, class _Rep, class _Period, class _Predicate> |
Evgeniy Stepanov | a3b25f8 | 2015-11-07 01:22:13 +0000 | [diff] [blame] | 250 | inline |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 251 | bool |
| 252 | condition_variable_any::wait_for(_Lock& __lock, |
| 253 | const chrono::duration<_Rep, _Period>& __d, |
| 254 | _Predicate __pred) |
| 255 | { |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 256 | return wait_until(__lock, chrono::steady_clock::now() + __d, |
Howard Hinnant | 0949eed | 2011-06-30 21:18:19 +0000 | [diff] [blame] | 257 | _VSTD::move(__pred)); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 260 | _LIBCPP_FUNC_VIS |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 261 | void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk); |
| 262 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 263 | _LIBCPP_END_NAMESPACE_STD |
| 264 | |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 265 | #endif // !_LIBCPP_HAS_NO_THREADS |
| 266 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 267 | #endif // _LIBCPP_CONDITION_VARIABLE |