|
| 1 | +use std::{ |
| 2 | + future::Future, |
| 3 | + sync::{ |
| 4 | + atomic::{AtomicUsize, Ordering}, |
| 5 | + mpsc, Arc, |
| 6 | + }, |
| 7 | +}; |
| 8 | + |
| 9 | +use async_task::{Runnable, Task}; |
| 10 | + |
| 11 | +use crate::DroppableFuture; |
| 12 | + |
| 13 | +pub struct TickedAsyncExecutor { |
| 14 | + channel: (mpsc::Sender<Runnable>, mpsc::Receiver<Runnable>), |
| 15 | + num_woken_tasks: Arc<AtomicUsize>, |
| 16 | + num_spawned_tasks: Arc<AtomicUsize>, |
| 17 | +} |
| 18 | + |
| 19 | +impl Default for TickedAsyncExecutor { |
| 20 | + fn default() -> Self { |
| 21 | + Self::new() |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// TODO, Observer: Task spawn/wake/drop events |
| 26 | +// TODO, Task Identifier String |
| 27 | +impl TickedAsyncExecutor { |
| 28 | + pub fn new() -> Self { |
| 29 | + Self { |
| 30 | + channel: mpsc::channel(), |
| 31 | + num_woken_tasks: Arc::new(AtomicUsize::new(0)), |
| 32 | + num_spawned_tasks: Arc::new(AtomicUsize::new(0)), |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + pub fn spawn<T>(&self, future: impl Future<Output = T> + Send + 'static) -> Task<T> |
| 37 | + where |
| 38 | + T: Send + 'static, |
| 39 | + { |
| 40 | + let future = self.droppable_future(future); |
| 41 | + let schedule = self.runnable_schedule_cb(); |
| 42 | + let (runnable, task) = async_task::spawn(future, schedule); |
| 43 | + runnable.schedule(); |
| 44 | + task |
| 45 | + } |
| 46 | + |
| 47 | + pub fn spawn_local<T>(&self, future: impl Future<Output = T> + 'static) -> Task<T> |
| 48 | + where |
| 49 | + T: 'static, |
| 50 | + { |
| 51 | + let future = self.droppable_future(future); |
| 52 | + let schedule = self.runnable_schedule_cb(); |
| 53 | + let (runnable, task) = async_task::spawn_local(future, schedule); |
| 54 | + runnable.schedule(); |
| 55 | + task |
| 56 | + } |
| 57 | + |
| 58 | + pub fn num_tasks(&self) -> usize { |
| 59 | + self.num_spawned_tasks.load(Ordering::Relaxed) |
| 60 | + } |
| 61 | + |
| 62 | + /// Run the woken tasks once |
| 63 | + /// |
| 64 | + /// NOTE: Will not run tasks that are woken/scheduled immediately after `Runnable::run` |
| 65 | + pub fn tick(&self) { |
| 66 | + let num_woken_tasks = self.num_woken_tasks.load(Ordering::Relaxed); |
| 67 | + self.channel |
| 68 | + .1 |
| 69 | + .try_iter() |
| 70 | + .take(num_woken_tasks) |
| 71 | + .for_each(|runnable| { |
| 72 | + runnable.run(); |
| 73 | + }); |
| 74 | + self.num_woken_tasks |
| 75 | + .fetch_sub(num_woken_tasks, Ordering::Relaxed); |
| 76 | + } |
| 77 | + |
| 78 | + fn droppable_future<F>(&self, future: F) -> DroppableFuture<F, impl Fn()> |
| 79 | + where |
| 80 | + F: Future, |
| 81 | + { |
| 82 | + self.num_spawned_tasks.fetch_add(1, Ordering::Relaxed); |
| 83 | + let num_spawned_tasks = self.num_spawned_tasks.clone(); |
| 84 | + DroppableFuture::new(future, move || { |
| 85 | + num_spawned_tasks.fetch_sub(1, Ordering::Relaxed); |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + fn runnable_schedule_cb(&self) -> impl Fn(Runnable) { |
| 90 | + let sender = self.channel.0.clone(); |
| 91 | + let num_woken_tasks = self.num_woken_tasks.clone(); |
| 92 | + move |runnable| { |
| 93 | + sender.send(runnable).unwrap_or(()); |
| 94 | + num_woken_tasks.fetch_add(1, Ordering::Relaxed); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#[cfg(test)] |
| 100 | +mod tests { |
| 101 | + use super::*; |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn test_multiple_tasks() { |
| 105 | + let executor = TickedAsyncExecutor::new(); |
| 106 | + executor |
| 107 | + .spawn_local(async move { |
| 108 | + println!("A: Start"); |
| 109 | + tokio::task::yield_now().await; |
| 110 | + println!("A: End"); |
| 111 | + }) |
| 112 | + .detach(); |
| 113 | + |
| 114 | + executor |
| 115 | + .spawn_local(async move { |
| 116 | + println!("B: Start"); |
| 117 | + tokio::task::yield_now().await; |
| 118 | + println!("B: End"); |
| 119 | + }) |
| 120 | + .detach(); |
| 121 | + |
| 122 | + // A, B, C: Start |
| 123 | + executor.tick(); |
| 124 | + assert_eq!(executor.num_tasks(), 2); |
| 125 | + |
| 126 | + // A, B, C: End |
| 127 | + executor.tick(); |
| 128 | + assert_eq!(executor.num_tasks(), 0); |
| 129 | + } |
| 130 | + |
| 131 | + #[test] |
| 132 | + fn test_task_cancellation() { |
| 133 | + let executor = TickedAsyncExecutor::new(); |
| 134 | + let task1 = executor.spawn_local(async move { |
| 135 | + loop { |
| 136 | + println!("A: Start"); |
| 137 | + tokio::task::yield_now().await; |
| 138 | + println!("A: End"); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + let task2 = executor.spawn_local(async move { |
| 143 | + loop { |
| 144 | + println!("B: Start"); |
| 145 | + tokio::task::yield_now().await; |
| 146 | + println!("B: End"); |
| 147 | + } |
| 148 | + }); |
| 149 | + assert_eq!(executor.num_tasks(), 2); |
| 150 | + executor.tick(); |
| 151 | + |
| 152 | + executor |
| 153 | + .spawn_local(async move { |
| 154 | + task1.cancel().await; |
| 155 | + task2.cancel().await; |
| 156 | + }) |
| 157 | + .detach(); |
| 158 | + assert_eq!(executor.num_tasks(), 3); |
| 159 | + |
| 160 | + // Since we have cancelled the tasks above, the loops should eventually end |
| 161 | + while executor.num_tasks() != 0 { |
| 162 | + executor.tick(); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments