// -*- C++ -*- //===---------------------- condition_variable ----------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #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; void notify_one() noexcept; void notify_all() noexcept; void wait(unique_lock& lock); template void wait(unique_lock& lock, Predicate pred); template cv_status wait_until(unique_lock& lock, const chrono::time_point& abs_time); template bool wait_until(unique_lock& lock, const chrono::time_point& abs_time, Predicate pred); template cv_status wait_for(unique_lock& lock, const chrono::duration& rel_time); template bool wait_for(unique_lock& lock, const chrono::duration& rel_time, Predicate pred); typedef pthread_cond_t* native_handle_type; native_handle_type native_handle(); }; void notify_all_at_thread_exit(condition_variable& cond, unique_lock lk); 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; void notify_one() noexcept; void notify_all() noexcept; template void wait(Lock& lock); template void wait(Lock& lock, Predicate pred); template cv_status wait_until(Lock& lock, const chrono::time_point& abs_time); template bool wait_until(Lock& lock, const chrono::time_point& abs_time, Predicate pred); template cv_status wait_for(Lock& lock, const chrono::duration& rel_time); template bool wait_for(Lock& lock, const chrono::duration& rel_time, Predicate pred); }; } // std */ #include <__config> #include <__mutex_base> #include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) #pragma GCC system_header #endif #if !_LIBCPP_SINGLE_THREADED _LIBCPP_BEGIN_NAMESPACE_STD class _LIBCPP_TYPE_VIS condition_variable_any { condition_variable __cv_; shared_ptr __mut_; public: condition_variable_any(); void notify_one() _NOEXCEPT; void notify_all() _NOEXCEPT; template void wait(_Lock& __lock); template void wait(_Lock& __lock, _Predicate __pred); template cv_status wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t); template bool wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred); template cv_status wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d); template bool wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred); }; inline _LIBCPP_INLINE_VISIBILITY condition_variable_any::condition_variable_any() : __mut_(make_shared()) {} inline _LIBCPP_INLINE_VISIBILITY void condition_variable_any::notify_one() _NOEXCEPT { {lock_guard __lx(*__mut_);} __cv_.notify_one(); } inline _LIBCPP_INLINE_VISIBILITY void condition_variable_any::notify_all() _NOEXCEPT { {lock_guard __lx(*__mut_);} __cv_.notify_all(); } struct __lock_external { template void operator()(_Lock* __m) {__m->lock();} }; template void condition_variable_any::wait(_Lock& __lock) { shared_ptr __mut = __mut_; unique_lock __lk(*__mut); __lock.unlock(); unique_ptr<_Lock, __lock_external> __lxx(&__lock); lock_guard > __lx(__lk, adopt_lock); __cv_.wait(__lk); } // __mut_.unlock(), __lock.lock() template inline _LIBCPP_INLINE_VISIBILITY void condition_variable_any::wait(_Lock& __lock, _Predicate __pred) { while (!__pred()) wait(__lock); } template cv_status condition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t) { shared_ptr __mut = __mut_; unique_lock __lk(*__mut); __lock.unlock(); unique_ptr<_Lock, __lock_external> __lxx(&__lock); lock_guard > __lx(__lk, adopt_lock); return __cv_.wait_until(__lk, __t); } // __mut_.unlock(), __lock.lock() template inline _LIBCPP_INLINE_VISIBILITY bool 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 inline _LIBCPP_INLINE_VISIBILITY cv_status condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d) { return wait_until(__lock, chrono::steady_clock::now() + __d); } template inline _LIBCPP_INLINE_VISIBILITY bool condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred) { return wait_until(__lock, chrono::steady_clock::now() + __d, _VSTD::move(__pred)); } _LIBCPP_FUNC_VIS void notify_all_at_thread_exit(condition_variable& cond, unique_lock lk); _LIBCPP_END_NAMESPACE_STD #endif // !_LIBCPP_SINGLE_THREADED #endif // _LIBCPP_CONDITION_VARIABLE