librf

current_scheduler.h
1 #pragma once
2
3 namespace resumef
4 {
8 struct get_current_scheduler_awaitor
9 {
10 bool await_ready() const noexcept
11 {
12 return false;
13 }
14 template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
15 bool await_suspend(coroutine_handle<_PromiseT> handler)
16 {
17 _PromiseT& promise = handler.promise();
18 auto* state = promise.get_state();
19 this->_scheduler = state->get_scheduler();
20
21 return false;
22 }
23 scheduler_t* await_resume() const noexcept
24 {
25 return _scheduler;
26 }
27 private:
28 scheduler_t* _scheduler;
29 #ifdef DOXYGEN_SKIP_PROPERTY
30 public:
38 static get_current_scheduler_awaitor get_current_scheduler() noexcept;
39
47 static scheduler_t* current_scheduler() noexcept;
48 #endif //DOXYGEN_SKIP_PROPERTY
49 };
50
57 inline get_current_scheduler_awaitor get_current_scheduler() noexcept
58 {
59 return {};
60 }
61
62
66 struct get_root_state_awaitor
67 {
68 bool await_ready() const noexcept
69 {
70 return false;
71 }
72 template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
73 bool await_suspend(coroutine_handle<_PromiseT> handler)
74 {
75 _PromiseT& promise = handler.promise();
76 auto* parent = promise.get_state();
77 this->_state = parent->get_root();
78
79 return false;
80 }
81 state_base_t* await_resume() const noexcept
82 {
83 return _state;
84 }
85 private:
86 state_base_t* _state;
87 #ifdef DOXYGEN_SKIP_PROPERTY
88 public:
96 static get_root_state_awaitor get_root_state() noexcept;
97
105 static state_base_t* root_state() noexcept;
106 #endif //DOXYGEN_SKIP_PROPERTY
107 };
108
115 inline get_root_state_awaitor get_root_state() noexcept
116 {
117 return {};
118 }
119
123 struct get_current_task_awaitor
124 {
125 bool await_ready() const noexcept
126 {
127 return false;
128 }
129 template<class _PromiseT, typename = std::enable_if_t<traits::is_promise_v<_PromiseT>>>
130 bool await_suspend(coroutine_handle<_PromiseT> handler)
131 {
132 _PromiseT& promise = handler.promise();
133 auto* parent = promise.get_state();
134 state_base_t * state = parent->get_root();
135 scheduler_t* sch = state->get_scheduler();
136
137 this->_task = sch->find_task(state);
138
139 return false;
140 }
141 task_t* await_resume() const noexcept
142 {
143 return _task;
144 }
145 private:
146 task_t* _task;
147 #ifdef DOXYGEN_SKIP_PROPERTY
148 public:
156 static get_root_state_awaitor get_current_task() noexcept;
157
165 static task_t* current_task() noexcept;
166 #endif //DOXYGEN_SKIP_PROPERTY
167 };
168
175 inline get_current_task_awaitor get_current_task() noexcept
176 {
177 return {};
178 }
179 }
resumef::get_root_state_awaitor
获得本协程绑定的跟state指针的可等待对象。
Definition: current_scheduler.h:66
resumef::get_current_scheduler_awaitor::current_scheduler
static scheduler_t * current_scheduler() noexcept
获得当前协程绑定的调度器。
resumef::get_current_scheduler_awaitor
获得本协程绑定的调度器的可等待对象。
Definition: current_scheduler.h:8
resumef::scheduler_t
协程调度器。
Definition: scheduler.h:10
resumef::get_current_task_awaitor::current_task
static task_t * current_task() noexcept
获得当前协程的task_t指针。
resumef::get_current_task_awaitor
获得本协程的task_t对象。
Definition: current_scheduler.h:123
resumef::get_root_state_awaitor::get_root_state
static get_root_state_awaitor get_root_state() noexcept
获得当前协程的跟state指针。
resumef::get_current_task_awaitor::get_current_task
static get_root_state_awaitor get_current_task() noexcept
获得当前协程的task_t指针。
resumef::state_base_t
state基类，state用于在协程的promise和future之间共享数据。
Definition: state.h:8
resumef::get_root_state_awaitor::root_state
static state_base_t * root_state() noexcept
获得当前协程的跟state指针。
resumef::get_current_scheduler_awaitor::get_current_scheduler
static get_current_scheduler_awaitor get_current_scheduler() noexcept
获得当前协程绑定的调度器。