-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection_49_async_runtime.ltl
More file actions
270 lines (227 loc) · 8.77 KB
/
Copy pathsection_49_async_runtime.ltl
File metadata and controls
270 lines (227 loc) · 8.77 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Tutorial 49 — Building an Async Runtime
// Futures, wakers, an event loop, and a task scheduler.
use std.collections.{HashMap, VecDeque}
use std.sync.{Arc, Mutex}
// ═══════════════════════════════════════════════════════════════════════════════
// Core Async Primitives
// ═══════════════════════════════════════════════════════════════════════════════
enum Poll<T> { Ready(T), Pending }
trait Future {
type Output
fn poll(mut self, cx: &mut Context) -> Poll<Self.Output>
}
type TaskId = u64
struct Waker {
task_id: TaskId,
wake_queue: Arc<Mutex<VecDeque<TaskId>>>,
}
impl Waker {
fn wake(self) {
let mut queue = self.wake_queue.lock().unwrap()
if !queue.contains(&self.task_id) { queue.push_back(self.task_id) }
}
fn clone(self) -> Waker {
Waker { task_id: self.task_id, wake_queue: self.wake_queue.clone() }
}
}
struct Context { waker: Waker }
impl Context {
fn waker(self) -> &Waker { &self.waker }
}
// ═══════════════════════════════════════════════════════════════════════════════
// Task and Executor
// ═══════════════════════════════════════════════════════════════════════════════
struct Task {
id: TaskId,
future: Box<dyn Future<Output = ()>>,
completed: bool,
}
struct Executor {
tasks: HashMap<TaskId, Task>,
ready_queue: Arc<Mutex<VecDeque<TaskId>>>,
next_id: TaskId,
}
impl Executor {
fn new() -> Executor {
Executor {
tasks: HashMap.new(),
ready_queue: Arc.new(Mutex.new(VecDeque.new())),
next_id: 1,
}
}
fn spawn<F: Future<Output = ()> + 'static>(mut self, future: F) -> TaskId {
let id = self.next_id
self.next_id += 1
self.tasks.insert(id, Task { id, future: Box.new(future), completed: false })
self.ready_queue.lock().unwrap().push_back(id)
id
}
fn run(mut self) {
loop {
let task_id = {
let mut queue = self.ready_queue.lock().unwrap()
queue.pop_front()
}
match task_id {
some(id) => {
if let some(task) = self.tasks.get_mut(&id) {
if task.completed { continue }
let waker = Waker {
task_id: id,
wake_queue: self.ready_queue.clone(),
}
let mut cx = Context { waker }
match task.future.poll(&mut cx) {
Poll.Ready(()) => { task.completed = true }
Poll.Pending => {} // will be re-queued via waker
}
}
}
none => {
// Check if all tasks done
if self.tasks.values().all(|t| t.completed) { break }
// In a real runtime, we'd park the thread here
std.thread.yield_now()
}
}
}
}
fn task_count(self) -> usize { self.tasks.len() }
fn completed_count(self) -> usize { self.tasks.values().filter(|t| t.completed).count() }
}
// ═══════════════════════════════════════════════════════════════════════════════
// Combinators
// ═══════════════════════════════════════════════════════════════════════════════
// Ready future — immediately resolves
struct Ready<T> { value: Option<T> }
impl<T> Future for Ready<T> {
type Output = T
fn poll(mut self, _cx: &mut Context) -> Poll<T> {
Poll.Ready(self.value.take().unwrap())
}
}
fn ready<T>(value: T) -> Ready<T> { Ready { value: some(value) } }
// Yield future — yields once, then completes
struct YieldNow { yielded: bool }
impl Future for YieldNow {
type Output = ()
fn poll(mut self, cx: &mut Context) -> Poll<()> {
if self.yielded { Poll.Ready(()) }
else { self.yielded = true; cx.waker().wake(); Poll.Pending }
}
}
fn yield_now() -> YieldNow { YieldNow { yielded: false } }
// Map future
struct Map<F: Future, Func> {
future: F,
func: Option<Func>,
}
impl<F: Future, Func: FnOnce(F.Output) -> U, U> Future for Map<F, Func> {
type Output = U
fn poll(mut self, cx: &mut Context) -> Poll<U> {
match self.future.poll(cx) {
Poll.Ready(val) => Poll.Ready(self.func.take().unwrap()(val)),
Poll.Pending => Poll.Pending,
}
}
}
// Then (flat map / and_then)
enum ThenState<F1, F2> { First(F1), Second(F2) }
struct Then<F1: Future, F2: Future, Func> {
state: ThenState<F1, F2>,
func: Option<Func>,
}
impl<F1: Future, F2: Future, Func: FnOnce(F1.Output) -> F2> Future for Then<F1, F2, Func> {
type Output = F2.Output
fn poll(mut self, cx: &mut Context) -> Poll<F2.Output> {
match &mut self.state {
ThenState.First(f1) => match f1.poll(cx) {
Poll.Ready(val) => {
let f2 = self.func.take().unwrap()(val)
self.state = ThenState.Second(f2)
self.poll(cx)
}
Poll.Pending => Poll.Pending,
},
ThenState.Second(f2) => f2.poll(cx),
}
}
}
// Join — run two futures concurrently
struct Join<A: Future, B: Future> {
a: Option<A>, b: Option<B>,
a_result: Option<A.Output>, b_result: Option<B.Output>,
}
impl<A: Future, B: Future> Future for Join<A, B> {
type Output = (A.Output, B.Output)
fn poll(mut self, cx: &mut Context) -> Poll<(A.Output, B.Output)> {
if let some(a) = &mut self.a {
if let Poll.Ready(val) = a.poll(cx) {
self.a_result = some(val); self.a = none
}
}
if let some(b) = &mut self.b {
if let Poll.Ready(val) = b.poll(cx) {
self.b_result = some(val); self.b = none
}
}
if self.a_result.is_some() && self.b_result.is_some() {
Poll.Ready((self.a_result.take().unwrap(), self.b_result.take().unwrap()))
} else { Poll.Pending }
}
}
fn join<A: Future, B: Future>(a: A, b: B) -> Join<A, B> {
Join { a: some(a), b: some(b), a_result: none, b_result: none }
}
// ── Timer (simulated) ────────────────────────────────────────────────────────
struct Timer {
ticks_remaining: u32,
}
impl Future for Timer {
type Output = ()
fn poll(mut self, cx: &mut Context) -> Poll<()> {
if self.ticks_remaining == 0 { return Poll.Ready(()) }
self.ticks_remaining -= 1
cx.waker().wake()
Poll.Pending
}
}
fn timer(ticks: u32) -> Timer { Timer { ticks_remaining: ticks } }
fn main() {
// Ready future
let mut cx_waker = Waker { task_id: 0, wake_queue: Arc.new(Mutex.new(VecDeque.new())) }
let mut cx = Context { waker: cx_waker }
let mut fut = ready(42)
match fut.poll(&mut cx) {
Poll.Ready(v) => assert(v == 42),
Poll.Pending => panic("should be ready"),
}
print("Ready future ✓")
// Yield future
let mut yf = yield_now()
match yf.poll(&mut cx) {
Poll.Pending => {}
_ => panic("should be pending"),
}
match yf.poll(&mut cx) {
Poll.Ready(()) => {}
_ => panic("should be ready"),
}
print("Yield future ✓")
// Timer
let mut t = timer(3)
assert(matches!(t.poll(&mut cx), Poll.Pending))
assert(matches!(t.poll(&mut cx), Poll.Pending))
assert(matches!(t.poll(&mut cx), Poll.Pending))
assert(matches!(t.poll(&mut cx), Poll.Ready(())))
print("Timer(3) ✓")
// Executor
let mut exec = Executor.new()
let t1 = exec.spawn(ready(()))
let t2 = exec.spawn(timer(2))
assert(exec.task_count() == 2)
exec.run()
assert(exec.completed_count() == 2)
print("Executor ran 2 tasks ✓")
print("Tutorial 49 — Async Runtime complete ✓")
}