2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_CONDITION_VARIABLE
11 #define _LIBCPP_CONDITION_VARIABLE
14 condition_variable synopsis
19 enum class cv_status { no_timeout, timeout };
21 class condition_variable
25 ~condition_variable();
27 condition_variable(const condition_variable&) = delete;
28 condition_variable& operator=(const condition_variable&) = delete;
30 void notify_one() noexcept;
31 void notify_all() noexcept;
33 void wait(unique_lock<mutex>& lock);
34 template <class Predicate>
35 void wait(unique_lock<mutex>& lock, Predicate pred);
37 template <class Clock, class Duration>
39 wait_until(unique_lock<mutex>& lock,
40 const chrono::time_point<Clock, Duration>& abs_time);
42 template <class Clock, class Duration, class Predicate>
44 wait_until(unique_lock<mutex>& lock,
45 const chrono::time_point<Clock, Duration>& abs_time,
48 template <class Rep, class Period>
50 wait_for(unique_lock<mutex>& lock,
51 const chrono::duration<Rep, Period>& rel_time);
53 template <class Rep, class Period, class Predicate>
55 wait_for(unique_lock<mutex>& lock,
56 const chrono::duration<Rep, Period>& rel_time,
59 typedef pthread_cond_t* native_handle_type;
60 native_handle_type native_handle();
63 void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
65 class condition_variable_any
68 condition_variable_any();
69 ~condition_variable_any();
71 condition_variable_any(const condition_variable_any&) = delete;
72 condition_variable_any& operator=(const condition_variable_any&) = delete;
74 void notify_one() noexcept;
75 void notify_all() noexcept;
78 void wait(Lock& lock);
79 template <class Lock, class Predicate>
80 void wait(Lock& lock, Predicate pred);
82 template <class Lock, class Clock, class Duration>
84 wait_until(Lock& lock,
85 const chrono::time_point<Clock, Duration>& abs_time);
87 template <class Lock, class Clock, class Duration, class Predicate>
89 wait_until(Lock& lock,
90 const chrono::time_point<Clock, Duration>& abs_time,
93 template <class Lock, class Rep, class Period>
96 const chrono::duration<Rep, Period>& rel_time);
98 template <class Lock, class Rep, class Period, class Predicate>
101 const chrono::duration<Rep, Period>& rel_time,
104 // [thread.condvarany.intwait], interruptible waits
105 template <class Lock, class Predicate>
106 bool wait(Lock& lock, stop_token stoken, Predicate pred); // since C++20
108 template <class Lock, class Clock, class Duration, class Predicate>
109 bool wait_until(Lock& lock, stop_token stoken,
110 const chrono::time_point<Clock, Duration>& abs_time, Predicate pred); // since C++20
112 template <class Lock, class Rep, class Period, class Predicate>
113 bool wait_for(Lock& lock, stop_token stoken,
114 const chrono::duration<Rep, Period>& rel_time, Predicate pred); // since C++20
121 #include <__assert> // all public C++ headers provide the assertion handler
122 #include <__availability>
123 #include <__chrono/duration.h>
124 #include <__chrono/steady_clock.h>
125 #include <__chrono/time_point.h>
126 #include <__condition_variable/condition_variable.h>
128 #include <__memory/shared_ptr.h>
129 #include <__memory/unique_ptr.h>
130 #include <__mutex/lock_guard.h>
131 #include <__mutex/mutex.h>
132 #include <__mutex/tag_types.h>
133 #include <__mutex/unique_lock.h>
134 #include <__stop_token/stop_token.h>
135 #include <__utility/move.h>
138 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
139 # pragma GCC system_header
142 #ifndef _LIBCPP_HAS_NO_THREADS
144 _LIBCPP_BEGIN_NAMESPACE_STD
146 class _LIBCPP_EXPORTED_FROM_ABI condition_variable_any
148 condition_variable __cv_;
149 shared_ptr<mutex> __mut_;
151 _LIBCPP_INLINE_VISIBILITY
152 condition_variable_any();
154 _LIBCPP_INLINE_VISIBILITY
155 void notify_one() _NOEXCEPT;
156 _LIBCPP_INLINE_VISIBILITY
157 void notify_all() _NOEXCEPT;
159 template <class _Lock>
160 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
161 void wait(_Lock& __lock);
162 template <class _Lock, class _Predicate>
163 _LIBCPP_INLINE_VISIBILITY
164 void wait(_Lock& __lock, _Predicate __pred);
166 template <class _Lock, class _Clock, class _Duration>
167 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
169 wait_until(_Lock& __lock,
170 const chrono::time_point<_Clock, _Duration>& __t);
172 template <class _Lock, class _Clock, class _Duration, class _Predicate>
174 _LIBCPP_INLINE_VISIBILITY
175 wait_until(_Lock& __lock,
176 const chrono::time_point<_Clock, _Duration>& __t,
179 template <class _Lock, class _Rep, class _Period>
181 _LIBCPP_INLINE_VISIBILITY
182 wait_for(_Lock& __lock,
183 const chrono::duration<_Rep, _Period>& __d);
185 template <class _Lock, class _Rep, class _Period, class _Predicate>
187 _LIBCPP_INLINE_VISIBILITY
188 wait_for(_Lock& __lock,
189 const chrono::duration<_Rep, _Period>& __d,
192 #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
194 template <class _Lock, class _Predicate>
195 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait(_Lock& __lock, stop_token __stoken, _Predicate __pred);
197 template <class _Lock, class _Clock, class _Duration, class _Predicate>
198 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait_until(_Lock& __lock, stop_token __stoken,
199 const chrono::time_point<_Clock, _Duration>& __abs_time, _Predicate __pred);
201 template <class _Lock, class _Rep, class _Period, class _Predicate>
202 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait_for(_Lock& __lock, stop_token __stoken,
203 const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred);
205 #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
209 condition_variable_any::condition_variable_any()
210 : __mut_(make_shared<mutex>()) {}
214 condition_variable_any::notify_one() _NOEXCEPT
216 {lock_guard<mutex> __lx(*__mut_);}
222 condition_variable_any::notify_all() _NOEXCEPT
224 {lock_guard<mutex> __lx(*__mut_);}
228 struct __lock_external
230 template <class _Lock>
231 _LIBCPP_HIDE_FROM_ABI void operator()(_Lock* __m) {__m->lock();}
234 template <class _Lock>
236 condition_variable_any::wait(_Lock& __lock)
238 shared_ptr<mutex> __mut = __mut_;
239 unique_lock<mutex> __lk(*__mut);
241 unique_ptr<_Lock, __lock_external> __lxx(&__lock);
242 lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
244 } // __mut_.unlock(), __lock.lock()
246 template <class _Lock, class _Predicate>
249 condition_variable_any::wait(_Lock& __lock, _Predicate __pred)
255 template <class _Lock, class _Clock, class _Duration>
257 condition_variable_any::wait_until(_Lock& __lock,
258 const chrono::time_point<_Clock, _Duration>& __t)
260 shared_ptr<mutex> __mut = __mut_;
261 unique_lock<mutex> __lk(*__mut);
263 unique_ptr<_Lock, __lock_external> __lxx(&__lock);
264 lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
265 return __cv_.wait_until(__lk, __t);
266 } // __mut_.unlock(), __lock.lock()
268 template <class _Lock, class _Clock, class _Duration, class _Predicate>
271 condition_variable_any::wait_until(_Lock& __lock,
272 const chrono::time_point<_Clock, _Duration>& __t,
276 if (wait_until(__lock, __t) == cv_status::timeout)
281 template <class _Lock, class _Rep, class _Period>
284 condition_variable_any::wait_for(_Lock& __lock,
285 const chrono::duration<_Rep, _Period>& __d)
287 return wait_until(__lock, chrono::steady_clock::now() + __d);
290 template <class _Lock, class _Rep, class _Period, class _Predicate>
293 condition_variable_any::wait_for(_Lock& __lock,
294 const chrono::duration<_Rep, _Period>& __d,
297 return wait_until(__lock, chrono::steady_clock::now() + __d,
298 _VSTD::move(__pred));
301 #if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
303 template <class _Lock, class _Predicate>
304 bool condition_variable_any::wait(_Lock& __lock, stop_token __stoken, _Predicate __pred) {
305 while (!__stoken.stop_requested()) {
313 template <class _Lock, class _Clock, class _Duration, class _Predicate>
314 bool condition_variable_any::wait_until(
315 _Lock& __lock, stop_token __stoken, const chrono::time_point<_Clock, _Duration>& __abs_time, _Predicate __pred) {
316 while (!__stoken.stop_requested()) {
319 if (wait_until(__lock, __abs_time) == cv_status::timeout)
325 template <class _Lock, class _Rep, class _Period, class _Predicate>
326 bool condition_variable_any::wait_for(
327 _Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred) {
328 return wait_until(__lock, std::move(__stoken), chrono::steady_clock::now() + __rel_time, std::move(__pred));
331 #endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
333 _LIBCPP_EXPORTED_FROM_ABI void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
335 _LIBCPP_END_NAMESPACE_STD
337 #endif // !_LIBCPP_HAS_NO_THREADS
339 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
345 # include <initializer_list>
348 # include <stdexcept>
349 # include <system_error>
350 # include <type_traits>
354 #endif // _LIBCPP_CONDITION_VARIABLE