1 // <condition_variable> -*- C++ -*-
3 // Copyright (C) 2008-2024 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
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.
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.
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/>.
25 /** @file include/condition_variable
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_CONDITION_VARIABLE
30 #define _GLIBCXX_CONDITION_VARIABLE 1
32 #pragma GCC system_header
34 #include <bits/requires_hosted.h> // threading primitive
36 #if __cplusplus < 201103L
37 # include <bits/c++0x_warning.h>
40 #include <bits/chrono.h>
41 #include <bits/error_constants.h>
42 #include <bits/std_mutex.h>
43 #include <bits/unique_lock.h>
44 #include <bits/alloc_traits.h>
45 #include <bits/shared_ptr.h>
46 #include <bits/cxxabi_forced.h>
48 #if __cplusplus > 201703L
49 # include <stop_token>
52 #if defined(_GLIBCXX_HAS_GTHREADS)
54 namespace std _GLIBCXX_VISIBILITY(default)
56 _GLIBCXX_BEGIN_NAMESPACE_VERSION
59 * @defgroup condition_variables Condition Variables
60 * @ingroup concurrency
62 * Classes for condition_variable support.
67 enum class cv_status { no_timeout, timeout };
69 /// condition_variable
70 class condition_variable
72 using steady_clock = chrono::steady_clock;
73 using system_clock = chrono::system_clock;
74 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
75 using __clock_t = steady_clock;
77 using __clock_t = system_clock;
83 typedef __gthread_cond_t* native_handle_type;
85 condition_variable() noexcept;
86 ~condition_variable() noexcept;
88 condition_variable(const condition_variable&) = delete;
89 condition_variable& operator=(const condition_variable&) = delete;
92 notify_one() noexcept;
95 notify_all() noexcept;
98 wait(unique_lock<mutex>& __lock);
100 template<typename _Predicate>
102 wait(unique_lock<mutex>& __lock, _Predicate __p)
108 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
109 template<typename _Duration>
111 wait_until(unique_lock<mutex>& __lock,
112 const chrono::time_point<steady_clock, _Duration>& __atime)
113 { return __wait_until_impl(__lock, __atime); }
116 template<typename _Duration>
118 wait_until(unique_lock<mutex>& __lock,
119 const chrono::time_point<system_clock, _Duration>& __atime)
120 { return __wait_until_impl(__lock, __atime); }
122 template<typename _Clock, typename _Duration>
124 wait_until(unique_lock<mutex>& __lock,
125 const chrono::time_point<_Clock, _Duration>& __atime)
127 #if __cplusplus > 201703L
128 static_assert(chrono::is_clock_v<_Clock>);
130 using __s_dur = typename __clock_t::duration;
131 const typename _Clock::time_point __c_entry = _Clock::now();
132 const __clock_t::time_point __s_entry = __clock_t::now();
133 const auto __delta = __atime - __c_entry;
134 const auto __s_atime = __s_entry +
135 chrono::__detail::ceil<__s_dur>(__delta);
137 if (__wait_until_impl(__lock, __s_atime) == cv_status::no_timeout)
138 return cv_status::no_timeout;
139 // We got a timeout when measured against __clock_t but
140 // we need to check against the caller-supplied clock
141 // to tell whether we should return a timeout.
142 if (_Clock::now() < __atime)
143 return cv_status::no_timeout;
144 return cv_status::timeout;
147 template<typename _Clock, typename _Duration, typename _Predicate>
149 wait_until(unique_lock<mutex>& __lock,
150 const chrono::time_point<_Clock, _Duration>& __atime,
154 if (wait_until(__lock, __atime) == cv_status::timeout)
159 template<typename _Rep, typename _Period>
161 wait_for(unique_lock<mutex>& __lock,
162 const chrono::duration<_Rep, _Period>& __rtime)
164 using __dur = typename steady_clock::duration;
165 return wait_until(__lock,
166 steady_clock::now() +
167 chrono::__detail::ceil<__dur>(__rtime));
170 template<typename _Rep, typename _Period, typename _Predicate>
172 wait_for(unique_lock<mutex>& __lock,
173 const chrono::duration<_Rep, _Period>& __rtime,
176 using __dur = typename steady_clock::duration;
177 return wait_until(__lock,
178 steady_clock::now() +
179 chrono::__detail::ceil<__dur>(__rtime),
185 { return _M_cond.native_handle(); }
188 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
189 template<typename _Dur>
191 __wait_until_impl(unique_lock<mutex>& __lock,
192 const chrono::time_point<steady_clock, _Dur>& __atime)
194 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
195 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
197 __gthread_time_t __ts =
199 static_cast<std::time_t>(__s.time_since_epoch().count()),
200 static_cast<long>(__ns.count())
203 _M_cond.wait_until(*__lock.mutex(), CLOCK_MONOTONIC, __ts);
205 return (steady_clock::now() < __atime
206 ? cv_status::no_timeout : cv_status::timeout);
210 template<typename _Dur>
212 __wait_until_impl(unique_lock<mutex>& __lock,
213 const chrono::time_point<system_clock, _Dur>& __atime)
215 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
216 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
218 __gthread_time_t __ts =
220 static_cast<std::time_t>(__s.time_since_epoch().count()),
221 static_cast<long>(__ns.count())
224 _M_cond.wait_until(*__lock.mutex(), __ts);
226 return (system_clock::now() < __atime
227 ? cv_status::no_timeout : cv_status::timeout);
232 notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
234 struct __at_thread_exit_elt
236 __at_thread_exit_elt* _M_next;
237 void (*_M_cb)(void*);
240 _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2)
242 /// condition_variable_any
243 // Like above, but mutex is not required to have try_lock.
244 class condition_variable_any
246 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
247 using __clock_t = chrono::steady_clock;
249 using __clock_t = chrono::system_clock;
251 condition_variable _M_cond;
252 shared_ptr<mutex> _M_mutex;
254 // scoped unlock - unlocks in ctor, re-locks in dtor
255 template<typename _Lock>
258 explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
260 #pragma GCC diagnostic push
261 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
262 ~_Unlock() noexcept(false)
264 if (uncaught_exception())
268 __catch(const __cxxabiv1::__forced_unwind&)
269 { __throw_exception_again; }
276 #pragma GCC diagnostic pop
278 _Unlock(const _Unlock&) = delete;
279 _Unlock& operator=(const _Unlock&) = delete;
285 condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { }
286 ~condition_variable_any() = default;
288 condition_variable_any(const condition_variable_any&) = delete;
289 condition_variable_any& operator=(const condition_variable_any&) = delete;
292 notify_one() noexcept
294 lock_guard<mutex> __lock(*_M_mutex);
295 _M_cond.notify_one();
299 notify_all() noexcept
301 lock_guard<mutex> __lock(*_M_mutex);
302 _M_cond.notify_all();
305 template<typename _Lock>
309 shared_ptr<mutex> __mutex = _M_mutex;
310 unique_lock<mutex> __my_lock(*__mutex);
311 _Unlock<_Lock> __unlock(__lock);
312 // *__mutex must be unlocked before re-locking __lock so move
313 // ownership of *__mutex lock to an object with shorter lifetime.
314 unique_lock<mutex> __my_lock2(std::move(__my_lock));
315 _M_cond.wait(__my_lock2);
319 template<typename _Lock, typename _Predicate>
321 wait(_Lock& __lock, _Predicate __p)
327 template<typename _Lock, typename _Clock, typename _Duration>
329 wait_until(_Lock& __lock,
330 const chrono::time_point<_Clock, _Duration>& __atime)
332 shared_ptr<mutex> __mutex = _M_mutex;
333 unique_lock<mutex> __my_lock(*__mutex);
334 _Unlock<_Lock> __unlock(__lock);
335 // *__mutex must be unlocked before re-locking __lock so move
336 // ownership of *__mutex lock to an object with shorter lifetime.
337 unique_lock<mutex> __my_lock2(std::move(__my_lock));
338 return _M_cond.wait_until(__my_lock2, __atime);
341 template<typename _Lock, typename _Clock,
342 typename _Duration, typename _Predicate>
344 wait_until(_Lock& __lock,
345 const chrono::time_point<_Clock, _Duration>& __atime,
349 if (wait_until(__lock, __atime) == cv_status::timeout)
354 template<typename _Lock, typename _Rep, typename _Period>
356 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
357 { return wait_until(__lock, __clock_t::now() + __rtime); }
359 template<typename _Lock, typename _Rep,
360 typename _Period, typename _Predicate>
362 wait_for(_Lock& __lock,
363 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
364 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
366 #ifdef __glibcxx_jthread
367 template <class _Lock, class _Predicate>
368 bool wait(_Lock& __lock,
372 if (__stoken.stop_requested())
377 std::stop_callback __cb(__stoken, [this] { notify_all(); });
378 shared_ptr<mutex> __mutex = _M_mutex;
381 unique_lock<mutex> __my_lock(*__mutex);
382 if (__stoken.stop_requested())
386 // *__mutex must be unlocked before re-locking __lock so move
387 // ownership of *__mutex lock to an object with shorter lifetime.
388 _Unlock<_Lock> __unlock(__lock);
389 unique_lock<mutex> __my_lock2(std::move(__my_lock));
390 _M_cond.wait(__my_lock2);
395 template <class _Lock, class _Clock, class _Duration, class _Predicate>
396 bool wait_until(_Lock& __lock,
398 const chrono::time_point<_Clock, _Duration>& __abs_time,
401 if (__stoken.stop_requested())
406 std::stop_callback __cb(__stoken, [this] { notify_all(); });
407 shared_ptr<mutex> __mutex = _M_mutex;
412 unique_lock<mutex> __my_lock(*__mutex);
413 if (__stoken.stop_requested())
417 _Unlock<_Lock> __u(__lock);
418 unique_lock<mutex> __my_lock2(std::move(__my_lock));
419 const auto __status = _M_cond.wait_until(__my_lock2, __abs_time);
420 __stop = (__status == std::cv_status::timeout) || __stoken.stop_requested();
430 template <class _Lock, class _Rep, class _Period, class _Predicate>
431 bool wait_for(_Lock& __lock,
433 const chrono::duration<_Rep, _Period>& __rel_time,
436 auto __abst = std::chrono::steady_clock::now() + __rel_time;
437 return wait_until(__lock,
445 _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)
447 /// @} group condition_variables
448 _GLIBCXX_END_NAMESPACE_VERSION
451 #endif // _GLIBCXX_HAS_GTHREADS
453 #endif // _GLIBCXX_CONDITION_VARIABLE