Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 1 | //! A single-threaded executor where join handles propagate panics from tasks. |
| 2 | |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 3 | use std::future::Future; |
| 4 | use std::panic::{resume_unwind, AssertUnwindSafe}; |
| 5 | use std::pin::Pin; |
| 6 | use std::task::{Context, Poll}; |
| 7 | use std::thread; |
| 8 | |
Stjepan Glavina | 42e3a69 | 2020-09-17 14:58:40 +0200 | [diff] [blame] | 9 | use async_task::Task; |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 10 | use crossbeam::channel::{unbounded, Sender}; |
Stjepan Glavina | ceeb5a1 | 2020-09-18 11:45:24 +0200 | [diff] [blame^] | 11 | use futures_lite::{future, FutureExt}; |
| 12 | use once_cell::sync::Lazy; |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 13 | |
| 14 | /// Spawns a future on the executor. |
| 15 | fn spawn<F, R>(future: F) -> JoinHandle<R> |
| 16 | where |
| 17 | F: Future<Output = R> + Send + 'static, |
| 18 | R: Send + 'static, |
| 19 | { |
Stjepan Glavina | ceeb5a1 | 2020-09-18 11:45:24 +0200 | [diff] [blame^] | 20 | // A channel that holds scheduled tasks. |
| 21 | static QUEUE: Lazy<Sender<Task>> = Lazy::new(|| { |
| 22 | let (sender, receiver) = unbounded::<Task>(); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 23 | |
Stjepan Glavina | ceeb5a1 | 2020-09-18 11:45:24 +0200 | [diff] [blame^] | 24 | // Start the executor thread. |
| 25 | thread::spawn(|| { |
| 26 | for task in receiver { |
| 27 | // No need for `catch_unwind()` here because panics are already caught. |
| 28 | task.run(); |
| 29 | } |
| 30 | }); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 31 | |
Stjepan Glavina | ceeb5a1 | 2020-09-18 11:45:24 +0200 | [diff] [blame^] | 32 | sender |
| 33 | }); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 34 | |
| 35 | // Create a future that catches panics within itself. |
| 36 | let future = AssertUnwindSafe(future).catch_unwind(); |
| 37 | |
| 38 | // Create a task that is scheduled by sending itself into the channel. |
| 39 | let schedule = |t| QUEUE.send(t).unwrap(); |
Stjepan Glavina | 42e3a69 | 2020-09-17 14:58:40 +0200 | [diff] [blame] | 40 | let (task, handle) = async_task::spawn(future, schedule); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 41 | |
| 42 | // Schedule the task by sending it into the channel. |
| 43 | task.schedule(); |
| 44 | |
| 45 | // Wrap the handle into one that propagates panics. |
| 46 | JoinHandle(handle) |
| 47 | } |
| 48 | |
| 49 | /// A join handle that propagates panics inside the task. |
Stjepan Glavina | 42e3a69 | 2020-09-17 14:58:40 +0200 | [diff] [blame] | 50 | struct JoinHandle<R>(async_task::JoinHandle<thread::Result<R>>); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 51 | |
| 52 | impl<R> Future for JoinHandle<R> { |
| 53 | type Output = Option<R>; |
| 54 | |
| 55 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 56 | match Pin::new(&mut self.0).poll(cx) { |
| 57 | Poll::Pending => Poll::Pending, |
| 58 | Poll::Ready(None) => Poll::Ready(None), |
| 59 | Poll::Ready(Some(Ok(val))) => Poll::Ready(Some(val)), |
| 60 | Poll::Ready(Some(Err(err))) => resume_unwind(err), |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | fn main() { |
| 66 | // Spawn a future that panics and block on it. |
| 67 | let handle = spawn(async { |
| 68 | panic!("Ooops!"); |
| 69 | }); |
Stjepan Glavina | ceeb5a1 | 2020-09-18 11:45:24 +0200 | [diff] [blame^] | 70 | future::block_on(handle); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 71 | } |