2 //===---------------------- condition_variable ----------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_CONDITION_VARIABLE
12 #define _LIBCPP_CONDITION_VARIABLE
15 condition_variable synopsis
20 enum class cv_status { no_timeout, timeout };
22 class condition_variable
26 ~condition_variable();
28 condition_variable(const condition_variable&) = delete;
29 condition_variable& operator=(const condition_variable&) = delete;
31 void notify_one() noexcept;
32 void notify_all() noexcept;
34 void wait(unique_lock<mutex>& lock);
35 template <class Predicate>
36 void wait(unique_lock<mutex>& lock, Predicate pred);
38 template <class Clock, class Duration>
40 wait_until(unique_lock<mutex>& lock,
41 const chrono::time_point<Clock, Duration>& abs_time);
43 template <class Clock, class Duration, class Predicate>
45 wait_until(unique_lock<mutex>& lock,
46 const chrono::time_point<Clock, Duration>& abs_time,
49 template <class Rep, class Period>
51 wait_for(unique_lock<mutex>& lock,
52 const chrono::duration<Rep, Period>& rel_time);
54 template <class Rep, class Period, class Predicate>
56 wait_for(unique_lock<mutex>& lock,
57 const chrono::duration<Rep, Period>& rel_time,
60 typedef pthread_cond_t* native_handle_type;
61 native_handle_type native_handle();
64 void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
66 class condition_variable_any
69 condition_variable_any();
70 ~condition_variable_any();
72 condition_variable_any(const condition_variable_any&) = delete;
73 condition_variable_any& operator=(const condition_variable_any&) = delete;
75 void notify_one() noexcept;
76 void notify_all() noexcept;
79 void wait(Lock& lock);
80 template <class Lock, class Predicate>
81 void wait(Lock& lock, Predicate pred);
83 template <class Lock, class Clock, class Duration>
85 wait_until(Lock& lock,
86 const chrono::time_point<Clock, Duration>& abs_time);
88 template <class Lock, class Clock, class Duration, class Predicate>
90 wait_until(Lock& lock,
91 const chrono::time_point<Clock, Duration>& abs_time,
94 template <class Lock, class Rep, class Period>
97 const chrono::duration<Rep, Period>& rel_time);
99 template <class Lock, class Rep, class Period, class Predicate>
102 const chrono::duration<Rep, Period>& rel_time,
111 #include <__mutex_base>
114 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
115 #pragma GCC system_header
118 #ifndef _LIBCPP_HAS_NO_THREADS
120 _LIBCPP_BEGIN_NAMESPACE_STD
122 class _LIBCPP_TYPE_VIS condition_variable_any
124 condition_variable __cv_;
125 shared_ptr<mutex> __mut_;
127 condition_variable_any();
129 void notify_one() _NOEXCEPT;
130 void notify_all() _NOEXCEPT;
132 template <class _Lock>
133 void wait(_Lock& __lock);
134 template <class _Lock, class _Predicate>
135 void wait(_Lock& __lock, _Predicate __pred);
137 template <class _Lock, class _Clock, class _Duration>
139 wait_until(_Lock& __lock,
140 const chrono::time_point<_Clock, _Duration>& __t);
142 template <class _Lock, class _Clock, class _Duration, class _Predicate>
144 wait_until(_Lock& __lock,
145 const chrono::time_point<_Clock, _Duration>& __t,
148 template <class _Lock, class _Rep, class _Period>
150 wait_for(_Lock& __lock,
151 const chrono::duration<_Rep, _Period>& __d);
153 template <class _Lock, class _Rep, class _Period, class _Predicate>
155 wait_for(_Lock& __lock,
156 const chrono::duration<_Rep, _Period>& __d,
160 inline _LIBCPP_INLINE_VISIBILITY
161 condition_variable_any::condition_variable_any()
162 : __mut_(make_shared<mutex>()) {}
164 inline _LIBCPP_INLINE_VISIBILITY
166 condition_variable_any::notify_one() _NOEXCEPT
168 {lock_guard<mutex> __lx(*__mut_);}
172 inline _LIBCPP_INLINE_VISIBILITY
174 condition_variable_any::notify_all() _NOEXCEPT
176 {lock_guard<mutex> __lx(*__mut_);}
180 struct __lock_external
182 template <class _Lock>
183 void operator()(_Lock* __m) {__m->lock();}
186 template <class _Lock>
188 condition_variable_any::wait(_Lock& __lock)
190 shared_ptr<mutex> __mut = __mut_;
191 unique_lock<mutex> __lk(*__mut);
193 unique_ptr<_Lock, __lock_external> __lxx(&__lock);
194 lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
196 } // __mut_.unlock(), __lock.lock()
198 template <class _Lock, class _Predicate>
199 inline _LIBCPP_INLINE_VISIBILITY
201 condition_variable_any::wait(_Lock& __lock, _Predicate __pred)
207 template <class _Lock, class _Clock, class _Duration>
209 condition_variable_any::wait_until(_Lock& __lock,
210 const chrono::time_point<_Clock, _Duration>& __t)
212 shared_ptr<mutex> __mut = __mut_;
213 unique_lock<mutex> __lk(*__mut);
215 unique_ptr<_Lock, __lock_external> __lxx(&__lock);
216 lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
217 return __cv_.wait_until(__lk, __t);
218 } // __mut_.unlock(), __lock.lock()
220 template <class _Lock, class _Clock, class _Duration, class _Predicate>
221 inline _LIBCPP_INLINE_VISIBILITY
223 condition_variable_any::wait_until(_Lock& __lock,
224 const chrono::time_point<_Clock, _Duration>& __t,
228 if (wait_until(__lock, __t) == cv_status::timeout)
233 template <class _Lock, class _Rep, class _Period>
234 inline _LIBCPP_INLINE_VISIBILITY
236 condition_variable_any::wait_for(_Lock& __lock,
237 const chrono::duration<_Rep, _Period>& __d)
239 return wait_until(__lock, chrono::steady_clock::now() + __d);
242 template <class _Lock, class _Rep, class _Period, class _Predicate>
243 inline _LIBCPP_INLINE_VISIBILITY
245 condition_variable_any::wait_for(_Lock& __lock,
246 const chrono::duration<_Rep, _Period>& __d,
249 return wait_until(__lock, chrono::steady_clock::now() + __d,
250 _VSTD::move(__pred));
254 void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
256 _LIBCPP_END_NAMESPACE_STD
258 #endif // !_LIBCPP_HAS_NO_THREADS
260 #endif // _LIBCPP_CONDITION_VARIABLE