waft_foundation/
executor.rs1use std::collections::VecDeque;
14use std::future::Future;
15use std::pin::Pin;
16use std::sync::{Arc, Mutex, MutexGuard};
17use std::task::{Context, Poll};
18
19use async_task::Runnable;
20use waft_embedder::Dispatcher;
21
22use crate::background;
23
24const POLL_BUDGET: usize = 100_000;
27
28type ReadyQueue = Arc<Mutex<VecDeque<Runnable>>>;
31
32pub(crate) struct ForegroundExecutor {
36 shared: ExecutorHandle,
37}
38
39impl ForegroundExecutor {
40 pub(crate) fn new(dispatcher: Arc<dyn Dispatcher>) -> ForegroundExecutor {
43 ForegroundExecutor {
44 shared: ExecutorHandle {
45 ready: Arc::default(),
46 dispatcher,
47 },
48 }
49 }
50
51 pub(crate) fn spawn<R: 'static>(&self, future: impl Future<Output = R> + 'static) -> Task<R> {
54 self.shared.spawn(future)
55 }
56
57 pub(crate) fn run_in_background<T: Send + 'static>(
59 &self,
60 work: impl FnOnce() -> T + Send + 'static,
61 ) -> Task<T> {
62 self.shared.run_in_background(work)
63 }
64
65 pub(crate) fn handle(&self) -> ExecutorHandle {
66 self.shared.clone()
67 }
68}
69
70impl Drop for ForegroundExecutor {
73 fn drop(&mut self) {
74 loop {
75 let queued: Vec<_> = lock(&self.shared.ready).drain(..).collect();
76 if queued.is_empty() {
77 break;
78 }
79 drop(queued);
80 }
81 }
82}
83
84#[derive(Clone)]
87pub(crate) struct ExecutorHandle {
88 ready: ReadyQueue,
89 dispatcher: Arc<dyn Dispatcher>,
90}
91
92impl ExecutorHandle {
93 pub(crate) fn spawn<R: 'static>(&self, future: impl Future<Output = R> + 'static) -> Task<R> {
96 spawn_on(&self.ready, &self.dispatcher, future)
97 }
98
99 pub(crate) fn run_in_background<T: Send + 'static>(
102 &self,
103 work: impl FnOnce() -> T + Send + 'static,
104 ) -> Task<T> {
105 self.spawn(background::run(&self.dispatcher, work))
106 }
107
108 pub(crate) fn poll_ready_tasks(&self) -> usize {
116 let mut polled = 0usize;
117 loop {
118 let next = lock(&self.ready).pop_front();
119 let Some(runnable) = next else {
120 break;
121 };
122 polled += 1;
123 assert!(
124 polled <= POLL_BUDGET,
125 "tasks did not converge after {POLL_BUDGET} polls; are two tasks waking each \
126 other in a cycle?"
127 );
128 runnable.run();
129 }
130 polled
131 }
132}
133
134fn spawn_on<R: 'static>(
135 ready: &ReadyQueue,
136 dispatcher: &Arc<dyn Dispatcher>,
137 future: impl Future<Output = R> + 'static,
138) -> Task<R> {
139 let ready = Arc::clone(ready);
140 let host = Arc::clone(dispatcher);
141 let schedule = move |runnable| {
146 lock(&ready).push_back(runnable);
147 host.wake_now();
148 };
149 let (runnable, task) = async_task::spawn_local(future, schedule);
150 runnable.schedule();
151 Task::spawned(task)
152}
153
154fn lock(ready: &ReadyQueue) -> MutexGuard<'_, VecDeque<Runnable>> {
155 ready
156 .lock()
157 .expect("the ready queue is never poisoned: nothing runs under its lock")
158}
159
160pub struct Task<T> {
166 inner: TaskInner<T>,
167}
168
169enum TaskInner<T> {
170 Ready(Option<T>),
172 Spawned(Option<async_task::Task<T>>),
173}
174
175impl<T> Task<T> {
176 pub fn ready(value: T) -> Task<T> {
179 Task {
180 inner: TaskInner::Ready(Some(value)),
181 }
182 }
183
184 pub(crate) fn spawned(task: async_task::Task<T>) -> Task<T> {
185 Task {
186 inner: TaskInner::Spawned(Some(task)),
187 }
188 }
189}
190
191impl<T> Unpin for Task<T> {}
194
195impl<T> Future for Task<T> {
196 type Output = T;
197
198 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
199 match &mut self.get_mut().inner {
200 TaskInner::Ready(value) => Poll::Ready(
201 value
202 .take()
203 .expect("a Task is polled until it completes, then never again"),
204 ),
205 TaskInner::Spawned(task) => {
206 let task = task
207 .as_mut()
208 .expect("a Task is polled until it completes, then never again");
209 Pin::new(task).poll(cx)
210 }
211 }
212 }
213}
214
215impl<T> Drop for Task<T> {
216 fn drop(&mut self) {
217 if let TaskInner::Spawned(task) = &mut self.inner
218 && let Some(task) = task.take()
219 {
220 task.detach();
221 }
222 }
223}
224
225#[cfg(test)]
226mod tests {
227 #![allow(clippy::disallowed_methods)]
229
230 use std::cell::Cell;
231 use std::future::poll_fn;
232 use std::rc::Rc;
233 use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
234 use std::task::Waker;
235
236 use waft_embedder::Instant;
237
238 use super::*;
239
240 struct CountingDispatcher(AtomicUsize);
242
243 impl Dispatcher for CountingDispatcher {
244 fn wake_at(&self, _timer_wakeup: Option<Instant>) {}
245
246 fn wake_now(&self) {
247 self.0.fetch_add(1, Ordering::AcqRel);
248 }
249
250 fn dispatch(&self, work: Box<dyn FnOnce() + Send>) {
251 work();
252 }
253 }
254
255 fn executor() -> (ForegroundExecutor, Arc<CountingDispatcher>) {
256 let host = Arc::new(CountingDispatcher(AtomicUsize::new(0)));
257 let executor = ForegroundExecutor::new(Arc::clone(&host) as Arc<dyn Dispatcher>);
258 (executor, host)
259 }
260
261 fn turns_asked(host: &CountingDispatcher) -> usize {
262 host.0.load(Ordering::Acquire)
263 }
264
265 #[test]
266 fn a_spawn_asks_the_host_for_a_turn_and_the_task_runs_in_it() {
267 let (executor, host) = executor();
268 drop(executor.spawn(async {}));
269 assert_eq!(turns_asked(&host), 1);
270 assert_eq!(executor.handle().poll_ready_tasks(), 1);
271 }
272
273 #[test]
274 fn a_task_woken_from_another_thread_asks_for_a_turn_and_runs_in_it() {
275 let (executor, host) = executor();
276 let parked: Arc<Mutex<Option<Waker>>> = Arc::default();
277 let done = Arc::new(AtomicBool::new(false));
278 let ran = Rc::new(Cell::new(false));
279 let (slot, flag, seen) = (Arc::clone(&parked), Arc::clone(&done), Rc::clone(&ran));
280 drop(executor.spawn(async move {
281 poll_fn(|cx| {
282 if flag.load(Ordering::Acquire) {
283 Poll::Ready(())
284 } else {
285 *slot.lock().unwrap() = Some(cx.waker().clone());
286 Poll::Pending
287 }
288 })
289 .await;
290 seen.set(true);
291 }));
292 let handle = executor.handle();
293 handle.poll_ready_tasks();
294 assert_eq!(
295 turns_asked(&host),
296 1,
297 "the spawn asked for a wake, which parked the task"
298 );
299
300 std::thread::spawn(move || {
301 done.store(true, Ordering::Release);
302 let waker = parked.lock().unwrap().take();
303 waker.expect("the wake parked the waker").wake();
304 })
305 .join()
306 .unwrap();
307 assert_eq!(
308 turns_asked(&host),
309 2,
310 "the worker's wake asks the host for another wake"
311 );
312 assert!(!ran.get(), "nothing runs outside the main thread's wake");
313 handle.poll_ready_tasks();
314 assert!(ran.get());
315 }
316}