1 // <condition_variable> -*- C++ -*-
3 // Copyright (C) 2008-2020 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 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
40 #include <bits/std_mutex.h>
41 #include <bits/unique_lock.h>
42 #include <ext/concurrence.h>
43 #include <bits/alloc_traits.h>
44 #include <bits/allocator.h>
45 #include <bits/unique_ptr.h>
46 #include <bits/shared_ptr.h>
47 #include <bits/cxxabi_forced.h>
49 #if __cplusplus > 201703L
50 #define __cpp_lib_jthread 201907L
54 #if defined(_GLIBCXX_HAS_GTHREADS)
56 namespace std _GLIBCXX_VISIBILITY(default)
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
61 * @defgroup condition_variables Condition Variables
62 * @ingroup concurrency
64 * Classes for condition_variable support.
69 enum class cv_status { no_timeout, timeout };
71 /// condition_variable
72 class condition_variable
74 using steady_clock = chrono::steady_clock;
75 using system_clock = chrono::system_clock;
76 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
77 using __clock_t = steady_clock;
79 using __clock_t = system_clock;
81 typedef __gthread_cond_t __native_type;
83 #ifdef __GTHREAD_COND_INIT
84 __native_type _M_cond = __GTHREAD_COND_INIT;
86 __native_type _M_cond;
90 typedef __native_type* native_handle_type;
92 condition_variable() noexcept;
93 ~condition_variable() noexcept;
95 condition_variable(const condition_variable&) = delete;
96 condition_variable& operator=(const condition_variable&) = delete;
99 notify_one() noexcept;
102 notify_all() noexcept;
105 wait(unique_lock<mutex>& __lock) noexcept;
107 template<typename _Predicate>
109 wait(unique_lock<mutex>& __lock, _Predicate __p)
115 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
116 template<typename _Duration>
118 wait_until(unique_lock<mutex>& __lock,
119 const chrono::time_point<steady_clock, _Duration>& __atime)
120 { return __wait_until_impl(__lock, __atime); }
123 template<typename _Duration>
125 wait_until(unique_lock<mutex>& __lock,
126 const chrono::time_point<system_clock, _Duration>& __atime)
127 { return __wait_until_impl(__lock, __atime); }
129 template<typename _Clock, typename _Duration>
131 wait_until(unique_lock<mutex>& __lock,
132 const chrono::time_point<_Clock, _Duration>& __atime)
134 #if __cplusplus > 201703L
135 static_assert(chrono::is_clock_v<_Clock>);
137 const typename _Clock::time_point __c_entry = _Clock::now();
138 const __clock_t::time_point __s_entry = __clock_t::now();
139 const auto __delta = __atime - __c_entry;
140 const auto __s_atime = __s_entry + __delta;
142 if (__wait_until_impl(__lock, __s_atime) == cv_status::no_timeout)
143 return cv_status::no_timeout;
144 // We got a timeout when measured against __clock_t but
145 // we need to check against the caller-supplied clock
146 // to tell whether we should return a timeout.
147 if (_Clock::now() < __atime)
148 return cv_status::no_timeout;
149 return cv_status::timeout;
152 template<typename _Clock, typename _Duration, typename _Predicate>
154 wait_until(unique_lock<mutex>& __lock,
155 const chrono::time_point<_Clock, _Duration>& __atime,
159 if (wait_until(__lock, __atime) == cv_status::timeout)
164 template<typename _Rep, typename _Period>
166 wait_for(unique_lock<mutex>& __lock,
167 const chrono::duration<_Rep, _Period>& __rtime)
169 using __dur = typename steady_clock::duration;
170 auto __reltime = chrono::duration_cast<__dur>(__rtime);
171 if (__reltime < __rtime)
173 return wait_until(__lock, steady_clock::now() + __reltime);
176 template<typename _Rep, typename _Period, typename _Predicate>
178 wait_for(unique_lock<mutex>& __lock,
179 const chrono::duration<_Rep, _Period>& __rtime,
182 using __dur = typename steady_clock::duration;
183 auto __reltime = chrono::duration_cast<__dur>(__rtime);
184 if (__reltime < __rtime)
186 return wait_until(__lock, steady_clock::now() + __reltime,
195 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
196 template<typename _Dur>
198 __wait_until_impl(unique_lock<mutex>& __lock,
199 const chrono::time_point<steady_clock, _Dur>& __atime)
201 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
202 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
204 __gthread_time_t __ts =
206 static_cast<std::time_t>(__s.time_since_epoch().count()),
207 static_cast<long>(__ns.count())
210 pthread_cond_clockwait(&_M_cond, __lock.mutex()->native_handle(),
214 return (steady_clock::now() < __atime
215 ? cv_status::no_timeout : cv_status::timeout);
219 template<typename _Dur>
221 __wait_until_impl(unique_lock<mutex>& __lock,
222 const chrono::time_point<system_clock, _Dur>& __atime)
224 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
225 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
227 __gthread_time_t __ts =
229 static_cast<std::time_t>(__s.time_since_epoch().count()),
230 static_cast<long>(__ns.count())
233 __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
236 return (system_clock::now() < __atime
237 ? cv_status::no_timeout : cv_status::timeout);
242 notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
244 struct __at_thread_exit_elt
246 __at_thread_exit_elt* _M_next;
247 void (*_M_cb)(void*);
250 inline namespace _V2 {
252 /// condition_variable_any
253 // Like above, but mutex is not required to have try_lock.
254 class condition_variable_any
256 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
257 using __clock_t = chrono::steady_clock;
259 using __clock_t = chrono::system_clock;
261 condition_variable _M_cond;
262 shared_ptr<mutex> _M_mutex;
264 // scoped unlock - unlocks in ctor, re-locks in dtor
265 template<typename _Lock>
268 explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
270 ~_Unlock() noexcept(false)
272 if (uncaught_exception())
276 __catch(const __cxxabiv1::__forced_unwind&)
277 { __throw_exception_again; }
285 _Unlock(const _Unlock&) = delete;
286 _Unlock& operator=(const _Unlock&) = delete;
292 condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { }
293 ~condition_variable_any() = default;
295 condition_variable_any(const condition_variable_any&) = delete;
296 condition_variable_any& operator=(const condition_variable_any&) = delete;
299 notify_one() noexcept
301 lock_guard<mutex> __lock(*_M_mutex);
302 _M_cond.notify_one();
306 notify_all() noexcept
308 lock_guard<mutex> __lock(*_M_mutex);
309 _M_cond.notify_all();
312 template<typename _Lock>
316 shared_ptr<mutex> __mutex = _M_mutex;
317 unique_lock<mutex> __my_lock(*__mutex);
318 _Unlock<_Lock> __unlock(__lock);
319 // *__mutex must be unlocked before re-locking __lock so move
320 // ownership of *__mutex lock to an object with shorter lifetime.
321 unique_lock<mutex> __my_lock2(std::move(__my_lock));
322 _M_cond.wait(__my_lock2);
326 template<typename _Lock, typename _Predicate>
328 wait(_Lock& __lock, _Predicate __p)
334 template<typename _Lock, typename _Clock, typename _Duration>
336 wait_until(_Lock& __lock,
337 const chrono::time_point<_Clock, _Duration>& __atime)
339 shared_ptr<mutex> __mutex = _M_mutex;
340 unique_lock<mutex> __my_lock(*__mutex);
341 _Unlock<_Lock> __unlock(__lock);
342 // *__mutex must be unlocked before re-locking __lock so move
343 // ownership of *__mutex lock to an object with shorter lifetime.
344 unique_lock<mutex> __my_lock2(std::move(__my_lock));
345 return _M_cond.wait_until(__my_lock2, __atime);
348 template<typename _Lock, typename _Clock,
349 typename _Duration, typename _Predicate>
351 wait_until(_Lock& __lock,
352 const chrono::time_point<_Clock, _Duration>& __atime,
356 if (wait_until(__lock, __atime) == cv_status::timeout)
361 template<typename _Lock, typename _Rep, typename _Period>
363 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
364 { return wait_until(__lock, __clock_t::now() + __rtime); }
366 template<typename _Lock, typename _Rep,
367 typename _Period, typename _Predicate>
369 wait_for(_Lock& __lock,
370 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
371 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
373 #ifdef __cpp_lib_jthread
374 template <class _Lock, class _Predicate>
375 bool wait(_Lock& __lock,
379 if (__stoken.stop_requested())
384 std::stop_callback __cb(__stoken, [this] { notify_all(); });
385 shared_ptr<mutex> __mutex = _M_mutex;
388 unique_lock<mutex> __my_lock(*__mutex);
389 if (__stoken.stop_requested())
393 // *__mutex must be unlocked before re-locking __lock so move
394 // ownership of *__mutex lock to an object with shorter lifetime.
395 _Unlock<_Lock> __unlock(__lock);
396 unique_lock<mutex> __my_lock2(std::move(__my_lock));
397 _M_cond.wait(__my_lock2);
402 template <class _Lock, class _Clock, class _Duration, class _Predicate>
403 bool wait_until(_Lock& __lock,
405 const chrono::time_point<_Clock, _Duration>& __abs_time,
408 if (__stoken.stop_requested())
413 std::stop_callback __cb(__stoken, [this] { notify_all(); });
414 shared_ptr<mutex> __mutex = _M_mutex;
419 unique_lock<mutex> __my_lock(*__mutex);
420 if (__stoken.stop_requested())
424 _Unlock<_Lock> __u(__lock);
425 unique_lock<mutex> __my_lock2(std::move(__my_lock));
426 const auto __status = _M_cond.wait_until(__my_lock2, __abs_time);
427 __stop = (__status == std::cv_status::timeout) || __stoken.stop_requested();
437 template <class _Lock, class _Rep, class _Period, class _Predicate>
438 bool wait_for(_Lock& __lock,
440 const chrono::duration<_Rep, _Period>& __rel_time,
443 auto __abst = std::chrono::steady_clock::now() + __rel_time;
444 return wait_until(__lock,
452 } // end inline namespace
454 // @} group condition_variables
455 _GLIBCXX_END_NAMESPACE_VERSION
458 #endif // _GLIBCXX_HAS_GTHREADS
460 #endif // _GLIBCXX_CONDITION_VARIABLE