xref: /freebsd/contrib/llvm-project/libcxx/include/condition_variable (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_CONDITION_VARIABLE
110b57cec5SDimitry Andric#define _LIBCPP_CONDITION_VARIABLE
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    condition_variable synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std
170b57cec5SDimitry Andric{
180b57cec5SDimitry Andric
190b57cec5SDimitry Andricenum class cv_status { no_timeout, timeout };
200b57cec5SDimitry Andric
210b57cec5SDimitry Andricclass condition_variable
220b57cec5SDimitry Andric{
230b57cec5SDimitry Andricpublic:
240b57cec5SDimitry Andric    condition_variable();
250b57cec5SDimitry Andric    ~condition_variable();
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric    condition_variable(const condition_variable&) = delete;
280b57cec5SDimitry Andric    condition_variable& operator=(const condition_variable&) = delete;
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric    void notify_one() noexcept;
310b57cec5SDimitry Andric    void notify_all() noexcept;
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric    void wait(unique_lock<mutex>& lock);
340b57cec5SDimitry Andric    template <class Predicate>
350b57cec5SDimitry Andric        void wait(unique_lock<mutex>& lock, Predicate pred);
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric    template <class Clock, class Duration>
380b57cec5SDimitry Andric        cv_status
390b57cec5SDimitry Andric        wait_until(unique_lock<mutex>& lock,
400b57cec5SDimitry Andric                   const chrono::time_point<Clock, Duration>& abs_time);
410b57cec5SDimitry Andric
420b57cec5SDimitry Andric    template <class Clock, class Duration, class Predicate>
430b57cec5SDimitry Andric        bool
440b57cec5SDimitry Andric        wait_until(unique_lock<mutex>& lock,
450b57cec5SDimitry Andric                   const chrono::time_point<Clock, Duration>& abs_time,
460b57cec5SDimitry Andric                   Predicate pred);
470b57cec5SDimitry Andric
480b57cec5SDimitry Andric    template <class Rep, class Period>
490b57cec5SDimitry Andric        cv_status
500b57cec5SDimitry Andric        wait_for(unique_lock<mutex>& lock,
510b57cec5SDimitry Andric                 const chrono::duration<Rep, Period>& rel_time);
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric    template <class Rep, class Period, class Predicate>
540b57cec5SDimitry Andric        bool
550b57cec5SDimitry Andric        wait_for(unique_lock<mutex>& lock,
560b57cec5SDimitry Andric                 const chrono::duration<Rep, Period>& rel_time,
570b57cec5SDimitry Andric                 Predicate pred);
580b57cec5SDimitry Andric
590b57cec5SDimitry Andric    typedef pthread_cond_t* native_handle_type;
600b57cec5SDimitry Andric    native_handle_type native_handle();
610b57cec5SDimitry Andric};
620b57cec5SDimitry Andric
630b57cec5SDimitry Andricvoid notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
640b57cec5SDimitry Andric
650b57cec5SDimitry Andricclass condition_variable_any
660b57cec5SDimitry Andric{
670b57cec5SDimitry Andricpublic:
680b57cec5SDimitry Andric    condition_variable_any();
690b57cec5SDimitry Andric    ~condition_variable_any();
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric    condition_variable_any(const condition_variable_any&) = delete;
720b57cec5SDimitry Andric    condition_variable_any& operator=(const condition_variable_any&) = delete;
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric    void notify_one() noexcept;
750b57cec5SDimitry Andric    void notify_all() noexcept;
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric    template <class Lock>
780b57cec5SDimitry Andric        void wait(Lock& lock);
790b57cec5SDimitry Andric    template <class Lock, class Predicate>
800b57cec5SDimitry Andric        void wait(Lock& lock, Predicate pred);
810b57cec5SDimitry Andric
820b57cec5SDimitry Andric    template <class Lock, class Clock, class Duration>
830b57cec5SDimitry Andric        cv_status
840b57cec5SDimitry Andric        wait_until(Lock& lock,
850b57cec5SDimitry Andric                   const chrono::time_point<Clock, Duration>& abs_time);
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric    template <class Lock, class Clock, class Duration, class Predicate>
880b57cec5SDimitry Andric        bool
890b57cec5SDimitry Andric        wait_until(Lock& lock,
900b57cec5SDimitry Andric                   const chrono::time_point<Clock, Duration>& abs_time,
910b57cec5SDimitry Andric                   Predicate pred);
920b57cec5SDimitry Andric
930b57cec5SDimitry Andric    template <class Lock, class Rep, class Period>
940b57cec5SDimitry Andric        cv_status
950b57cec5SDimitry Andric        wait_for(Lock& lock,
960b57cec5SDimitry Andric                 const chrono::duration<Rep, Period>& rel_time);
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric    template <class Lock, class Rep, class Period, class Predicate>
990b57cec5SDimitry Andric        bool
1000b57cec5SDimitry Andric        wait_for(Lock& lock,
1010b57cec5SDimitry Andric                 const chrono::duration<Rep, Period>& rel_time,
1020b57cec5SDimitry Andric                 Predicate pred);
1035f757f3fSDimitry Andric
1045f757f3fSDimitry Andric    // [thread.condvarany.intwait], interruptible waits
1055f757f3fSDimitry Andric    template <class Lock, class Predicate>
1065f757f3fSDimitry Andric      bool wait(Lock& lock, stop_token stoken, Predicate pred);                               // since C++20
1075f757f3fSDimitry Andric
1085f757f3fSDimitry Andric    template <class Lock, class Clock, class Duration, class Predicate>
1095f757f3fSDimitry Andric      bool wait_until(Lock& lock, stop_token stoken,
1105f757f3fSDimitry Andric                      const chrono::time_point<Clock, Duration>& abs_time, Predicate pred);   // since C++20
1115f757f3fSDimitry Andric
1125f757f3fSDimitry Andric    template <class Lock, class Rep, class Period, class Predicate>
1135f757f3fSDimitry Andric      bool wait_for(Lock& lock, stop_token stoken,
1145f757f3fSDimitry Andric                    const chrono::duration<Rep, Period>& rel_time, Predicate pred);           // since C++20
1150b57cec5SDimitry Andric};
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric}  // std
1180b57cec5SDimitry Andric
1190b57cec5SDimitry Andric*/
1200b57cec5SDimitry Andric
12106c3fb27SDimitry Andric#include <__chrono/duration.h>
12206c3fb27SDimitry Andric#include <__chrono/steady_clock.h>
12306c3fb27SDimitry Andric#include <__chrono/time_point.h>
12406c3fb27SDimitry Andric#include <__condition_variable/condition_variable.h>
1250b57cec5SDimitry Andric#include <__config>
126bdd1243dSDimitry Andric#include <__memory/shared_ptr.h>
12706c3fb27SDimitry Andric#include <__mutex/lock_guard.h>
12806c3fb27SDimitry Andric#include <__mutex/mutex.h>
12906c3fb27SDimitry Andric#include <__mutex/tag_types.h>
13006c3fb27SDimitry Andric#include <__mutex/unique_lock.h>
1317a6dacacSDimitry Andric#include <__stop_token/stop_callback.h>
1325f757f3fSDimitry Andric#include <__stop_token/stop_token.h>
13306c3fb27SDimitry Andric#include <__utility/move.h>
13404eeddc0SDimitry Andric#include <version>
1350b57cec5SDimitry Andric
1360b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1370b57cec5SDimitry Andric#  pragma GCC system_header
1380b57cec5SDimitry Andric#endif
1390b57cec5SDimitry Andric
140*b3edf446SDimitry Andric_LIBCPP_PUSH_MACROS
141*b3edf446SDimitry Andric#include <__undef_macros>
142*b3edf446SDimitry Andric
1430b57cec5SDimitry Andric#ifndef _LIBCPP_HAS_NO_THREADS
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
1460b57cec5SDimitry Andric
147cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI condition_variable_any {
1480b57cec5SDimitry Andric  condition_variable __cv_;
1490b57cec5SDimitry Andric  shared_ptr<mutex> __mut_;
1500b57cec5SDimitry Andric
151cb14a3feSDimitry Andricpublic:
152cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI condition_variable_any();
153cb14a3feSDimitry Andric
154cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT;
155cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT;
1560b57cec5SDimitry Andric
1570b57cec5SDimitry Andric  template <class _Lock>
158cb14a3feSDimitry Andric  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS void wait(_Lock& __lock);
1590b57cec5SDimitry Andric  template <class _Lock, class _Predicate>
160cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void wait(_Lock& __lock, _Predicate __pred);
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andric  template <class _Lock, class _Clock, class _Duration>
163cb14a3feSDimitry Andric  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS cv_status
164cb14a3feSDimitry Andric  wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t);
1650b57cec5SDimitry Andric
1660b57cec5SDimitry Andric  template <class _Lock, class _Clock, class _Duration, class _Predicate>
167cb14a3feSDimitry Andric  bool _LIBCPP_HIDE_FROM_ABI
168cb14a3feSDimitry Andric  wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred);
1690b57cec5SDimitry Andric
1700b57cec5SDimitry Andric  template <class _Lock, class _Rep, class _Period>
171cb14a3feSDimitry Andric  cv_status _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d);
1720b57cec5SDimitry Andric
1730b57cec5SDimitry Andric  template <class _Lock, class _Rep, class _Period, class _Predicate>
174cb14a3feSDimitry Andric  bool _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred);
1755f757f3fSDimitry Andric
1765f757f3fSDimitry Andric#  if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
1775f757f3fSDimitry Andric
1785f757f3fSDimitry Andric  template <class _Lock, class _Predicate>
1795f757f3fSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait(_Lock& __lock, stop_token __stoken, _Predicate __pred);
1805f757f3fSDimitry Andric
1815f757f3fSDimitry Andric  template <class _Lock, class _Clock, class _Duration, class _Predicate>
182cb14a3feSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait_until(
183cb14a3feSDimitry Andric      _Lock& __lock, stop_token __stoken, const chrono::time_point<_Clock, _Duration>& __abs_time, _Predicate __pred);
1845f757f3fSDimitry Andric
1855f757f3fSDimitry Andric  template <class _Lock, class _Rep, class _Period, class _Predicate>
186cb14a3feSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
187cb14a3feSDimitry Andric  wait_for(_Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred);
1885f757f3fSDimitry Andric
1895f757f3fSDimitry Andric#  endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
1900b57cec5SDimitry Andric};
1910b57cec5SDimitry Andric
192cb14a3feSDimitry Andricinline condition_variable_any::condition_variable_any() : __mut_(make_shared<mutex>()) {}
1930b57cec5SDimitry Andric
194cb14a3feSDimitry Andricinline void condition_variable_any::notify_one() _NOEXCEPT {
1950b57cec5SDimitry Andric  { lock_guard<mutex> __lx(*__mut_); }
1960b57cec5SDimitry Andric  __cv_.notify_one();
1970b57cec5SDimitry Andric}
1980b57cec5SDimitry Andric
199cb14a3feSDimitry Andricinline void condition_variable_any::notify_all() _NOEXCEPT {
2000b57cec5SDimitry Andric  { lock_guard<mutex> __lx(*__mut_); }
2010b57cec5SDimitry Andric  __cv_.notify_all();
2020b57cec5SDimitry Andric}
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andrictemplate <class _Lock>
2057a6dacacSDimitry Andricstruct __unlock_guard {
2067a6dacacSDimitry Andric  _Lock& __lock_;
2077a6dacacSDimitry Andric
2087a6dacacSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __unlock_guard(_Lock& __lock) : __lock_(__lock) { __lock_.unlock(); }
2097a6dacacSDimitry Andric
2107a6dacacSDimitry Andric  _LIBCPP_HIDE_FROM_ABI ~__unlock_guard() _NOEXCEPT // turns exception to std::terminate
2117a6dacacSDimitry Andric  {
2127a6dacacSDimitry Andric    __lock_.lock();
213cb14a3feSDimitry Andric  }
2147a6dacacSDimitry Andric
2157a6dacacSDimitry Andric  __unlock_guard(const __unlock_guard&)            = delete;
2167a6dacacSDimitry Andric  __unlock_guard& operator=(const __unlock_guard&) = delete;
2170b57cec5SDimitry Andric};
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andrictemplate <class _Lock>
220cb14a3feSDimitry Andricvoid condition_variable_any::wait(_Lock& __lock) {
2210b57cec5SDimitry Andric  shared_ptr<mutex> __mut = __mut_;
2220b57cec5SDimitry Andric  unique_lock<mutex> __lk(*__mut);
2237a6dacacSDimitry Andric  __unlock_guard<_Lock> __unlock(__lock);
22406c3fb27SDimitry Andric  lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
2250b57cec5SDimitry Andric  __cv_.wait(__lk);
2260b57cec5SDimitry Andric} // __mut_.unlock(), __lock.lock()
2270b57cec5SDimitry Andric
2280b57cec5SDimitry Andrictemplate <class _Lock, class _Predicate>
229cb14a3feSDimitry Andricinline void condition_variable_any::wait(_Lock& __lock, _Predicate __pred) {
2300b57cec5SDimitry Andric  while (!__pred())
2310b57cec5SDimitry Andric    wait(__lock);
2320b57cec5SDimitry Andric}
2330b57cec5SDimitry Andric
2340b57cec5SDimitry Andrictemplate <class _Lock, class _Clock, class _Duration>
235cb14a3feSDimitry Andriccv_status condition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t) {
2360b57cec5SDimitry Andric  shared_ptr<mutex> __mut = __mut_;
2370b57cec5SDimitry Andric  unique_lock<mutex> __lk(*__mut);
2387a6dacacSDimitry Andric  __unlock_guard<_Lock> __unlock(__lock);
23906c3fb27SDimitry Andric  lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
2400b57cec5SDimitry Andric  return __cv_.wait_until(__lk, __t);
2410b57cec5SDimitry Andric} // __mut_.unlock(), __lock.lock()
2420b57cec5SDimitry Andric
2430b57cec5SDimitry Andrictemplate <class _Lock, class _Clock, class _Duration, class _Predicate>
244cb14a3feSDimitry Andricinline bool
245cb14a3feSDimitry Andriccondition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred) {
2460b57cec5SDimitry Andric  while (!__pred())
2470b57cec5SDimitry Andric    if (wait_until(__lock, __t) == cv_status::timeout)
2480b57cec5SDimitry Andric      return __pred();
2490b57cec5SDimitry Andric  return true;
2500b57cec5SDimitry Andric}
2510b57cec5SDimitry Andric
2520b57cec5SDimitry Andrictemplate <class _Lock, class _Rep, class _Period>
253cb14a3feSDimitry Andricinline cv_status condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d) {
2540b57cec5SDimitry Andric  return wait_until(__lock, chrono::steady_clock::now() + __d);
2550b57cec5SDimitry Andric}
2560b57cec5SDimitry Andric
2570b57cec5SDimitry Andrictemplate <class _Lock, class _Rep, class _Period, class _Predicate>
258cb14a3feSDimitry Andricinline bool
259cb14a3feSDimitry Andriccondition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred) {
260cb14a3feSDimitry Andric  return wait_until(__lock, chrono::steady_clock::now() + __d, std::move(__pred));
2610b57cec5SDimitry Andric}
2620b57cec5SDimitry Andric
2635f757f3fSDimitry Andric#  if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
2645f757f3fSDimitry Andric
2655f757f3fSDimitry Andrictemplate <class _Lock, class _Predicate>
2667a6dacacSDimitry Andricbool condition_variable_any::wait(_Lock& __user_lock, stop_token __stoken, _Predicate __pred) {
2677a6dacacSDimitry Andric  if (__stoken.stop_requested())
2687a6dacacSDimitry Andric    return __pred();
2697a6dacacSDimitry Andric
2707a6dacacSDimitry Andric  // Per https://eel.is/c++draft/thread.condition.condvarany#general-note-2,
2717a6dacacSDimitry Andric  // we do need to take a copy of the shared pointer __mut_
2727a6dacacSDimitry Andric  // This ensures that a thread can call the destructor immediately after calling
2737a6dacacSDimitry Andric  // notify_all, without waiting all the wait calls.
2747a6dacacSDimitry Andric  // A thread can also safely call the destructor immediately after calling
2757a6dacacSDimitry Andric  // request_stop, as the call to request_stop would evaluate the callback,
2767a6dacacSDimitry Andric  // which accesses the internal condition variable, immediately on the same thread.
2777a6dacacSDimitry Andric  // In this situation, it is OK even without copying a shared ownership the internal
2787a6dacacSDimitry Andric  // condition variable. However, this needs the evaluation of stop_callback to
2797a6dacacSDimitry Andric  // happen-before the destruction.
2807a6dacacSDimitry Andric  // The spec only says "Only the notification to unblock the wait needs to happen
2817a6dacacSDimitry Andric  // before destruction". To make this work, we need to copy the shared ownership of
2827a6dacacSDimitry Andric  // the internal condition variable inside this function, which is not possible
2837a6dacacSDimitry Andric  // with the current ABI.
2847a6dacacSDimitry Andric  shared_ptr<mutex> __mut = __mut_;
2857a6dacacSDimitry Andric
2867a6dacacSDimitry Andric  stop_callback __cb(__stoken, [this] { notify_all(); });
2877a6dacacSDimitry Andric
2887a6dacacSDimitry Andric  while (true) {
2895f757f3fSDimitry Andric    if (__pred())
2905f757f3fSDimitry Andric      return true;
2917a6dacacSDimitry Andric
2927a6dacacSDimitry Andric    // We need to take the internal lock before checking stop_requested,
2937a6dacacSDimitry Andric    // so that the notification cannot come in between the stop_requested
2947a6dacacSDimitry Andric    // check and entering the wait.
2957a6dacacSDimitry Andric    // Note that the stop_callback takes the same internal lock before notifying
2967a6dacacSDimitry Andric    unique_lock<mutex> __internal_lock(*__mut);
2977a6dacacSDimitry Andric    if (__stoken.stop_requested())
2987a6dacacSDimitry Andric      break;
2997a6dacacSDimitry Andric
3007a6dacacSDimitry Andric    __unlock_guard<_Lock> __unlock(__user_lock);
3017a6dacacSDimitry Andric    unique_lock<mutex> __internal_lock2(
3027a6dacacSDimitry Andric        std::move(__internal_lock)); // switch unlock order between __internal_lock and __user_lock
3037a6dacacSDimitry Andric    __cv_.wait(__internal_lock2);
3047a6dacacSDimitry Andric  } // __internal_lock2.unlock(), __user_lock.lock()
3055f757f3fSDimitry Andric  return __pred();
3065f757f3fSDimitry Andric}
3075f757f3fSDimitry Andric
3085f757f3fSDimitry Andrictemplate <class _Lock, class _Clock, class _Duration, class _Predicate>
3095f757f3fSDimitry Andricbool condition_variable_any::wait_until(
3107a6dacacSDimitry Andric    _Lock& __user_lock,
3117a6dacacSDimitry Andric    stop_token __stoken,
3127a6dacacSDimitry Andric    const chrono::time_point<_Clock, _Duration>& __abs_time,
3137a6dacacSDimitry Andric    _Predicate __pred) {
3147a6dacacSDimitry Andric  if (__stoken.stop_requested())
3157a6dacacSDimitry Andric    return __pred();
3167a6dacacSDimitry Andric
3177a6dacacSDimitry Andric  shared_ptr<mutex> __mut = __mut_;
3187a6dacacSDimitry Andric  stop_callback __cb(__stoken, [this] { notify_all(); });
3197a6dacacSDimitry Andric
3207a6dacacSDimitry Andric  while (true) {
3215f757f3fSDimitry Andric    if (__pred())
3225f757f3fSDimitry Andric      return true;
3237a6dacacSDimitry Andric
3247a6dacacSDimitry Andric    unique_lock<mutex> __internal_lock(*__mut);
3257a6dacacSDimitry Andric    if (__stoken.stop_requested())
3267a6dacacSDimitry Andric      break;
3277a6dacacSDimitry Andric
3287a6dacacSDimitry Andric    __unlock_guard<_Lock> __unlock(__user_lock);
3297a6dacacSDimitry Andric    unique_lock<mutex> __internal_lock2(
3307a6dacacSDimitry Andric        std::move(__internal_lock)); // switch unlock order between __internal_lock and __user_lock
3317a6dacacSDimitry Andric
3327a6dacacSDimitry Andric    if (__cv_.wait_until(__internal_lock2, __abs_time) == cv_status::timeout)
3337a6dacacSDimitry Andric      break;
3347a6dacacSDimitry Andric  } // __internal_lock2.unlock(), __user_lock.lock()
3355f757f3fSDimitry Andric  return __pred();
3365f757f3fSDimitry Andric}
3375f757f3fSDimitry Andric
3385f757f3fSDimitry Andrictemplate <class _Lock, class _Rep, class _Period, class _Predicate>
3395f757f3fSDimitry Andricbool condition_variable_any::wait_for(
3405f757f3fSDimitry Andric    _Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred) {
3415f757f3fSDimitry Andric  return wait_until(__lock, std::move(__stoken), chrono::steady_clock::now() + __rel_time, std::move(__pred));
3425f757f3fSDimitry Andric}
3435f757f3fSDimitry Andric
3445f757f3fSDimitry Andric#  endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
3455f757f3fSDimitry Andric
34606c3fb27SDimitry Andric_LIBCPP_EXPORTED_FROM_ABI void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
3470b57cec5SDimitry Andric
3480b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
3490b57cec5SDimitry Andric
3500b57cec5SDimitry Andric#endif // !_LIBCPP_HAS_NO_THREADS
3510b57cec5SDimitry Andric
352*b3edf446SDimitry Andric_LIBCPP_POP_MACROS
353*b3edf446SDimitry Andric
354bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
35506c3fb27SDimitry Andric#  include <atomic>
356bdd1243dSDimitry Andric#  include <concepts>
35706c3fb27SDimitry Andric#  include <cstdint>
35806c3fb27SDimitry Andric#  include <cstdlib>
35906c3fb27SDimitry Andric#  include <cstring>
36006c3fb27SDimitry Andric#  include <initializer_list>
3615f757f3fSDimitry Andric#  include <iosfwd>
36206c3fb27SDimitry Andric#  include <new>
36306c3fb27SDimitry Andric#  include <stdexcept>
36406c3fb27SDimitry Andric#  include <system_error>
365bdd1243dSDimitry Andric#  include <type_traits>
36606c3fb27SDimitry Andric#  include <typeinfo>
367bdd1243dSDimitry Andric#endif
368bdd1243dSDimitry Andric
3690b57cec5SDimitry Andric#endif // _LIBCPP_CONDITION_VARIABLE
370