xref: /freebsd/contrib/llvm-project/libcxx/include/condition_variable (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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 <__config>
110#include <__mutex_base>
111#include <memory>
112#include <version>
113
114#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
115#pragma GCC system_header
116#endif
117
118#ifndef _LIBCPP_HAS_NO_THREADS
119
120_LIBCPP_BEGIN_NAMESPACE_STD
121
122class _LIBCPP_TYPE_VIS condition_variable_any
123{
124    condition_variable __cv_;
125    shared_ptr<mutex>  __mut_;
126public:
127    _LIBCPP_INLINE_VISIBILITY
128    condition_variable_any();
129
130    _LIBCPP_INLINE_VISIBILITY
131    void notify_one() _NOEXCEPT;
132    _LIBCPP_INLINE_VISIBILITY
133    void notify_all() _NOEXCEPT;
134
135    template <class _Lock>
136        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
137        void wait(_Lock& __lock);
138    template <class _Lock, class _Predicate>
139        _LIBCPP_INLINE_VISIBILITY
140        void wait(_Lock& __lock, _Predicate __pred);
141
142    template <class _Lock, class _Clock, class _Duration>
143        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
144        cv_status
145        wait_until(_Lock& __lock,
146                   const chrono::time_point<_Clock, _Duration>& __t);
147
148    template <class _Lock, class _Clock, class _Duration, class _Predicate>
149        bool
150        _LIBCPP_INLINE_VISIBILITY
151        wait_until(_Lock& __lock,
152                   const chrono::time_point<_Clock, _Duration>& __t,
153                   _Predicate __pred);
154
155    template <class _Lock, class _Rep, class _Period>
156        cv_status
157        _LIBCPP_INLINE_VISIBILITY
158        wait_for(_Lock& __lock,
159                 const chrono::duration<_Rep, _Period>& __d);
160
161    template <class _Lock, class _Rep, class _Period, class _Predicate>
162        bool
163        _LIBCPP_INLINE_VISIBILITY
164        wait_for(_Lock& __lock,
165                 const chrono::duration<_Rep, _Period>& __d,
166                 _Predicate __pred);
167};
168
169inline
170condition_variable_any::condition_variable_any()
171    : __mut_(make_shared<mutex>()) {}
172
173inline
174void
175condition_variable_any::notify_one() _NOEXCEPT
176{
177    {lock_guard<mutex> __lx(*__mut_);}
178    __cv_.notify_one();
179}
180
181inline
182void
183condition_variable_any::notify_all() _NOEXCEPT
184{
185    {lock_guard<mutex> __lx(*__mut_);}
186    __cv_.notify_all();
187}
188
189struct __lock_external
190{
191    template <class _Lock>
192    void operator()(_Lock* __m) {__m->lock();}
193};
194
195template <class _Lock>
196void
197condition_variable_any::wait(_Lock& __lock)
198{
199    shared_ptr<mutex> __mut = __mut_;
200    unique_lock<mutex> __lk(*__mut);
201    __lock.unlock();
202    unique_ptr<_Lock, __lock_external> __lxx(&__lock);
203    lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
204    __cv_.wait(__lk);
205}  // __mut_.unlock(), __lock.lock()
206
207template <class _Lock, class _Predicate>
208inline
209void
210condition_variable_any::wait(_Lock& __lock, _Predicate __pred)
211{
212    while (!__pred())
213        wait(__lock);
214}
215
216template <class _Lock, class _Clock, class _Duration>
217cv_status
218condition_variable_any::wait_until(_Lock& __lock,
219                                   const chrono::time_point<_Clock, _Duration>& __t)
220{
221    shared_ptr<mutex> __mut = __mut_;
222    unique_lock<mutex> __lk(*__mut);
223    __lock.unlock();
224    unique_ptr<_Lock, __lock_external> __lxx(&__lock);
225    lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);
226    return __cv_.wait_until(__lk, __t);
227}  // __mut_.unlock(), __lock.lock()
228
229template <class _Lock, class _Clock, class _Duration, class _Predicate>
230inline
231bool
232condition_variable_any::wait_until(_Lock& __lock,
233                                   const chrono::time_point<_Clock, _Duration>& __t,
234                                   _Predicate __pred)
235{
236    while (!__pred())
237        if (wait_until(__lock, __t) == cv_status::timeout)
238            return __pred();
239    return true;
240}
241
242template <class _Lock, class _Rep, class _Period>
243inline
244cv_status
245condition_variable_any::wait_for(_Lock& __lock,
246                                 const chrono::duration<_Rep, _Period>& __d)
247{
248    return wait_until(__lock, chrono::steady_clock::now() + __d);
249}
250
251template <class _Lock, class _Rep, class _Period, class _Predicate>
252inline
253bool
254condition_variable_any::wait_for(_Lock& __lock,
255                                 const chrono::duration<_Rep, _Period>& __d,
256                                 _Predicate __pred)
257{
258    return wait_until(__lock, chrono::steady_clock::now() + __d,
259                      _VSTD::move(__pred));
260}
261
262_LIBCPP_FUNC_VIS
263void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
264
265_LIBCPP_END_NAMESPACE_STD
266
267#endif // !_LIBCPP_HAS_NO_THREADS
268
269#endif // _LIBCPP_CONDITION_VARIABLE
270