]> gcc.gnu.org Git - gcc.git/blame - libstdc++-v3/include/std/condition_variable
syntax_option_type.cc: Avoid -Wall warnings.
[gcc.git] / libstdc++-v3 / include / std / condition_variable
CommitLineData
68a97d24
BK
1// <condition_variable> -*- C++ -*-
2
cdf5f5a3 3// Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
68a97d24
BK
4//
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
68a97d24
BK
9// any later version.
10
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.
15
748086b7
JJ
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.
68a97d24 19
748086b7
JJ
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/>.
68a97d24
BK
24
25/** @file condition_variable
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_CONDITION_VARIABLE
30#define _GLIBCXX_CONDITION_VARIABLE 1
31
32#pragma GCC system_header
33
34#ifndef __GXX_EXPERIMENTAL_CXX0X__
ab65a4c7 35# include <bits/c++0x_warning.h>
57317d2a 36#else
68a97d24 37
7b800287 38#include <chrono>
68a97d24
BK
39#include <mutex> // unique_lock
40
7b800287
CF
41#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
42
d5cf2021 43namespace std
68a97d24 44{
5b9daa7e
BK
45 /**
46 * @defgroup condition_variables Condition Variables
47 * @ingroup concurrency
48 *
49 * Classes for condition_variable support.
50 * @{
51 */
52
cdf5f5a3
PC
53 /// cv_status
54 enum class cv_status { no_timeout, timeout };
55
68a97d24
BK
56 /// condition_variable
57 class condition_variable
58 {
f7459b6c
BK
59 typedef chrono::system_clock __clock_t;
60 typedef __gthread_cond_t __native_type;
61 __native_type _M_cond;
88399079 62
68a97d24 63 public:
f7459b6c 64 typedef __native_type* native_handle_type;
68a97d24 65
50a681c4
JH
66 condition_variable() throw ();
67 ~condition_variable() throw ();
68a97d24 68
7b800287
CF
69 condition_variable(const condition_variable&) = delete;
70 condition_variable& operator=(const condition_variable&) = delete;
71
d5cf2021 72 void
68a97d24
BK
73 notify_one();
74
d5cf2021 75 void
68a97d24
BK
76 notify_all();
77
d5cf2021 78 void
68a97d24
BK
79 wait(unique_lock<mutex>& __lock);
80
81 template<typename _Predicate>
d5cf2021 82 void
68a97d24
BK
83 wait(unique_lock<mutex>& __lock, _Predicate __p)
84 {
85 while (!__p())
86 wait(__lock);
87 }
d5cf2021 88
88399079 89 template<typename _Duration>
cdf5f5a3 90 cv_status
88399079
CF
91 wait_until(unique_lock<mutex>& __lock,
92 const chrono::time_point<__clock_t, _Duration>& __atime)
93 { return __wait_until_impl(__lock, __atime); }
94
95 template<typename _Clock, typename _Duration>
cdf5f5a3 96 cv_status
88399079 97 wait_until(unique_lock<mutex>& __lock,
7b800287
CF
98 const chrono::time_point<_Clock, _Duration>& __atime)
99 {
88399079 100 // DR 887 - Sync unknown clock to known clock.
023cee96
PC
101 const typename _Clock::time_point __c_entry = _Clock::now();
102 const __clock_t::time_point __s_entry = __clock_t::now();
103 const chrono::nanoseconds __delta = __atime - __c_entry;
104 const __clock_t::time_point __s_atime = __s_entry + __delta;
7b800287 105
88399079 106 return __wait_until_impl(__lock, __s_atime);
7b800287 107 }
d3098c94
CF
108
109 template<typename _Clock, typename _Duration, typename _Predicate>
110 bool
111 wait_until(unique_lock<mutex>& __lock,
112 const chrono::time_point<_Clock, _Duration>& __atime,
88399079
CF
113 _Predicate __p)
114 {
f7459b6c 115 while (!__p())
cdf5f5a3 116 if (wait_until(__lock, __atime) == cv_status::timeout)
88399079 117 return __p();
88399079
CF
118 return true;
119 }
d3098c94
CF
120
121 template<typename _Rep, typename _Period>
cdf5f5a3 122 cv_status
d3098c94 123 wait_for(unique_lock<mutex>& __lock,
7b800287 124 const chrono::duration<_Rep, _Period>& __rtime)
88399079 125 { return wait_until(__lock, __clock_t::now() + __rtime); }
d3098c94
CF
126
127 template<typename _Rep, typename _Period, typename _Predicate>
128 bool
129 wait_for(unique_lock<mutex>& __lock,
130 const chrono::duration<_Rep, _Period>& __rtime,
88399079
CF
131 _Predicate __p)
132 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
68a97d24 133
d5cf2021
BK
134 native_handle_type
135 native_handle()
7b800287 136 { return &_M_cond; }
68a97d24
BK
137
138 private:
88399079 139 template<typename _Clock, typename _Duration>
cdf5f5a3 140 cv_status
88399079
CF
141 __wait_until_impl(unique_lock<mutex>& __lock,
142 const chrono::time_point<_Clock, _Duration>& __atime)
143 {
144 chrono::time_point<__clock_t, chrono::seconds> __s =
d5cf2021
BK
145 chrono::time_point_cast<chrono::seconds>(__atime);
146
88399079 147 chrono::nanoseconds __ns =
d5cf2021
BK
148 chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
149
150 __gthread_time_t __ts =
151 {
152 static_cast<std::time_t>(__s.time_since_epoch().count()),
153 static_cast<long>(__ns.count())
154 };
155
156 __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
157 &__ts);
158
cdf5f5a3
PC
159 return (_Clock::now() < __atime
160 ? cv_status::no_timeout : cv_status::timeout);
88399079 161 }
68a97d24
BK
162 };
163
164 /// condition_variable_any
b7200e3f 165 // Like above, but mutex is not required to have try_lock.
68a97d24
BK
166 class condition_variable_any
167 {
b7200e3f
JW
168 typedef chrono::system_clock __clock_t;
169 condition_variable _M_cond;
170 mutex _M_mutex;
f7459b6c 171
68a97d24 172 public:
b7200e3f 173 typedef condition_variable::native_handle_type native_handle_type;
68a97d24 174
50a681c4
JH
175 condition_variable_any() throw ();
176 ~condition_variable_any() throw ();
d5cf2021 177
7b800287
CF
178 condition_variable_any(const condition_variable_any&) = delete;
179 condition_variable_any& operator=(const condition_variable_any&) = delete;
68a97d24 180
d5cf2021 181 void
b7200e3f
JW
182 notify_one()
183 {
184 lock_guard<mutex> __lock(_M_mutex);
185 _M_cond.notify_one();
186 }
68a97d24 187
d5cf2021 188 void
b7200e3f
JW
189 notify_all()
190 {
191 lock_guard<mutex> __lock(_M_mutex);
192 _M_cond.notify_all();
193 }
68a97d24
BK
194
195 template<typename _Lock>
d5cf2021 196 void
b7200e3f
JW
197 wait(_Lock& __lock)
198 {
199 unique_lock<mutex> __my_lock(_M_mutex);
200 __lock.unlock();
201 _M_cond.wait(__my_lock);
202 __lock.lock();
203 }
204
68a97d24
BK
205
206 template<typename _Lock, typename _Predicate>
d5cf2021 207 void
cdf5f5a3
PC
208 wait(_Lock& __lock, _Predicate __p)
209 {
210 while (!__p())
211 wait(__lock);
212 }
68a97d24 213
d3098c94 214 template<typename _Lock, typename _Clock, typename _Duration>
cdf5f5a3 215 cv_status
d3098c94 216 wait_until(_Lock& __lock,
b7200e3f
JW
217 const chrono::time_point<_Clock, _Duration>& __atime)
218 {
219 unique_lock<mutex> __my_lock(_M_mutex);
220 __lock.unlock();
221 cv_status __status = _M_cond.wait_until(__my_lock, __atime);
222 __lock.lock();
223 return __status;
224 }
68a97d24 225
d5cf2021 226 template<typename _Lock, typename _Clock,
d3098c94 227 typename _Duration, typename _Predicate>
d5cf2021 228 bool
d3098c94
CF
229 wait_until(_Lock& __lock,
230 const chrono::time_point<_Clock, _Duration>& __atime,
cdf5f5a3
PC
231 _Predicate __p)
232 {
233 while (!__p())
234 if (wait_until(__lock, __atime) == cv_status::timeout)
235 return __p();
236 return true;
237 }
d5cf2021 238
d3098c94 239 template<typename _Lock, typename _Rep, typename _Period>
cdf5f5a3 240 cv_status
b7200e3f
JW
241 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
242 { return wait_until(__lock, __clock_t::now() + __rtime); }
d3098c94
CF
243
244 template<typename _Lock, typename _Rep,
245 typename _Period, typename _Predicate>
246 bool
d5cf2021 247 wait_for(_Lock& __lock,
b7200e3f
JW
248 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
249 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
68a97d24 250
d5cf2021 251 native_handle_type
7b800287 252 native_handle()
b7200e3f 253 { return _M_cond.native_handle(); }
68a97d24 254 };
5b9daa7e
BK
255
256 // @} group condition_variables
68a97d24
BK
257}
258
7b800287
CF
259#endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
260
57317d2a
PC
261#endif // __GXX_EXPERIMENTAL_CXX0X__
262
263#endif // _GLIBCXX_CONDITION_VARIABLE
This page took 0.289651 seconds and 5 git commands to generate.