Clean up libstdc++ includes slightly.
[official-gcc.git] / libstdc++-v3 / include / std / condition_variable
blobfbed043f3c743b2062e14edbc45fecae67570f3d
1 // <condition_variable> -*- C++ -*-
3 // Copyright (C) 2008-2015 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 <mutex>
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
53   /**
54    * @defgroup condition_variables Condition Variables
55    * @ingroup concurrency
56    *
57    * Classes for condition_variable support.
58    * @{
59    */
61   /// cv_status
62   enum class cv_status { no_timeout, timeout };
63   
64   /// condition_variable
65   class condition_variable
66   {
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;
72 #else
73     __native_type                       _M_cond;
74 #endif
76   public:
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;
85     void
86     notify_one() noexcept;
88     void
89     notify_all() noexcept;
91     void
92     wait(unique_lock<mutex>& __lock);
94     template<typename _Predicate>
95       void
96       wait(unique_lock<mutex>& __lock, _Predicate __p)
97       {
98         while (!__p())
99           wait(__lock);
100       }
102     template<typename _Duration>
103       cv_status
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>
109       cv_status
110       wait_until(unique_lock<mutex>& __lock,
111                  const chrono::time_point<_Clock, _Duration>& __atime)
112       {
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);
120       }
122     template<typename _Clock, typename _Duration, typename _Predicate>
123       bool
124       wait_until(unique_lock<mutex>& __lock,
125                  const chrono::time_point<_Clock, _Duration>& __atime,
126                  _Predicate __p)
127       {
128         while (!__p())
129           if (wait_until(__lock, __atime) == cv_status::timeout)
130             return __p();
131         return true;
132       }
134     template<typename _Rep, typename _Period>
135       cv_status
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>
141       bool
142       wait_for(unique_lock<mutex>& __lock,
143                const chrono::duration<_Rep, _Period>& __rtime,
144                _Predicate __p)
145       { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
147     native_handle_type
148     native_handle()
149     { return &_M_cond; }
151   private:
152     template<typename _Dur>
153       cv_status
154       __wait_until_impl(unique_lock<mutex>& __lock,
155                         const chrono::time_point<__clock_t, _Dur>& __atime)
156       {
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 =
161           {
162             static_cast<std::time_t>(__s.time_since_epoch().count()),
163             static_cast<long>(__ns.count())
164           };
166         __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
167                                  &__ts);
169         return (__clock_t::now() < __atime
170                 ? cv_status::no_timeout : cv_status::timeout);
171       }
172   };
174   void
175   notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
177   struct __at_thread_exit_elt
178   {
179     __at_thread_exit_elt* _M_next;
180     void (*_M_cb)(void*);
181   };
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
188   {
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>
195       struct _Unlock
196       {
197         explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
199         ~_Unlock() noexcept(false)
200         {
201           if (uncaught_exception())
202             {
203               __try
204               { _M_lock.lock(); }
205               __catch(const __cxxabiv1::__forced_unwind&)
206               { __throw_exception_again; }
207               __catch(...)
208               { }
209             }
210           else
211             _M_lock.lock();
212         }
214         _Unlock(const _Unlock&) = delete;
215         _Unlock& operator=(const _Unlock&) = delete;
217         _Lock& _M_lock;
218       };
220   public:
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;
227     void
228     notify_one() noexcept
229     {
230       lock_guard<mutex> __lock(*_M_mutex);
231       _M_cond.notify_one();
232     }
234     void
235     notify_all() noexcept
236     {
237       lock_guard<mutex> __lock(*_M_mutex);
238       _M_cond.notify_all();
239     }
241     template<typename _Lock>
242       void
243       wait(_Lock& __lock)
244       {
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);
252       }
253       
255     template<typename _Lock, typename _Predicate>
256       void
257       wait(_Lock& __lock, _Predicate __p)
258       {
259         while (!__p())
260           wait(__lock);
261       }
263     template<typename _Lock, typename _Clock, typename _Duration>
264       cv_status
265       wait_until(_Lock& __lock,
266                  const chrono::time_point<_Clock, _Duration>& __atime)
267       {
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);
275       }
277     template<typename _Lock, typename _Clock,
278              typename _Duration, typename _Predicate>
279       bool
280       wait_until(_Lock& __lock,
281                  const chrono::time_point<_Clock, _Duration>& __atime,
282                  _Predicate __p)
283       {
284         while (!__p())
285           if (wait_until(__lock, __atime) == cv_status::timeout)
286             return __p();
287         return true;
288       }
290     template<typename _Lock, typename _Rep, typename _Period>
291       cv_status
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>
297       bool
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)); }
301   };
303   } // end inline namespace
305   // @} group condition_variables
306 _GLIBCXX_END_NAMESPACE_VERSION
307 } // namespace
309 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
311 #endif // C++11
313 #endif // _GLIBCXX_CONDITION_VARIABLE