]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/condition_variable
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / condition_variable
CommitLineData
68a97d24
BK
1// <condition_variable> -*- C++ -*-
2
7adcbafe 3// Copyright (C) 2008-2022 Free Software Foundation, Inc.
68a97d24
BK
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
748086b7 8// Free Software Foundation; either version 3, or (at your option)
68a97d24
BK
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
68a97d24 19
748086b7
JJ
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
68a97d24 24
f910786b 25/** @file include/condition_variable
68a97d24
BK
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_CONDITION_VARIABLE
30#define _GLIBCXX_CONDITION_VARIABLE 1
31
32#pragma GCC system_header
33
734f5023 34#if __cplusplus < 201103L
ab65a4c7 35# include <bits/c++0x_warning.h>
57317d2a 36#else
68a97d24 37
7f78718b 38#include <bits/chrono.h>
0c3e5dd1 39#include <bits/std_mutex.h>
1fba0606 40#include <bits/unique_lock.h>
3429db0f 41#include <bits/alloc_traits.h>
3429db0f 42#include <bits/shared_ptr.h>
c8c03058 43#include <bits/cxxabi_forced.h>
68a97d24 44
942c4b32 45#if __cplusplus > 201703L
aa12ab2e 46# include <stop_token>
942c4b32
TR
47#endif
48
8ba7f29e 49#if defined(_GLIBCXX_HAS_GTHREADS)
7b800287 50
12ffa228
BK
51namespace std _GLIBCXX_VISIBILITY(default)
52{
53_GLIBCXX_BEGIN_NAMESPACE_VERSION
53dc5044 54
5b9daa7e
BK
55 /**
56 * @defgroup condition_variables Condition Variables
57 * @ingroup concurrency
58 *
59 * Classes for condition_variable support.
60 * @{
61 */
62
cdf5f5a3
PC
63 /// cv_status
64 enum class cv_status { no_timeout, timeout };
33ac58d5 65
68a97d24
BK
66 /// condition_variable
67 class condition_variable
68 {
ad4d1d21
MC
69 using steady_clock = chrono::steady_clock;
70 using system_clock = chrono::system_clock;
71#ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
72 using __clock_t = steady_clock;
73#else
74 using __clock_t = system_clock;
75#endif
b81e920e 76
7d2a98a7 77 __condvar _M_cond;
88399079 78
68a97d24 79 public:
7d2a98a7 80 typedef __gthread_cond_t* native_handle_type;
68a97d24 81
b81e920e
JW
82 condition_variable() noexcept;
83 ~condition_variable() noexcept;
68a97d24 84
7b800287
CF
85 condition_variable(const condition_variable&) = delete;
86 condition_variable& operator=(const condition_variable&) = delete;
87
d5cf2021 88 void
b81e920e 89 notify_one() noexcept;
68a97d24 90
d5cf2021 91 void
b81e920e 92 notify_all() noexcept;
68a97d24 93
d5cf2021 94 void
9e18a253 95 wait(unique_lock<mutex>& __lock);
68a97d24
BK
96
97 template<typename _Predicate>
d5cf2021 98 void
68a97d24
BK
99 wait(unique_lock<mutex>& __lock, _Predicate __p)
100 {
101 while (!__p())
102 wait(__lock);
103 }
d5cf2021 104
ad4d1d21
MC
105#ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
106 template<typename _Duration>
107 cv_status
108 wait_until(unique_lock<mutex>& __lock,
109 const chrono::time_point<steady_clock, _Duration>& __atime)
110 { return __wait_until_impl(__lock, __atime); }
111#endif
112
88399079 113 template<typename _Duration>
cdf5f5a3 114 cv_status
88399079 115 wait_until(unique_lock<mutex>& __lock,
ad4d1d21 116 const chrono::time_point<system_clock, _Duration>& __atime)
88399079
CF
117 { return __wait_until_impl(__lock, __atime); }
118
119 template<typename _Clock, typename _Duration>
cdf5f5a3 120 cv_status
88399079 121 wait_until(unique_lock<mutex>& __lock,
7b800287
CF
122 const chrono::time_point<_Clock, _Duration>& __atime)
123 {
bf1fc37b
JW
124#if __cplusplus > 201703L
125 static_assert(chrono::is_clock_v<_Clock>);
126#endif
e05ff300 127 using __s_dur = typename __clock_t::duration;
023cee96
PC
128 const typename _Clock::time_point __c_entry = _Clock::now();
129 const __clock_t::time_point __s_entry = __clock_t::now();
c25639b1 130 const auto __delta = __atime - __c_entry;
e05ff300
MC
131 const auto __s_atime = __s_entry +
132 chrono::__detail::ceil<__s_dur>(__delta);
7b800287 133
2f593432
MC
134 if (__wait_until_impl(__lock, __s_atime) == cv_status::no_timeout)
135 return cv_status::no_timeout;
136 // We got a timeout when measured against __clock_t but
137 // we need to check against the caller-supplied clock
138 // to tell whether we should return a timeout.
139 if (_Clock::now() < __atime)
140 return cv_status::no_timeout;
141 return cv_status::timeout;
7b800287 142 }
d3098c94
CF
143
144 template<typename _Clock, typename _Duration, typename _Predicate>
145 bool
146 wait_until(unique_lock<mutex>& __lock,
147 const chrono::time_point<_Clock, _Duration>& __atime,
88399079
CF
148 _Predicate __p)
149 {
f7459b6c 150 while (!__p())
cdf5f5a3 151 if (wait_until(__lock, __atime) == cv_status::timeout)
88399079 152 return __p();
88399079
CF
153 return true;
154 }
d3098c94
CF
155
156 template<typename _Rep, typename _Period>
cdf5f5a3 157 cv_status
d3098c94 158 wait_for(unique_lock<mutex>& __lock,
7b800287 159 const chrono::duration<_Rep, _Period>& __rtime)
83fd5e73 160 {
ad4d1d21 161 using __dur = typename steady_clock::duration;
e05ff300
MC
162 return wait_until(__lock,
163 steady_clock::now() +
164 chrono::__detail::ceil<__dur>(__rtime));
83fd5e73 165 }
d3098c94
CF
166
167 template<typename _Rep, typename _Period, typename _Predicate>
168 bool
169 wait_for(unique_lock<mutex>& __lock,
170 const chrono::duration<_Rep, _Period>& __rtime,
88399079 171 _Predicate __p)
83fd5e73 172 {
ad4d1d21 173 using __dur = typename steady_clock::duration;
e05ff300
MC
174 return wait_until(__lock,
175 steady_clock::now() +
176 chrono::__detail::ceil<__dur>(__rtime),
29b26763 177 std::move(__p));
83fd5e73 178 }
68a97d24 179
d5cf2021
BK
180 native_handle_type
181 native_handle()
7d2a98a7 182 { return _M_cond.native_handle(); }
68a97d24
BK
183
184 private:
ad4d1d21
MC
185#ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
186 template<typename _Dur>
187 cv_status
188 __wait_until_impl(unique_lock<mutex>& __lock,
189 const chrono::time_point<steady_clock, _Dur>& __atime)
190 {
191 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
192 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
193
194 __gthread_time_t __ts =
195 {
196 static_cast<std::time_t>(__s.time_since_epoch().count()),
197 static_cast<long>(__ns.count())
198 };
199
7d2a98a7 200 _M_cond.wait_until(*__lock.mutex(), CLOCK_MONOTONIC, __ts);
ad4d1d21
MC
201
202 return (steady_clock::now() < __atime
203 ? cv_status::no_timeout : cv_status::timeout);
204 }
205#endif
206
c25639b1 207 template<typename _Dur>
cdf5f5a3 208 cv_status
88399079 209 __wait_until_impl(unique_lock<mutex>& __lock,
ad4d1d21 210 const chrono::time_point<system_clock, _Dur>& __atime)
88399079 211 {
c25639b1
JW
212 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
213 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
d5cf2021
BK
214
215 __gthread_time_t __ts =
216 {
217 static_cast<std::time_t>(__s.time_since_epoch().count()),
218 static_cast<long>(__ns.count())
219 };
220
7d2a98a7 221 _M_cond.wait_until(*__lock.mutex(), __ts);
d5cf2021 222
ad4d1d21 223 return (system_clock::now() < __atime
cdf5f5a3 224 ? cv_status::no_timeout : cv_status::timeout);
88399079 225 }
68a97d24
BK
226 };
227
9db7c931
JW
228 void
229 notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
230
231 struct __at_thread_exit_elt
232 {
233 __at_thread_exit_elt* _M_next;
234 void (*_M_cb)(void*);
235 };
236
3429db0f
JW
237 inline namespace _V2 {
238
68a97d24 239 /// condition_variable_any
b7200e3f 240 // Like above, but mutex is not required to have try_lock.
68a97d24
BK
241 class condition_variable_any
242 {
ad4d1d21
MC
243#ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
244 using __clock_t = chrono::steady_clock;
245#else
246 using __clock_t = chrono::system_clock;
247#endif
b7200e3f 248 condition_variable _M_cond;
3429db0f 249 shared_ptr<mutex> _M_mutex;
f7459b6c 250
7f426c93
JW
251 // scoped unlock - unlocks in ctor, re-locks in dtor
252 template<typename _Lock>
253 struct _Unlock
254 {
255 explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
256
0943b558
JW
257#pragma GCC diagnostic push
258#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
7f426c93
JW
259 ~_Unlock() noexcept(false)
260 {
261 if (uncaught_exception())
315eb4bb
JW
262 {
263 __try
264 { _M_lock.lock(); }
265 __catch(const __cxxabiv1::__forced_unwind&)
266 { __throw_exception_again; }
267 __catch(...)
268 { }
269 }
7f426c93
JW
270 else
271 _M_lock.lock();
272 }
0943b558 273#pragma GCC diagnostic pop
7f426c93
JW
274
275 _Unlock(const _Unlock&) = delete;
276 _Unlock& operator=(const _Unlock&) = delete;
277
278 _Lock& _M_lock;
279 };
280
68a97d24 281 public:
3429db0f
JW
282 condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { }
283 ~condition_variable_any() = default;
d5cf2021 284
7b800287
CF
285 condition_variable_any(const condition_variable_any&) = delete;
286 condition_variable_any& operator=(const condition_variable_any&) = delete;
68a97d24 287
d5cf2021 288 void
b81e920e 289 notify_one() noexcept
b7200e3f 290 {
3429db0f 291 lock_guard<mutex> __lock(*_M_mutex);
b7200e3f
JW
292 _M_cond.notify_one();
293 }
68a97d24 294
d5cf2021 295 void
b81e920e 296 notify_all() noexcept
b7200e3f 297 {
3429db0f 298 lock_guard<mutex> __lock(*_M_mutex);
b7200e3f
JW
299 _M_cond.notify_all();
300 }
68a97d24
BK
301
302 template<typename _Lock>
d5cf2021 303 void
b7200e3f
JW
304 wait(_Lock& __lock)
305 {
3429db0f
JW
306 shared_ptr<mutex> __mutex = _M_mutex;
307 unique_lock<mutex> __my_lock(*__mutex);
7f426c93 308 _Unlock<_Lock> __unlock(__lock);
3429db0f
JW
309 // *__mutex must be unlocked before re-locking __lock so move
310 // ownership of *__mutex lock to an object with shorter lifetime.
5d020aa2
JW
311 unique_lock<mutex> __my_lock2(std::move(__my_lock));
312 _M_cond.wait(__my_lock2);
b7200e3f 313 }
33ac58d5 314
68a97d24
BK
315
316 template<typename _Lock, typename _Predicate>
d5cf2021 317 void
cdf5f5a3
PC
318 wait(_Lock& __lock, _Predicate __p)
319 {
320 while (!__p())
321 wait(__lock);
322 }
68a97d24 323
d3098c94 324 template<typename _Lock, typename _Clock, typename _Duration>
cdf5f5a3 325 cv_status
d3098c94 326 wait_until(_Lock& __lock,
b7200e3f
JW
327 const chrono::time_point<_Clock, _Duration>& __atime)
328 {
3429db0f
JW
329 shared_ptr<mutex> __mutex = _M_mutex;
330 unique_lock<mutex> __my_lock(*__mutex);
7f426c93 331 _Unlock<_Lock> __unlock(__lock);
3429db0f
JW
332 // *__mutex must be unlocked before re-locking __lock so move
333 // ownership of *__mutex lock to an object with shorter lifetime.
7f426c93
JW
334 unique_lock<mutex> __my_lock2(std::move(__my_lock));
335 return _M_cond.wait_until(__my_lock2, __atime);
b7200e3f 336 }
68a97d24 337
d5cf2021 338 template<typename _Lock, typename _Clock,
d3098c94 339 typename _Duration, typename _Predicate>
d5cf2021 340 bool
d3098c94
CF
341 wait_until(_Lock& __lock,
342 const chrono::time_point<_Clock, _Duration>& __atime,
cdf5f5a3
PC
343 _Predicate __p)
344 {
345 while (!__p())
346 if (wait_until(__lock, __atime) == cv_status::timeout)
347 return __p();
348 return true;
349 }
d5cf2021 350
d3098c94 351 template<typename _Lock, typename _Rep, typename _Period>
cdf5f5a3 352 cv_status
b7200e3f
JW
353 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
354 { return wait_until(__lock, __clock_t::now() + __rtime); }
d3098c94
CF
355
356 template<typename _Lock, typename _Rep,
357 typename _Period, typename _Predicate>
358 bool
d5cf2021 359 wait_for(_Lock& __lock,
b7200e3f
JW
360 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
361 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
942c4b32
TR
362
363#ifdef __cpp_lib_jthread
364 template <class _Lock, class _Predicate>
9e3c1eb7
TR
365 bool wait(_Lock& __lock,
366 stop_token __stoken,
367 _Predicate __p)
942c4b32
TR
368 {
369 if (__stoken.stop_requested())
370 {
371 return __p();
372 }
373
374 std::stop_callback __cb(__stoken, [this] { notify_all(); });
375 shared_ptr<mutex> __mutex = _M_mutex;
376 while (!__p())
377 {
378 unique_lock<mutex> __my_lock(*__mutex);
379 if (__stoken.stop_requested())
380 {
381 return false;
382 }
383 // *__mutex must be unlocked before re-locking __lock so move
384 // ownership of *__mutex lock to an object with shorter lifetime.
385 _Unlock<_Lock> __unlock(__lock);
386 unique_lock<mutex> __my_lock2(std::move(__my_lock));
387 _M_cond.wait(__my_lock2);
388 }
389 return true;
390 }
391
392 template <class _Lock, class _Clock, class _Duration, class _Predicate>
9e3c1eb7
TR
393 bool wait_until(_Lock& __lock,
394 stop_token __stoken,
395 const chrono::time_point<_Clock, _Duration>& __abs_time,
396 _Predicate __p)
942c4b32
TR
397 {
398 if (__stoken.stop_requested())
399 {
400 return __p();
401 }
402
403 std::stop_callback __cb(__stoken, [this] { notify_all(); });
404 shared_ptr<mutex> __mutex = _M_mutex;
405 while (!__p())
406 {
407 bool __stop;
408 {
409 unique_lock<mutex> __my_lock(*__mutex);
410 if (__stoken.stop_requested())
411 {
412 return false;
413 }
414 _Unlock<_Lock> __u(__lock);
415 unique_lock<mutex> __my_lock2(std::move(__my_lock));
416 const auto __status = _M_cond.wait_until(__my_lock2, __abs_time);
417 __stop = (__status == std::cv_status::timeout) || __stoken.stop_requested();
418 }
419 if (__stop)
420 {
421 return __p();
422 }
423 }
424 return true;
425 }
426
427 template <class _Lock, class _Rep, class _Period, class _Predicate>
9e3c1eb7
TR
428 bool wait_for(_Lock& __lock,
429 stop_token __stoken,
430 const chrono::duration<_Rep, _Period>& __rel_time,
431 _Predicate __p)
942c4b32
TR
432 {
433 auto __abst = std::chrono::steady_clock::now() + __rel_time;
9e3c1eb7
TR
434 return wait_until(__lock,
435 std::move(__stoken),
436 __abst,
437 std::move(__p));
942c4b32
TR
438 }
439#endif
68a97d24 440 };
5b9daa7e 441
3429db0f
JW
442 } // end inline namespace
443
f0b88346 444 /// @} group condition_variables
12ffa228
BK
445_GLIBCXX_END_NAMESPACE_VERSION
446} // namespace
68a97d24 447
8ba7f29e 448#endif // _GLIBCXX_HAS_GTHREADS
734f5023 449#endif // C++11
57317d2a 450#endif // _GLIBCXX_CONDITION_VARIABLE