1 // <condition_variable> -*- C++ -*-
3 // Copyright (C) 2008-2018 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 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
39 #include <bits/std_mutex.h>
40 #include <bits/unique_lock.h>
41 #include <ext/concurrence.h>
42 #include <bits/alloc_traits.h>
43 #include <bits/allocator.h>
44 #include <bits/unique_ptr.h>
45 #include <bits/shared_ptr.h>
46 #include <bits/cxxabi_forced.h>
48 #if defined(_GLIBCXX_HAS_GTHREADS)
50 namespace std _GLIBCXX_VISIBILITY(default)
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 * @defgroup condition_variables Condition Variables
56 * @ingroup concurrency
58 * Classes for condition_variable support.
63 enum class cv_status { no_timeout, timeout };
65 /// condition_variable
66 class condition_variable
68 typedef chrono::system_clock __clock_t;
69 typedef __gthread_cond_t __native_type;
71 #ifdef __GTHREAD_COND_INIT
72 __native_type _M_cond = __GTHREAD_COND_INIT;
74 __native_type _M_cond;
78 typedef __native_type* native_handle_type;
80 condition_variable() noexcept;
81 ~condition_variable() noexcept;
83 condition_variable(const condition_variable&) = delete;
84 condition_variable& operator=(const condition_variable&) = delete;
87 notify_one() noexcept;
90 notify_all() noexcept;
93 wait(unique_lock<mutex>& __lock) noexcept;
95 template<typename _Predicate>
97 wait(unique_lock<mutex>& __lock, _Predicate __p)
103 template<typename _Duration>
105 wait_until(unique_lock<mutex>& __lock,
106 const chrono::time_point<__clock_t, _Duration>& __atime)
107 { return __wait_until_impl(__lock, __atime); }
109 template<typename _Clock, typename _Duration>
111 wait_until(unique_lock<mutex>& __lock,
112 const chrono::time_point<_Clock, _Duration>& __atime)
114 // DR 887 - Sync unknown clock to known clock.
115 const typename _Clock::time_point __c_entry = _Clock::now();
116 const __clock_t::time_point __s_entry = __clock_t::now();
117 const auto __delta = __atime - __c_entry;
118 const auto __s_atime = __s_entry + __delta;
120 if (__wait_until_impl(__lock, __s_atime) == cv_status::no_timeout)
121 return cv_status::no_timeout;
122 // We got a timeout when measured against __clock_t but
123 // we need to check against the caller-supplied clock
124 // to tell whether we should return a timeout.
125 if (_Clock::now() < __atime)
126 return cv_status::no_timeout;
127 return cv_status::timeout;
130 template<typename _Clock, typename _Duration, typename _Predicate>
132 wait_until(unique_lock<mutex>& __lock,
133 const chrono::time_point<_Clock, _Duration>& __atime,
137 if (wait_until(__lock, __atime) == cv_status::timeout)
142 template<typename _Rep, typename _Period>
144 wait_for(unique_lock<mutex>& __lock,
145 const chrono::duration<_Rep, _Period>& __rtime)
147 using __dur = typename __clock_t::duration;
148 auto __reltime = chrono::duration_cast<__dur>(__rtime);
149 if (__reltime < __rtime)
151 return wait_until(__lock, __clock_t::now() + __reltime);
154 template<typename _Rep, typename _Period, typename _Predicate>
156 wait_for(unique_lock<mutex>& __lock,
157 const chrono::duration<_Rep, _Period>& __rtime,
160 using __dur = typename __clock_t::duration;
161 auto __reltime = chrono::duration_cast<__dur>(__rtime);
162 if (__reltime < __rtime)
164 return wait_until(__lock, __clock_t::now() + __reltime, std::move(__p));
172 template<typename _Dur>
174 __wait_until_impl(unique_lock<mutex>& __lock,
175 const chrono::time_point<__clock_t, _Dur>& __atime)
177 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
178 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
180 __gthread_time_t __ts =
182 static_cast<std::time_t>(__s.time_since_epoch().count()),
183 static_cast<long>(__ns.count())
186 __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
189 return (__clock_t::now() < __atime
190 ? cv_status::no_timeout : cv_status::timeout);
195 notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
197 struct __at_thread_exit_elt
199 __at_thread_exit_elt* _M_next;
200 void (*_M_cb)(void*);
203 inline namespace _V2 {
205 /// condition_variable_any
206 // Like above, but mutex is not required to have try_lock.
207 class condition_variable_any
209 typedef chrono::system_clock __clock_t;
210 condition_variable _M_cond;
211 shared_ptr<mutex> _M_mutex;
213 // scoped unlock - unlocks in ctor, re-locks in dtor
214 template<typename _Lock>
217 explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
219 ~_Unlock() noexcept(false)
221 if (uncaught_exception())
225 __catch(const __cxxabiv1::__forced_unwind&)
226 { __throw_exception_again; }
234 _Unlock(const _Unlock&) = delete;
235 _Unlock& operator=(const _Unlock&) = delete;
241 condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { }
242 ~condition_variable_any() = default;
244 condition_variable_any(const condition_variable_any&) = delete;
245 condition_variable_any& operator=(const condition_variable_any&) = delete;
248 notify_one() noexcept
250 lock_guard<mutex> __lock(*_M_mutex);
251 _M_cond.notify_one();
255 notify_all() noexcept
257 lock_guard<mutex> __lock(*_M_mutex);
258 _M_cond.notify_all();
261 template<typename _Lock>
265 shared_ptr<mutex> __mutex = _M_mutex;
266 unique_lock<mutex> __my_lock(*__mutex);
267 _Unlock<_Lock> __unlock(__lock);
268 // *__mutex must be unlocked before re-locking __lock so move
269 // ownership of *__mutex lock to an object with shorter lifetime.
270 unique_lock<mutex> __my_lock2(std::move(__my_lock));
271 _M_cond.wait(__my_lock2);
275 template<typename _Lock, typename _Predicate>
277 wait(_Lock& __lock, _Predicate __p)
283 template<typename _Lock, typename _Clock, typename _Duration>
285 wait_until(_Lock& __lock,
286 const chrono::time_point<_Clock, _Duration>& __atime)
288 shared_ptr<mutex> __mutex = _M_mutex;
289 unique_lock<mutex> __my_lock(*__mutex);
290 _Unlock<_Lock> __unlock(__lock);
291 // *__mutex must be unlocked before re-locking __lock so move
292 // ownership of *__mutex lock to an object with shorter lifetime.
293 unique_lock<mutex> __my_lock2(std::move(__my_lock));
294 return _M_cond.wait_until(__my_lock2, __atime);
297 template<typename _Lock, typename _Clock,
298 typename _Duration, typename _Predicate>
300 wait_until(_Lock& __lock,
301 const chrono::time_point<_Clock, _Duration>& __atime,
305 if (wait_until(__lock, __atime) == cv_status::timeout)
310 template<typename _Lock, typename _Rep, typename _Period>
312 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
313 { return wait_until(__lock, __clock_t::now() + __rtime); }
315 template<typename _Lock, typename _Rep,
316 typename _Period, typename _Predicate>
318 wait_for(_Lock& __lock,
319 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
320 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
323 } // end inline namespace
325 // @} group condition_variables
326 _GLIBCXX_END_NAMESPACE_VERSION
329 #endif // _GLIBCXX_HAS_GTHREADS
331 #endif // _GLIBCXX_CONDITION_VARIABLE