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 | |
| 31 | void notify_one(); |
| 32 | void notify_all(); |
| 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 | |
| 75 | void notify_one(); |
| 76 | void notify_all(); |
| 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 | |
| 114 | #pragma GCC system_header |
| 115 | |
| 116 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 117 | |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 118 | class _LIBCPP_VISIBLE condition_variable_any |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 119 | { |
| 120 | condition_variable __cv_; |
| 121 | shared_ptr<mutex> __mut_; |
| 122 | public: |
| 123 | condition_variable_any(); |
| 124 | |
| 125 | void notify_one(); |
| 126 | void notify_all(); |
| 127 | |
| 128 | template <class _Lock> |
| 129 | void wait(_Lock& __lock); |
| 130 | template <class _Lock, class _Predicate> |
| 131 | void wait(_Lock& __lock, _Predicate __pred); |
| 132 | |
| 133 | template <class _Lock, class _Clock, class _Duration> |
| 134 | cv_status |
| 135 | wait_until(_Lock& __lock, |
| 136 | const chrono::time_point<_Clock, _Duration>& __t); |
| 137 | |
| 138 | template <class _Lock, class _Clock, class _Duration, class _Predicate> |
| 139 | bool |
| 140 | wait_until(_Lock& __lock, |
| 141 | const chrono::time_point<_Clock, _Duration>& __t, |
| 142 | _Predicate __pred); |
| 143 | |
| 144 | template <class _Lock, class _Rep, class _Period> |
| 145 | cv_status |
| 146 | wait_for(_Lock& __lock, |
| 147 | const chrono::duration<_Rep, _Period>& __d); |
| 148 | |
| 149 | template <class _Lock, class _Rep, class _Period, class _Predicate> |
| 150 | bool |
| 151 | wait_for(_Lock& __lock, |
| 152 | const chrono::duration<_Rep, _Period>& __d, |
| 153 | _Predicate __pred); |
| 154 | }; |
| 155 | |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 156 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 157 | condition_variable_any::condition_variable_any() |
| 158 | : __mut_(make_shared<mutex>()) {} |
| 159 | |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 160 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 161 | void |
| 162 | condition_variable_any::notify_one() |
| 163 | { |
| 164 | {lock_guard<mutex> _(*__mut_);} |
| 165 | __cv_.notify_one(); |
| 166 | } |
| 167 | |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 168 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 169 | void |
| 170 | condition_variable_any::notify_all() |
| 171 | { |
| 172 | {lock_guard<mutex> _(*__mut_);} |
| 173 | __cv_.notify_all(); |
| 174 | } |
| 175 | |
| 176 | struct __lock_external |
| 177 | { |
| 178 | template <class _Lock> |
| 179 | void operator()(_Lock* __m) {__m->lock();} |
| 180 | }; |
| 181 | |
| 182 | template <class _Lock> |
| 183 | void |
| 184 | condition_variable_any::wait(_Lock& __lock) |
| 185 | { |
| 186 | shared_ptr<mutex> __mut = __mut_; |
| 187 | unique_lock<mutex> __lk(*__mut); |
| 188 | __lock.unlock(); |
| 189 | unique_ptr<_Lock, __lock_external> __(&__lock); |
| 190 | lock_guard<unique_lock<mutex> > _(__lk, adopt_lock); |
| 191 | __cv_.wait(__lk); |
| 192 | } // __mut_.unlock(), __lock.lock() |
| 193 | |
| 194 | template <class _Lock, class _Predicate> |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 195 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 196 | void |
| 197 | condition_variable_any::wait(_Lock& __lock, _Predicate __pred) |
| 198 | { |
| 199 | while (!__pred()) |
| 200 | wait(__lock); |
| 201 | } |
| 202 | |
| 203 | template <class _Lock, class _Clock, class _Duration> |
| 204 | cv_status |
| 205 | condition_variable_any::wait_until(_Lock& __lock, |
| 206 | const chrono::time_point<_Clock, _Duration>& __t) |
| 207 | { |
| 208 | shared_ptr<mutex> __mut = __mut_; |
| 209 | unique_lock<mutex> __lk(*__mut); |
| 210 | __lock.unlock(); |
| 211 | unique_ptr<_Lock, __lock_external> __(&__lock); |
| 212 | lock_guard<unique_lock<mutex> > _(__lk, adopt_lock); |
| 213 | return __cv_.wait_until(__lk, __t); |
| 214 | } // __mut_.unlock(), __lock.lock() |
| 215 | |
| 216 | template <class _Lock, class _Clock, class _Duration, class _Predicate> |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 217 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 218 | bool |
| 219 | condition_variable_any::wait_until(_Lock& __lock, |
| 220 | const chrono::time_point<_Clock, _Duration>& __t, |
| 221 | _Predicate __pred) |
| 222 | { |
| 223 | while (!__pred()) |
| 224 | if (wait_until(__lock, __t) == cv_status::timeout) |
| 225 | return __pred(); |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | template <class _Lock, class _Rep, class _Period> |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 230 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 231 | cv_status |
| 232 | condition_variable_any::wait_for(_Lock& __lock, |
| 233 | const chrono::duration<_Rep, _Period>& __d) |
| 234 | { |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 235 | return wait_until(__lock, chrono::steady_clock::now() + __d); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | template <class _Lock, class _Rep, class _Period, class _Predicate> |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 239 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 240 | bool |
| 241 | condition_variable_any::wait_for(_Lock& __lock, |
| 242 | const chrono::duration<_Rep, _Period>& __d, |
| 243 | _Predicate __pred) |
| 244 | { |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 245 | return wait_until(__lock, chrono::steady_clock::now() + __d, |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 246 | _STD::move(__pred)); |
| 247 | } |
| 248 | |
Howard Hinnant | 422a53f | 2010-09-21 21:28:23 +0000 | [diff] [blame] | 249 | _LIBCPP_VISIBLE |
Howard Hinnant | e6e4d01 | 2010-09-03 21:46:37 +0000 | [diff] [blame] | 250 | void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk); |
| 251 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 252 | _LIBCPP_END_NAMESPACE_STD |
| 253 | |
| 254 | #endif // _LIBCPP_CONDITION_VARIABLE |