1 // <condition_variable> -*- C++ -*-
3 // Copyright (C) 2008-2013 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 <mutex> // unique_lock
41 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 * @defgroup condition_variables Condition Variables
49 * @ingroup concurrency
51 * Classes for condition_variable support.
56 enum class cv_status { no_timeout, timeout };
58 /// condition_variable
59 class condition_variable
61 typedef chrono::system_clock __clock_t;
62 typedef __gthread_cond_t __native_type;
64 #ifdef __GTHREAD_COND_INIT
65 __native_type _M_cond = __GTHREAD_COND_INIT;
67 __native_type _M_cond;
71 typedef __native_type* native_handle_type;
73 condition_variable() noexcept;
74 ~condition_variable() noexcept;
76 condition_variable(const condition_variable&) = delete;
77 condition_variable& operator=(const condition_variable&) = delete;
80 notify_one() noexcept;
83 notify_all() noexcept;
86 wait(unique_lock<mutex>& __lock);
88 template<typename _Predicate>
90 wait(unique_lock<mutex>& __lock, _Predicate __p)
96 template<typename _Duration>
98 wait_until(unique_lock<mutex>& __lock,
99 const chrono::time_point<__clock_t, _Duration>& __atime)
100 { return __wait_until_impl(__lock, __atime); }
102 template<typename _Clock, typename _Duration>
104 wait_until(unique_lock<mutex>& __lock,
105 const chrono::time_point<_Clock, _Duration>& __atime)
107 // DR 887 - Sync unknown clock to known clock.
108 const typename _Clock::time_point __c_entry = _Clock::now();
109 const __clock_t::time_point __s_entry = __clock_t::now();
110 const auto __delta = __atime - __c_entry;
111 const auto __s_atime = __s_entry + __delta;
113 return __wait_until_impl(__lock, __s_atime);
116 template<typename _Clock, typename _Duration, typename _Predicate>
118 wait_until(unique_lock<mutex>& __lock,
119 const chrono::time_point<_Clock, _Duration>& __atime,
123 if (wait_until(__lock, __atime) == cv_status::timeout)
128 template<typename _Rep, typename _Period>
130 wait_for(unique_lock<mutex>& __lock,
131 const chrono::duration<_Rep, _Period>& __rtime)
132 { return wait_until(__lock, __clock_t::now() + __rtime); }
134 template<typename _Rep, typename _Period, typename _Predicate>
136 wait_for(unique_lock<mutex>& __lock,
137 const chrono::duration<_Rep, _Period>& __rtime,
139 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
146 template<typename _Dur>
148 __wait_until_impl(unique_lock<mutex>& __lock,
149 const chrono::time_point<__clock_t, _Dur>& __atime)
151 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
152 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
154 __gthread_time_t __ts =
156 static_cast<std::time_t>(__s.time_since_epoch().count()),
157 static_cast<long>(__ns.count())
160 __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
163 return (__clock_t::now() < __atime
164 ? cv_status::no_timeout : cv_status::timeout);
168 /// condition_variable_any
169 // Like above, but mutex is not required to have try_lock.
170 class condition_variable_any
172 typedef chrono::system_clock __clock_t;
173 condition_variable _M_cond;
176 // scoped unlock - unlocks in ctor, re-locks in dtor
177 template<typename _Lock>
180 explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
182 ~_Unlock() noexcept(false)
184 if (uncaught_exception())
185 __try { _M_lock.lock(); } __catch(...) { }
190 _Unlock(const _Unlock&) = delete;
191 _Unlock& operator=(const _Unlock&) = delete;
198 condition_variable_any() noexcept;
199 ~condition_variable_any() noexcept;
201 condition_variable_any(const condition_variable_any&) = delete;
202 condition_variable_any& operator=(const condition_variable_any&) = delete;
205 notify_one() noexcept
207 lock_guard<mutex> __lock(_M_mutex);
208 _M_cond.notify_one();
212 notify_all() noexcept
214 lock_guard<mutex> __lock(_M_mutex);
215 _M_cond.notify_all();
218 template<typename _Lock>
222 unique_lock<mutex> __my_lock(_M_mutex);
223 _Unlock<_Lock> __unlock(__lock);
224 // _M_mutex must be unlocked before re-locking __lock so move
225 // ownership of _M_mutex lock to an object with shorter lifetime.
226 unique_lock<mutex> __my_lock2(std::move(__my_lock));
227 _M_cond.wait(__my_lock2);
231 template<typename _Lock, typename _Predicate>
233 wait(_Lock& __lock, _Predicate __p)
239 template<typename _Lock, typename _Clock, typename _Duration>
241 wait_until(_Lock& __lock,
242 const chrono::time_point<_Clock, _Duration>& __atime)
244 unique_lock<mutex> __my_lock(_M_mutex);
245 _Unlock<_Lock> __unlock(__lock);
246 // _M_mutex must be unlocked before re-locking __lock so move
247 // ownership of _M_mutex lock to an object with shorter lifetime.
248 unique_lock<mutex> __my_lock2(std::move(__my_lock));
249 return _M_cond.wait_until(__my_lock2, __atime);
252 template<typename _Lock, typename _Clock,
253 typename _Duration, typename _Predicate>
255 wait_until(_Lock& __lock,
256 const chrono::time_point<_Clock, _Duration>& __atime,
260 if (wait_until(__lock, __atime) == cv_status::timeout)
265 template<typename _Lock, typename _Rep, typename _Period>
267 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
268 { return wait_until(__lock, __clock_t::now() + __rtime); }
270 template<typename _Lock, typename _Rep,
271 typename _Period, typename _Predicate>
273 wait_for(_Lock& __lock,
274 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
275 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
278 // @} group condition_variables
279 _GLIBCXX_END_NAMESPACE_VERSION
282 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
286 #endif // _GLIBCXX_CONDITION_VARIABLE