1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// 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
7//
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
30    void notify_one() noexcept;
31    void notify_all() noexcept;
32
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
63void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
64
65class 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
74    void notify_one() noexcept;
75    void notify_all() noexcept;
76
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
109#include <__assert> // all public C++ headers provide the assertion handler
110#include <__chrono/duration.h>
111#include <__chrono/steady_clock.h>
112#include <__chrono/time_point.h>
113#include <__condition_variable/condition_variable.h>
114#include <__config>
115#include <__memory/shared_ptr.h>
116#include <__memory/unique_ptr.h>
117#include <__mutex/lock_guard.h>
118#include <__mutex/mutex.h>
119#include <__mutex/tag_types.h>
120#include <__mutex/unique_lock.h>
121#include <__utility/move.h>
122#include <version>
123
124#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
125#  pragma GCC system_header
126#endif
127
128#ifndef _LIBCPP_HAS_NO_THREADS
129
130_LIBCPP_BEGIN_NAMESPACE_STD
131
132class _LIBCPP_EXPORTED_FROM_ABI condition_variable_any
133{
134    condition_variable __cv_;
135    shared_ptr<mutex>  __mut_;
136public:
137    _LIBCPP_INLINE_VISIBILITY
138    condition_variable_any();
139
140    _LIBCPP_INLINE_VISIBILITY
141    void notify_one() _NOEXCEPT;
142    _LIBCPP_INLINE_VISIBILITY
143    void notify_all() _NOEXCEPT;
144
145    template <class _Lock>
146        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
147        void wait(_Lock& __lock);
148    template <class _Lock, class _Predicate>
149        _LIBCPP_INLINE_VISIBILITY
150        void wait(_Lock& __lock, _Predicate __pred);
151
152    template <class _Lock, class _Clock, class _Duration>
153        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
154        cv_status
155        wait_until(_Lock& __lock,
156                   const chrono::time_point<_Clock, _Duration>& __t);
157
158    template <class _Lock, class _Clock, class _Duration, class _Predicate>
159        bool
160        _LIBCPP_INLINE_VISIBILITY
161        wait_until(_Lock& __lock,
162                   const chrono::time_point<_Clock, _Duration>& __t,
163                   _Predicate __pred);
164
165    template <class _Lock, class _Rep, class _Period>
166        cv_status
167        _LIBCPP_INLINE_VISIBILITY
168        wait_for(_Lock& __lock,
169                 const chrono::duration<_Rep, _Period>& __d);
170
171    template <class _Lock, class _Rep, class _Period, class _Predicate>
172        bool
173        _LIBCPP_INLINE_VISIBILITY
174        wait_for(_Lock& __lock,
175                 const chrono::duration<_Rep, _Period>& __d,
176                 _Predicate __pred);
177};
178
179inline
180condition_variable_any::condition_variable_any()
181    : __mut_(make_shared<mutex>()) {}
182
183inline
184void
185condition_variable_any::notify_one() _NOEXCEPT
186{
187    {lock_guard<mutex> __lx(*__mut_);}
188    __cv_.notify_one();
189}
190
191inline
192void
193condition_variable_any::notify_all() _NOEXCEPT
194{
195    {lock_guard<mutex> __lx(*__mut_);}
196    __cv_.notify_all();
197}
198
199struct __lock_external
200{
201    template <class _Lock>
202    _LIBCPP_HIDE_FROM_ABI void operator()(_Lock* __m) {__m->lock();}
203};
204
205template <class _Lock>
206void
207condition_variable_any::wait(_Lock& __lock)
208{
209    shared_ptr<mutex> __mut = __mut_;
210    unique_lock<mutex> __lk(*__mut);
211    __lock.unlock();
212    unique_ptr<_Lock, __lock_external> __lxx(&__lock);
213    lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
214    __cv_.wait(__lk);
215}  // __mut_.unlock(), __lock.lock()
216
217template <class _Lock, class _Predicate>
218inline
219void
220condition_variable_any::wait(_Lock& __lock, _Predicate __pred)
221{
222    while (!__pred())
223        wait(__lock);
224}
225
226template <class _Lock, class _Clock, class _Duration>
227cv_status
228condition_variable_any::wait_until(_Lock& __lock,
229                                   const chrono::time_point<_Clock, _Duration>& __t)
230{
231    shared_ptr<mutex> __mut = __mut_;
232    unique_lock<mutex> __lk(*__mut);
233    __lock.unlock();
234    unique_ptr<_Lock, __lock_external> __lxx(&__lock);
235    lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
236    return __cv_.wait_until(__lk, __t);
237}  // __mut_.unlock(), __lock.lock()
238
239template <class _Lock, class _Clock, class _Duration, class _Predicate>
240inline
241bool
242condition_variable_any::wait_until(_Lock& __lock,
243                                   const chrono::time_point<_Clock, _Duration>& __t,
244                                   _Predicate __pred)
245{
246    while (!__pred())
247        if (wait_until(__lock, __t) == cv_status::timeout)
248            return __pred();
249    return true;
250}
251
252template <class _Lock, class _Rep, class _Period>
253inline
254cv_status
255condition_variable_any::wait_for(_Lock& __lock,
256                                 const chrono::duration<_Rep, _Period>& __d)
257{
258    return wait_until(__lock, chrono::steady_clock::now() + __d);
259}
260
261template <class _Lock, class _Rep, class _Period, class _Predicate>
262inline
263bool
264condition_variable_any::wait_for(_Lock& __lock,
265                                 const chrono::duration<_Rep, _Period>& __d,
266                                 _Predicate __pred)
267{
268    return wait_until(__lock, chrono::steady_clock::now() + __d,
269                      _VSTD::move(__pred));
270}
271
272_LIBCPP_EXPORTED_FROM_ABI void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
273
274_LIBCPP_END_NAMESPACE_STD
275
276#endif // !_LIBCPP_HAS_NO_THREADS
277
278#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
279#  include <atomic>
280#  include <concepts>
281#  include <cstdint>
282#  include <cstdlib>
283#  include <cstring>
284#  include <initializer_list>
285#  include <new>
286#  include <stdexcept>
287#  include <system_error>
288#  include <type_traits>
289#  include <typeinfo>
290#endif
291
292#endif // _LIBCPP_CONDITION_VARIABLE
293