]>
Commit | Line | Data |
---|---|---|
68a97d24 BK |
1 | // <condition_variable> -*- C++ -*- |
2 | ||
b63d8901 | 3 | // Copyright (C) 2008, 2009, 2010, 2011, 2012 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 | 24 | |
f910786b | 25 | /** @file include/condition_variable |
68a97d24 BK |
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 | ||
734f5023 | 34 | #if __cplusplus < 201103L |
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 | ||
12ffa228 BK |
43 | namespace std _GLIBCXX_VISIBILITY(default) |
44 | { | |
45 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
53dc5044 | 46 | |
5b9daa7e BK |
47 | /** |
48 | * @defgroup condition_variables Condition Variables | |
49 | * @ingroup concurrency | |
50 | * | |
51 | * Classes for condition_variable support. | |
52 | * @{ | |
53 | */ | |
54 | ||
cdf5f5a3 PC |
55 | /// cv_status |
56 | enum class cv_status { no_timeout, timeout }; | |
57 | ||
68a97d24 BK |
58 | /// condition_variable |
59 | class condition_variable | |
60 | { | |
f7459b6c BK |
61 | typedef chrono::system_clock __clock_t; |
62 | typedef __gthread_cond_t __native_type; | |
b81e920e JW |
63 | |
64 | #ifdef __GTHREAD_COND_INIT | |
65 | __native_type _M_cond = __GTHREAD_COND_INIT; | |
66 | #else | |
f7459b6c | 67 | __native_type _M_cond; |
b81e920e | 68 | #endif |
88399079 | 69 | |
68a97d24 | 70 | public: |
f7459b6c | 71 | typedef __native_type* native_handle_type; |
68a97d24 | 72 | |
b81e920e JW |
73 | condition_variable() noexcept; |
74 | ~condition_variable() noexcept; | |
68a97d24 | 75 | |
7b800287 CF |
76 | condition_variable(const condition_variable&) = delete; |
77 | condition_variable& operator=(const condition_variable&) = delete; | |
78 | ||
d5cf2021 | 79 | void |
b81e920e | 80 | notify_one() noexcept; |
68a97d24 | 81 | |
d5cf2021 | 82 | void |
b81e920e | 83 | notify_all() noexcept; |
68a97d24 | 84 | |
d5cf2021 | 85 | void |
68a97d24 BK |
86 | wait(unique_lock<mutex>& __lock); |
87 | ||
88 | template<typename _Predicate> | |
d5cf2021 | 89 | void |
68a97d24 BK |
90 | wait(unique_lock<mutex>& __lock, _Predicate __p) |
91 | { | |
92 | while (!__p()) | |
93 | wait(__lock); | |
94 | } | |
d5cf2021 | 95 | |
88399079 | 96 | template<typename _Duration> |
cdf5f5a3 | 97 | cv_status |
88399079 CF |
98 | wait_until(unique_lock<mutex>& __lock, |
99 | const chrono::time_point<__clock_t, _Duration>& __atime) | |
100 | { return __wait_until_impl(__lock, __atime); } | |
101 | ||
102 | template<typename _Clock, typename _Duration> | |
cdf5f5a3 | 103 | cv_status |
88399079 | 104 | wait_until(unique_lock<mutex>& __lock, |
7b800287 CF |
105 | const chrono::time_point<_Clock, _Duration>& __atime) |
106 | { | |
88399079 | 107 | // DR 887 - Sync unknown clock to known clock. |
023cee96 PC |
108 | const typename _Clock::time_point __c_entry = _Clock::now(); |
109 | const __clock_t::time_point __s_entry = __clock_t::now(); | |
110 | const chrono::nanoseconds __delta = __atime - __c_entry; | |
111 | const __clock_t::time_point __s_atime = __s_entry + __delta; | |
7b800287 | 112 | |
88399079 | 113 | return __wait_until_impl(__lock, __s_atime); |
7b800287 | 114 | } |
d3098c94 CF |
115 | |
116 | template<typename _Clock, typename _Duration, typename _Predicate> | |
117 | bool | |
118 | wait_until(unique_lock<mutex>& __lock, | |
119 | const chrono::time_point<_Clock, _Duration>& __atime, | |
88399079 CF |
120 | _Predicate __p) |
121 | { | |
f7459b6c | 122 | while (!__p()) |
cdf5f5a3 | 123 | if (wait_until(__lock, __atime) == cv_status::timeout) |
88399079 | 124 | return __p(); |
88399079 CF |
125 | return true; |
126 | } | |
d3098c94 CF |
127 | |
128 | template<typename _Rep, typename _Period> | |
cdf5f5a3 | 129 | cv_status |
d3098c94 | 130 | wait_for(unique_lock<mutex>& __lock, |
7b800287 | 131 | const chrono::duration<_Rep, _Period>& __rtime) |
88399079 | 132 | { return wait_until(__lock, __clock_t::now() + __rtime); } |
d3098c94 CF |
133 | |
134 | template<typename _Rep, typename _Period, typename _Predicate> | |
135 | bool | |
136 | wait_for(unique_lock<mutex>& __lock, | |
137 | const chrono::duration<_Rep, _Period>& __rtime, | |
88399079 CF |
138 | _Predicate __p) |
139 | { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); } | |
68a97d24 | 140 | |
d5cf2021 BK |
141 | native_handle_type |
142 | native_handle() | |
7b800287 | 143 | { return &_M_cond; } |
68a97d24 BK |
144 | |
145 | private: | |
88399079 | 146 | template<typename _Clock, typename _Duration> |
cdf5f5a3 | 147 | cv_status |
88399079 CF |
148 | __wait_until_impl(unique_lock<mutex>& __lock, |
149 | const chrono::time_point<_Clock, _Duration>& __atime) | |
150 | { | |
151 | chrono::time_point<__clock_t, chrono::seconds> __s = | |
d5cf2021 BK |
152 | chrono::time_point_cast<chrono::seconds>(__atime); |
153 | ||
88399079 | 154 | chrono::nanoseconds __ns = |
d5cf2021 BK |
155 | chrono::duration_cast<chrono::nanoseconds>(__atime - __s); |
156 | ||
157 | __gthread_time_t __ts = | |
158 | { | |
159 | static_cast<std::time_t>(__s.time_since_epoch().count()), | |
160 | static_cast<long>(__ns.count()) | |
161 | }; | |
162 | ||
163 | __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(), | |
164 | &__ts); | |
165 | ||
cdf5f5a3 PC |
166 | return (_Clock::now() < __atime |
167 | ? cv_status::no_timeout : cv_status::timeout); | |
88399079 | 168 | } |
68a97d24 BK |
169 | }; |
170 | ||
171 | /// condition_variable_any | |
b7200e3f | 172 | // Like above, but mutex is not required to have try_lock. |
68a97d24 BK |
173 | class condition_variable_any |
174 | { | |
b7200e3f JW |
175 | typedef chrono::system_clock __clock_t; |
176 | condition_variable _M_cond; | |
177 | mutex _M_mutex; | |
f7459b6c | 178 | |
7f426c93 JW |
179 | // scoped unlock - unlocks in ctor, re-locks in dtor |
180 | template<typename _Lock> | |
181 | struct _Unlock | |
182 | { | |
183 | explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); } | |
184 | ||
185 | ~_Unlock() noexcept(false) | |
186 | { | |
187 | if (uncaught_exception()) | |
188 | __try { _M_lock.lock(); } __catch(...) { } | |
189 | else | |
190 | _M_lock.lock(); | |
191 | } | |
192 | ||
193 | _Unlock(const _Unlock&) = delete; | |
194 | _Unlock& operator=(const _Unlock&) = delete; | |
195 | ||
196 | _Lock& _M_lock; | |
197 | }; | |
198 | ||
68a97d24 | 199 | public: |
68a97d24 | 200 | |
b81e920e JW |
201 | condition_variable_any() noexcept; |
202 | ~condition_variable_any() noexcept; | |
d5cf2021 | 203 | |
7b800287 CF |
204 | condition_variable_any(const condition_variable_any&) = delete; |
205 | condition_variable_any& operator=(const condition_variable_any&) = delete; | |
68a97d24 | 206 | |
d5cf2021 | 207 | void |
b81e920e | 208 | notify_one() noexcept |
b7200e3f JW |
209 | { |
210 | lock_guard<mutex> __lock(_M_mutex); | |
211 | _M_cond.notify_one(); | |
212 | } | |
68a97d24 | 213 | |
d5cf2021 | 214 | void |
b81e920e | 215 | notify_all() noexcept |
b7200e3f JW |
216 | { |
217 | lock_guard<mutex> __lock(_M_mutex); | |
218 | _M_cond.notify_all(); | |
219 | } | |
68a97d24 BK |
220 | |
221 | template<typename _Lock> | |
d5cf2021 | 222 | void |
b7200e3f JW |
223 | wait(_Lock& __lock) |
224 | { | |
5d020aa2 | 225 | unique_lock<mutex> __my_lock(_M_mutex); |
7f426c93 | 226 | _Unlock<_Lock> __unlock(__lock); |
5d020aa2 JW |
227 | // _M_mutex must be unlocked before re-locking __lock so move |
228 | // ownership of _M_mutex lock to an object with shorter lifetime. | |
229 | unique_lock<mutex> __my_lock2(std::move(__my_lock)); | |
230 | _M_cond.wait(__my_lock2); | |
b7200e3f JW |
231 | } |
232 | ||
68a97d24 BK |
233 | |
234 | template<typename _Lock, typename _Predicate> | |
d5cf2021 | 235 | void |
cdf5f5a3 PC |
236 | wait(_Lock& __lock, _Predicate __p) |
237 | { | |
238 | while (!__p()) | |
239 | wait(__lock); | |
240 | } | |
68a97d24 | 241 | |
d3098c94 | 242 | template<typename _Lock, typename _Clock, typename _Duration> |
cdf5f5a3 | 243 | cv_status |
d3098c94 | 244 | wait_until(_Lock& __lock, |
b7200e3f JW |
245 | const chrono::time_point<_Clock, _Duration>& __atime) |
246 | { | |
7f426c93 JW |
247 | unique_lock<mutex> __my_lock(_M_mutex); |
248 | _Unlock<_Lock> __unlock(__lock); | |
249 | // _M_mutex must be unlocked before re-locking __lock so move | |
250 | // ownership of _M_mutex lock to an object with shorter lifetime. | |
251 | unique_lock<mutex> __my_lock2(std::move(__my_lock)); | |
252 | return _M_cond.wait_until(__my_lock2, __atime); | |
b7200e3f | 253 | } |
68a97d24 | 254 | |
d5cf2021 | 255 | template<typename _Lock, typename _Clock, |
d3098c94 | 256 | typename _Duration, typename _Predicate> |
d5cf2021 | 257 | bool |
d3098c94 CF |
258 | wait_until(_Lock& __lock, |
259 | const chrono::time_point<_Clock, _Duration>& __atime, | |
cdf5f5a3 PC |
260 | _Predicate __p) |
261 | { | |
262 | while (!__p()) | |
263 | if (wait_until(__lock, __atime) == cv_status::timeout) | |
264 | return __p(); | |
265 | return true; | |
266 | } | |
d5cf2021 | 267 | |
d3098c94 | 268 | template<typename _Lock, typename _Rep, typename _Period> |
cdf5f5a3 | 269 | cv_status |
b7200e3f JW |
270 | wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime) |
271 | { return wait_until(__lock, __clock_t::now() + __rtime); } | |
d3098c94 CF |
272 | |
273 | template<typename _Lock, typename _Rep, | |
274 | typename _Period, typename _Predicate> | |
275 | bool | |
d5cf2021 | 276 | wait_for(_Lock& __lock, |
b7200e3f JW |
277 | const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p) |
278 | { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); } | |
68a97d24 | 279 | }; |
5b9daa7e BK |
280 | |
281 | // @} group condition_variables | |
12ffa228 BK |
282 | _GLIBCXX_END_NAMESPACE_VERSION |
283 | } // namespace | |
68a97d24 | 284 | |
7b800287 CF |
285 | #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1 |
286 | ||
734f5023 | 287 | #endif // C++11 |
57317d2a PC |
288 | |
289 | #endif // _GLIBCXX_CONDITION_VARIABLE |