blob: 92088f3e1b227cdd82838b6356f1c42e7282ccf0 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_CONDITION_VARIABLE
11#define _LIBCPP_CONDITION_VARIABLE
12
13/*
14 condition_variable synopsis
15
16namespace std
17{
18
19enum class cv_status { no_timeout, timeout };
20
21class condition_variable
22{
23public:
24 condition_variable();
25 ~condition_variable();
26
27 condition_variable(const condition_variable&) = delete;
28 condition_variable& operator=(const condition_variable&) = delete;
29
Howard Hinnant0fb1b0a2012-07-21 16:32:53 +000030 void notify_one() noexcept;
31 void notify_all() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000032
33 void wait(unique_lock<mutex>& lock);
34 template <class Predicate>
35 void wait(unique_lock<mutex>& lock, Predicate pred);
36
37 template <class Clock, class Duration>
38 cv_status
39 wait_until(unique_lock<mutex>& lock,
40 const chrono::time_point<Clock, Duration>& abs_time);
41
42 template <class Clock, class Duration, class Predicate>
43 bool
44 wait_until(unique_lock<mutex>& lock,
45 const chrono::time_point<Clock, Duration>& abs_time,
46 Predicate pred);
47
48 template <class Rep, class Period>
49 cv_status
50 wait_for(unique_lock<mutex>& lock,
51 const chrono::duration<Rep, Period>& rel_time);
52
53 template <class Rep, class Period, class Predicate>
54 bool
55 wait_for(unique_lock<mutex>& lock,
56 const chrono::duration<Rep, Period>& rel_time,
57 Predicate pred);
58
59 typedef pthread_cond_t* native_handle_type;
60 native_handle_type native_handle();
61};
62
Howard Hinnant3820f6b2010-08-27 20:10:19 +000063void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
64
Howard Hinnantc51e1022010-05-11 19:42:16 +000065class condition_variable_any
66{
67public:
68 condition_variable_any();
69 ~condition_variable_any();
70
71 condition_variable_any(const condition_variable_any&) = delete;
72 condition_variable_any& operator=(const condition_variable_any&) = delete;
73
Howard Hinnant0fb1b0a2012-07-21 16:32:53 +000074 void notify_one() noexcept;
75 void notify_all() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000076
77 template <class Lock>
78 void wait(Lock& lock);
79 template <class Lock, class Predicate>
80 void wait(Lock& lock, Predicate pred);
81
82 template <class Lock, class Clock, class Duration>
83 cv_status
84 wait_until(Lock& lock,
85 const chrono::time_point<Clock, Duration>& abs_time);
86
87 template <class Lock, class Clock, class Duration, class Predicate>
88 bool
89 wait_until(Lock& lock,
90 const chrono::time_point<Clock, Duration>& abs_time,
91 Predicate pred);
92
93 template <class Lock, class Rep, class Period>
94 cv_status
95 wait_for(Lock& lock,
96 const chrono::duration<Rep, Period>& rel_time);
97
98 template <class Lock, class Rep, class Period, class Predicate>
99 bool
100 wait_for(Lock& lock,
101 const chrono::duration<Rep, Period>& rel_time,
102 Predicate pred);
103};
104
105} // std
106
107*/
108
Louis Dionneb4fce352022-03-25 12:55:36 -0400109#include <__assert> // all public C++ headers provide the assertion handler
Howard Hinnantc51e1022010-05-11 19:42:16 +0000110#include <__config>
111#include <__mutex_base>
Vitaly Buka5a807d92022-09-02 19:35:10 -0700112#include <memory>
Mark de Weverce8f12c2021-12-22 18:14:14 +0100113#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000115#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -0500116# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000117#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000119#ifndef _LIBCPP_HAS_NO_THREADS
120
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121_LIBCPP_BEGIN_NAMESPACE_STD
122
Howard Hinnant8331b762013-03-06 23:30:19 +0000123class _LIBCPP_TYPE_VIS condition_variable_any
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124{
125 condition_variable __cv_;
126 shared_ptr<mutex> __mut_;
127public:
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000129 condition_variable_any();
130
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0fb1b0a2012-07-21 16:32:53 +0000132 void notify_one() _NOEXCEPT;
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000133 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant0fb1b0a2012-07-21 16:32:53 +0000134 void notify_all() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135
136 template <class _Lock>
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000137 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000138 void wait(_Lock& __lock);
139 template <class _Lock, class _Predicate>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000141 void wait(_Lock& __lock, _Predicate __pred);
142
143 template <class _Lock, class _Clock, class _Duration>
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000144 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000145 cv_status
146 wait_until(_Lock& __lock,
147 const chrono::time_point<_Clock, _Duration>& __t);
148
149 template <class _Lock, class _Clock, class _Duration, class _Predicate>
150 bool
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000151 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152 wait_until(_Lock& __lock,
153 const chrono::time_point<_Clock, _Duration>& __t,
154 _Predicate __pred);
155
156 template <class _Lock, class _Rep, class _Period>
157 cv_status
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000158 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159 wait_for(_Lock& __lock,
160 const chrono::duration<_Rep, _Period>& __d);
161
162 template <class _Lock, class _Rep, class _Period, class _Predicate>
163 bool
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000164 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000165 wait_for(_Lock& __lock,
166 const chrono::duration<_Rep, _Period>& __d,
167 _Predicate __pred);
168};
169
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000170inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171condition_variable_any::condition_variable_any()
172 : __mut_(make_shared<mutex>()) {}
173
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000174inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175void
Howard Hinnant0fb1b0a2012-07-21 16:32:53 +0000176condition_variable_any::notify_one() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177{
Howard Hinnant49e145e2012-10-30 19:06:59 +0000178 {lock_guard<mutex> __lx(*__mut_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000179 __cv_.notify_one();
180}
181
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000182inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000183void
Howard Hinnant0fb1b0a2012-07-21 16:32:53 +0000184condition_variable_any::notify_all() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185{
Howard Hinnant49e145e2012-10-30 19:06:59 +0000186 {lock_guard<mutex> __lx(*__mut_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187 __cv_.notify_all();
188}
189
190struct __lock_external
191{
192 template <class _Lock>
193 void operator()(_Lock* __m) {__m->lock();}
194};
195
196template <class _Lock>
197void
198condition_variable_any::wait(_Lock& __lock)
199{
200 shared_ptr<mutex> __mut = __mut_;
201 unique_lock<mutex> __lk(*__mut);
202 __lock.unlock();
Howard Hinnant49e145e2012-10-30 19:06:59 +0000203 unique_ptr<_Lock, __lock_external> __lxx(&__lock);
204 lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205 __cv_.wait(__lk);
206} // __mut_.unlock(), __lock.lock()
207
208template <class _Lock, class _Predicate>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000209inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210void
211condition_variable_any::wait(_Lock& __lock, _Predicate __pred)
212{
213 while (!__pred())
214 wait(__lock);
215}
216
217template <class _Lock, class _Clock, class _Duration>
218cv_status
219condition_variable_any::wait_until(_Lock& __lock,
220 const chrono::time_point<_Clock, _Duration>& __t)
221{
222 shared_ptr<mutex> __mut = __mut_;
223 unique_lock<mutex> __lk(*__mut);
224 __lock.unlock();
Howard Hinnant49e145e2012-10-30 19:06:59 +0000225 unique_ptr<_Lock, __lock_external> __lxx(&__lock);
226 lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000227 return __cv_.wait_until(__lk, __t);
228} // __mut_.unlock(), __lock.lock()
229
230template <class _Lock, class _Clock, class _Duration, class _Predicate>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000231inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232bool
233condition_variable_any::wait_until(_Lock& __lock,
234 const chrono::time_point<_Clock, _Duration>& __t,
235 _Predicate __pred)
236{
237 while (!__pred())
238 if (wait_until(__lock, __t) == cv_status::timeout)
239 return __pred();
240 return true;
241}
242
243template <class _Lock, class _Rep, class _Period>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000244inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245cv_status
246condition_variable_any::wait_for(_Lock& __lock,
247 const chrono::duration<_Rep, _Period>& __d)
248{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000249 return wait_until(__lock, chrono::steady_clock::now() + __d);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250}
251
252template <class _Lock, class _Rep, class _Period, class _Predicate>
Evgeniy Stepanov2fb1cb62015-11-07 01:22:13 +0000253inline
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254bool
255condition_variable_any::wait_for(_Lock& __lock,
256 const chrono::duration<_Rep, _Period>& __d,
257 _Predicate __pred)
258{
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000259 return wait_until(__lock, chrono::steady_clock::now() + __d,
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000260 _VSTD::move(__pred));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261}
262
Howard Hinnant8331b762013-03-06 23:30:19 +0000263_LIBCPP_FUNC_VIS
Nikolas Klauser7b8c0502022-07-08 18:17:26 +0200264void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
Howard Hinnante6a10852010-09-03 21:46:37 +0000265
Howard Hinnantc51e1022010-05-11 19:42:16 +0000266_LIBCPP_END_NAMESPACE_STD
267
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000268#endif // !_LIBCPP_HAS_NO_THREADS
269
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400270#endif // _LIBCPP_CONDITION_VARIABLE