OSDN Git Service

2009-02-12 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 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /** @file condition_variable
31  *  This is a Standard C++ Library header.
32  */
33
34 #ifndef _GLIBCXX_CONDITION_VARIABLE
35 #define _GLIBCXX_CONDITION_VARIABLE 1
36
37 #pragma GCC system_header
38
39 #ifndef __GXX_EXPERIMENTAL_CXX0X__
40 # include <c++0x_warning.h>
41 #else
42
43 #include <chrono>
44 #include <mutex> // unique_lock
45
46 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
47
48 namespace std
49 {
50   /// condition_variable
51   class condition_variable
52   {
53     typedef chrono::system_clock        __clock_t;
54     typedef __gthread_cond_t            __native_type;
55     __native_type                       _M_cond;
56
57   public:
58     typedef __native_type*              native_handle_type;
59
60     condition_variable();
61     ~condition_variable();
62
63     condition_variable(const condition_variable&) = delete;
64     condition_variable& operator=(const condition_variable&) = delete;
65
66     void
67     notify_one();
68
69     void
70     notify_all();
71
72     void
73     wait(unique_lock<mutex>& __lock);
74
75     template<typename _Predicate>
76       void
77       wait(unique_lock<mutex>& __lock, _Predicate __p)
78       {
79         while (!__p())
80           wait(__lock);
81       }
82
83     template<typename _Duration>
84       bool
85       wait_until(unique_lock<mutex>& __lock,
86                  const chrono::time_point<__clock_t, _Duration>& __atime)
87       { return __wait_until_impl(__lock, __atime); }
88
89     template<typename _Clock, typename _Duration>
90       bool
91       wait_until(unique_lock<mutex>& __lock,
92                  const chrono::time_point<_Clock, _Duration>& __atime)
93       {
94         // DR 887 - Sync unknown clock to known clock.
95         typename _Clock::time_point __c_entry = _Clock::now();
96         __clock_t::time_point __s_entry = __clock_t::now();
97         chrono::nanoseconds __delta = __atime - __c_entry;
98         __clock_t::time_point __s_atime = __s_entry + __delta;
99
100         return __wait_until_impl(__lock, __s_atime);
101       }
102
103     template<typename _Clock, typename _Duration, typename _Predicate>
104       bool
105       wait_until(unique_lock<mutex>& __lock,
106                  const chrono::time_point<_Clock, _Duration>& __atime,
107                  _Predicate __p)
108       {
109         while (!__p())
110           if (!wait_until(__lock, __atime))
111             return __p();
112
113         return true;
114       }
115
116     template<typename _Rep, typename _Period>
117       bool
118       wait_for(unique_lock<mutex>& __lock,
119                const chrono::duration<_Rep, _Period>& __rtime)
120       { return wait_until(__lock, __clock_t::now() + __rtime); }
121
122     template<typename _Rep, typename _Period, typename _Predicate>
123       bool
124       wait_for(unique_lock<mutex>& __lock,
125                const chrono::duration<_Rep, _Period>& __rtime,
126                _Predicate __p)
127       { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
128
129     native_handle_type
130     native_handle()
131     { return &_M_cond; }
132
133   private:
134     template<typename _Clock, typename _Duration>
135       bool
136       __wait_until_impl(unique_lock<mutex>& __lock,
137                         const chrono::time_point<_Clock, _Duration>& __atime)
138       {
139         chrono::time_point<__clock_t, chrono::seconds> __s =
140           chrono::time_point_cast<chrono::seconds>(__atime);
141
142         chrono::nanoseconds __ns =
143           chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
144
145         __gthread_time_t __ts =
146           {
147             static_cast<std::time_t>(__s.time_since_epoch().count()),
148             static_cast<long>(__ns.count())
149           };
150
151         __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
152                                  &__ts);
153
154         return _Clock::now() < __atime;
155       }
156   };
157
158   /// condition_variable_any
159   // Like above, only mutex may not have try_lock.
160   class condition_variable_any
161   {
162     typedef __gthread_cond_t            __native_type;
163     __native_type                       _M_cond;
164
165   public:
166     typedef __native_type*              native_handle_type;
167
168     condition_variable_any();
169     ~condition_variable_any();
170
171     condition_variable_any(const condition_variable_any&) = delete;
172     condition_variable_any& operator=(const condition_variable_any&) = delete;
173
174     void
175     notify_one();
176
177     void
178     notify_all();
179
180     template<typename _Lock>
181       void
182       wait(_Lock& __lock);
183
184     template<typename _Lock, typename _Predicate>
185       void
186       wait(_Lock& __lock, _Predicate __p);
187
188     template<typename _Lock, typename _Clock, typename _Duration>
189       bool
190       wait_until(_Lock& __lock,
191                  const chrono::time_point<_Clock, _Duration>& __atime);
192
193     template<typename _Lock, typename _Clock,
194              typename _Duration, typename _Predicate>
195       bool
196       wait_until(_Lock& __lock,
197                  const chrono::time_point<_Clock, _Duration>& __atime,
198                  _Predicate __p);
199
200     template<typename _Lock, typename _Rep, typename _Period>
201       bool
202       wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime);
203
204     template<typename _Lock, typename _Rep,
205              typename _Period, typename _Predicate>
206       bool
207       wait_for(_Lock& __lock,
208                const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p);
209
210     native_handle_type
211     native_handle()
212     { return &_M_cond; }
213   };
214 }
215
216 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
217
218 #endif // __GXX_EXPERIMENTAL_CXX0X__
219
220 #endif // _GLIBCXX_CONDITION_VARIABLE