aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/cxxsupp/libcxx/include/condition_variable
blob: ac44eb324816d821b352f4c929474f2ff5462e75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP_CONDITION_VARIABLE
#define _LIBCPP_CONDITION_VARIABLE

/*
    condition_variable synopsis

namespace std
{

enum class cv_status { no_timeout, timeout };

class condition_variable
{
public:
    condition_variable();
    ~condition_variable();

    condition_variable(const condition_variable&) = delete;
    condition_variable& operator=(const condition_variable&) = delete;

    void notify_one() noexcept;
    void notify_all() noexcept;

    void wait(unique_lock<mutex>& lock);
    template <class Predicate>
        void wait(unique_lock<mutex>& lock, Predicate pred);

    template <class Clock, class Duration>
        cv_status
        wait_until(unique_lock<mutex>& lock,
                   const chrono::time_point<Clock, Duration>& abs_time);

    template <class Clock, class Duration, class Predicate>
        bool
        wait_until(unique_lock<mutex>& lock,
                   const chrono::time_point<Clock, Duration>& abs_time,
                   Predicate pred);

    template <class Rep, class Period>
        cv_status
        wait_for(unique_lock<mutex>& lock,
                 const chrono::duration<Rep, Period>& rel_time);

    template <class Rep, class Period, class Predicate>
        bool
        wait_for(unique_lock<mutex>& lock,
                 const chrono::duration<Rep, Period>& rel_time,
                 Predicate pred);

    typedef pthread_cond_t* native_handle_type;
    native_handle_type native_handle();
};

void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);

class condition_variable_any
{
public:
    condition_variable_any();
    ~condition_variable_any();

    condition_variable_any(const condition_variable_any&) = delete;
    condition_variable_any& operator=(const condition_variable_any&) = delete;

    void notify_one() noexcept;
    void notify_all() noexcept;

    template <class Lock>
        void wait(Lock& lock);
    template <class Lock, class Predicate>
        void wait(Lock& lock, Predicate pred);

    template <class Lock, class Clock, class Duration>
        cv_status
        wait_until(Lock& lock,
                   const chrono::time_point<Clock, Duration>& abs_time);

    template <class Lock, class Clock, class Duration, class Predicate>
        bool
        wait_until(Lock& lock,
                   const chrono::time_point<Clock, Duration>& abs_time,
                   Predicate pred);

    template <class Lock, class Rep, class Period>
        cv_status
        wait_for(Lock& lock,
                 const chrono::duration<Rep, Period>& rel_time);

    template <class Lock, class Rep, class Period, class Predicate>
        bool
        wait_for(Lock& lock,
                 const chrono::duration<Rep, Period>& rel_time,
                 Predicate pred);
};

}  // std

*/

#include <__assert> // all public C++ headers provide the assertion handler
#include <__chrono/duration.h>
#include <__chrono/steady_clock.h>
#include <__chrono/time_point.h>
#include <__condition_variable/condition_variable.h>
#include <__config>
#include <__memory/shared_ptr.h>
#include <__memory/unique_ptr.h>
#include <__mutex/lock_guard.h>
#include <__mutex/mutex.h>
#include <__mutex/tag_types.h>
#include <__mutex/unique_lock.h>
#include <__utility/move.h>
#include <version>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#  pragma GCC system_header
#endif

#ifndef _LIBCPP_HAS_NO_THREADS

_LIBCPP_BEGIN_NAMESPACE_STD

class _LIBCPP_EXPORTED_FROM_ABI condition_variable_any
{
    condition_variable __cv_;
    shared_ptr<mutex>  __mut_;
public:
    _LIBCPP_INLINE_VISIBILITY
    condition_variable_any();

    _LIBCPP_INLINE_VISIBILITY
    void notify_one() _NOEXCEPT;
    _LIBCPP_INLINE_VISIBILITY
    void notify_all() _NOEXCEPT;

    template <class _Lock>
        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
        void wait(_Lock& __lock);
    template <class _Lock, class _Predicate>
        _LIBCPP_INLINE_VISIBILITY
        void wait(_Lock& __lock, _Predicate __pred);

