1 // <condition_variable> -*- C++ -*-
3 // Copyright (C) 2008-2018 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>
39 #include <bits/std_mutex.h>
40 #include <ext/concurrence.h>
41 #include <bits/alloc_traits.h>
42 #include <bits/allocator.h>
43 #include <bits/unique_ptr.h>
44 #include <bits/shared_ptr.h>
45 #include <bits/cxxabi_forced.h>
47 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
49 namespace std _GLIBCXX_VISIBILITY(default)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
54 * @defgroup condition_variables Condition Variables
55 * @ingroup concurrency
57 * Classes for condition_variable support.
62 enum class cv_status { no_timeout, timeout };
64 /// condition_variable
65 class condition_variable
67 typedef chrono::system_clock __clock_t;
68 typedef __gthread_cond_t __native_type;
70 #ifdef __GTHREAD_COND_INIT
71 __native_type _M_cond = __GTHREAD_COND_INIT;
73 __native_type _M_cond;
77 typedef __native_type* native_handle_type;
79 condition_variable() noexcept;
80 ~condition_variable() noexcept;
82 condition_variable(const condition_variable&) = delete;
83 condition_variable& operator=(const condition_variable&) = delete;
86 notify_one() noexcept;
89 notify_all() noexcept;
92 wait(unique_lock<mutex>& __lock) noexcept;
94 template<typename _Predicate>
96 wait(unique_lock<mutex>& __lock, _Predicate __p)
102 template<typename _Duration>
104 wait_until(unique_lock<mutex>& __lock,
105 const chrono::time_point<__clock_t, _Duration>& __atime)
106 { return __wait_until_impl(__lock, __atime); }
108 template<typename _Clock, typename _Duration>
110 wait_until(unique_lock<mutex>& __lock,
111 const chrono::time_point<_Clock, _Duration>& __atime)
113 // DR 887 - Sync unknown clock to known clock.
114 const typename _Clock::time_point __c_entry = _Clock::now();
115 const __clock_t::time_point __s_entry = __clock_t::now();
116 const auto __delta = __atime - __c_entry;
117 const auto __s_atime = __s_entry + __delta;
119 return __wait_until_impl(__lock, __s_atime);
122 template<typename _Clock, typename _Duration, typename _Predicate>
124 wait_until(unique_lock<mutex>& __lock,
125 const chrono::time_point<_Clock, _Duration>& __atime,
129 if (wait_until(__lock, __atime) == cv_status::timeout)
134 template<typename _Rep, typename _Period>
136 wait_for(unique_lock<mutex>& __lock,
137 const chrono::duration<_Rep, _Period>& __rtime)
139 using __dur = typename __clock_t::duration;
140 auto __reltime = chrono::duration_cast<__dur>(__rtime);
141 if (__reltime < __rtime)
143 return wait_until(__lock, __clock_t::now() + __reltime);
146 template<typename _Rep, typename _Period, typename _Predicate>
148 wait_for(unique_lock<mutex>& __lock,
149 const chrono::duration<_Rep, _Period>& __rtime,
152 using __dur = typename __clock_t::duration;
153 auto __reltime = chrono::duration_cast<__dur>(__rtime);
154 if (__reltime < __rtime)
156 return wait_until(__lock, __clock_t::now() + __reltime, std::move(__p));
164 template<typename _Dur>
166 __wait_until_impl(unique_lock<mutex>& __lock,
167 const chrono::time_point<__clock_t, _Dur>& __atime)
169 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
170 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
172 __gthread_time_t __ts =
174 static_cast<std::time_t>(__s.time_since_epoch().count()),
175 static_cast<long>(__ns.count())
178 __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
181 return (__clock_t::now() < __atime
182 ? cv_status::no_timeout : cv_status::timeout);
187 notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
189 struct __at_thread_exit_elt
191 __at_thread_exit_elt* _M_next;
192 void (*_M_cb)(void*);
195 inline namespace _V2 {
197 /// condition_variable_any
198 // Like above, but mutex is not required to have try_lock.
199 class condition_variable_any
201 typedef chrono::system_clock __clock_t;
202 condition_variable _M_cond;
203 shared_ptr<mutex> _M_mutex;
205 // scoped unlock - unlocks in ctor, re-locks in dtor
206 template<typename _Lock>
209 explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
211 ~_Unlock() noexcept(false)
213 if (uncaught_exception())
217 __catch(const __cxxabiv1::__forced_unwind&)
218 { __throw_exception_again; }
226 _Unlock(const _Unlock&) = delete;
227 _Unlock& operator=(const _Unlock&) = delete;
233 condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { }
234 ~condition_variable_any() = default;
236 condition_variable_any(const condition_variable_any&) = delete;
237 condition_variable_any& operator=(const condition_variable_any&) = delete;
240 notify_one() noexcept
242 lock_guard<mutex> __lock(*_M_mutex);
243 _M_cond.notify_one();
247 notify_all() noexcept
249 lock_guard<mutex> __lock(*_M_mutex);
250 _M_cond.notify_all();
253 template<typename _Lock>
257 shared_ptr<mutex> __mutex = _M_mutex;
258 unique_lock<mutex> __my_lock(*__mutex);
259 _Unlock<_Lock> __unlock(__lock);
260 // *__mutex must be unlocked before re-locking __lock so move
261 // ownership of *__mutex lock to an object with shorter lifetime.
262 unique_lock<mutex> __my_lock2(std::move(__my_lock));
263 _M_cond.wait(__my_lock2);
267 template<typename _Lock, typename _Predicate>
269 wait(_Lock& __lock, _Predicate __p)
275 template<typename _Lock, typename _Clock, typename _Duration>
277 wait_until(_Lock& __lock,
278 const chrono::time_point<_Clock, _Duration>& __atime)
280 shared_ptr<mutex> __mutex = _M_mutex;
281 unique_lock<mutex> __my_lock(*__mutex);
282 _Unlock<_Lock> __unlock(__lock);
283 // *__mutex must be unlocked before re-locking __lock so move
284 // ownership of *__mutex lock to an object with shorter lifetime.
285 unique_lock<mutex> __my_lock2(std::move(__my_lock));
286 return _M_cond.wait_until(__my_lock2, __atime);
289 template<typename _Lock, typename _Clock,
290 typename _Duration, typename _Predicate>
292 wait_until(_Lock& __lock,
293 const chrono::time_point<_Clock, _Duration>& __atime,
297 if (wait_until(__lock, __atime) == cv_status::timeout)
302 template<typename _Lock, typename _Rep, typename _Period>
304 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
305 { return wait_until(__lock, __clock_t::now() + __rtime); }
307 template<typename _Lock, typename _Rep,
308 typename _Period, typename _Predicate>
310 wait_for(_Lock& __lock,
311 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
312 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
315 } // end inline namespace
317 // @} group condition_variables
318 _GLIBCXX_END_NAMESPACE_VERSION
321 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
325 #endif // _GLIBCXX_CONDITION_VARIABLE