-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathfuture.rs
More file actions
215 lines (191 loc) · 6.84 KB
/
future.rs
File metadata and controls
215 lines (191 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Future for submitting operations to the runtime.
use std::{
cell::RefCell,
future::Future,
marker::PhantomData,
pin::Pin,
rc::Rc,
task::{Context, Poll, Waker},
};
use compio_buf::BufResult;
use compio_driver::{Extra, Key, OpCode, Proactor, PushEntry};
use futures_util::future::FusedFuture;
use crate::{
CancelToken,
future::{poll_task, poll_task_with_extra, submit_raw},
waker::{get_ext, get_waker},
};
pub(crate) trait ContextExt {
/// Remove all wrapped [`ExtWaker`] and return the underlying waker.
///
/// This is the same as calling [`Context::waker`] if the waker was never
/// wrapped.
fn get_waker(&self) -> &Waker;
/// Get the cancel token
fn get_cancel(&mut self) -> Option<&CancelToken>;
/// Set the ext data associated with the waker to an [`Extra`].
fn as_extra(&mut self, default: impl FnOnce() -> Extra) -> Option<Extra>;
}
impl ContextExt for Context<'_> {
fn get_waker(&self) -> &Waker {
get_waker(self.waker())
}
fn get_cancel(&mut self) -> Option<&CancelToken> {
get_ext(self.waker())?.get_cancel()
}
fn as_extra(&mut self, default: impl FnOnce() -> Extra) -> Option<Extra> {
let ext = get_ext(self.waker())?;
let mut extra = default();
ext.set_extra(&mut extra);
Some(extra)
}
}
pin_project_lite::pin_project! {
/// Returned [`Future`] for [`Runtime::submit`].
///
/// When this is dropped and the operation hasn't finished yet, it will try to
/// cancel the operation.
///
/// By default, this implements `Future<Output = BufResult<usize, T>>`. If
/// [`Extra`] is needed, call [`.with_extra()`] to get a `Submit<T, Extra>`
/// which implements `Future<Output = (BufResult<usize, T>, Extra)>`.
///
/// [`.with_extra()`]: Submit::with_extra
pub struct Submit<T: OpCode, E = ()> {
driver: Rc<RefCell<Proactor>>,
state: Option<State<T, E>>,
}
impl<T: OpCode, E> PinnedDrop for Submit<T, E> {
fn drop(this: Pin<&mut Self>) {
let this = this.project();
if let Some(State::Submitted { key, .. }) = this.state.take() {
this.driver.borrow_mut().cancel(key);
}
}
}
}
enum State<T: OpCode, E> {
Idle { op: T },
Submitted { key: Key<T>, _p: PhantomData<E> },
}
impl<T: OpCode, E> State<T, E> {
fn submitted(key: Key<T>) -> Self {
State::Submitted {
key,
_p: PhantomData,
}
}
}
impl<T: OpCode> Submit<T, ()> {
pub(crate) fn new(driver: Rc<RefCell<Proactor>>, op: T) -> Self {
Submit {
driver,
state: Some(State::Idle { op }),
}
}
/// Convert this future to one that returns [`Extra`] along with the result.
///
/// This is useful if you need to access extra information provided by the
/// runtime upon completion of the operation.
pub fn with_extra(mut self) -> Submit<T, Extra> {
let driver = self.driver.clone();
let Some(state) = self.state.take() else {
return Submit {
driver,
state: None,
};
};
let state = match state {
State::Submitted { key, .. } => State::Submitted {
key,
_p: PhantomData,
},
State::Idle { op } => State::Idle { op },
};
Submit {
driver,
state: Some(state),
}
}
}
impl<T: OpCode + 'static> Future for Submit<T, ()> {
type Output = BufResult<usize, T>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
loop {
match this.state.take().expect("Cannot poll after ready") {
State::Submitted { key, .. } => {
let entry = poll_task(&mut this.driver.borrow_mut(), cx.get_waker(), key);
match entry {
PushEntry::Pending(key) => {
*this.state = Some(State::submitted(key));
return Poll::Pending;
}
PushEntry::Ready(res) => return Poll::Ready(res),
}
}
State::Idle { op } => {
let extra = cx.as_extra(|| this.driver.borrow().default_extra());
let entry = submit_raw(&mut this.driver.borrow_mut(), op, extra);
match entry {
PushEntry::Pending(key) => {
// TODO: Should we register it only the first time or every time it's
// being polled?
if let Some(cancel) = cx.get_cancel() {
cancel.register(&key);
};
*this.state = Some(State::submitted(key))
}
PushEntry::Ready(res) => {
return Poll::Ready(res);
}
}
}
}
}
}
}
impl<T: OpCode + 'static> Future for Submit<T, Extra> {
type Output = (BufResult<usize, T>, Extra);
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
loop {
match this.state.take().expect("Cannot poll after ready") {
State::Submitted { key, .. } => {
let entry =
poll_task_with_extra(&mut this.driver.borrow_mut(), cx.get_waker(), key);
match entry {
PushEntry::Pending(key) => {
*this.state = Some(State::submitted(key));
return Poll::Pending;
}
PushEntry::Ready(res) => return Poll::Ready(res),
}
}
State::Idle { op } => {
let extra = cx.as_extra(|| this.driver.borrow().default_extra());
let entry = submit_raw(&mut this.driver.borrow_mut(), op, extra);
match entry {
PushEntry::Pending(key) => {
if let Some(cancel) = cx.get_cancel() {
cancel.register(&key);
}
*this.state = Some(State::submitted(key))
}
PushEntry::Ready(res) => {
return Poll::Ready((res, this.driver.borrow().default_extra()));
}
}
}
}
}
}
}
impl<T: OpCode, E> FusedFuture for Submit<T, E>
where
Submit<T, E>: Future,
{
fn is_terminated(&self) -> bool {
self.state.is_none()
}
}