2010-05-11 19:42:16 +00:00
|
|
|
// -*- C++ -*-
|
2021-11-17 21:25:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-05-11 19:42:16 +00:00
|
|
|
//
|
2019-01-19 10:56:40 +00:00
|
|
|
// 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
|
2010-05-11 19:42:16 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
2012-07-21 16:32:53 +00:00
|
|
|
void notify_one() noexcept;
|
|
|
|
void notify_all() noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
|
|
|
|
|
2010-05-11 19:42:16 +00:00
|
|
|
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;
|
|
|
|
|
2012-07-21 16:32:53 +00:00
|
|
|
void notify_one() noexcept;
|
|
|
|
void notify_all() noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
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);
|
2023-06-21 16:40:07 +00:00
|
|
|
|
|
|
|
// [thread.condvarany.intwait], interruptible waits
|
|
|
|
template <class Lock, class Predicate>
|
|
|
|
bool wait(Lock& lock, stop_token stoken, Predicate pred); // since C++20
|
|
|
|
|
|
|
|
template <class Lock, class Clock, class Duration, class Predicate>
|
|
|
|
bool wait_until(Lock& lock, stop_token stoken,
|
|
|
|
const chrono::time_point<Clock, Duration>& abs_time, Predicate pred); // since C++20
|
|
|
|
|
|
|
|
template <class Lock, class Rep, class Period, class Predicate>
|
|
|
|
bool wait_for(Lock& lock, stop_token stoken,
|
|
|
|
const chrono::duration<Rep, Period>& rel_time, Predicate pred); // since C++20
|
2010-05-11 19:42:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // std
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2022-03-25 16:55:36 +00:00
|
|
|
#include <__assert> // all public C++ headers provide the assertion handler
|
2023-06-21 16:40:07 +00:00
|
|
|
#include <__availability>
|
2023-03-09 00:14:03 +00:00
|
|
|
#include <__chrono/duration.h>
|
|
|
|
#include <__chrono/steady_clock.h>
|
|
|
|
#include <__chrono/time_point.h>
|
|
|
|
#include <__condition_variable/condition_variable.h>
|
2010-05-11 19:42:16 +00:00
|
|
|
#include <__config>
|
2022-09-04 22:01:15 +00:00
|
|
|
#include <__memory/shared_ptr.h>
|
|
|
|
#include <__memory/unique_ptr.h>
|
2023-03-09 00:14:03 +00:00
|
|
|
#include <__mutex/lock_guard.h>
|
|
|
|
#include <__mutex/mutex.h>
|
|
|
|
#include <__mutex/tag_types.h>
|
|
|
|
#include <__mutex/unique_lock.h>
|
2023-06-21 16:40:07 +00:00
|
|
|
#include <__stop_token/stop_token.h>
|
2023-03-09 00:14:03 +00:00
|
|
|
#include <__utility/move.h>
|
2021-12-22 17:14:14 +00:00
|
|
|
#include <version>
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2011-10-17 20:05:10 +00:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-02 01:16:40 +00:00
|
|
|
# pragma GCC system_header
|
2011-10-17 20:05:10 +00:00
|
|
|
#endif
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2014-09-05 19:45:05 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_THREADS
|
|
|
|
|
2010-05-11 19:42:16 +00:00
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2023-06-14 17:17:50 +00:00
|
|
|
class _LIBCPP_EXPORTED_FROM_ABI condition_variable_any {
|
2010-05-11 19:42:16 +00:00
|
|
|
condition_variable __cv_;
|
|
|
|
shared_ptr<mutex> __mut_;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-05-11 19:42:16 +00:00
|
|
|
public:
|
|
|
|
_LIBCPP_HIDE_FROM_ABI condition_variable_any();
|
|
|
|
|
2012-07-21 16:32:53 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT;
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
template <class _Lock>
|
2017-03-02 03:22:18 +00:00
|
|
|
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS void wait(_Lock& __lock);
|
2010-05-11 19:42:16 +00:00
|
|
|
template <class _Lock, class _Predicate>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void wait(_Lock& __lock, _Predicate __pred);
|
|
|
|
|
|
|
|
template <class _Lock, class _Clock, class _Duration>
|
2017-03-02 03:22:18 +00:00
|
|
|
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS cv_status
|
2010-05-11 19:42:16 +00:00
|
|
|
wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t);
|
|
|
|
|
|
|
|
template <class _Lock, class _Clock, class _Duration, class _Predicate>
|
2015-11-07 01:22:13 +00:00
|
|
|
bool _LIBCPP_HIDE_FROM_ABI
|
2010-05-11 19:42:16 +00:00
|
|
|
wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred);
|
|
|
|
|
|
|
|
template <class _Lock, class _Rep, class _Period>
|
|
|
|
cv_status _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d);
|
|
|
|
|
|
|
|
template <class _Lock, class _Rep, class _Period, class _Predicate>
|
|
|
|
bool _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred);
|
2023-06-21 16:40:07 +00:00
|
|
|
|
|
|
|
# if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
|
|
|
|
|
|
|
|
template <class _Lock, class _Predicate>
|
|
|
|
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait(_Lock& __lock, stop_token __stoken, _Predicate __pred);
|
|
|
|
|
|
|
|
template <class _Lock, class _Clock, class _Duration, class _Predicate>
|
|
|
|
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait_until(
|
|
|
|
_Lock& __lock, stop_token __stoken, const chrono::time_point<_Clock, _Duration>& __abs_time, _Predicate __pred);
|
|
|
|
|
|
|
|
template <class _Lock, class _Rep, class _Period, class _Predicate>
|
|
|
|
_LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
|
|
|
|
wait_for(_Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred);
|
|
|
|
|
|
|
|
# endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
|
2010-05-11 19:42:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline condition_variable_any::condition_variable_any() : __mut_(make_shared<mutex>()) {}
|
|
|
|
|
2012-07-21 16:32:53 +00:00
|
|
|
inline void condition_variable_any::notify_one() _NOEXCEPT {
|
2012-10-30 19:06:59 +00:00
|
|
|
{ lock_guard<mutex> __lx(*__mut_); }
|
2010-05-11 19:42:16 +00:00
|
|
|
__cv_.notify_one();
|
|
|
|
}
|
|
|
|
|
2012-07-21 16:32:53 +00:00
|
|
|
inline void condition_variable_any::notify_all() _NOEXCEPT {
|
2012-10-30 19:06:59 +00:00
|
|
|
{ lock_guard<mutex> __lx(*__mut_); }
|
2010-05-11 19:42:16 +00:00
|
|
|
__cv_.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct __lock_external {
|
|
|
|
template <class _Lock>
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void operator()(_Lock* __m) {
|
|
|
|
__m->lock();
|
|
|
|
}
|
2010-05-11 19:42:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Lock>
|
|
|
|
void condition_variable_any::wait(_Lock& __lock) {
|
|
|
|
shared_ptr<mutex> __mut = __mut_;
|
|
|
|
unique_lock<mutex> __lk(*__mut);
|
|
|
|
__lock.unlock();
|
2012-10-30 19:06:59 +00:00
|
|
|
unique_ptr<_Lock, __lock_external> __lxx(&__lock);
|
2023-03-06 20:43:26 +00:00
|
|
|
lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
|
2010-05-11 19:42:16 +00:00
|
|
|
__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();
|
2012-10-30 19:06:59 +00:00
|
|
|
unique_ptr<_Lock, __lock_external> __lxx(&__lock);
|
2023-03-06 20:43:26 +00:00
|
|
|
lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
|
2010-05-11 19:42:16 +00:00
|
|
|
return __cv_.wait_until(__lk, __t);
|
|
|
|
} // __mut_.unlock(), __lock.lock()
|
|
|
|
|
|
|
|
template <class _Lock, class _Clock, class _Duration, class _Predicate>
|
2015-11-07 01:22:13 +00:00
|
|
|
inline bool
|
2010-05-11 19:42:16 +00:00
|
|
|
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) {
|
2010-11-20 19:16:30 +00:00
|
|
|
return wait_until(__lock, chrono::steady_clock::now() + __d);
|
2010-05-11 19:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Lock, class _Rep, class _Period, class _Predicate>
|
2015-11-07 01:22:13 +00:00
|
|
|
inline bool
|
2010-05-11 19:42:16 +00:00
|
|
|
condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred) {
|
2010-11-20 19:16:30 +00:00
|
|
|
return wait_until(__lock, chrono::steady_clock::now() + __d, std::move(__pred));
|
2010-05-11 19:42:16 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 16:40:07 +00:00
|
|
|
# if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
|
|
|
|
|
|
|
|
template <class _Lock, class _Predicate>
|
|
|
|
bool condition_variable_any::wait(_Lock& __lock, stop_token __stoken, _Predicate __pred) {
|
|
|
|
while (!__stoken.stop_requested()) {
|
|
|
|
if (__pred())
|
|
|
|
return true;
|
|
|
|
wait(__lock);
|
|
|
|
}
|
|
|
|
return __pred();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Lock, class _Clock, class _Duration, class _Predicate>
|
|
|
|
bool condition_variable_any::wait_until(
|
|
|
|
_Lock& __lock, stop_token __stoken, const chrono::time_point<_Clock, _Duration>& __abs_time, _Predicate __pred) {
|
|
|
|
while (!__stoken.stop_requested()) {
|
|
|
|
if (__pred())
|
|
|
|
return true;
|
|
|
|
if (wait_until(__lock, __abs_time) == cv_status::timeout)
|
|
|
|
return __pred();
|
|
|
|
}
|
|
|
|
return __pred();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Lock, class _Rep, class _Period, class _Predicate>
|
|
|
|
bool condition_variable_any::wait_for(
|
|
|
|
_Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred) {
|
|
|
|
return wait_until(__lock, std::move(__stoken), chrono::steady_clock::now() + __rel_time, std::move(__pred));
|
|
|
|
}
|
|
|
|
|
|
|
|
# endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
|
|
|
|
|
2023-06-14 17:17:50 +00:00
|
|
|
_LIBCPP_EXPORTED_FROM_ABI void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
|
2010-09-03 21:46:37 +00:00
|
|
|
|
2010-05-11 19:42:16 +00:00
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
2014-09-05 19:45:05 +00:00
|
|
|
#endif // !_LIBCPP_HAS_NO_THREADS
|
|
|
|
|
2022-11-02 19:27:42 +00:00
|
|
|
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
|
2023-01-31 18:23:30 +00:00
|
|
|
# include <atomic>
|
2022-11-02 19:27:42 +00:00
|
|
|
# include <concepts>
|
2023-03-09 00:14:03 +00:00
|
|
|
# include <cstdint>
|
|
|
|
# include <cstdlib>
|
|
|
|
# include <cstring>
|
|
|
|
# include <initializer_list>
|
2023-10-29 17:31:37 +00:00
|
|
|
# include <iosfwd>
|
2023-03-09 00:14:03 +00:00
|
|
|
# include <new>
|
|
|
|
# include <stdexcept>
|
2023-04-08 15:29:31 +00:00
|
|
|
# include <system_error>
|
2022-12-20 23:07:17 +00:00
|
|
|
# include <type_traits>
|
2023-03-09 00:14:03 +00:00
|
|
|
# include <typeinfo>
|
2022-11-02 19:27:42 +00:00
|
|
|
#endif
|
|
|
|
|
2021-04-20 16:03:32 +00:00
|
|
|
#endif // _LIBCPP_CONDITION_VARIABLE
|