brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.6 KiB · 45b8214 Raw
268 lines · plain
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CXX03_CONDITION_VARIABLE11#define _LIBCPP___CXX03_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    // [thread.condvarany.intwait], interruptible waits105    template <class Lock, class Predicate>106      bool wait(Lock& lock, stop_token stoken, Predicate pred);                               // since C++20107 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++20111 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++20115};116 117}  // std118 119*/120 121#include <__cxx03/__chrono/duration.h>122#include <__cxx03/__chrono/steady_clock.h>123#include <__cxx03/__chrono/time_point.h>124#include <__cxx03/__condition_variable/condition_variable.h>125#include <__cxx03/__config>126#include <__cxx03/__memory/shared_ptr.h>127#include <__cxx03/__mutex/lock_guard.h>128#include <__cxx03/__mutex/mutex.h>129#include <__cxx03/__mutex/tag_types.h>130#include <__cxx03/__mutex/unique_lock.h>131#include <__cxx03/__utility/move.h>132#include <__cxx03/version>133 134#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)135#  pragma GCC system_header136#endif137 138_LIBCPP_PUSH_MACROS139#include <__cxx03/__undef_macros>140 141#ifndef _LIBCPP_HAS_NO_THREADS142 143_LIBCPP_BEGIN_NAMESPACE_STD144 145class _LIBCPP_EXPORTED_FROM_ABI condition_variable_any {146  condition_variable __cv_;147  shared_ptr<mutex> __mut_;148 149public:150  _LIBCPP_HIDE_FROM_ABI condition_variable_any();151 152  _LIBCPP_HIDE_FROM_ABI void notify_one() _NOEXCEPT;153  _LIBCPP_HIDE_FROM_ABI void notify_all() _NOEXCEPT;154 155  template <class _Lock>156  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS void wait(_Lock& __lock);157  template <class _Lock, class _Predicate>158  _LIBCPP_HIDE_FROM_ABI void wait(_Lock& __lock, _Predicate __pred);159 160  template <class _Lock, class _Clock, class _Duration>161  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS cv_status162  wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t);163 164  template <class _Lock, class _Clock, class _Duration, class _Predicate>165  bool _LIBCPP_HIDE_FROM_ABI166  wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred);167 168  template <class _Lock, class _Rep, class _Period>169  cv_status _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d);170 171  template <class _Lock, class _Rep, class _Period, class _Predicate>172  bool _LIBCPP_HIDE_FROM_ABI wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred);173};174 175inline condition_variable_any::condition_variable_any() : __mut_(make_shared<mutex>()) {}176 177inline void condition_variable_any::notify_one() _NOEXCEPT {178  { lock_guard<mutex> __lx(*__mut_); }179  __cv_.notify_one();180}181 182inline void condition_variable_any::notify_all() _NOEXCEPT {183  { lock_guard<mutex> __lx(*__mut_); }184  __cv_.notify_all();185}186 187template <class _Lock>188struct __unlock_guard {189  _Lock& __lock_;190 191  _LIBCPP_HIDE_FROM_ABI __unlock_guard(_Lock& __lock) : __lock_(__lock) { __lock_.unlock(); }192 193  _LIBCPP_HIDE_FROM_ABI ~__unlock_guard() _NOEXCEPT // turns exception to std::terminate194  {195    __lock_.lock();196  }197 198  __unlock_guard(const __unlock_guard&)            = delete;199  __unlock_guard& operator=(const __unlock_guard&) = delete;200};201 202template <class _Lock>203void condition_variable_any::wait(_Lock& __lock) {204  shared_ptr<mutex> __mut = __mut_;205  unique_lock<mutex> __lk(*__mut);206  __unlock_guard<_Lock> __unlock(__lock);207  lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());208  __cv_.wait(__lk);209} // __mut_.unlock(), __lock.lock()210 211template <class _Lock, class _Predicate>212inline void condition_variable_any::wait(_Lock& __lock, _Predicate __pred) {213  while (!__pred())214    wait(__lock);215}216 217template <class _Lock, class _Clock, class _Duration>218cv_status condition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t) {219  shared_ptr<mutex> __mut = __mut_;220  unique_lock<mutex> __lk(*__mut);221  __unlock_guard<_Lock> __unlock(__lock);222  lock_guard<unique_lock<mutex> > __lx(__lk, adopt_lock_t());223  return __cv_.wait_until(__lk, __t);224} // __mut_.unlock(), __lock.lock()225 226template <class _Lock, class _Clock, class _Duration, class _Predicate>227inline bool228condition_variable_any::wait_until(_Lock& __lock, const chrono::time_point<_Clock, _Duration>& __t, _Predicate __pred) {229  while (!__pred())230    if (wait_until(__lock, __t) == cv_status::timeout)231      return __pred();232  return true;233}234 235template <class _Lock, class _Rep, class _Period>236inline cv_status condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d) {237  return wait_until(__lock, chrono::steady_clock::now() + __d);238}239 240template <class _Lock, class _Rep, class _Period, class _Predicate>241inline bool242condition_variable_any::wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred) {243  return wait_until(__lock, chrono::steady_clock::now() + __d, std::move(__pred));244}245 246_LIBCPP_EXPORTED_FROM_ABI void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);247 248_LIBCPP_END_NAMESPACE_STD249 250#endif // !_LIBCPP_HAS_NO_THREADS251 252_LIBCPP_POP_MACROS253 254#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)255#  include <__cxx03/atomic>256#  include <__cxx03/cstdint>257#  include <__cxx03/cstdlib>258#  include <__cxx03/cstring>259#  include <__cxx03/iosfwd>260#  include <__cxx03/new>261#  include <__cxx03/stdexcept>262#  include <__cxx03/system_error>263#  include <__cxx03/type_traits>264#  include <__cxx03/typeinfo>265#endif266 267#endif // _LIBCPP___CXX03_CONDITION_VARIABLE268