blob: 1af2484abd7bc66ce83d06d3cbf4cae167604ad2 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------- condition_variable ----------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_CONDITION_VARIABLE
12#define _LIBCPP_CONDITION_VARIABLE
13
14/*
15 condition_variable synopsis
16
17namespace std
18{
19
20enum class cv_status { no_timeout, timeout };
21
22class condition_variable
23{
24public:
25 condition_variable();
26 ~condition_variable();
27
28 condition_variable(const condition_variable&) = delete;
29 condition_variable& operator=(const condition_variable&) = delete;
30
Howard Hinnantc8f74132012-07-21 16:32:53 +000031 void notify_one() noexcept;
32 void notify_all() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000033
34 void wait(unique_lock<mutex>& lock);
35 template <class Predicate>
36 void wait(unique_lock<mutex>& lock, Predicate pred);
37
38 template <class Clock, class Duration>
39 cv_status
40 wait_until(unique_lock<mutex>& lock,
41 const chrono::time_point<Clock, Duration>& abs_time);
42
43 template <class Clock, class Duration, class Predicate>
44 bool
45 wait_until(unique_lock<mutex>& lock,
46 const chrono::time_point<Clock, Duration>& abs_time,
47 Predicate pred);
48
49 template <class Rep, class Period>
50 cv_status
51 wait_for(unique_lock<mutex>& lock,
52 const chrono::duration<Rep, Period>& rel_time);
53
54 template <class Rep, class Period, class Predicate>
55 bool
56 wait_for(unique_lock<mutex>& lock,
57 const chrono::duration<Rep, Period>& rel_time,
58 Predicate pred);
59
60 typedef pthread_cond_t* native_handle_type;
61 native_handle_type native_handle();
62};
63
Howard Hinnant47499b12010-08-27 20:10:19 +000064void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
65
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000066class condition_variable_any
67{
68public:
69 condition_variable_any();
70 ~condition_variable_any();
71
72 condition_variable_any(const condition_variable_any&) = delete;
73 condition_variable_any& operator=(const condition_variable_any&) = delete;
74
Howard Hinnantc8f74132012-07-21 16:32:53 +000075 void notify_one() noexcept;
76 void notify_all() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077
78 template <class Lock>
79 void wait(Lock& lock);
80 template <class Lock, class Predicate>
81 void wait(Lock& lock, Predicate pred);
82
83 template <class Lock, class Clock, class Duration>
84 cv_status
85 wait_until(Lock& lock,
86 const chrono::time_point<Clock, Duration>& abs_time);
87
88 template <class Lock, class Clock, class Duration, class Predicate>
89 bool
90 wait_until(Lock& lock,
91 const chrono::time_point<Clock, Duration>& abs_time,
92 Predicate pred);
93
94 template <class Lock, class Rep, class Period>
95 cv_status
96 wait_for(Lock& lock,
97 const chrono::duration<Rep, Period>& rel_time);
98
99 template <class Lock, class Rep, class Period, class Predicate>
100 bool
101 wait_for(Lock& lock,
102 const chrono::duration<Rep, Period>& rel_time,
103 Predicate pred);
104};
105
106} // std
107
108*/
109
110#include <__config>
111#include <__mutex_base>
112#include <memory>
113
Howard Hinnant08e17472011-10-17 20:05:10 +0000114#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000116#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000118#ifndef _LIBCPP_HAS_NO_THREADS
119
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000120_LIBCPP_BEGIN_NAMESPACE_STD
121
Howard Hinnant83eade62013-03-06 23:30:19 +0000122class _LIBCPP_TYPE_VIS condition_variable_any
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123{
124 condition_variable __cv_;
125 shared_ptr<mutex> __mut_;
126public:
127 condition_variable_any();
128
Howard Hinnantc8f74132012-07-21 16:32:53 +0000129 void notify_one() _NOEXCEPT;
130 void notify_all() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131
132 template <class _Lock>
133 void wait(_Lock& __lock);
134 template <class _Lock, class _Predicate>
135 void wait(_Lock& __lock, _Predicate __pred);
136
137 template <class _Lock, class _Clock, class _Duration>
138 cv_status
139 wait_until(_Lock& __lock,
140 const chrono::time_point<_Clock, _Duration>& __t);
141
142 template <class _Lock, class _Clock, class _Duration, class _Predicate>
143 bool
144 wait_until(_Lock& __lock,
145 const chrono::time_point<_Clock, _Duration>& __t,
146 _Predicate __pred);
147
148 template <class _Lock, class _Rep, class _Period>
149 cv_status
150 wait_for(_Lock& __lock,
151 const chrono::duration<_Rep, _Period>& __d);
152
153 template <class _Lock, class _Rep, class _Period, class _Predicate>
154 bool
155 wait_for(_Lock& __lock,
156 const chrono::duration<_Rep, _Period>& __d,
157 _Predicate __pred);
158};
159
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700160inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000161condition_variable_any::condition_variable_any()
162 : __mut_(make_shared<mutex>()) {}
163
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700164inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165void
Howard Hinnantc8f74132012-07-21 16:32:53 +0000166condition_variable_any::notify_one() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000167{
Howard Hinnant9c0df142012-10-30 19:06:59 +0000168 {lock_guard<mutex> __lx(*__mut_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000169 __cv_.notify_one();
170}
171
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700172inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000173void
Howard Hinnantc8f74132012-07-21 16:32:53 +0000174condition_variable_any::notify_all() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000175{
Howard Hinnant9c0df142012-10-30 19:06:59 +0000176 {lock_guard<mutex> __lx(*__mut_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000177 __cv_.notify_all();
178}
179
180struct __lock_external
181{
182 template <class _Lock>
183 void operator()(_Lock* __m) {__m->lock();}
184};
185
186template <class _Lock>
187void
188condition_variable_any::wait(_Lock& __lock)
189{
190 shared_ptr<mutex> __mut = __mut_;
191 unique_lock<mutex> __lk(*__mut);
192 __lock.unlock();
Howard Hinnant9c0df142012-10-30 19:06:59 +0000193 unique_ptr<_Lock, __lock_external> __lxx(&__lock);
194 lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195 __cv_.wait(__lk);
196} // __mut_.unlock(), __lock.lock()
197
198template <class _Lock, class _Predicate>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700199inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000200void
201condition_variable_any::wait(_Lock& __lock, _Predicate __pred)
202{
203 while (!__pred())
204 wait(__lock);
205}
206
207template <class _Lock, class _Clock, class _Duration>
208cv_status
209condition_variable_any::wait_until(_Lock& __lock,
210 const chrono::time_point<_Clock, _Duration>& __t)
211{
212 shared_ptr<mutex> __mut = __mut_;
213 unique_lock<mutex> __lk(*__mut);
214 __lock.unlock();
Howard Hinnant9c0df142012-10-30 19:06:59 +0000215 unique_ptr<_Lock, __lock_external> __lxx(&__lock);
216 lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000217 return __cv_.wait_until(__lk, __t);
218} // __mut_.unlock(), __lock.lock()
219
220template <class _Lock, class _Clock, class _Duration, class _Predicate>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700221inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222bool
223condition_variable_any::wait_until(_Lock& __lock,
224 const chrono::time_point<_Clock, _Duration>& __t,
225 _Predicate __pred)
226{
227 while (!__pred())
228 if (wait_until(__lock, __t) == cv_status::timeout)
229 return __pred();
230 return true;
231}
232
233template <class _Lock, class _Rep, class _Period>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700234inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235cv_status
236condition_variable_any::wait_for(_Lock& __lock,
237 const chrono::duration<_Rep, _Period>& __d)
238{
Howard Hinnantf8f85212010-11-20 19:16:30 +0000239 return wait_until(__lock, chrono::steady_clock::now() + __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000240}
241
242template <class _Lock, class _Rep, class _Period, class _Predicate>
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700243inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000244bool
245condition_variable_any::wait_for(_Lock& __lock,
246 const chrono::duration<_Rep, _Period>& __d,
247 _Predicate __pred)
248{
Howard Hinnantf8f85212010-11-20 19:16:30 +0000249 return wait_until(__lock, chrono::steady_clock::now() + __d,
Howard Hinnant0949eed2011-06-30 21:18:19 +0000250 _VSTD::move(__pred));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000251}
252
Howard Hinnant83eade62013-03-06 23:30:19 +0000253_LIBCPP_FUNC_VIS
Howard Hinnante6e4d012010-09-03 21:46:37 +0000254void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
255
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256_LIBCPP_END_NAMESPACE_STD
257
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000258#endif // !_LIBCPP_HAS_NO_THREADS
259
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260#endif // _LIBCPP_CONDITION_VARIABLE