RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / toolchains / hndtools-arm-linux-2.6.36-uclibc-4.5.3 / arm-brcm-linux-uclibcgnueabi / include / c++ / 4.5.3 / condition_variable
blobc5ce207a9050b1ea120c38b7bd00fe71883729d9
1 // <condition_variable> -*- C++ -*-
3 // Copyright (C) 2008, 2009, 2010 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 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 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
36 #else
38 #include <chrono>
39 #include <mutex> // unique_lock
41 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
43 namespace std
45   /**
46    * @defgroup condition_variables Condition Variables
47    * @ingroup concurrency
48    *
49    * Classes for condition_variable support.
50    * @{
51    */
53   /// cv_status
54   enum class cv_status { no_timeout, timeout };
55   
56   /// condition_variable
57   class condition_variable
58   {
59     typedef chrono::system_clock        __clock_t;
60     typedef __gthread_cond_t            __native_type;
61     __native_type                       _M_cond;
63   public:
64     typedef __native_type*              native_handle_type;
66     condition_variable() throw ();
67     ~condition_variable() throw ();
69     condition_variable(const condition_variable&) = delete;
70     condition_variable& operator=(const condition_variable&) = delete;
72     void
73     notify_one();
75     void
76     notify_all();
78     void
79     wait(unique_lock<mutex>& __lock);
81     template<typename _Predicate>
82       void
83       wait(unique_lock<mutex>& __lock, _Predicate __p)
84       {
85         while (!__p())
86           wait(__lock);
87       }
89     template<typename _Duration>
90       cv_status
91       wait_until(unique_lock<mutex>& __lock,
92                  const chrono::time_point<__clock_t, _Duration>& __atime)
93       { return __wait_until_impl(__lock, __atime); }
95     template<typename _Clock, typename _Duration>
96       cv_status
97       wait_until(unique_lock<mutex>& __lock,
98                  const chrono::time_point<_Clock, _Duration>& __atime)
99       {
100         // DR 887 - Sync unknown clock to known clock.
101         const typename _Clock::time_point __c_entry = _Clock::now();
102         const __clock_t::time_point __s_entry = __clock_t::now();
103         const chrono::nanoseconds __delta = __atime - __c_entry;
104         const __clock_t::time_point __s_atime = __s_entry + __delta;
106         return __wait_until_impl(__lock, __s_atime);
107       }
109     template<typename _Clock, typename _Duration, typename _Predicate>
110       bool
111       wait_until(unique_lock<mutex>& __lock,
112                  const chrono::time_point<_Clock, _Duration>& __atime,
113                  _Predicate __p)
114       {
115         while (!__p())
116           if (wait_until(__lock, __atime) == cv_status::timeout)
117             return __p();
118         return true;
119       }
121     template<typename _Rep, typename _Period>
122       cv_status
123       wait_for(unique_lock<mutex>& __lock,
124                const chrono::duration<_Rep, _Period>& __rtime)
125       { return wait_until(__lock, __clock_t::now() + __rtime); }
127     template<typename _Rep, typename _Period, typename _Predicate>
128       bool
129       wait_for(unique_lock<mutex>& __lock,
130                const chrono::duration<_Rep, _Period>& __rtime,
131                _Predicate __p)
132       { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
134     native_handle_type
135     native_handle()
136     { return &_M_cond; }
138   private:
139     template<typename _Clock, typename _Duration>
140       cv_status
141       __wait_until_impl(unique_lock<mutex>& __lock,
142                         const chrono::time_point<_Clock, _Duration>& __atime)
143       {
144         chrono::time_point<__clock_t, chrono::seconds> __s =
145           chrono::time_point_cast<chrono::seconds>(__atime);
147         chrono::nanoseconds __ns =
148           chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
150         __gthread_time_t __ts =
151           {
152             static_cast<std::time_t>(__s.time_since_epoch().count()),
153             static_cast<long>(__ns.count())
154           };
156         __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
157                                  &__ts);
159         return (_Clock::now() < __atime
160                 ? cv_status::no_timeout : cv_status::timeout);
161       }
162   };
164   /// condition_variable_any
165   // Like above, but mutex is not required to have try_lock.
166   class condition_variable_any
167   {
168     typedef chrono::system_clock        __clock_t;
169     condition_variable                  _M_cond;
170     mutex                               _M_mutex;
172   public:
173     typedef condition_variable::native_handle_type      native_handle_type;
175     condition_variable_any() throw ();
176     ~condition_variable_any() throw ();
178     condition_variable_any(const condition_variable_any&) = delete;
179     condition_variable_any& operator=(const condition_variable_any&) = delete;
181     void
182     notify_one()
183     {
184       lock_guard<mutex> __lock(_M_mutex);
185       _M_cond.notify_one();
186     }
188     void
189     notify_all()
190     {
191       lock_guard<mutex> __lock(_M_mutex);
192       _M_cond.notify_all();
193     }
195     template<typename _Lock>
196       void
197       wait(_Lock& __lock)
198       {
199         unique_lock<mutex> __my_lock(_M_mutex);
200         __lock.unlock();
201         _M_cond.wait(__my_lock);
202         __lock.lock();
203       }
204       
206     template<typename _Lock, typename _Predicate>
207       void
208       wait(_Lock& __lock, _Predicate __p)
209       {
210         while (!__p())
211           wait(__lock);
212       }
214     template<typename _Lock, typename _Clock, typename _Duration>
215       cv_status
216       wait_until(_Lock& __lock,
217                  const chrono::time_point<_Clock, _Duration>& __atime)
218       {
219         unique_lock<mutex> __my_lock(_M_mutex);
220         __lock.unlock();
221         cv_status __status = _M_cond.wait_until(__my_lock, __atime);
222         __lock.lock();
223         return __status;
224       }
226     template<typename _Lock, typename _Clock,
227              typename _Duration, typename _Predicate>
228       bool
229       wait_until(_Lock& __lock,
230                  const chrono::time_point<_Clock, _Duration>& __atime,
231                  _Predicate __p)
232       {
233         while (!__p())
234           if (wait_until(__lock, __atime) == cv_status::timeout)
235             return __p();
236         return true;
237       }
239     template<typename _Lock, typename _Rep, typename _Period>
240       cv_status
241       wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
242       { return wait_until(__lock, __clock_t::now() + __rtime); }
244     template<typename _Lock, typename _Rep,
245              typename _Period, typename _Predicate>
246       bool
247       wait_for(_Lock& __lock,
248                const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
249       { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
251     native_handle_type
252     native_handle()
253     { return _M_cond.native_handle(); }
254   };
256   // @} group condition_variables
259 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
261 #endif // __GXX_EXPERIMENTAL_CXX0X__
263 #endif // _GLIBCXX_CONDITION_VARIABLE