xref: /freebsd/contrib/llvm-project/libcxx/include/condition_variable (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
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 <__config>
111#include <__mutex_base>
112#include <memory>
113#include <version>
114
115#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
116#  pragma GCC system_header
117#endif
118
119#ifndef _LIBCPP_HAS_NO_THREADS
120
121_LIBCPP_BEGIN_NAMESPACE_STD
122
123class _LIBCPP_TYPE_VIS condition_variable_any
124{
125    condition_variable __cv_;
126    shared_ptr<mutex>  __mut_;
127public:
128    _LIBCPP_INLINE_VISIBILITY
129    condition_variable_any();
130
131    _LIBCPP_INLINE_VISIBILITY
132    void notify_one() _NOEXCEPT;
133    _LIBCPP_INLINE_VISIBILITY
134    void notify_all() _NOEXCEPT;
135
136    template <class _Lock>
137        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
138        void wait(_Lock& __lock);
139    template <class _Lock, class _Predicate>
140        _LIBCPP_INLINE_VISIBILITY
141        void wait(_Lock& __lock, _Predicate __pred);
142
143    template <class _Lock, class _Clock, class _Duration>
144        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
145        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
151        _LIBCPP_INLINE_VISIBILITY
152        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
158        _LIBCPP_INLINE_VISIBILITY
159        wait_for(_Lock& __lock,
160                 const chrono::duration<_Rep, _Period>& __d);
161
162    template <class _Lock, class _Rep, class _Period, class _Predicate>
163        bool
164        _LIBCPP_INLINE_VISIBILITY
165        wait_for(_Lock& __lock,
166                 const chrono::duration<_Rep, _Period>& __d,
167                 _Predicate __pred);
168};
169
170inline
171condition_variable_any::condition_variable_any()
172    : __mut_(make_shared<mutex>()) {}
173
174inline
175void
176condition_variable_any::notify_one() _NOEXCEPT
177{
178    {lock_guard<mutex> __lx(*__mut_);}
179    __cv_.notify_one();
180}
181
182inline
183void
184condition_variable_any::notify_all() _NOEXCEPT
185{
186    {lock_guard<mutex> __lx(*__mut_);}
187    __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();
203    unique_ptr<_Lock, __lock_external> __lxx(&__lock);
204    lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
205    __cv_.wait(__lk);
206}  // __mut_.unlock(), __lock.lock()
207
208template <class _Lock, class _Predicate>
209inline
210void
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();
225    unique_ptr<_Lock, __lock_external> __lxx(&__lock);
226    lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
227    return __cv_.wait_until(__lk, __t);
228}  // __mut_.unlock(), __lock.lock()
229
230template <class _Lock, class _Clock, class _Duration, class _Predicate>
231inline
232bool
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>
244inline
245cv_status
246condition_variable_any::wait_for(_Lock& __lock,
247                                 const chrono::duration<_Rep, _Period>& __d)
248{
249    return wait_until(__lock, chrono::steady_clock::now() + __d);
250}
251
252template <class _Lock, class _Rep, class _Period, class _Predicate>
253inline
254bool
255condition_variable_any::wait_for(_Lock& __lock,
256                                 const chrono::duration<_Rep, _Period>& __d,
257                                 _Predicate __pred)
258{
259    return wait_until(__lock, chrono::steady_clock::now() + __d,
260                      _VSTD::move(__pred));
261}
262
263_LIBCPP_FUNC_VIS
264void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
265
266_LIBCPP_END_NAMESPACE_STD
267
268#endif // !_LIBCPP_HAS_NO_THREADS
269
270#endif // _LIBCPP_CONDITION_VARIABLE
271