1 // <condition_variable> -*- C++ -*-
3 // Copyright (C) 2008-2017 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 <ext/concurrence.h>
41 #include <bits/alloc_traits.h>
42 #include <bits/allocator.h>
43 #include <bits/unique_ptr.h>
44 #include <bits/shared_ptr.h>
45 #include <bits/cxxabi_forced.h>
47 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
49 namespace std _GLIBCXX_VISIBILITY(default)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
54 * @defgroup condition_variables Condition Variables
55 * @ingroup concurrency
57 * Classes for condition_variable support.
62 enum class cv_status { no_timeout, timeout };
64 /// condition_variable
65 class condition_variable
67 typedef chrono::system_clock __clock_t;
68 typedef __gthread_cond_t __native_type;
70 #ifdef __GTHREAD_COND_INIT
71 __native_type _M_cond = __GTHREAD_COND_INIT;
73 __native_type _M_cond;
77 typedef __native_type* native_handle_type;
79 condition_variable() noexcept;
80 ~condition_variable() noexcept;
82 condition_variable(const condition_variable&) = delete;
83 condition_variable& operator=(const condition_variable&) = delete;
86 notify_one() noexcept;
89 notify_all() noexcept;
92 wait(unique_lock<mutex>& __lock) noexcept;
94 template<typename _Predicate>
96 wait(unique_lock<mutex>& __lock, _Predicate __p)
102 template<typename _Duration>
104 wait_until(unique_lock<mutex>& __lock,
105 const chrono::time_point<__clock_t, _Duration>& __atime)
106 { return __wait_until_impl(__lock, __atime); }
108 template<typename _Clock, typename _Duration>
110 wait_until(unique_lock<mutex>& __lock,
111 const chrono::time_point<_Clock, _Duration>& __atime)
113 // DR 887 - Sync unknown clock to known clock.
114 const typename _Clock::time_point __c_entry = _Clock::now();
115 const __clock_t::time_point __s_entry = __clock_t::now();
116 const auto __delta = __atime - __c_entry;
117 const auto __s_atime = __s_entry + __delta;
119 return __wait_until_impl(__lock, __s_atime);
122 template<typename _Clock, typename _Duration, typename _Predicate>
124 wait_until(unique_lock<mutex>& __lock,
125 const chrono::time_point<_Clock, _Duration>& __atime,
129 if (wait_until(__lock, __atime) == cv_status::timeout)
134 template<typename _Rep, typename _Period>
136 wait_for(unique_lock<mutex>& __lock,
137 const chrono::duration<_Rep, _Period>& __rtime)
138 { return wait_until(__lock, __clock_t::now() + __rtime); }
140 template<typename _Rep, typename _Period, typename _Predicate>
142 wait_for(unique_lock<mutex>& __lock,
143 const chrono::duration<_Rep, _Period>& __rtime,
145 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
152 template<typename _Dur>
154 __wait_until_impl(unique_lock<mutex>& __lock,
155 const chrono::time_point<__clock_t, _Dur>& __atime)
157 auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
158 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
160 __gthread_time_t __ts =
162 static_cast<std::time_t>(__s.time_since_epoch().count()),
163 static_cast<long>(__ns.count())
166 __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
169 return (__clock_t::now() < __atime
170 ? cv_status::no_timeout : cv_status::timeout);
175 notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
177 struct __at_thread_exit_elt
179 __at_thread_exit_elt* _M_next;
180 void (*_M_cb)(void*);
183 inline namespace _V2 {
185 /// condition_variable_any
186 // Like above, but mutex is not required to have try_lock.
187 class condition_variable_any
189 typedef chrono::system_clock __clock_t;
190 condition_variable _M_cond;
191 shared_ptr<mutex> _M_mutex;
193 // scoped unlock - unlocks in ctor, re-locks in dtor
194 template<typename _Lock>
197 explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
199 ~_Unlock() noexcept(false)
201 if (uncaught_exception())
205 __catch(const __cxxabiv1::__forced_unwind&)
206 { __throw_exception_again; }
214 _Unlock(const _Unlock&) = delete;
215 _Unlock& operator=(const _Unlock&) = delete;
221 condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { }
222 ~condition_variable_any() = default;
224 condition_variable_any(const condition_variable_any&) = delete;
225 condition_variable_any& operator=(const condition_variable_any&) = delete;
228 notify_one() noexcept
230 lock_guard<mutex> __lock(*_M_mutex);
231 _M_cond.notify_one();
235 notify_all() noexcept
237 lock_guard<mutex> __lock(*_M_mutex);
238 _M_cond.notify_all();
241 template<typename _Lock>
245 shared_ptr<mutex> __mutex = _M_mutex;
246 unique_lock<mutex> __my_lock(*__mutex);
247 _Unlock<_Lock> __unlock(__lock);
248 // *__mutex must be unlocked before re-locking __lock so move
249 // ownership of *__mutex lock to an object with shorter lifetime.
250 unique_lock<mutex> __my_lock2(std::move(__my_lock));
251 _M_cond.wait(__my_lock2);
255 template<typename _Lock, typename _Predicate>
257 wait(_Lock& __lock, _Predicate __p)
263 template<typename _Lock, typename _Clock, typename _Duration>
265 wait_until(_Lock& __lock,
266 const chrono::time_point<_Clock, _Duration>& __atime)
268 shared_ptr<mutex> __mutex = _M_mutex;
269 unique_lock<mutex> __my_lock(*__mutex);
270 _Unlock<_Lock> __unlock(__lock);
271 // *__mutex must be unlocked before re-locking __lock so move
272 // ownership of *__mutex lock to an object with shorter lifetime.
273 unique_lock<mutex> __my_lock2(std::move(__my_lock));
274 return _M_cond.wait_until(__my_lock2, __atime);
277 template<typename _Lock, typename _Clock,
278 typename _Duration, typename _Predicate>
280 wait_until(_Lock& __lock,
281 const chrono::time_point<_Clock, _Duration>& __atime,
285 if (wait_until(__lock, __atime) == cv_status::timeout)
290 template<typename _Lock, typename _Rep, typename _Period>
292 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
293 { return wait_until(__lock, __clock_t::now() + __rtime); }
295 template<typename _Lock, typename _Rep,
296 typename _Period, typename _Predicate>
298 wait_for(_Lock& __lock,
299 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
300 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
303 } // end inline namespace
305 // @} group condition_variables
306 _GLIBCXX_END_NAMESPACE_VERSION
309 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
313 #endif // _GLIBCXX_CONDITION_VARIABLE