libstdc++
condition_variable
Go to the documentation of this file.
1 // <condition_variable> -*- C++ -*-
2 
3 // Copyright (C) 2008-2017 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 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
36 #else
37 
38 #include <chrono>
39 #include <bits/std_mutex.h>
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>
46 
47 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
48 
49 namespace std _GLIBCXX_VISIBILITY(default)
50 {
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 
53  /**
54  * @defgroup condition_variables Condition Variables
55  * @ingroup concurrency
56  *
57  * Classes for condition_variable support.
58  * @{
59  */
60 
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;
69 
70 #ifdef __GTHREAD_COND_INIT
71  __native_type _M_cond = __GTHREAD_COND_INIT;
72 #else
73  __native_type _M_cond;
74 #endif
75 
76  public:
77  typedef __native_type* native_handle_type;
78 
79  condition_variable() noexcept;
80  ~condition_variable() noexcept;
81 
82  condition_variable(const condition_variable&) = delete;
83  condition_variable& operator=(const condition_variable&) = delete;
84 
85  void
86  notify_one() noexcept;
87 
88  void
89  notify_all() noexcept;
90 
91  void
92  wait(unique_lock<mutex>& __lock) noexcept;
93 
94  template<typename _Predicate>
95  void
96  wait(unique_lock<mutex>& __lock, _Predicate __p)
97  {
98  while (!__p())
99  wait(__lock);
100  }
101 
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); }
107 
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;
118 
119  return __wait_until_impl(__lock, __s_atime);
120  }
121 
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  }
133 
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); }
139 
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)); }
146 
147  native_handle_type
148  native_handle()
149  { return &_M_cond; }
150 
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);
159 
160  __gthread_time_t __ts =
161  {
162  static_cast<std::time_t>(__s.time_since_epoch().count()),
163  static_cast<long>(__ns.count())
164  };
165 
166  __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
167  &__ts);
168 
169  return (__clock_t::now() < __atime
170  ? cv_status::no_timeout : cv_status::timeout);
171  }
172  };
173 
174  void
175  notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
176 
177  struct __at_thread_exit_elt
178  {
179  __at_thread_exit_elt* _M_next;
180  void (*_M_cb)(void*);
181  };
182 
183  inline namespace _V2 {
184 
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;
192 
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(); }
198 
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  }
213 
214  _Unlock(const _Unlock&) = delete;
215  _Unlock& operator=(const _Unlock&) = delete;
216 
217  _Lock& _M_lock;
218  };
219 
220  public:
221  condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { }
222  ~condition_variable_any() = default;
223 
224  condition_variable_any(const condition_variable_any&) = delete;
225  condition_variable_any& operator=(const condition_variable_any&) = delete;
226 
227  void
228  notify_one() noexcept
229  {
230  lock_guard<mutex> __lock(*_M_mutex);
231  _M_cond.notify_one();
232  }
233 
234  void
235  notify_all() noexcept
236  {
237  lock_guard<mutex> __lock(*_M_mutex);
238  _M_cond.notify_all();
239  }
240 
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 
254 
255  template<typename _Lock, typename _Predicate>
256  void
257  wait(_Lock& __lock, _Predicate __p)
258  {
259  while (!__p())
260  wait(__lock);
261  }
262 
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  }
276 
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  }
289 
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); }
294 
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  };
302 
303  } // end inline namespace
304 
305  // @} group condition_variables
306 _GLIBCXX_END_NAMESPACE_VERSION
307 } // namespace
308 
309 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
310 
311 #endif // C++11
312 
313 #endif // _GLIBCXX_CONDITION_VARIABLE