Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/c++/v1/__cxx03/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___CXX03_CONDITION_VARIABLE
0011 #define _LIBCPP___CXX03_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 #include <__cxx03/__chrono/duration.h>
0122 #include <__cxx03/__chrono/steady_clock.h>
0123 #include <__cxx03/__chrono/time_point.h>
0124 #include <__cxx03/__condition_variable/condition_variable.h>
0125 #include <__cxx03/__config>
0126 #include <__cxx03/__memory/shared_ptr.h>
0127 #include <__cxx03/__mutex/lock_guard.h>
0128 #include <__cxx03/__mutex/mutex.h>
0129 #include <__cxx03/__mutex/tag_types.h>
0130 #include <__cxx03/__mutex/unique_lock.h>
0131 #include <__cxx03/__stop_token/stop_callback.h>
0132 #include <__cxx03/__stop_token/stop_token.h>
0133 #include <__cxx03/__utility/move.h>
0134 #include <__cxx03/version>
0135 
0136 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0137 #  pragma GCC system_header
0138 #endif
0139 
0140 _LIBCPP_PUSH_MACROS
0141 #include <__cxx03/__undef_macros>
0142 
0143 #ifndef _LIBCPP_HAS_NO_THREADS
0144 
0145 _LIBCPP_BEGIN_NAMESPACE_STD
0146 
0147 class _LIBCPP_EXPORTED_FROM_ABI condition_variable_any {
0148   condition_variable __cv_;
0149   shared_ptr<mutex> __mut_;
0150 
0151 public:
0152   _LIBCPP_HIDE_FROM_ABI condition_variable_any();
0153 
0154   _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT;
0155   _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT;
0156 
0157   template <class _Lock>
0158   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS void wait(_Lock& __lock);
0159   template <class _Lock, class _Predicate>
0160   _LIBCPP_HIDE_FROM_ABI void wait(_Lock& __lock, _Predicate __pred);
0161 
0162   template <class _Lock, class _Clock, class _Duration>
0163   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS cv_status
0164   wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t);
0165 
0166   template <class _Lock, class _Clock, class _Duration, class _Predicate>
0167   bool _LIBCPP_HIDE_FROM_ABI
0168   wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred);
0169 
0170   template <class _Lock, class _Rep, class _Period>
0171   cv_status _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d);
0172 
0173   template <class _Lock, class _Rep, class _Period, class _Predicate>
0174   bool _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred);
0175 
0176 #  if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
0177 
0178   template <class _Lock, class _Predicate>
0179   _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait(_Lock& __lock, stop_token __stoken, _Predicate __pred);
0180 
0181   template <class _Lock, class _Clock, class _Duration, class _Predicate>
0182   _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool wait_until(
0183       _Lock& __lock, stop_token __stoken, const chrono::time_point<_Clock, _Duration>& __abs_time, _Predicate __pred);
0184 
0185   template <class _Lock, class _Rep, class _Period, class _Predicate>
0186   _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
0187   wait_for(_Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred);
0188 
0189 #  endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
0190 };
0191 
0192 inline condition_variable_any::condition_variable_any() : __mut_(make_shared<mutex>()) {}
0193 
0194 inline void condition_variable_any::notify_one() _NOEXCEPT {
0195   { lock_guard<mutex> __lx(*__mut_); }
0196   __cv_.notify_one();
0197 }
0198 
0199 inline void condition_variable_any::notify_all() _NOEXCEPT {
0200   { lock_guard<mutex> __lx(*__mut_); }
0201   __cv_.notify_all();
0202 }
0203 
0204 template <class _Lock>
0205 struct __unlock_guard {
0206   _Lock& __lock_;
0207 
0208   _LIBCPP_HIDE_FROM_ABI __unlock_guard(_Lock& __lock) : __lock_(__lock) { __lock_.unlock(); }
0209 
0210   _LIBCPP_HIDE_FROM_ABI ~__unlock_guard() _NOEXCEPT // turns exception to std::terminate
0211   {
0212     __lock_.lock();
0213   }
0214 
0215   __unlock_guard(const __unlock_guard&)            = delete;
0216   __unlock_guard& operator=(const __unlock_guard&) = delete;
0217 };
0218 
0219 template <class _Lock>
0220 void condition_variable_any::wait(_Lock& __lock) {
0221   shared_ptr<mutex> __mut = __mut_;
0222   unique_lock<mutex> __lk(*__mut);
0223   __unlock_guard<_Lock> __unlock(__lock);
0224   lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
0225   __cv_.wait(__lk);
0226 } // __mut_.unlock(), __lock.lock()
0227 
0228 template <class _Lock, class _Predicate>
0229 inline void condition_variable_any::wait(_Lock& __lock, _Predicate __pred) {
0230   while (!__pred())
0231     wait(__lock);
0232 }
0233 
0234 template <class _Lock, class _Clock, class _Duration>
0235 cv_status condition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t) {
0236   shared_ptr<mutex> __mut = __mut_;
0237   unique_lock<mutex> __lk(*__mut);
0238   __unlock_guard<_Lock> __unlock(__lock);
0239   lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());
0240   return __cv_.wait_until(__lk, __t);
0241 } // __mut_.unlock(), __lock.lock()
0242 
0243 template <class _Lock, class _Clock, class _Duration, class _Predicate>
0244 inline bool
0245 condition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred) {
0246   while (!__pred())
0247     if (wait_until(__lock, __t) == cv_status::timeout)
0248       return __pred();
0249   return true;
0250 }
0251 
0252 template <class _Lock, class _Rep, class _Period>
0253 inline cv_status condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d) {
0254   return wait_until(__lock, chrono::steady_clock::now() + __d);
0255 }
0256 
0257 template <class _Lock, class _Rep, class _Period, class _Predicate>
0258 inline bool
0259 condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred) {
0260   return wait_until(__lock, chrono::steady_clock::now() + __d, std::move(__pred));
0261 }
0262 
0263 #  if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
0264 
0265 template <class _Lock, class _Predicate>
0266 bool condition_variable_any::wait(_Lock& __user_lock, stop_token __stoken, _Predicate __pred) {
0267   if (__stoken.stop_requested())
0268     return __pred();
0269 
0270   // Per https://eel.is/c++draft/thread.condition.condvarany#general-note-2,
0271   // we do need to take a copy of the shared pointer __mut_
0272   // This ensures that a thread can call the destructor immediately after calling
0273   // notify_all, without waiting all the wait calls.
0274   // A thread can also safely call the destructor immediately after calling
0275   // request_stop, as the call to request_stop would evaluate the callback,
0276   // which accesses the internal condition variable, immediately on the same thread.
0277   // In this situation, it is OK even without copying a shared ownership the internal
0278   // condition variable. However, this needs the evaluation of stop_callback to
0279   // happen-before the destruction.
0280   // The spec only says "Only the notification to unblock the wait needs to happen
0281   // before destruction". To make this work, we need to copy the shared ownership of
0282   // the internal condition variable inside this function, which is not possible
0283   // with the current ABI.
0284   shared_ptr<mutex> __mut = __mut_;
0285 
0286   stop_callback __cb(__stoken, [this] { notify_all(); });
0287 
0288   while (true) {
0289     if (__pred())
0290       return true;
0291 
0292     // We need to take the internal lock before checking stop_requested,
0293     // so that the notification cannot come in between the stop_requested
0294     // check and entering the wait.
0295     // Note that the stop_callback takes the same internal lock before notifying
0296     unique_lock<mutex> __internal_lock(*__mut);
0297     if (__stoken.stop_requested())
0298       break;
0299 
0300     __unlock_guard<_Lock> __unlock(__user_lock);
0301     unique_lock<mutex> __internal_lock2(
0302         std::move(__internal_lock)); // switch unlock order between __internal_lock and __user_lock
0303     __cv_.wait(__internal_lock2);
0304   } // __internal_lock2.unlock(), __user_lock.lock()
0305   return __pred();
0306 }
0307 
0308 template <class _Lock, class _Clock, class _Duration, class _Predicate>
0309 bool condition_variable_any::wait_until(
0310     _Lock& __user_lock,
0311     stop_token __stoken,
0312     const chrono::time_point<_Clock, _Duration>& __abs_time,
0313     _Predicate __pred) {
0314   if (__stoken.stop_requested())
0315     return __pred();
0316 
0317   shared_ptr<mutex> __mut = __mut_;
0318   stop_callback __cb(__stoken, [this] { notify_all(); });
0319 
0320   while (true) {
0321     if (__pred())
0322       return true;
0323 
0324     unique_lock<mutex> __internal_lock(*__mut);
0325     if (__stoken.stop_requested())
0326       break;
0327 
0328     __unlock_guard<_Lock> __unlock(__user_lock);
0329     unique_lock<mutex> __internal_lock2(
0330         std::move(__internal_lock)); // switch unlock order between __internal_lock and __user_lock
0331 
0332     if (__cv_.wait_until(__internal_lock2, __abs_time) == cv_status::timeout)
0333       break;
0334   } // __internal_lock2.unlock(), __user_lock.lock()
0335   return __pred();
0336 }
0337 
0338 template <class _Lock, class _Rep, class _Period, class _Predicate>
0339 bool condition_variable_any::wait_for(
0340     _Lock& __lock, stop_token __stoken, const chrono::duration<_Rep, _Period>& __rel_time, _Predicate __pred) {
0341   return wait_until(__lock, std::move(__stoken), chrono::steady_clock::now() + __rel_time, std::move(__pred));
0342 }
0343 
0344 #  endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_STOP_TOKEN)
0345 
0346 _LIBCPP_EXPORTED_FROM_ABI void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
0347 
0348 _LIBCPP_END_NAMESPACE_STD
0349 
0350 #endif // !_LIBCPP_HAS_NO_THREADS
0351 
0352 _LIBCPP_POP_MACROS
0353 
0354 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
0355 #  include <__cxx03/atomic>
0356 #  include <__cxx03/concepts>
0357 #  include <__cxx03/cstdint>
0358 #  include <__cxx03/cstdlib>
0359 #  include <__cxx03/cstring>
0360 #  include <__cxx03/initializer_list>
0361 #  include <__cxx03/iosfwd>
0362 #  include <__cxx03/new>
0363 #  include <__cxx03/stdexcept>
0364 #  include <__cxx03/system_error>
0365 #  include <__cxx03/type_traits>
0366 #  include <__cxx03/typeinfo>
0367 #endif
0368 
0369 #endif // _LIBCPP___CXX03_CONDITION_VARIABLE