    template <class _Lock, class _Clock, class _Duration>
        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
        cv_status
        wait_until(_Lock& __lock,
                   const chrono::time_point<_Clock, _Duration>& __t);

    template <class _Lock, class _Clock, class _Duration, class _Predicate>
        bool
        _LIBCPP_INLINE_VISIBILITY
        wait_until(_Lock& __lock,
                   const chrono::time_point<_Clock, _Duration>& __t,
                   _Predicate __pred);

    template <class _Lock, class _Rep, class _Period>
        cv_status
        _LIBCPP_INLINE_VISIBILITY
        wait_for(_Lock& __lock,
                 const chrono::duration<_Rep, _Period>& __d);

    template <class _Lock, class _Rep, class _Period, class _Predicate>
        bool
        _LIBCPP_INLINE_VISIBILITY
        wait_for(_Lock& __lock,
                 const chrono::duration<_Rep, _Period>& __d,
                 _Predicate __pred);
};

inline
condition_variable_any::condition_variable_any()
    : __mut_(make_shared<mutex>()) {}

inline
void
condition_variable_any::notify_one() _NOEXCEPT
{
    {lock_guard<mutex> __lx(*__mut_);}
    __cv_.notify_one();
}

inline
void
condition_variable_any::notify_all() _NOEXCEPT
{
    {lock_guard<mutex> __lx(*__mut_);}
    __cv_.notify_all();
}

struct __lock_external
{
    template <class _Lock>
    _LIBCPP_HIDE_FROM_ABI void operator()(_Lock* __m) {__m->lock();}
};

template <class _Lock>
void
condition_variable_any::wait(_Lock& __lock)
{
    shared_ptr<mutex> __mut = __mut_;
    unique_lock<mutex> __lk(*__mut);
    __lock.unlock();
    unique_ptr<_Lock, __lock_external> __lxx(&__lock);
    lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
    __cv_.wait(__lk);
}  // __mut_.unlock(), __lock.lock()

template <class _Lock, class _Predicate>
inline
void
condition_variable_any::wait(_Lock& __lock, _Predicate __pred)
{
    while (!__pred())
        wait(__lock);
}

template <class _Lock, class _Clock, class _Duration>
cv_status
condition_variable_any::wait_until(_Lock& __lock,
                                   const chrono::time_point<_Clock, _Duration>& __t)
{
    shared_ptr<mutex> __mut = __mut_;
    unique_lock<mutex> __lk(*__mut);
    __lock.unlock();
    unique_ptr<_Lock, __lock_external> __lxx(&__lock);
    lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
    return __cv_.wait_until(__lk, __t);
}  // __mut_.unlock(), __lock.lock()

template <class _Lock, class _Clock, class _Duration, class _Predicate>
inline
bool
condition_variable_any::wait_until(_Lock& __lock,
                                   const chrono::time_point<_Clock, _Duration>& __t,
                                   _Predicate __pred)
{
    while (!__pred())
        if (wait_until(__lock, __t) == cv_status::timeout)
            return __pred();
    return true;
}

template <class _Lock, class _Rep, class _Period>
inline
cv_status
condition_variable_any::wait_for(_Lock& __lock,
                                 const chrono::duration<_Rep, _Period>& __d)
{
    return wait_until(__lock, chrono::steady_clock::now() + __d);
}

template <class _Lock, class _Rep, class _Period, class _Predicate>
inline
bool
condition_variable_any::wait_for(_Lock& __lock,
                                 const chrono::duration<_Rep, _Period>& __d,
                                 _Predicate __pred)
{
    return wait_until(__lock, chrono::steady_clock::now() + __d,
                      _VSTD::move(__pred));
}

_LIBCPP_EXPORTED_FROM_ABI void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);

_LIBCPP_END_NAMESPACE_STD

#endif // !_LIBCPP_HAS_NO_THREADS

#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
#  include <atomic>
#  include <concepts>
#  include <cstdint>
#  include <cstdlib>
#  include <cstring>
#  include <initializer_list>
#  include <new>
#  include <stdexcept>
#  include <system_error>
#  include <type_traits>
#  include <typeinfo>
#endif

#endif // _LIBCPP_CONDITION_VARIABLE