Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
condition_variable273 linesDownload Raw Back to include
1// -*- C++ -*-2//===---------------------- condition_variable ----------------------------===//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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCUDACXX_CONDITION_VARIABLE11#define _LIBCUDACXX_CONDITION_VARIABLE12 13/*14    condition_variable synopsis15 16namespace std17{18 19enum class cv_status { no_timeout, timeout };20 21class condition_variable22{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_status39        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        bool44        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_status50        wait_for(unique_lock<mutex>& lock,51                 const chrono::duration<Rep, Period>& rel_time);52 53    template <class Rep, class Period, class Predicate>54        bool55        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_any66{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_status84        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        bool89        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_status95        wait_for(Lock& lock,96                 const chrono::duration<Rep, Period>& rel_time);97 98    template <class Lock, class Rep, class Period, class Predicate>99        bool100        wait_for(Lock& lock,101                 const chrono::duration<Rep, Period>& rel_time,102                 Predicate pred);103};104 105}  // std106 107*/108 109#include <__config>110#include <__mutex_base>111#include <memory>112 113#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)114#  pragma GCC system_header115#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)116#  pragma clang system_header117#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)118#  pragma system_header119#endif // no system header120 121#ifndef _LIBCUDACXX_HAS_NO_THREADS122 123_LIBCUDACXX_BEGIN_NAMESPACE_STD124 125class _LIBCUDACXX_TYPE_VIS condition_variable_any126{127    condition_variable __cv_;128    shared_ptr<mutex>  __mut_;129public:130    _LIBCUDACXX_INLINE_VISIBILITY131    condition_variable_any();132 133    _LIBCUDACXX_INLINE_VISIBILITY134    void notify_one() noexcept;135    _LIBCUDACXX_INLINE_VISIBILITY136    void notify_all() noexcept;137 138    template <class _Lock>139        _LIBCUDACXX_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS140        void wait(_Lock& __lock);141    template <class _Lock, class _Predicate>142        _LIBCUDACXX_INLINE_VISIBILITY143        void wait(_Lock& __lock, _Predicate __pred);144 145    template <class _Lock, class _Clock, class _Duration>146        _LIBCUDACXX_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS147        cv_status148        wait_until(_Lock& __lock,149                   const chrono::time_point<_Clock, _Duration>& __t);150 151    template <class _Lock, class _Clock, class _Duration, class _Predicate>152        bool153        _LIBCUDACXX_INLINE_VISIBILITY154        wait_until(_Lock& __lock,155                   const chrono::time_point<_Clock, _Duration>& __t,156                   _Predicate __pred);157 158    template <class _Lock, class _Rep, class _Period>159        cv_status160        _LIBCUDACXX_INLINE_VISIBILITY161        wait_for(_Lock& __lock,162                 const chrono::duration<_Rep, _Period>& __d);163 164    template <class _Lock, class _Rep, class _Period, class _Predicate>165        bool166        _LIBCUDACXX_INLINE_VISIBILITY167        wait_for(_Lock& __lock,168                 const chrono::duration<_Rep, _Period>& __d,169                 _Predicate __pred);170};171 172inline173condition_variable_any::condition_variable_any()174    : __mut_(make_shared<mutex>()) {}175 176inline177void178condition_variable_any::notify_one() noexcept179{180    {lock_guard<mutex> __lx(*__mut_);}181    __cv_.notify_one();182}183 184inline185void186condition_variable_any::notify_all() noexcept187{188    {lock_guard<mutex> __lx(*__mut_);}189    __cv_.notify_all();190}191 192struct __lock_external193{194    template <class _Lock>195    void operator()(_Lock* __m) {__m->lock();}196};197 198template <class _Lock>199void200condition_variable_any::wait(_Lock& __lock)201{202    shared_ptr<mutex> __mut = __mut_;203    unique_lock<mutex> __lk(*__mut);204    __lock.unlock();205    unique_ptr<_Lock, __lock_external> __lxx(&__lock);206    lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);207    __cv_.wait(__lk);208}  // __mut_.unlock(), __lock.lock()209 210template <class _Lock, class _Predicate>211inline212void213condition_variable_any::wait(_Lock& __lock, _Predicate __pred)214{215    while (!__pred())216        wait(__lock);217}218 219template <class _Lock, class _Clock, class _Duration>220cv_status221condition_variable_any::wait_until(_Lock& __lock,222                                   const chrono::time_point<_Clock, _Duration>& __t)223{224    shared_ptr<mutex> __mut = __mut_;225    unique_lock<mutex> __lk(*__mut);226    __lock.unlock();227    unique_ptr<_Lock, __lock_external> __lxx(&__lock);228    lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock);229    return __cv_.wait_until(__lk, __t);230}  // __mut_.unlock(), __lock.lock()231 232template <class _Lock, class _Clock, class _Duration, class _Predicate>233inline234bool235condition_variable_any::wait_until(_Lock& __lock,236                                   const chrono::time_point<_Clock, _Duration>& __t,237                                   _Predicate __pred)238{239    while (!__pred())240        if (wait_until(__lock, __t) == cv_status::timeout)241            return __pred();242    return true;243}244 245template <class _Lock, class _Rep, class _Period>246inline247cv_status248condition_variable_any::wait_for(_Lock& __lock,249                                 const chrono::duration<_Rep, _Period>& __d)250{251    return wait_until(__lock, chrono::steady_clock::now() + __d);252}253 254template <class _Lock, class _Rep, class _Period, class _Predicate>255inline256bool257condition_variable_any::wait_for(_Lock& __lock,258                                 const chrono::duration<_Rep, _Period>& __d,259                                 _Predicate __pred)260{261    return wait_until(__lock, chrono::steady_clock::now() + __d,262                      _CUDA_VSTD::move(__pred));263}264 265_LIBCUDACXX_FUNC_VIS266void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);267 268_LIBCUDACXX_END_NAMESPACE_STD269 270#endif // !_LIBCUDACXX_HAS_NO_THREADS271 272#endif  // _LIBCUDACXX_CONDITION_VARIABLE273 
codekingpro/portable-devtools · Team Ai