1 // <condition_variable> -*- C++ -*-
3 // Copyright (C) 2008, 2009, 2010 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 #ifndef __GXX_EXPERIMENTAL_CXX0X__
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;
63 __native_type _M_cond;
66 typedef __native_type* native_handle_type;
68 condition_variable() throw ();
69 ~condition_variable() throw ();
71 condition_variable(const condition_variable&) = delete;
72 condition_variable& operator=(const condition_variable&) = delete;
81 wait(unique_lock<mutex>& __lock);
83 template<typename _Predicate>
85 wait(unique_lock<mutex>& __lock, _Predicate __p)
91 template<typename _Duration>
93 wait_until(unique_lock<mutex>& __lock,
94 const chrono::time_point<__clock_t, _Duration>& __atime)
95 { return __wait_until_impl(__lock, __atime); }
97 template<typename _Clock, typename _Duration>
99 wait_until(unique_lock<mutex>& __lock,
100 const chrono::time_point<_Clock, _Duration>& __atime)
102 // DR 887 - Sync unknown clock to known clock.
103 const typename _Clock::time_point __c_entry = _Clock::now();
104 const __clock_t::time_point __s_entry = __clock_t::now();
105 const chrono::nanoseconds __delta = __atime - __c_entry;
106 const __clock_t::time_point __s_atime = __s_entry + __delta;
108 return __wait_until_impl(__lock, __s_atime);
111 template<typename _Clock, typename _Duration, typename _Predicate>
113 wait_until(unique_lock<mutex>& __lock,
114 const chrono::time_point<_Clock, _Duration>& __atime,
118 if (wait_until(__lock, __atime) == cv_status::timeout)
123 template<typename _Rep, typename _Period>
125 wait_for(unique_lock<mutex>& __lock,
126 const chrono::duration<_Rep, _Period>& __rtime)
127 { return wait_until(__lock, __clock_t::now() + __rtime); }
129 template<typename _Rep, typename _Period, typename _Predicate>
131 wait_for(unique_lock<mutex>& __lock,
132 const chrono::duration<_Rep, _Period>& __rtime,
134 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
141 template<typename _Clock, typename _Duration>
143 __wait_until_impl(unique_lock<mutex>& __lock,
144 const chrono::time_point<_Clock, _Duration>& __atime)
146 chrono::time_point<__clock_t, chrono::seconds> __s =
147 chrono::time_point_cast<chrono::seconds>(__atime);
149 chrono::nanoseconds __ns =
150 chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
152 __gthread_time_t __ts =
154 static_cast<std::time_t>(__s.time_since_epoch().count()),
155 static_cast<long>(__ns.count())
158 __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
161 return (_Clock::now() < __atime
162 ? cv_status::no_timeout : cv_status::timeout);
166 /// condition_variable_any
167 // Like above, but mutex is not required to have try_lock.
168 class condition_variable_any
170 typedef chrono::system_clock __clock_t;
171 condition_variable _M_cond;
175 typedef condition_variable::native_handle_type native_handle_type;
177 condition_variable_any() throw ();
178 ~condition_variable_any() throw ();
180 condition_variable_any(const condition_variable_any&) = delete;
181 condition_variable_any& operator=(const condition_variable_any&) = delete;
186 lock_guard<mutex> __lock(_M_mutex);
187 _M_cond.notify_one();
193 lock_guard<mutex> __lock(_M_mutex);
194 _M_cond.notify_all();
197 template<typename _Lock>
201 unique_lock<mutex> __my_lock(_M_mutex);
203 _M_cond.wait(__my_lock);
208 template<typename _Lock, typename _Predicate>
210 wait(_Lock& __lock, _Predicate __p)
216 template<typename _Lock, typename _Clock, typename _Duration>
218 wait_until(_Lock& __lock,
219 const chrono::time_point<_Clock, _Duration>& __atime)
221 unique_lock<mutex> __my_lock(_M_mutex);
223 cv_status __status = _M_cond.wait_until(__my_lock, __atime);
228 template<typename _Lock, typename _Clock,
229 typename _Duration, typename _Predicate>
231 wait_until(_Lock& __lock,
232 const chrono::time_point<_Clock, _Duration>& __atime,
236 if (wait_until(__lock, __atime) == cv_status::timeout)
241 template<typename _Lock, typename _Rep, typename _Period>
243 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
244 { return wait_until(__lock, __clock_t::now() + __rtime); }
246 template<typename _Lock, typename _Rep,
247 typename _Period, typename _Predicate>
249 wait_for(_Lock& __lock,
250 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
251 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
255 { return _M_cond.native_handle(); }
258 // @} group condition_variables
259 _GLIBCXX_END_NAMESPACE_VERSION
262 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
264 #endif // __GXX_EXPERIMENTAL_CXX0X__
266 #endif // _GLIBCXX_CONDITION_VARIABLE