libstdc++
condition_variable
Go to the documentation of this file.
1 // <condition_variable> -*- C++ -*-
2 
3 // Copyright (C) 2008, 2009, 2010, 2011, 2012 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
60  {
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,
95  { return __wait_until_impl(__lock, __atime); }
96 
97  template<typename _Clock, typename _Duration>
98  cv_status
99  wait_until(unique_lock<mutex>& __lock,
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,
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,
145  {
148 
149  chrono::nanoseconds __ns =
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.
169  {
171  condition_variable _M_cond;
172  mutex _M_mutex;
173 
174  // scoped unlock - unlocks in ctor, re-locks in dtor
175  template<typename _Lock>
176  struct _Unlock
177  {
178  explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
179 
180  ~_Unlock() noexcept(false)
181  {
182  if (uncaught_exception())
183  __try { _M_lock.lock(); } __catch(...) { }
184  else
185  _M_lock.lock();
186  }
187 
188  _Unlock(const _Unlock&) = delete;
189  _Unlock& operator=(const _Unlock&) = delete;
190 
191  _Lock& _M_lock;
192  };
193 
194  public:
195  typedef condition_variable::native_handle_type native_handle_type;
196 
197  condition_variable_any() throw ();
198  ~condition_variable_any() throw ();
199 
201  condition_variable_any& operator=(const condition_variable_any&) = delete;
202 
203  void
204  notify_one()
205  {
206  lock_guard<mutex> __lock(_M_mutex);
207  _M_cond.notify_one();
208  }
209 
210  void
211  notify_all()
212  {
213  lock_guard<mutex> __lock(_M_mutex);
214  _M_cond.notify_all();
215  }
216 
217  template<typename _Lock>
218  void
219  wait(_Lock& __lock)
220  {
221  unique_lock<mutex> __my_lock(_M_mutex);
222  _Unlock<_Lock> __unlock(__lock);
223  // _M_mutex must be unlocked before re-locking __lock so move
224  // ownership of _M_mutex lock to an object with shorter lifetime.
225  unique_lock<mutex> __my_lock2(std::move(__my_lock));
226  _M_cond.wait(__my_lock2);
227  }
228 
229 
230  template<typename _Lock, typename _Predicate>
231  void
232  wait(_Lock& __lock, _Predicate __p)
233  {
234  while (!__p())
235  wait(__lock);
236  }
237 
238  template<typename _Lock, typename _Clock, typename _Duration>
239  cv_status
240  wait_until(_Lock& __lock,
242  {
243  unique_lock<mutex> __my_lock(_M_mutex);
244  _Unlock<_Lock> __unlock(__lock);
245  // _M_mutex must be unlocked before re-locking __lock so move
246  // ownership of _M_mutex lock to an object with shorter lifetime.
247  unique_lock<mutex> __my_lock2(std::move(__my_lock));
248  return _M_cond.wait_until(__my_lock2, __atime);
249  }
250 
251  template<typename _Lock, typename _Clock,
252  typename _Duration, typename _Predicate>
253  bool
254  wait_until(_Lock& __lock,
256  _Predicate __p)
257  {
258  while (!__p())
259  if (wait_until(__lock, __atime) == cv_status::timeout)
260  return __p();
261  return true;
262  }
263 
264  template<typename _Lock, typename _Rep, typename _Period>
265  cv_status
266  wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
267  { return wait_until(__lock, __clock_t::now() + __rtime); }
268 
269  template<typename _Lock, typename _Rep,
270  typename _Period, typename _Predicate>
271  bool
272  wait_for(_Lock& __lock,
273  const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
274  { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
275 
276  native_handle_type
277  native_handle()
278  { return _M_cond.native_handle(); }
279  };
280 
281  // @} group condition_variables
282 _GLIBCXX_END_NAMESPACE_VERSION
283 } // namespace
284 
285 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
286 
287 #endif // __GXX_EXPERIMENTAL_CXX0X__
288 
289 #endif // _GLIBCXX_CONDITION_VARIABLE
cv_status
cv_status
mutex
Definition: mutex:64
Scoped lock idiom.
Definition: mutex:439
condition_variable_any
bool uncaught_exception() __attribute__((__pure__))
system_clock
Definition: chrono:653
constexpr enable_if< __is_duration< _ToDur >::value, _ToDur >::type duration_cast(const duration< _Rep, _Period > &__d)
duration_cast
Definition: chrono:173
condition_variable
unique_lock
Definition: mutex:462
constexpr enable_if< __is_duration< _ToDur >::value, time_point< _Clock, _ToDur > >::type time_point_cast(const time_point< _Clock, _Dur > &__t)
time_point_cast
Definition: chrono:575
time_point
Definition: chrono:66
duration
Definition: chrono:63