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