Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 1 | //! A single-threaded executor where join handles catch panics inside tasks. |
| 2 | |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 3 | use std::future::Future; |
| 4 | use std::panic::AssertUnwindSafe; |
| 5 | use std::thread; |
| 6 | |
| 7 | use crossbeam::channel::{unbounded, Sender}; |
| 8 | use futures::executor; |
| 9 | use futures::future::FutureExt; |
| 10 | use lazy_static::lazy_static; |
| 11 | |
Stjepan Glavina | fcfa4ab | 2019-11-25 18:39:17 +0100 | [diff] [blame] | 12 | type Task = async_task::Task<()>; |
| 13 | type JoinHandle<T> = async_task::JoinHandle<T, ()>; |
| 14 | |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 15 | /// Spawns a future on the executor. |
Stjepan Glavina | fcfa4ab | 2019-11-25 18:39:17 +0100 | [diff] [blame] | 16 | fn spawn<F, R>(future: F) -> JoinHandle<thread::Result<R>> |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 17 | where |
| 18 | F: Future<Output = R> + Send + 'static, |
| 19 | R: Send + 'static, |
| 20 | { |
| 21 | lazy_static! { |
| 22 | // A channel that holds scheduled tasks. |
Stjepan Glavina | fcfa4ab | 2019-11-25 18:39:17 +0100 | [diff] [blame] | 23 | static ref QUEUE: Sender<Task> = { |
| 24 | let (sender, receiver) = unbounded::<Task>(); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 25 | |
| 26 | // Start the executor thread. |
| 27 | thread::spawn(|| { |
| 28 | for task in receiver { |
| 29 | // No need for `catch_unwind()` here because panics are already caught. |
| 30 | task.run(); |
| 31 | } |
| 32 | }); |
| 33 | |
| 34 | sender |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | // Create a future that catches panics within itself. |
| 39 | let future = AssertUnwindSafe(future).catch_unwind(); |
| 40 | |
| 41 | // Create a task that is scheduled by sending itself into the channel. |
| 42 | let schedule = |t| QUEUE.send(t).unwrap(); |
| 43 | let (task, handle) = async_task::spawn(future, schedule, ()); |
| 44 | |
| 45 | // Schedule the task by sending it into the channel. |
| 46 | task.schedule(); |
| 47 | |
| 48 | handle |
| 49 | } |
| 50 | |
| 51 | fn main() { |
| 52 | // Spawn a future that completes succesfully. |
| 53 | let handle = spawn(async { |
| 54 | println!("Hello, world!"); |
| 55 | }); |
| 56 | |
| 57 | // Block on the future and report its result. |
| 58 | match executor::block_on(handle) { |
Stjepan Glavina | d7b17fb | 2020-04-14 14:38:57 +0200 | [diff] [blame] | 59 | None => println!("The task was canceled."), |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 60 | Some(Ok(val)) => println!("The task completed with {:?}", val), |
| 61 | Some(Err(_)) => println!("The task has panicked"), |
| 62 | } |
| 63 | |
| 64 | // Spawn a future that panics. |
| 65 | let handle = spawn(async { |
| 66 | panic!("Ooops!"); |
| 67 | }); |
| 68 | |
| 69 | // Block on the future and report its result. |
| 70 | match executor::block_on(handle) { |
Stjepan Glavina | d7b17fb | 2020-04-14 14:38:57 +0200 | [diff] [blame] | 71 | None => println!("The task was canceled."), |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 72 | Some(Ok(val)) => println!("The task completed with {:?}", val), |
| 73 | Some(Err(_)) => println!("The task has panicked"), |
| 74 | } |
| 75 | } |