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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file condition_variable
31 * This is a Standard C++ Library header.
34 #ifndef _GLIBCXX_CONDITION_VARIABLE
35 #define _GLIBCXX_CONDITION_VARIABLE 1
37 #pragma GCC system_header
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
44 #include <mutex> // unique_lock
46 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
50 /// condition_variable
51 class condition_variable
53 typedef chrono::system_clock __clock_t;
54 typedef __gthread_cond_t __native_type;
55 __native_type _M_cond;
58 typedef __native_type* native_handle_type;
61 ~condition_variable();
63 condition_variable(const condition_variable&) = delete;
64 condition_variable& operator=(const condition_variable&) = delete;
73 wait(unique_lock<mutex>& __lock);
75 template<typename _Predicate>
77 wait(unique_lock<mutex>& __lock, _Predicate __p)
83 template<typename _Duration>
85 wait_until(unique_lock<mutex>& __lock,
86 const chrono::time_point<__clock_t, _Duration>& __atime)
87 { return __wait_until_impl(__lock, __atime); }
89 template<typename _Clock, typename _Duration>
91 wait_until(unique_lock<mutex>& __lock,
92 const chrono::time_point<_Clock, _Duration>& __atime)
94 // DR 887 - Sync unknown clock to known clock.
95 typename _Clock::time_point __c_entry = _Clock::now();
96 __clock_t::time_point __s_entry = __clock_t::now();
97 chrono::nanoseconds __delta = __atime - __c_entry;
98 __clock_t::time_point __s_atime = __s_entry + __delta;
100 return __wait_until_impl(__lock, __s_atime);
103 template<typename _Clock, typename _Duration, typename _Predicate>
105 wait_until(unique_lock<mutex>& __lock,
106 const chrono::time_point<_Clock, _Duration>& __atime,
110 if (!wait_until(__lock, __atime))
116 template<typename _Rep, typename _Period>
118 wait_for(unique_lock<mutex>& __lock,
119 const chrono::duration<_Rep, _Period>& __rtime)
120 { return wait_until(__lock, __clock_t::now() + __rtime); }
122 template<typename _Rep, typename _Period, typename _Predicate>
124 wait_for(unique_lock<mutex>& __lock,
125 const chrono::duration<_Rep, _Period>& __rtime,
127 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
134 template<typename _Clock, typename _Duration>
136 __wait_until_impl(unique_lock<mutex>& __lock,
137 const chrono::time_point<_Clock, _Duration>& __atime)
139 chrono::time_point<__clock_t, chrono::seconds> __s =
140 chrono::time_point_cast<chrono::seconds>(__atime);
142 chrono::nanoseconds __ns =
143 chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
145 __gthread_time_t __ts =
147 static_cast<std::time_t>(__s.time_since_epoch().count()),
148 static_cast<long>(__ns.count())
151 __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
154 return _Clock::now() < __atime;
158 /// condition_variable_any
159 // Like above, only mutex may not have try_lock.
160 class condition_variable_any
162 typedef __gthread_cond_t __native_type;
163 __native_type _M_cond;
166 typedef __native_type* native_handle_type;
168 condition_variable_any();
169 ~condition_variable_any();
171 condition_variable_any(const condition_variable_any&) = delete;
172 condition_variable_any& operator=(const condition_variable_any&) = delete;
180 template<typename _Lock>
184 template<typename _Lock, typename _Predicate>
186 wait(_Lock& __lock, _Predicate __p);
188 template<typename _Lock, typename _Clock, typename _Duration>
190 wait_until(_Lock& __lock,
191 const chrono::time_point<_Clock, _Duration>& __atime);
193 template<typename _Lock, typename _Clock,
194 typename _Duration, typename _Predicate>
196 wait_until(_Lock& __lock,
197 const chrono::time_point<_Clock, _Duration>& __atime,
200 template<typename _Lock, typename _Rep, typename _Period>
202 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime);
204 template<typename _Lock, typename _Rep,
205 typename _Period, typename _Predicate>
207 wait_for(_Lock& __lock,
208 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p);
216 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
218 #endif // __GXX_EXPERIMENTAL_CXX0X__
220 #endif // _GLIBCXX_CONDITION_VARIABLE