Debian Codegraph @

llvm-project/libcxx/include/condition_variable

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