Warning, /include/c++/v1/condition_variable is written in an unsupported language. File is not indexed.
0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009
0010 #ifndef _LIBCPP_CONDITION_VARIABLE
0011 #define _LIBCPP_CONDITION_VARIABLE
0012
0013 /*
0014 condition_variable synopsis
0015
0016 namespace std
0017 {
0018
0019 enum class cv_status { no_timeout, timeout };
0020
0021 class condition_variable
0022 {
0023 public:
0024 condition_variable();
0025 ~condition_variable();
0026
0027 condition_variable(const condition_variable&) = delete;
0028 condition_variable& operator=(const condition_variable&) = delete;
0029
0030 void notify_one() noexcept;
0031 void notify_all() noexcept;
0032
0033 void wait(unique_lock<mutex>& lock);
0034 template <class Predicate>
0035 void wait(unique_lock<mutex>& lock, Predicate pred);
0036
0037 template <class Clock, class Duration>
0038 cv_status
0039 wait_until(unique_lock<mutex>& lock,
0040 const chrono::time_point<Clock, Duration>& abs_time);
0041
0042 template <class Clock, class Duration, class Predicate>
0043 bool
0044 wait_until(unique_lock<mutex>& lock,
0045 const chrono::time_point<Clock, Duration>& abs_time,
0046 Predicate pred);
0047
0048 template <class Rep, class Period>
0049 cv_status
0050 wait_for(unique_lock<mutex>& lock,
0051 const chrono::duration<Rep, Period>& rel_time);
0052
0053 template <class Rep, class Period, class Predicate>
0054 bool
0055 wait_for(unique_lock<mutex>& lock,
0056 const chrono::duration<Rep, Period>& rel_time,
0057 Predicate pred);
0058
0059 typedef pthread_cond_t* native_handle_type;
0060 native_handle_type native_handle();
0061 };
0062
0063 void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
0064
0065 class condition_variable_any
0066 {
0067 public:
0068 condition_variable_any();
0069 ~condition_variable_any();
0070
0071 condition_variable_any(const condition_variable_any&) = delete;
0072 condition_variable_any& operator=(const condition_variable_any&) = delete;
0073
0074 void notify_one() noexcept;
0075 void notify_all() noexcept;
0076
0077 template <class Lock>
0078 void wait(Lock& lock);
0079 template <class Lock, class Predicate>
0080 void wait(Lock& lock, Predicate pred);
0081
0082 template <class Lock, class Clock, class Duration>
0083 cv_status
0084 wait_until(Lock& lock,
0085 const chrono::time_point<Clock, Duration>& abs_time);
0086
0087 template <class Lock, class Clock, class Duration, class Predicate>
0088 bool
0089 wait_until(Lock& lock,
0090 const chrono::time_point<Clock, Duration>& abs_time,
0091 Predicate pred);
0092
0093 template <class Lock, class Rep, class Period>
0094 cv_status
0095 wait_for(Lock& lock,
0096 const chrono::duration<Rep, Period>& rel_time);
0097
0098 template <class Lock, class Rep, class Period, class Predicate>
0099 bool
0100 wait_for(Lock& lock,
0101 const chrono::duration<Rep, Period>& rel_time,
0102 Predicate pred);
0103
0104 // [thread.condvarany.intwait], interruptible waits
0105 template <class Lock, class Predicate>
0106 bool wait(Lock& lock, stop_token stoken, Predicate pred); // since C++20
0107
0108 template <class Lock, class Clock, class Duration, class Predicate>
0109 bool wait_until(Lock& lock, stop_token stoken,
0110 const chrono::time_point<Clock, Duration>& abs_time, Predicate pred); // since C++20
0111
0112 template <class Lock, class Rep, class Period, class Predicate>
0113 bool wait_for(Lock& lock, stop_token stoken,
0114 const chrono::duration<Rep, Period>& rel_time, Predicate pred); // since C++20
0115 };
0116
0117 } // std
0118
0119 */
0120
0121 #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
0122 # include <__cxx03/condition_variable>
0123 #else
0124 # include <__chrono/duration.h>
0125 # include <__chrono/steady_clock.h>
0126 # include <__chrono/time_point.h>
0127 # include <__condition_variable/condition_variable.h>
0128 # include <__config>
0129 # include <__memory/shared_ptr.h>
0130 # include <__mutex/lock_guard.h>
0131 # include <__mutex/mutex.h>
0132 # include <__mutex/tag_types.h>
0133 # include <__mutex/unique_lock.h>
0134 # include <__stop_token/stop_callback.h>
0135 # include <__stop_token/stop_token.h>
0136 # include <__utility/move.h>
0137 # include <version>
0138
0139 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0140 # pragma GCC system_header
0141 # endif
0142
0143 _LIBCPP_PUSH_MACROS
0144 # include <__undef_macros>
0145
0146 # if _LIBCPP_HAS_THREADS
0147
0148 _LIBCPP_BEGIN_NAMESPACE_STD
0149
0150 class _LIBCPP_EXPORTED_FROM_ABI condition_variable_any {
0151 condition_variable __cv_;
0152 shared_ptr<mutex> __mut_;
0153
0154 public:
0155 _LIBCPP_HIDE_FROM_ABI condition_variable_any();
0156
0157 _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT;
0158 _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT;
0159
0160 template <class _Lock>
0161 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS void wait(_Lock& __lock);
0162 template <class _Lock, class _Predicate>
0163 _LIBCPP_HIDE_FROM_ABI void wait(_Lock& __lock, _Predicate __pred);
0164
0165 template <class _Lock, class _Clock, class _Duration>
0166 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS cv_status
0167 wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t);
0168
0169 template <class _Lock, class _Clock, class _Duration, class _Predicate>
0170 bool _LIBCPP_HIDE_FROM_ABI
0171 wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred);
0172
0173 template <class _Lock, class _Rep, class _Period>
0174 cv_status _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d);
0175
0176 template <class _Lock, class _Rep, class _Period, class _Predicate>
0177 bool _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred);
0178
0179 # if _LIBCPP_STD_VER >= 20
0180
0181 template <class _Lock, class _Predicate>
0182 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait(_Lock& __lock, stop_token __stoken, _Predicate __pred);
0183
0184 template <class _Lock, class _Clock, class _Duration, class _Predicate>
0185 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait_until(
0186 _Lock& __lock, stop_token __stoken, const chrono::time_point<_Clock, _Duration>& __abs_time, _Predicate __pred);
0187
0188 template <class _Lock, class _Rep, class _Period, class _Predicate>
0189 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
0190 wait_for(_Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred);
0191
0192 # endif // _LIBCPP_STD_VER >= 20
0193 };
0194
0195 inline condition_variable_any::condition_variable_any() : __mut_(make_shared<mutex>()) {}
0196
0197 inline void condition_variable_any::notify_one() _NOEXCEPT {
0198 { lock_guard<mutex> __lx(*__mut_); }
0199 __cv_.notify_one();
0200 }
0201
0202 inline void condition_variable_any::notify_all() _NOEXCEPT {
0203 { lock_guard<mutex> __lx(*__mut_); }
0204 __cv_.notify_all();
0205 }
0206
0207 template <class _Lock>
0208 struct __unlock_guard {
0209 _Lock& __lock_;
0210
0211 _LIBCPP_HIDE_FROM_ABI __unlock_guard(_Lock& __lock) : __lock_(__lock) { __lock_.unlock(); }
0212
0213 _LIBCPP_HIDE_FROM_ABI ~__unlock_guard() _NOEXCEPT // turns exception to std::terminate
0214 {
0215 __lock_.lock();
0216 }
0217
0218 __unlock_guard(const __unlock_guard&) = delete;
0219 __unlock_guard& operator=(const __unlock_guard&) = delete;
0220 };
0221
0222 template <class _Lock>
0223 void condition_variable_any::wait(_Lock& __lock) {
0224 shared_ptr<mutex> __mut = __mut_;
0225 unique_lock<mutex> __lk(*__mut);
0226 __unlock_guard<_Lock> __unlock(__lock);
0227 lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
0228 __cv_.wait(__lk);
0229 } // __mut_.unlock(), __lock.lock()
0230
0231 template <class _Lock, class _Predicate>
0232 inline void condition_variable_any::wait(_Lock& __lock, _Predicate __pred) {
0233 while (!__pred())
0234 wait(__lock);
0235 }
0236
0237 template <class _Lock, class _Clock, class _Duration>
0238 cv_status condition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t) {
0239 shared_ptr<mutex> __mut = __mut_;
0240 unique_lock<mutex> __lk(*__mut);
0241 __unlock_guard<_Lock> __unlock(__lock);
0242 lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
0243 return __cv_.wait_until(__lk, __t);
0244 } // __mut_.unlock(), __lock.lock()
0245
0246 template <class _Lock, class _Clock, class _Duration, class _Predicate>
0247 inline bool
0248 condition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred) {
0249 while (!__pred())
0250 if (wait_until(__lock, __t) == cv_status::timeout)
0251 return __pred();
0252 return true;
0253 }
0254
0255 template <class _Lock, class _Rep, class _Period>
0256 inline cv_status condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d) {
0257 return wait_until(__lock, chrono::steady_clock::now() + __d);
0258 }
0259
0260 template <class _Lock, class _Rep, class _Period, class _Predicate>
0261 inline bool
0262 condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred) {
0263 return wait_until(__lock, chrono::steady_clock::now() + __d, std::move(__pred));
0264 }
0265
0266 # if _LIBCPP_STD_VER >= 20
0267
0268 template <class _Lock, class _Predicate>
0269 bool condition_variable_any::wait(_Lock& __user_lock, stop_token __stoken, _Predicate __pred) {
0270 if (__stoken.stop_requested())
0271 return __pred();
0272
0273 // Per https://eel.is/c++draft/thread.condition.condvarany#general-note-2,
0274 // we do need to take a copy of the shared pointer __mut_
0275 // This ensures that a thread can call the destructor immediately after calling
0276 // notify_all, without waiting all the wait calls.
0277 // A thread can also safely call the destructor immediately after calling
0278 // request_stop, as the call to request_stop would evaluate the callback,
0279 // which accesses the internal condition variable, immediately on the same thread.
0280 // In this situation, it is OK even without copying a shared ownership the internal
0281 // condition variable. However, this needs the evaluation of stop_callback to
0282 // happen-before the destruction.
0283 // The spec only says "Only the notification to unblock the wait needs to happen
0284 // before destruction". To make this work, we need to copy the shared ownership of
0285 // the internal condition variable inside this function, which is not possible
0286 // with the current ABI.
0287 shared_ptr<mutex> __mut = __mut_;
0288
0289 stop_callback __cb(__stoken, [this] { notify_all(); });
0290
0291 while (true) {
0292 if (__pred())
0293 return true;
0294
0295 // We need to take the internal lock before checking stop_requested,
0296 // so that the notification cannot come in between the stop_requested
0297 // check and entering the wait.
0298 // Note that the stop_callback takes the same internal lock before notifying
0299 unique_lock<mutex> __internal_lock(*__mut);
0300 if (__stoken.stop_requested())
0301 break;
0302
0303 __unlock_guard<_Lock> __unlock(__user_lock);
0304 unique_lock<mutex> __internal_lock2(
0305 std::move(__internal_lock)); // switch unlock order between __internal_lock and __user_lock
0306 __cv_.wait(__internal_lock2);
0307 } // __internal_lock2.unlock(), __user_lock.lock()
0308 return __pred();
0309 }
0310
0311 template <class _Lock, class _Clock, class _Duration, class _Predicate>
0312 bool condition_variable_any::wait_until(
0313 _Lock& __user_lock,
0314 stop_token __stoken,
0315 const chrono::time_point<_Clock, _Duration>& __abs_time,
0316 _Predicate __pred) {
0317 if (__stoken.stop_requested())
0318 return __pred();
0319
0320 shared_ptr<mutex> __mut = __mut_;
0321 stop_callback __cb(__stoken, [this] { notify_all(); });
0322
0323 while (true) {
0324 if (__pred())
0325 return true;
0326
0327 unique_lock<mutex> __internal_lock(*__mut);
0328 if (__stoken.stop_requested())
0329 break;
0330
0331 __unlock_guard<_Lock> __unlock(__user_lock);
0332 unique_lock<mutex> __internal_lock2(
0333 std::move(__internal_lock)); // switch unlock order between __internal_lock and __user_lock
0334
0335 if (__cv_.wait_until(__internal_lock2, __abs_time) == cv_status::timeout)
0336 break;
0337 } // __internal_lock2.unlock(), __user_lock.lock()
0338 return __pred();
0339 }
0340
0341 template <class _Lock, class _Rep, class _Period, class _Predicate>
0342 bool condition_variable_any::wait_for(
0343 _Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred) {
0344 return wait_until(__lock, std::move(__stoken), chrono::steady_clock::now() + __rel_time, std::move(__pred));
0345 }
0346
0347 # endif // _LIBCPP_STD_VER >= 20
0348
0349 _LIBCPP_EXPORTED_FROM_ABI void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
0350
0351 _LIBCPP_END_NAMESPACE_STD
0352
0353 # endif // _LIBCPP_HAS_THREADS
0354
0355 _LIBCPP_POP_MACROS
0356
0357 # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
0358 # include <atomic>
0359 # include <concepts>
0360 # include <cstdint>
0361 # include <cstdlib>
0362 # include <cstring>
0363 # include <initializer_list>
0364 # include <iosfwd>
0365 # include <new>
0366 # include <stdexcept>
0367 # include <system_error>
0368 # include <type_traits>
0369 # include <typeinfo>
0370 # endif
0371 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
0372
0373 #endif // _LIBCPP_CONDITION_VARIABLE