1 // <condition_variable> -*- C++ -*-
3 // Copyright (C) 2008, 2009 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 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 <c++0x_warning.h>
39 #include <mutex> // unique_lock
41 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
46 * @defgroup condition_variables Condition Variables
47 * @ingroup concurrency
49 * Classes for condition_variable support.
53 /// condition_variable
54 class condition_variable
56 typedef chrono::system_clock __clock_t;
57 typedef __gthread_cond_t __native_type;
58 __native_type _M_cond;
61 typedef __native_type* native_handle_type;
63 condition_variable() throw ();
64 ~condition_variable() throw ();
66 condition_variable(const condition_variable&) = delete;
67 condition_variable& operator=(const condition_variable&) = delete;
76 wait(unique_lock<mutex>& __lock);
78 template<typename _Predicate>
80 wait(unique_lock<mutex>& __lock, _Predicate __p)
86 template<typename _Duration>
88 wait_until(unique_lock<mutex>& __lock,
89 const chrono::time_point<__clock_t, _Duration>& __atime)
90 { return __wait_until_impl(__lock, __atime); }
92 template<typename _Clock, typename _Duration>
94 wait_until(unique_lock<mutex>& __lock,
95 const chrono::time_point<_Clock, _Duration>& __atime)
97 // DR 887 - Sync unknown clock to known clock.
98 typename _Clock::time_point __c_entry = _Clock::now();
99 __clock_t::time_point __s_entry = __clock_t::now();
100 chrono::nanoseconds __delta = __atime - __c_entry;
101 __clock_t::time_point __s_atime = __s_entry + __delta;
103 return __wait_until_impl(__lock, __s_atime);
106 template<typename _Clock, typename _Duration, typename _Predicate>
108 wait_until(unique_lock<mutex>& __lock,
109 const chrono::time_point<_Clock, _Duration>& __atime,
113 if (!wait_until(__lock, __atime))
119 template<typename _Rep, typename _Period>
121 wait_for(unique_lock<mutex>& __lock,
122 const chrono::duration<_Rep, _Period>& __rtime)
123 { return wait_until(__lock, __clock_t::now() + __rtime); }
125 template<typename _Rep, typename _Period, typename _Predicate>
127 wait_for(unique_lock<mutex>& __lock,
128 const chrono::duration<_Rep, _Period>& __rtime,
130 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
137 template<typename _Clock, typename _Duration>
139 __wait_until_impl(unique_lock<mutex>& __lock,
140 const chrono::time_point<_Clock, _Duration>& __atime)
142 chrono::time_point<__clock_t, chrono::seconds> __s =
143 chrono::time_point_cast<chrono::seconds>(__atime);
145 chrono::nanoseconds __ns =
146 chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
148 __gthread_time_t __ts =
150 static_cast<std::time_t>(__s.time_since_epoch().count()),
151 static_cast<long>(__ns.count())
154 __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
157 return _Clock::now() < __atime;
161 /// condition_variable_any
162 // Like above, only mutex may not have try_lock.
163 class condition_variable_any
165 typedef __gthread_cond_t __native_type;
166 __native_type _M_cond;
169 typedef __native_type* native_handle_type;
171 condition_variable_any() throw ();
172 ~condition_variable_any() throw ();
174 condition_variable_any(const condition_variable_any&) = delete;
175 condition_variable_any& operator=(const condition_variable_any&) = delete;
183 template<typename _Lock>
187 template<typename _Lock, typename _Predicate>
189 wait(_Lock& __lock, _Predicate __p);
191 template<typename _Lock, typename _Clock, typename _Duration>
193 wait_until(_Lock& __lock,
194 const chrono::time_point<_Clock, _Duration>& __atime);
196 template<typename _Lock, typename _Clock,
197 typename _Duration, typename _Predicate>
199 wait_until(_Lock& __lock,
200 const chrono::time_point<_Clock, _Duration>& __atime,
203 template<typename _Lock, typename _Rep, typename _Period>
205 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime);
207 template<typename _Lock, typename _Rep,
208 typename _Period, typename _Predicate>
210 wait_for(_Lock& __lock,
211 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p);
218 // @} group condition_variables
221 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
223 #endif // __GXX_EXPERIMENTAL_CXX0X__
225 #endif // _GLIBCXX_CONDITION_VARIABLE