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
use std::{
cell::UnsafeCell,
future::Future,
pin::Pin,
task::{Context, Poll, Waker},
};
use super::{
raw::{self, Vtable},
state::State,
utils::UnsafeCellExt,
Schedule,
};
#[repr(C)]
pub(crate) struct Cell<T: Future, S> {
pub(crate) header: Header,
pub(crate) core: Core<T, S>,
pub(crate) trailer: Trailer,
}
pub(crate) struct Core<T: Future, S> {
/// Scheduler used to drive this future
pub(crate) scheduler: S,
/// Either the future or the output
pub(crate) stage: CoreStage<T>,
}
pub(crate) struct CoreStage<T: Future> {
stage: UnsafeCell<Stage<T>>,
}
pub(crate) enum Stage<T: Future> {
Running(T),
Finished(T::Output),
Consumed,
}
#[repr(C)]
pub(crate) struct Header {
/// State
pub(crate) state: State,
/// Table of function pointers for executing actions on the task.
pub(crate) vtable: &'static Vtable,
/// Thread ID(sync: used for wake task on its thread; sync disabled: do checking)
pub(crate) owner_id: usize,
}
pub(crate) struct Trailer {
/// Consumer task waiting on completion of this task.
pub(crate) waker: UnsafeCell<Option<Waker>>,
}
impl<T: Future, S: Schedule> Cell<T, S> {
/// Allocates a new task cell, containing the header, trailer, and core
/// structures.
pub(crate) fn new(owner_id: usize, future: T, scheduler: S) -> Box<Cell<T, S>> {
Box::new(Cell {
header: Header {
state: State::new(),
vtable: raw::vtable::<T, S>(),
owner_id,
},
core: Core {
scheduler,
stage: CoreStage {
stage: UnsafeCell::new(Stage::Running(future)),
},
},
trailer: Trailer {
waker: UnsafeCell::new(None),
},
})
}
}
impl<T: Future> CoreStage<T> {
pub(crate) fn with_mut<R>(&self, f: impl FnOnce(*mut Stage<T>) -> R) -> R {
self.stage.with_mut(f)
}
pub(crate) fn poll(&self, mut cx: Context<'_>) -> Poll<T::Output> {
let res = {
self.with_mut(|ptr| {
// Safety: The caller ensures mutual exclusion to the field.
let future = match unsafe { &mut *ptr } {
Stage::Running(future) => future,
_ => unreachable!("unexpected stage"),
};
// Safety: The caller ensures the future is pinned.
let future = unsafe { Pin::new_unchecked(future) };
future.poll(&mut cx)
})
};
if res.is_ready() {
self.drop_future_or_output();
}
res
}
/// Drop the future
///
/// # Safety
///
/// The caller must ensure it is safe to mutate the `stage` field.
pub(crate) fn drop_future_or_output(&self) {
// Safety: the caller ensures mutual exclusion to the field.
unsafe {
self.set_stage(Stage::Consumed);
}
}
/// Store the task output
///
/// # Safety
///
/// The caller must ensure it is safe to mutate the `stage` field.
pub(crate) fn store_output(&self, output: T::Output) {
// Safety: the caller ensures mutual exclusion to the field.
unsafe {
self.set_stage(Stage::Finished(output));
}
}
/// Take the task output
///
/// # Safety
///
/// The caller must ensure it is safe to mutate the `stage` field.
pub(crate) fn take_output(&self) -> T::Output {
use std::mem;
self.with_mut(|ptr| {
// Safety:: the caller ensures mutual exclusion to the field.
match mem::replace(unsafe { &mut *ptr }, Stage::Consumed) {
Stage::Finished(output) => output,
_ => panic!("JoinHandle polled after completion"),
}
})
}
unsafe fn set_stage(&self, stage: Stage<T>) {
self.with_mut(|ptr| *ptr = stage)
}
}
impl Header {
#[allow(unused)]
pub(crate) fn get_owner_id(&self) -> usize {
// safety: If there are concurrent writes, then that write has violated
// the safety requirements on `set_owner_id`.
self.owner_id
}
}
impl Trailer {
pub(crate) unsafe fn set_waker(&self, waker: Option<Waker>) {
self.waker.with_mut(|ptr| {
*ptr = waker;
});
}
pub(crate) unsafe fn will_wake(&self, waker: &Waker) -> bool {
self.waker
.with(|ptr| (*ptr).as_ref().unwrap().will_wake(waker))
}
pub(crate) fn wake_join(&self) {
self.waker.with(|ptr| match unsafe { &*ptr } {
Some(waker) => waker.wake_by_ref(),
None => panic!("waker missing"),
});
}
}