Report early wakeup of condition_variable::wait_until as no_timeout
[official-gcc.git] / libstdc++-v3 / include / std / condition_variable
blobc00afa2b7ae2169f7ba81ade07544381a7ec050a
1 // <condition_variable> -*- C++ -*-
3 // Copyright (C) 2008-2018 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
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.
27  */
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>
36 #else
38 #include <chrono>
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
54   /**
55    * @defgroup condition_variables Condition Variables
56    * @ingroup concurrency
57    *
58    * Classes for condition_variable support.
59    * @{
60    */
62   /// cv_status
63   enum class cv_status { no_timeout, timeout };
65   /// condition_variable
66   class condition_variable
67   {
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;
73 #else
74     __native_type                       _M_cond;
75 #endif
77   public:
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;
86     void
87     notify_one() noexcept;
89     void
90     notify_all() noexcept;
92     void
93     wait(unique_lock<mutex>& __lock) noexcept;
95     template<typename _Predicate>
96       void
97       wait(unique_lock<mutex>& __lock, _Predicate __p)
98       {
99         while (!__p())
100           wait(__lock);
101       }
103     template<typename _Duration>
104       cv_status
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>
110       cv_status
111       wait_until(unique_lock<mutex>& __lock,
112                  const chrono::time_point<_Clock, _Duration>& __atime)
113       {
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;
128       }
130     template<typename _Clock, typename _Duration, typename _Predicate>
131       bool
132       wait_until(unique_lock<mutex>& __lock,
133                  const chrono::time_point<_Clock, _Duration>& __atime,
134                  _Predicate __p)
135       {
136         while (!__p())
137           if (wait_until(__lock, __atime) == cv_status::timeout)
138             return __p();
139         return true;
140       }
142     template<typename _Rep, typename _Period>
143       cv_status
144       wait_for(unique_lock<mutex>& __lock,
145                const chrono::duration<_Rep, _Period>& __rtime)
146       {
147         using __dur = typename __clock_t::duration;
148         auto __reltime = chrono::duration_cast<__dur>(__rtime);
149         if (__reltime < __rtime)
150           ++__reltime;
151         return wait_until(__lock, __clock_t::now() + __reltime);
152       }
154     template<typename _Rep, typename _Period, typename _Predicate>
155       bool
156       wait_for(unique_lock<mutex>& __lock,
157                const chrono::duration<_Rep, _Period>& __rtime,
158                _Predicate __p)
159       {
160         using __dur = typename __clock_t::duration;
161         auto __reltime = chrono::duration_cast<__dur>(__rtime);
162         if (__reltime < __rtime)
163           ++__reltime;
164         return wait_until(__lock, __clock_t::now() + __reltime, std::move(__p));
165       }
167     native_handle_type
168     native_handle()
169     { return &_M_cond; }
171   private:
172     template<typename _Dur>
173       cv_status
174       __wait_until_impl(unique_lock<mutex>& __lock,
175                         const chrono::time_point<__clock_t, _Dur>& __atime)
176       {
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 =
181           {
182             static_cast<std::time_t>(__s.time_since_epoch().count()),
183             static_cast<long>(__ns.count())
184           };
186         __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
187                                  &__ts);
189         return (__clock_t::now() < __atime
190                 ? cv_status::no_timeout : cv_status::timeout);
191       }
192   };
194   void
195   notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
197   struct __at_thread_exit_elt
198   {
199     __at_thread_exit_elt* _M_next;
200     void (*_M_cb)(void*);
201   };
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
208   {
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>
215       struct _Unlock
216       {
217         explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
219         ~_Unlock() noexcept(false)
220         {
221           if (uncaught_exception())
222             {
223               __try
224               { _M_lock.lock(); }
225               __catch(const __cxxabiv1::__forced_unwind&)
226               { __throw_exception_again; }
227               __catch(...)
228               { }
229             }
230           else
231             _M_lock.lock();
232         }
234         _Unlock(const _Unlock&) = delete;
235         _Unlock& operator=(const _Unlock&) = delete;
237         _Lock& _M_lock;
238       };
240   public:
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;
247     void
248     notify_one() noexcept
249     {
250       lock_guard<mutex> __lock(*_M_mutex);
251       _M_cond.notify_one();
252     }
254     void
255     notify_all() noexcept
256     {
257       lock_guard<mutex> __lock(*_M_mutex);
258       _M_cond.notify_all();
259     }
261     template<typename _Lock>
262       void
263       wait(_Lock& __lock)
264       {
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);
272       }
275     template<typename _Lock, typename _Predicate>
276       void
277       wait(_Lock& __lock, _Predicate __p)
278       {
279         while (!__p())
280           wait(__lock);
281       }
283     template<typename _Lock, typename _Clock, typename _Duration>
284       cv_status
285       wait_until(_Lock& __lock,
286                  const chrono::time_point<_Clock, _Duration>& __atime)
287       {
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);
295       }
297     template<typename _Lock, typename _Clock,
298              typename _Duration, typename _Predicate>
299       bool
300       wait_until(_Lock& __lock,
301                  const chrono::time_point<_Clock, _Duration>& __atime,
302                  _Predicate __p)
303       {
304         while (!__p())
305           if (wait_until(__lock, __atime) == cv_status::timeout)
306             return __p();
307         return true;
308       }
310     template<typename _Lock, typename _Rep, typename _Period>
311       cv_status
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>
317       bool
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)); }
321   };
323   } // end inline namespace
325   // @} group condition_variables
326 _GLIBCXX_END_NAMESPACE_VERSION
327 } // namespace
329 #endif // _GLIBCXX_HAS_GTHREADS
330 #endif // C++11
331 #endif // _GLIBCXX_CONDITION_VARIABLE