dope_core/executor/task/context/
mod.rs1use core::{cell, mem, pin, task::Poll};
2
3use o3::base::cell::region;
4
5use crate::{
6 driver::{
7 self,
8 schedule::{
9 app,
10 ready::{self, wake::Waker},
11 },
12 },
13 executor::task,
14};
15pub mod raw;
16pub mod wake;
17
18mod pollcall;
19
20pub use pollcall::PollCall;
21
22#[derive(Clone, Copy, Eq, PartialEq)]
23pub enum Requeue {
24 Idle,
25 Requested,
26}
27
28const _: () = {
29 assert!(mem::size_of::<Requeue>() == mem::size_of::<bool>());
30 assert!(mem::align_of::<Requeue>() == mem::align_of::<bool>());
31};
32
33#[must_use = "a continuation either carries a permit or reports exhaustion"]
35pub enum Continue<'poll, 'd> {
36 Permit(app::Permit<'poll, 'd>),
37 Exhausted,
38}
39
40const _: () = {
41 assert!(mem::size_of::<Continue<'static, 'static>>() == mem::size_of::<u8>());
42};
43
44#[must_use = "a poll either carries its result or reports exhaustion"]
46pub enum PollResult<T> {
47 Polled(Poll<T>),
48 Exhausted,
49}
50
51const _: () = {
52 assert!(mem::size_of::<PollResult<()>>() == mem::size_of::<Option<Poll<()>>>());
53};
54
55pub struct Context<'poll, 'd: 'poll> {
56 wake: wake::Waker<'d>,
57 root: wake::RootWaker<'d>,
58 work: app::Work<'poll, 'd>,
59 driver: &'poll mut driver::Local<'d>,
60 requeue: cell::Cell<Requeue>,
61}
62
63impl<'poll, 'd: 'poll> Context<'poll, 'd> {
64 pub(crate) fn from_waker(
65 wake: wake::Waker<'d>,
66 root: wake::RootWaker<'d>,
67 work: app::Work<'poll, 'd>,
68 driver: &'poll mut driver::Local<'d>,
69 ) -> Self {
70 Self {
71 wake,
72 root,
73 work,
74 driver,
75 requeue: cell::Cell::new(Requeue::Idle),
76 }
77 }
78
79 pub fn new(
81 target: ready::Target<'d>,
82 work: app::Work<'poll, 'd>,
83 driver: &'poll mut driver::Local<'d>,
84 ) -> Self {
85 let root = wake::RootWaker::from(target);
86 Self::from_waker(root.into(), root, work, driver)
87 }
88
89 pub fn admitted<'call, 'turn>(
90 &'call mut self,
91 _permit: app::Permit<'turn, 'd>,
92 ) -> raw::Admission<'call, 'poll, 'd>
93 where
94 'turn: 'poll,
95 {
96 raw::Admission { context: self }
97 }
98
99 pub const fn waker(&self) -> wake::Waker<'d> {
100 self.wake
101 }
102
103 pub fn root_waker(&self) -> wake::RootWaker<'d> {
106 self.root
107 }
108
109 pub fn completion(&self) -> Waker<'d> {
111 self.wake.completion()
112 }
113
114 pub fn work(&self) -> app::Work<'poll, 'd> {
115 self.work
116 }
117
118 pub fn driver(&mut self) -> &mut driver::Local<'d> {
119 self.driver
120 }
121
122 pub fn poll<'call, F>(
125 &'call mut self,
126 fiber: pin::Pin<&'call mut task::Task<F>>,
127 ) -> Poll<F::Output>
128 where
129 F: task::Fiber<'d>,
130 {
131 match self.try_poll(fiber) {
132 PollResult::Polled(poll) => poll,
133 PollResult::Exhausted => Poll::Pending,
134 }
135 }
136
137 pub fn try_poll<'call, F>(
140 &'call mut self,
141 fiber: pin::Pin<&'call mut task::Task<F>>,
142 ) -> PollResult<F::Output>
143 where
144 F: task::Fiber<'d>,
145 {
146 let mut state = fiber.project().state;
147 let Some(fiber) = state.as_mut().as_pin_mut() else {
148 return PollResult::Polled(Poll::Pending);
149 };
150 let Continue::Permit(_permit) = self.try_continue() else {
151 return PollResult::Exhausted;
152 };
153 let result = F::poll(PollCall {
154 fiber,
155 context: self,
156 });
157 if result.is_ready() {
158 state.set(None);
159 }
160 PollResult::Polled(result)
161 }
162
163 pub fn try_continue(&self) -> Continue<'poll, 'd> {
166 match self.work.permit() {
167 Some(permit) => Continue::Permit(permit),
168 None => {
169 self.requeue();
170 Continue::Exhausted
171 }
172 }
173 }
174
175 pub(crate) fn child<'call>(&'call mut self, wake: wake::Waker<'d>) -> Context<'call, 'd> {
176 Context::from_waker(wake, self.root, self.work, &mut *self.driver)
177 }
178
179 pub fn region_token(&mut self) -> &mut region::Token<region::Identity<'d>> {
180 self.driver.region_token()
181 }
182
183 pub fn wake(&self) {
184 self.wake.wake();
185 }
186
187 pub(crate) fn requeue(&self) {
191 self.requeue.set(Requeue::Requested);
192 }
193
194 pub fn into_requeue(self) -> Requeue {
195 self.requeue.replace(Requeue::Idle)
196 }
197}
198
199impl<'poll, 'd: 'poll> Drop for Context<'poll, 'd> {
200 fn drop(&mut self) {
201 if self.requeue.get() == Requeue::Requested {
202 self.wake.wake();
203 }
204 }
205}