librf
sleep.h
1 //Э̵Ķʱ
2 //ʱȡˣ timer_canceled_exception 쳣
3 //ʹco_awaitsleep_for/sleep_untilǴ÷ܴﵽȴĿġһЧĶʱޱҪڴʹ
4 //
5 #pragma once
6 
7 namespace resumef
8 {
17  future_t<> sleep_until_(std::chrono::system_clock::time_point tp_, scheduler_t& scheduler_);
18 
25  inline future_t<> sleep_for_(std::chrono::system_clock::duration dt_, scheduler_t& scheduler_)
26  {
27  return sleep_until_(std::chrono::system_clock::now() + dt_, scheduler_);
28  }
29 
36  template<class _Rep, class _Period>
37  inline future_t<> sleep_for(std::chrono::duration<_Rep, _Period> dt_, scheduler_t& scheduler_)
38  {
39  return sleep_for_(std::chrono::duration_cast<std::chrono::system_clock::duration>(dt_), scheduler_);
40  }
41 
48  template<class _Clock, class _Duration = typename _Clock::duration>
49  inline future_t<> sleep_until(std::chrono::time_point<_Clock, _Duration> tp_, scheduler_t& scheduler_)
50  {
51  return sleep_until_(std::chrono::time_point_cast<std::chrono::system_clock::duration>(tp_), scheduler_);
52  }
53 
60  template<class _Rep, class _Period>
61  inline future_t<> sleep_for(std::chrono::duration<_Rep, _Period> dt_)
62  {
63  scheduler_t* sch = current_scheduler();
64  co_await sleep_for_(std::chrono::duration_cast<std::chrono::system_clock::duration>(dt_), *sch);
65  }
66 
73  template<class _Clock, class _Duration>
74  inline future_t<> sleep_until(std::chrono::time_point<_Clock, _Duration> tp_)
75  {
76  scheduler_t* sch = current_scheduler();
77  co_await sleep_until_(std::chrono::time_point_cast<std::chrono::system_clock::duration>(tp_), *sch);
78  }
79 
86  template <class Rep, class Period>
87  inline future_t<> operator co_await(std::chrono::duration<Rep, Period> dt_)
88  {
89  scheduler_t* sch = current_scheduler();
90  co_await sleep_for(dt_, *sch);
91  }
92 
93 }