master
  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    // [thread.condvarany.intwait], interruptible waits
105    template <class Lock, class Predicate>
106      bool wait(Lock& lock, stop_token stoken, Predicate pred);                               // since C++20
107
108    template <class Lock, class Clock, class Duration, class Predicate>
109      bool wait_until(Lock& lock, stop_token stoken,
110                      const chrono::time_point<Clock, Duration>& abs_time, Predicate pred);   // since C++20
111
112    template <class Lock, class Rep, class Period, class Predicate>
113      bool wait_for(Lock& lock, stop_token stoken,
114                    const chrono::duration<Rep, Period>& rel_time, Predicate pred);           // since C++20
115};
116
117}  // std
118
119*/
120
121#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
122#  include <__cxx03/condition_variable>
123#else
124#  include <__chrono/duration.h>
125#  include <__chrono/steady_clock.h>
126#  include <__chrono/time_point.h>
127#  include <__condition_variable/condition_variable.h>
128#  include <__config>
129#  include <__memory/shared_ptr.h>
130#  include <__mutex/lock_guard.h>
131#  include <__mutex/mutex.h>
132#  include <__mutex/tag_types.h>
133#  include <__mutex/unique_lock.h>
134#  include <__stop_token/stop_callback.h>
135#  include <__stop_token/stop_token.h>
136#  include <__utility/move.h>
137#  include <version>
138
139#  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
140#    pragma GCC system_header
141#  endif
142
143_LIBCPP_PUSH_MACROS
144#  include <__undef_macros>
145
146#  if _LIBCPP_HAS_THREADS
147
148_LIBCPP_BEGIN_NAMESPACE_STD
149
150template <class _Lock>
151struct __unlock_guard {
152  _Lock& __lock_;
153
154  _LIBCPP_HIDE_FROM_ABI __unlock_guard(_Lock& __lock) : __lock_(__lock) { __lock_.unlock(); }
155
156  _LIBCPP_HIDE_FROM_ABI ~__unlock_guard() _NOEXCEPT // turns exception to std::terminate
157  {
158    __lock_.lock();
159  }
160
161  __unlock_guard(const __unlock_guard&)            = delete;
162  __unlock_guard& operator=(const __unlock_guard&) = delete;
163};
164
165class _LIBCPP_EXPORTED_FROM_ABI condition_variable_any {
166  condition_variable __cv_;
167  shared_ptr<mutex> __mut_;
168
169public:
170  _LIBCPP_HIDE_FROM_ABI condition_variable_any();
171
172  _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT;
173  _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT;
174
175  template <class _Lock>
176  _LIBCPP_HIDE_FROM_ABI void wait(_Lock& __lock) {
177    shared_ptr<mutex> __mut = __mut_;
178    unique_lock<mutex> __lk(*__mut);
179    __unlock_guard<_Lock> __unlock(__lock);
180    lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
181    __cv_.wait(__lk);
182  } // __mut_.unlock(), __lock.lock()
183
184  template <class _Lock, class _Predicate>
185  _LIBCPP_HIDE_FROM_ABI void wait(_Lock& __lock, _Predicate __pred);
186
187  template <class _Lock, class _Clock, class _Duration>
188  _LIBCPP_HIDE_FROM_ABI cv_status wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t) {
189    shared_ptr<mutex> __mut = __mut_;
190    unique_lock<mutex> __lk(*__mut);
191    __unlock_guard<_Lock> __unlock(__lock);
192    lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
193    return __cv_.wait_until(__lk, __t);
194  } // __mut_.unlock(), __lock.lock()
195
196  template <class _Lock, class _Clock, class _Duration, class _Predicate>
197  bool _LIBCPP_HIDE_FROM_ABI
198  wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred);
199
200  template <class _Lock, class _Rep, class _Period>
201  cv_status _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d);
202
203  template <class _Lock, class _Rep, class _Period, class _Predicate>
204  bool _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred);
205
206#    if _LIBCPP_STD_VER >= 20
207
208  template <class _Lock, class _Predicate>
209  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait(_Lock& __lock, stop_token __stoken, _Predicate __pred);
210
211  template <class _Lock, class _Clock, class _Duration, class _Predicate>
212  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait_until(
213      _Lock& __lock, stop_token __stoken, const chrono::time_point<_Clock, _Duration>& __abs_time, _Predicate __pred);
214
215  template <class _Lock, class _Rep, class _Period, class _Predicate>
216  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
217  wait_for(_Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred);
218
219#    endif // _LIBCPP_STD_VER >= 20
220};
221
222inline condition_variable_any::condition_variable_any() : __mut_(make_shared<mutex>()) {}
223
224inline void condition_variable_any::notify_one() _NOEXCEPT {
225  { lock_guard<mutex> __lx(*__mut_); }
226  __cv_.notify_one();
227}
228
229inline void condition_variable_any::notify_all() _NOEXCEPT {
230  { lock_guard<mutex> __lx(*__mut_); }
231  __cv_.notify_all();
232}
233
234template <class _Lock, class _Predicate>
235inline void condition_variable_any::wait(_Lock& __lock, _Predicate __pred) {
236  while (!__pred())
237    wait(__lock);
238}
239
240template <class _Lock, class _Clock, class _Duration, class _Predicate>
241inline bool
242condition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred) {
243  while (!__pred())
244    if (wait_until(__lock, __t) == cv_status::timeout)
245      return __pred();
246  return true;
247}
248
249template <class _Lock, class _Rep, class _Period>
250inline cv_status condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d) {
251  return wait_until(__lock, chrono::steady_clock::now() + __d);
252}
253
254template <class _Lock, class _Rep, class _Period, class _Predicate>
255inline bool
256condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred) {
257  return wait_until(__lock, chrono::steady_clock::now() + __d, std::move(__pred));
258}
259
260#    if _LIBCPP_STD_VER >= 20
261
262template <class _Lock, class _Predicate>
263bool condition_variable_any::wait(_Lock& __user_lock, stop_token __stoken, _Predicate __pred) {
264  if (__stoken.stop_requested())
265    return __pred();
266
267  // Per https://eel.is/c++draft/thread.condition.condvarany#general-note-2,
268  // we do need to take a copy of the shared pointer __mut_
269  // This ensures that a thread can call the destructor immediately after calling
270  // notify_all, without waiting all the wait calls.
271  // A thread can also safely call the destructor immediately after calling
272  // request_stop, as the call to request_stop would evaluate the callback,
273  // which accesses the internal condition variable, immediately on the same thread.
274  // In this situation, it is OK even without copying a shared ownership the internal
275  // condition variable. However, this needs the evaluation of stop_callback to
276  // happen-before the destruction.
277  // The spec only says "Only the notification to unblock the wait needs to happen
278  // before destruction". To make this work, we need to copy the shared ownership of
279  // the internal condition variable inside this function, which is not possible
280  // with the current ABI.
281  shared_ptr<mutex> __mut = __mut_;
282
283  stop_callback __cb(__stoken, [this] { notify_all(); });
284
285  while (true) {
286    if (__pred())
287      return true;
288
289    // We need to take the internal lock before checking stop_requested,
290    // so that the notification cannot come in between the stop_requested
291    // check and entering the wait.
292    // Note that the stop_callback takes the same internal lock before notifying
293    unique_lock<mutex> __internal_lock(*__mut);
294    if (__stoken.stop_requested())
295      break;
296
297    __unlock_guard<_Lock> __unlock(__user_lock);
298    unique_lock<mutex> __internal_lock2(
299        std::move(__internal_lock)); // switch unlock order between __internal_lock and __user_lock
300    __cv_.wait(__internal_lock2);
301  } // __internal_lock2.unlock(), __user_lock.lock()
302  return __pred();
303}
304
305template <class _Lock, class _Clock, class _Duration, class _Predicate>
306bool condition_variable_any::wait_until(
307    _Lock& __user_lock,
308    stop_token __stoken,
309    const chrono::time_point<_Clock, _Duration>& __abs_time,
310    _Predicate __pred) {
311  if (__stoken.stop_requested())
312    return __pred();
313
314  shared_ptr<mutex> __mut = __mut_;
315  stop_callback __cb(__stoken, [this] { notify_all(); });
316
317  while (true) {
318    if (__pred())
319      return true;
320
321    unique_lock<mutex> __internal_lock(*__mut);
322    if (__stoken.stop_requested())
323      break;
324
325    __unlock_guard<_Lock> __unlock(__user_lock);
326    unique_lock<mutex> __internal_lock2(
327        std::move(__internal_lock)); // switch unlock order between __internal_lock and __user_lock
328
329    if (__cv_.wait_until(__internal_lock2, __abs_time) == cv_status::timeout)
330      break;
331  } // __internal_lock2.unlock(), __user_lock.lock()
332  return __pred();
333}
334
335template <class _Lock, class _Rep, class _Period, class _Predicate>
336bool condition_variable_any::wait_for(
337    _Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred) {
338  return wait_until(__lock, std::move(__stoken), chrono::steady_clock::now() + __rel_time, std::move(__pred));
339}
340
341#    endif // _LIBCPP_STD_VER >= 20
342
343_LIBCPP_EXPORTED_FROM_ABI void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
344
345_LIBCPP_END_NAMESPACE_STD
346
347#  endif // _LIBCPP_HAS_THREADS
348
349_LIBCPP_POP_MACROS
350
351#  if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
352#    include <atomic>
353#    include <concepts>
354#    include <cstdint>
355#    include <cstdlib>
356#    include <cstring>
357#    include <initializer_list>
358#    include <iosfwd>
359#    include <new>
360#    include <optional>
361#    include <stdexcept>
362#    include <system_error>
363#    include <type_traits>
364#    include <typeinfo>
365#  endif
366#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
367
368#endif // _LIBCPP_CONDITION_VARIABLE