OSDN Git Service

2011-01-30 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / condition_variable
1 // <condition_variable> -*- C++ -*-
2
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.
10
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.
15
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.
19
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/>.
24
25 /** @file include/condition_variable
26  *  This is a Standard C++ Library header.
27  */
28
29 #ifndef _GLIBCXX_CONDITION_VARIABLE
30 #define _GLIBCXX_CONDITION_VARIABLE 1
31
32 #pragma GCC system_header
33
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
36 #else
37
38 #include <chrono>
39 #include <mutex> // unique_lock
40
41 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
42
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47   /**
48    * @defgroup condition_variables Condition Variables
49    * @ingroup concurrency
50    *
51    * Classes for condition_variable support.
52    * @{
53    */
54
55   /// cv_status
56   enum class cv_status { no_timeout, timeout };
57   
58   /// condition_variable
59   class condition_variable
60   {
61     typedef chrono::system_clock        __clock_t;
62     typedef __gthread_cond_t            __native_type;
63     __native_type                       _M_cond;
64
65   public:
66     typedef __native_type*              native_handle_type;
67
68     condition_variable() throw ();
69     ~condition_variable() throw ();
70
71     condition_variable(const condition_variable&) = delete;
72     condition_variable& operator=(const condition_variable&) = delete;
73
74     void
75     notify_one();
76
77     void
78     notify_all();
79
80     void
81     wait(unique_lock<mutex>& __lock);
82
83     template<typename _Predicate>
84       void
85       wait(unique_lock<mutex>& __lock, _Predicate __p)
86       {
87         while (!__p())
88           wait(__lock);
89       }
90
91     template<typename _Duration>
92       cv_status
93       wait_until(unique_lock<mutex>& __lock,
94                  const chrono::time_point<__clock_t, _Duration>& __atime)
95       { return __wait_until_impl(__lock, __atime); }
96
97     template<typename _Clock, typename _Duration>
98       cv_status
99       wait_until(unique_lock<mutex>& __lock,
100                  const chrono::time_point<_Clock, _Duration>& __atime)
101       {
102         // DR 887 - Sync unknown clock to known clock.
103         const typename _Clock::time_point __c_entry = _Clock::now();
104         const __clock_t::time_point __s_entry = __clock_t::now();
105         const chrono::nanoseconds __delta = __atime - __c_entry;
106         const __clock_t::time_point __s_atime = __s_entry + __delta;
107
108         return __wait_until_impl(__lock, __s_atime);
109       }
110
111     template<typename _Clock, typename _Duration, typename _Predicate>
112       bool
113       wait_until(unique_lock<mutex>& __lock,
114                  const chrono::time_point<_Clock, _Duration>& __atime,
115                  _Predicate __p)
116       {
117         while (!__p())
118           if (wait_until(__lock, __atime) == cv_status::timeout)
119             return __p();
120         return true;
121       }
122
123     template<typename _Rep, typename _Period>
124       cv_status
125       wait_for(unique_lock<mutex>& __lock,
126                const chrono::duration<_Rep, _Period>& __rtime)
127       { return wait_until(__lock, __clock_t::now() + __rtime); }
128
129     template<typename _Rep, typename _Period, typename _Predicate>
130       bool
131       wait_for(unique_lock<mutex>& __lock,
132                const chrono::duration<_Rep, _Period>& __rtime,
133                _Predicate __p)
134       { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
135
136     native_handle_type
137     native_handle()
138     { return &_M_cond; }
139
140   private:
141     template<typename _Clock, typename _Duration>
142       cv_status
143       __wait_until_impl(unique_lock<mutex>& __lock,
144                         const chrono::time_point<_Clock, _Duration>& __atime)
145       {
146         chrono::time_point<__clock_t, chrono::seconds> __s =
147           chrono::time_point_cast<chrono::seconds>(__atime);
148
149         chrono::nanoseconds __ns =
150           chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
151
152         __gthread_time_t __ts =
153           {
154             static_cast<std::time_t>(__s.time_since_epoch().count()),
155             static_cast<long>(__ns.count())
156           };
157
158         __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
159                                  &__ts);
160
161         return (_Clock::now() < __atime
162                 ? cv_status::no_timeout : cv_status::timeout);
163       }
164   };
165
166   /// condition_variable_any
167   // Like above, but mutex is not required to have try_lock.
168   class condition_variable_any
169   {
170     typedef chrono::system_clock        __clock_t;
171     condition_variable                  _M_cond;
172     mutex                               _M_mutex;
173
174   public:
175     typedef condition_variable::native_handle_type      native_handle_type;
176
177     condition_variable_any() throw ();
178     ~condition_variable_any() throw ();
179
180     condition_variable_any(const condition_variable_any&) = delete;
181     condition_variable_any& operator=(const condition_variable_any&) = delete;
182
183     void
184     notify_one()
185     {
186       lock_guard<mutex> __lock(_M_mutex);
187       _M_cond.notify_one();
188     }
189
190     void
191     notify_all()
192     {
193       lock_guard<mutex> __lock(_M_mutex);
194       _M_cond.notify_all();
195     }
196
197     template<typename _Lock>
198       void
199       wait(_Lock& __lock)
200       {
201         unique_lock<mutex> __my_lock(_M_mutex);
202         __lock.unlock();
203         _M_cond.wait(__my_lock);
204         __lock.lock();
205       }
206       
207
208     template<typename _Lock, typename _Predicate>
209       void
210       wait(_Lock& __lock, _Predicate __p)
211       {
212         while (!__p())
213           wait(__lock);
214       }
215
216     template<typename _Lock, typename _Clock, typename _Duration>
217       cv_status
218       wait_until(_Lock& __lock,
219                  const chrono::time_point<_Clock, _Duration>& __atime)
220       {
221         unique_lock<mutex> __my_lock(_M_mutex);
222         __lock.unlock();
223         cv_status __status = _M_cond.wait_until(__my_lock, __atime);
224         __lock.lock();
225         return __status;
226       }
227
228     template<typename _Lock, typename _Clock,
229              typename _Duration, typename _Predicate>
230       bool
231       wait_until(_Lock& __lock,
232                  const chrono::time_point<_Clock, _Duration>& __atime,
233                  _Predicate __p)
234       {
235         while (!__p())
236           if (wait_until(__lock, __atime) == cv_status::timeout)
237             return __p();
238         return true;
239       }
240
241     template<typename _Lock, typename _Rep, typename _Period>
242       cv_status
243       wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
244       { return wait_until(__lock, __clock_t::now() + __rtime); }
245
246     template<typename _Lock, typename _Rep,
247              typename _Period, typename _Predicate>
248       bool
249       wait_for(_Lock& __lock,
250                const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
251       { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
252
253     native_handle_type
254     native_handle()
255     { return _M_cond.native_handle(); }
256   };
257
258   // @} group condition_variables
259 _GLIBCXX_END_NAMESPACE_VERSION
260 } // namespace
261
262 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
263
264 #endif // __GXX_EXPERIMENTAL_CXX0X__
265
266 #endif // _GLIBCXX_CONDITION_VARIABLE