xref: /freebsd/contrib/llvm-project/libcxx/include/condition_variable (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
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
12181ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler
1225f757f3fSDimitry Andric#include <__availability>
12306c3fb27SDimitry Andric#include <__chrono/duration.h>
12406c3fb27SDimitry Andric#include <__chrono/steady_clock.h>
12506c3fb27SDimitry Andric#include <__chrono/time_point.h>
12606c3fb27SDimitry Andric#include <__condition_variable/condition_variable.h>
1270b57cec5SDimitry Andric#include <__config>
128bdd1243dSDimitry Andric#include <__memory/shared_ptr.h>
12906c3fb27SDimitry Andric#include <__mutex/lock_guard.h>
13006c3fb27SDimitry Andric#include <__mutex/mutex.h>
13106c3fb27SDimitry Andric#include <__mutex/tag_types.h>
13206c3fb27SDimitry Andric#include <__mutex/unique_lock.h>
133*7a6dacacSDimitry Andric#include <__stop_token/stop_callback.h>
1345f757f3fSDimitry Andric#include <__stop_token/stop_token.h>
13506c3fb27SDimitry Andric#include <__utility/move.h>
13604eeddc0SDimitry Andric#include <version>
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1390b57cec5SDimitry Andric#  pragma GCC system_header
1400b57cec5SDimitry Andric#endif
1410b57cec5SDimitry Andric
1420b57cec5SDimitry Andric#ifndef _LIBCPP_HAS_NO_THREADS
1430b57cec5SDimitry Andric
1440b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
1450b57cec5SDimitry Andric
146cb14a3feSDimitry Andricclass _LIBCPP_EXPORTED_FROM_ABI condition_variable_any {
1470b57cec5SDimitry Andric  condition_variable __cv_;
1480b57cec5SDimitry Andric  shared_ptr<mutex> __mut_;
1490b57cec5SDimitry Andric
150cb14a3feSDimitry Andricpublic:
151cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI condition_variable_any();
152cb14a3feSDimitry Andric
153cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT;
154cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT;
1550b57cec5SDimitry Andric
1560b57cec5SDimitry Andric  template <class _Lock>
157cb14a3feSDimitry Andric  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS void wait(_Lock& __lock);
1580b57cec5SDimitry Andric  template <class _Lock, class _Predicate>
159cb14a3feSDimitry Andric  _LIBCPP_HIDE_FROM_ABI void wait(_Lock& __lock, _Predicate __pred);
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric  template <class _Lock, class _Clock, class _Duration>
162cb14a3feSDimitry Andric  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS cv_status
163cb14a3feSDimitry Andric  wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t);
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric  template <class _Lock, class _Clock, class _Duration, class _Predicate>
166cb14a3feSDimitry Andric  bool _LIBCPP_HIDE_FROM_ABI
167cb14a3feSDimitry Andric  wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred);
1680b57cec5SDimitry Andric
1690b57cec5SDimitry Andric  template <class _Lock, class _Rep, class _Period>
170cb14a3feSDimitry Andric  cv_status _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d);
1710b57cec5SDimitry Andric
1720b57cec5SDimitry Andric  template <class _Lock, class _Rep, class _Period, class _Predicate>
173cb14a3feSDimitry Andric  bool _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred);
1745f757f3fSDimitry Andric
1755f757f3fSDimitry Andric#  if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
1765f757f3fSDimitry Andric
1775f757f3fSDimitry Andric  template <class _Lock, class _Predicate>
1785f757f3fSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait(_Lock& __lock, stop_token __stoken, _Predicate __pred);
1795f757f3fSDimitry Andric
1805f757f3fSDimitry Andric  template <class _Lock, class _Clock, class _Duration, class _Predicate>
181cb14a3feSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait_until(
182cb14a3feSDimitry Andric      _Lock& __lock, stop_token __stoken, const chrono::time_point<_Clock, _Duration>& __abs_time, _Predicate __pred);
1835f757f3fSDimitry Andric
1845f757f3fSDimitry Andric  template <class _Lock, class _Rep, class _Period, class _Predicate>
185cb14a3feSDimitry Andric  _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
186cb14a3feSDimitry Andric  wait_for(_Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred);
1875f757f3fSDimitry Andric
1885f757f3fSDimitry Andric#  endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
1890b57cec5SDimitry Andric};
1900b57cec5SDimitry Andric
191cb14a3feSDimitry Andricinline condition_variable_any::condition_variable_any() : __mut_(make_shared<mutex>()) {}
1920b57cec5SDimitry Andric
193cb14a3feSDimitry Andricinline void condition_variable_any::notify_one() _NOEXCEPT {
1940b57cec5SDimitry Andric  { lock_guard<mutex> __lx(*__mut_); }
1950b57cec5SDimitry Andric  __cv_.notify_one();
1960b57cec5SDimitry Andric}
1970b57cec5SDimitry Andric
198cb14a3feSDimitry Andricinline void condition_variable_any::notify_all() _NOEXCEPT {
1990b57cec5SDimitry Andric  { lock_guard<mutex> __lx(*__mut_); }
2000b57cec5SDimitry Andric  __cv_.notify_all();
2010b57cec5SDimitry Andric}
2020b57cec5SDimitry Andric
2030b57cec5SDimitry Andrictemplate <class _Lock>
204*7a6dacacSDimitry Andricstruct __unlock_guard {
205*7a6dacacSDimitry Andric  _Lock& __lock_;
206*7a6dacacSDimitry Andric
207*7a6dacacSDimitry Andric  _LIBCPP_HIDE_FROM_ABI __unlock_guard(_Lock& __lock) : __lock_(__lock) { __lock_.unlock(); }
208*7a6dacacSDimitry Andric
209*7a6dacacSDimitry Andric  _LIBCPP_HIDE_FROM_ABI ~__unlock_guard() _NOEXCEPT // turns exception to std::terminate
210*7a6dacacSDimitry Andric  {
211*7a6dacacSDimitry Andric    __lock_.lock();
212cb14a3feSDimitry Andric  }
213*7a6dacacSDimitry Andric
214*7a6dacacSDimitry Andric  __unlock_guard(const __unlock_guard&)            = delete;
215*7a6dacacSDimitry Andric  __unlock_guard& operator=(const __unlock_guard&) = delete;
2160b57cec5SDimitry Andric};
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andrictemplate <class _Lock>
219cb14a3feSDimitry Andricvoid condition_variable_any::wait(_Lock& __lock) {
2200b57cec5SDimitry Andric  shared_ptr<mutex> __mut = __mut_;
2210b57cec5SDimitry Andric  unique_lock<mutex> __lk(*__mut);
222*7a6dacacSDimitry Andric  __unlock_guard<_Lock> __unlock(__lock);
22306c3fb27SDimitry Andric  lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
2240b57cec5SDimitry Andric  __cv_.wait(__lk);
2250b57cec5SDimitry Andric} // __mut_.unlock(), __lock.lock()
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andrictemplate <class _Lock, class _Predicate>
228cb14a3feSDimitry Andricinline void condition_variable_any::wait(_Lock& __lock, _Predicate __pred) {
2290b57cec5SDimitry Andric  while (!__pred())
2300b57cec5SDimitry Andric    wait(__lock);
2310b57cec5SDimitry Andric}
2320b57cec5SDimitry Andric
2330b57cec5SDimitry Andrictemplate <class _Lock, class _Clock, class _Duration>
234cb14a3feSDimitry Andriccv_status condition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t) {
2350b57cec5SDimitry Andric  shared_ptr<mutex> __mut = __mut_;
2360b57cec5SDimitry Andric  unique_lock<mutex> __lk(*__mut);
237*7a6dacacSDimitry Andric  __unlock_guard<_Lock> __unlock(__lock);
23806c3fb27SDimitry Andric  lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
2390b57cec5SDimitry Andric  return __cv_.wait_until(__lk, __t);
2400b57cec5SDimitry Andric} // __mut_.unlock(), __lock.lock()
2410b57cec5SDimitry Andric
2420b57cec5SDimitry Andrictemplate <class _Lock, class _Clock, class _Duration, class _Predicate>
243cb14a3feSDimitry Andricinline bool
244cb14a3feSDimitry Andriccondition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred) {
2450b57cec5SDimitry Andric  while (!__pred())
2460b57cec5SDimitry Andric    if (wait_until(__lock, __t) == cv_status::timeout)
2470b57cec5SDimitry Andric      return __pred();
2480b57cec5SDimitry Andric  return true;
2490b57cec5SDimitry Andric}
2500b57cec5SDimitry Andric
2510b57cec5SDimitry Andrictemplate <class _Lock, class _Rep, class _Period>
252cb14a3feSDimitry Andricinline cv_status condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d) {
2530b57cec5SDimitry Andric  return wait_until(__lock, chrono::steady_clock::now() + __d);
2540b57cec5SDimitry Andric}
2550b57cec5SDimitry Andric
2560b57cec5SDimitry Andrictemplate <class _Lock, class _Rep, class _Period, class _Predicate>
257cb14a3feSDimitry Andricinline bool
258cb14a3feSDimitry Andriccondition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred) {
259cb14a3feSDimitry Andric  return wait_until(__lock, chrono::steady_clock::now() + __d, std::move(__pred));
2600b57cec5SDimitry Andric}
2610b57cec5SDimitry Andric
2625f757f3fSDimitry Andric#  if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
2635f757f3fSDimitry Andric
2645f757f3fSDimitry Andrictemplate <class _Lock, class _Predicate>
265*7a6dacacSDimitry Andricbool condition_variable_any::wait(_Lock& __user_lock, stop_token __stoken, _Predicate __pred) {
266*7a6dacacSDimitry Andric  if (__stoken.stop_requested())
267*7a6dacacSDimitry Andric    return __pred();
268*7a6dacacSDimitry Andric
269*7a6dacacSDimitry Andric  // Per https://eel.is/c++draft/thread.condition.condvarany#general-note-2,
270*7a6dacacSDimitry Andric  // we do need to take a copy of the shared pointer __mut_
271*7a6dacacSDimitry Andric  // This ensures that a thread can call the destructor immediately after calling
272*7a6dacacSDimitry Andric  // notify_all, without waiting all the wait calls.
273*7a6dacacSDimitry Andric  // A thread can also safely call the destructor immediately after calling
274*7a6dacacSDimitry Andric  // request_stop, as the call to request_stop would evaluate the callback,
275*7a6dacacSDimitry Andric  // which accesses the internal condition variable, immediately on the same thread.
276*7a6dacacSDimitry Andric  // In this situation, it is OK even without copying a shared ownership the internal
277*7a6dacacSDimitry Andric  // condition variable. However, this needs the evaluation of stop_callback to
278*7a6dacacSDimitry Andric  // happen-before the destruction.
279*7a6dacacSDimitry Andric  // The spec only says "Only the notification to unblock the wait needs to happen
280*7a6dacacSDimitry Andric  // before destruction". To make this work, we need to copy the shared ownership of
281*7a6dacacSDimitry Andric  // the internal condition variable inside this function, which is not possible
282*7a6dacacSDimitry Andric  // with the current ABI.
283*7a6dacacSDimitry Andric  shared_ptr<mutex> __mut = __mut_;
284*7a6dacacSDimitry Andric
285*7a6dacacSDimitry Andric  stop_callback __cb(__stoken, [this] { notify_all(); });
286*7a6dacacSDimitry Andric
287*7a6dacacSDimitry Andric  while (true) {
2885f757f3fSDimitry Andric    if (__pred())
2895f757f3fSDimitry Andric      return true;
290*7a6dacacSDimitry Andric
291*7a6dacacSDimitry Andric    // We need to take the internal lock before checking stop_requested,
292*7a6dacacSDimitry Andric    // so that the notification cannot come in between the stop_requested
293*7a6dacacSDimitry Andric    // check and entering the wait.
294*7a6dacacSDimitry Andric    // Note that the stop_callback takes the same internal lock before notifying
295*7a6dacacSDimitry Andric    unique_lock<mutex> __internal_lock(*__mut);
296*7a6dacacSDimitry Andric    if (__stoken.stop_requested())
297*7a6dacacSDimitry Andric      break;
298*7a6dacacSDimitry Andric
299*7a6dacacSDimitry Andric    __unlock_guard<_Lock> __unlock(__user_lock);
300*7a6dacacSDimitry Andric    unique_lock<mutex> __internal_lock2(
301*7a6dacacSDimitry Andric        std::move(__internal_lock)); // switch unlock order between __internal_lock and __user_lock
302*7a6dacacSDimitry Andric    __cv_.wait(__internal_lock2);
303*7a6dacacSDimitry Andric  } // __internal_lock2.unlock(), __user_lock.lock()
3045f757f3fSDimitry Andric  return __pred();
3055f757f3fSDimitry Andric}
3065f757f3fSDimitry Andric
3075f757f3fSDimitry Andrictemplate <class _Lock, class _Clock, class _Duration, class _Predicate>
3085f757f3fSDimitry Andricbool condition_variable_any::wait_until(
309*7a6dacacSDimitry Andric    _Lock& __user_lock,
310*7a6dacacSDimitry Andric    stop_token __stoken,
311*7a6dacacSDimitry Andric    const chrono::time_point<_Clock, _Duration>& __abs_time,
312*7a6dacacSDimitry Andric    _Predicate __pred) {
313*7a6dacacSDimitry Andric  if (__stoken.stop_requested())
314*7a6dacacSDimitry Andric    return __pred();
315*7a6dacacSDimitry Andric
316*7a6dacacSDimitry Andric  shared_ptr<mutex> __mut = __mut_;
317*7a6dacacSDimitry Andric  stop_callback __cb(__stoken, [this] { notify_all(); });
318*7a6dacacSDimitry Andric
319*7a6dacacSDimitry Andric  while (true) {
3205f757f3fSDimitry Andric    if (__pred())
3215f757f3fSDimitry Andric      return true;
322*7a6dacacSDimitry Andric
323*7a6dacacSDimitry Andric    unique_lock<mutex> __internal_lock(*__mut);
324*7a6dacacSDimitry Andric    if (__stoken.stop_requested())
325*7a6dacacSDimitry Andric      break;
326*7a6dacacSDimitry Andric
327*7a6dacacSDimitry Andric    __unlock_guard<_Lock> __unlock(__user_lock);
328*7a6dacacSDimitry Andric    unique_lock<mutex> __internal_lock2(
329*7a6dacacSDimitry Andric        std::move(__internal_lock)); // switch unlock order between __internal_lock and __user_lock
330*7a6dacacSDimitry Andric
331*7a6dacacSDimitry Andric    if (__cv_.wait_until(__internal_lock2, __abs_time) == cv_status::timeout)
332*7a6dacacSDimitry Andric      break;
333*7a6dacacSDimitry Andric  } // __internal_lock2.unlock(), __user_lock.lock()
3345f757f3fSDimitry Andric  return __pred();
3355f757f3fSDimitry Andric}
3365f757f3fSDimitry Andric
3375f757f3fSDimitry Andrictemplate <class _Lock, class _Rep, class _Period, class _Predicate>
3385f757f3fSDimitry Andricbool condition_variable_any::wait_for(
3395f757f3fSDimitry Andric    _Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred) {
3405f757f3fSDimitry Andric  return wait_until(__lock, std::move(__stoken), chrono::steady_clock::now() + __rel_time, std::move(__pred));
3415f757f3fSDimitry Andric}
3425f757f3fSDimitry Andric
3435f757f3fSDimitry Andric#  endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
3445f757f3fSDimitry Andric
34506c3fb27SDimitry Andric_LIBCPP_EXPORTED_FROM_ABI void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
3460b57cec5SDimitry Andric
3470b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
3480b57cec5SDimitry Andric
3490b57cec5SDimitry Andric#endif // !_LIBCPP_HAS_NO_THREADS
3500b57cec5SDimitry Andric
351bdd1243dSDimitry Andric#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
35206c3fb27SDimitry Andric#  include <atomic>
353bdd1243dSDimitry Andric#  include <concepts>
35406c3fb27SDimitry Andric#  include <cstdint>
35506c3fb27SDimitry Andric#  include <cstdlib>
35606c3fb27SDimitry Andric#  include <cstring>
35706c3fb27SDimitry Andric#  include <initializer_list>
3585f757f3fSDimitry Andric#  include <iosfwd>
35906c3fb27SDimitry Andric#  include <new>
36006c3fb27SDimitry Andric#  include <stdexcept>
36106c3fb27SDimitry Andric#  include <system_error>
362bdd1243dSDimitry Andric#  include <type_traits>
36306c3fb27SDimitry Andric#  include <typeinfo>
364bdd1243dSDimitry Andric#endif
365bdd1243dSDimitry Andric
3660b57cec5SDimitry Andric#endif // _LIBCPP_CONDITION_VARIABLE
367