Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 1 | //! A simple single-threaded executor. |
| 2 | |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 3 | use std::future::Future; |
| 4 | use std::panic::catch_unwind; |
| 5 | use std::thread; |
| 6 | |
| 7 | use crossbeam::channel::{unbounded, Sender}; |
| 8 | use futures::executor; |
| 9 | use lazy_static::lazy_static; |
| 10 | |
Stjepan Glavina | fcfa4ab | 2019-11-25 18:39:17 +0100 | [diff] [blame^] | 11 | type Task = async_task::Task<()>; |
| 12 | type JoinHandle<T> = async_task::JoinHandle<T, ()>; |
| 13 | |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 14 | /// Spawns a future on the executor. |
Stjepan Glavina | fcfa4ab | 2019-11-25 18:39:17 +0100 | [diff] [blame^] | 15 | fn spawn<F, R>(future: F) -> JoinHandle<R> |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 16 | where |
| 17 | F: Future<Output = R> + Send + 'static, |
| 18 | R: Send + 'static, |
| 19 | { |
| 20 | lazy_static! { |
| 21 | // A channel that holds scheduled tasks. |
Stjepan Glavina | fcfa4ab | 2019-11-25 18:39:17 +0100 | [diff] [blame^] | 22 | static ref QUEUE: Sender<Task> = { |
| 23 | let (sender, receiver) = unbounded::<Task>(); |
Stjepan Glavina | 1479e86 | 2019-08-12 20:18:51 +0200 | [diff] [blame] | 24 | |
| 25 | // Start the executor thread. |
| 26 | thread::spawn(|| { |
| 27 | for task in receiver { |
| 28 | // Ignore panics for simplicity. |
| 29 | let _ignore_panic = catch_unwind(|| task.run()); |
| 30 | } |
| 31 | }); |
| 32 | |
| 33 | sender |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | // Create a task that is scheduled by sending itself into the channel. |
| 38 | let schedule = |t| QUEUE.send(t).unwrap(); |
| 39 | let (task, handle) = async_task::spawn(future, schedule, ()); |
| 40 | |
| 41 | // Schedule the task by sending it into the channel. |
| 42 | task.schedule(); |
| 43 | |
| 44 | handle |
| 45 | } |
| 46 | |
| 47 | fn main() { |
| 48 | // Spawn a future and await its result. |
| 49 | let handle = spawn(async { |
| 50 | println!("Hello, world!"); |
| 51 | }); |
| 52 | executor::block_on(handle); |
| 53 | } |