1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339


 
//! Types for spawning and waiting on groups of tasks.
//!
//! This crate provides onthe [TaskCollection](crate::TaskCollection) for the
//! grouping of spawned tasks. The TaskCollection type is created using a
//! [Spawner](crate::Spawner) implementation. Any tasks spawned via the
//! TaskCollections `spawn` method are tracked with minimal overhead.
//! `await`ing the TaskCollection will yield until all the spawned tasks for
//! that collection have completed.
//!
//! The following example shows how to use a TaskCollection to wait for spawned tasks to finish:
//!
//! ```rust
//! # use task_collection::TaskCollection;
//! # use futures::channel::mpsc;
//! # use futures::StreamExt;
//! # use core::time::Duration;
//!
//! fn main() {
//!     let runtime = tokio::runtime::Runtime::new().unwrap();
//!     let (tx, mut rx) = mpsc::unbounded::<u64>();
//!     
//!     runtime.spawn(async move {
//!         (0..10).for_each(|v| {
//!             tx.unbounded_send(v).expect("Failed to send");
//!         })
//!     });
//!     
//!     runtime.block_on(async {
//!         let collection = TaskCollection::new(&runtime);
//!         while let Some(val) = rx.next().await {
//!             collection.spawn(async move {
//!                 // Simulate some async work
//!                 tokio::time::sleep(Duration::from_secs(val)).await;
//!                 println!("Value {}", val);
//!             });
//!         }
//!
//!         collection.await;
//!         println!("All values printed");
//!     });
//! }
//! ```

#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc;

use core::task::Poll;
use core::{future::Future, sync::atomic::AtomicUsize};
use core::{ops::Deref, sync::atomic::Ordering::SeqCst};

use futures_util::task::AtomicWaker;

pub struct TaskCollection<S, T> {
    spawner: S,
    tracker: T,
}

impl<S> TaskCollection<S, ()>
where
    S: Spawner,
{
    pub fn with_static_tracker(
        spawner: S,
        tracker: &'static Tracker,
    ) -> TaskCollection<S, &'static Tracker> {
        TaskCollection { spawner, tracker }
    }

    #[cfg(feature = "alloc")]
    pub fn new(spawner: S) -> TaskCollection<S, alloc::sync::Arc<Tracker>> {
        TaskCollection {
            spawner,
            tracker: alloc::sync::Arc::new(Tracker::new()),
        }
    }
}

impl<S, T> TaskCollection<S, T>
where
    S: Spawner,
    T: 'static + Deref<Target = Tracker> + Clone + Send,
{
    pub fn spawn<F, R>(&self, future: F)
    where
        F: Future<Output = R> + Send + 'static,
    {
        let tracker = self.create_task();
        self.spawner.spawn(async {
            let _ = future.await;
            core::mem::drop(tracker);
        });
    }

    fn create_task(&self) -> Task<T> {
        let mut current_tasks = self.tracker.active_tasks.load(SeqCst);

        loop {
            if current_tasks == usize::MAX {
                panic!();
            }

            let new_tasks = current_tasks + 1;

            let actual_current =
                self.tracker
                    .active_tasks
                    .compare_and_swap(current_tasks, new_tasks, SeqCst);

            if current_tasks == actual_current {
                return Task {
                    inner: self.tracker.clone(),
                };
            }

            current_tasks = actual_current;
        }
    }
}

impl<S, T> Future for TaskCollection<S, T>
where
    T: core::ops::Deref<Target = Tracker>,
{
    type Output = ();

    fn poll(
        self: core::pin::Pin<&mut Self>,
        cx: &mut core::task::Context<'_>,
    ) -> core::task::Poll<Self::Output> {
        let active_tasks = self.tracker.active_tasks.load(SeqCst);

        if active_tasks == 0 {
            Poll::Ready(())
        } else {
            self.tracker.waker.register(cx.waker());

            let active_tasks = self.tracker.active_tasks.load(SeqCst);
            if active_tasks == 0 {
                Poll::Ready(())
            } else {
                Poll::Pending
            }
        }
    }
}

struct Task<T>
where
    T: Deref<Target = Tracker>,
{
    inner: T,
}

impl<T> Drop for Task<T>
where
    T: Deref<Target = Tracker>,
{
    fn drop(&mut self) {
        let previous = self.inner.active_tasks.fetch_sub(1, SeqCst);

        if previous == 1 {
            self.inner.waker.wake();
        }
    }
}

pub struct Tracker {
    waker: AtomicWaker,
    active_tasks: AtomicUsize,
}

impl Tracker {
    pub const fn new() -> Tracker {
        Tracker {
            waker: AtomicWaker::new(),
            active_tasks: AtomicUsize::new(0),
        }
    }
}

pub trait Spawner {
    fn spawn<F>(&self, future: F)
    where
        F: Future<Output = ()> + Send + 'static;
}

#[cfg(feature = "smol")]
impl Spawner for &smol::Executor<'_> {
    fn spawn<F>(&self, future: F)
    where
        F: core::future::Future<Output = ()> + Send + 'static,
    {
        smol::Executor::spawn(self, future).detach();
    }
}

#[cfg(feature = "tokio")]
impl Spawner for &tokio::runtime::Runtime {
    fn spawn<F>(&self, future: F)
    where
        F: core::future::Future<Output = ()> + Send + 'static,
    {
        tokio::runtime::Runtime::spawn(self, future);
    }
}

#[cfg(feature = "tokio")]
pub struct GlobalTokioSpawner;

#[cfg(feature = "tokio")]
impl Spawner for GlobalTokioSpawner {
    fn spawn<F>(&self, future: F)
    where
        F: core::future::Future<Output = ()> + Send + 'static,
    {
        tokio::spawn(future);
    }
}

#[cfg(feature = "async-std")]
pub struct AsyncStdSpawner;

#[cfg(feature = "async-std")]
impl Spawner for AsyncStdSpawner {
    fn spawn<F>(&self, future: F)
    where
        F: core::future::Future<Output = ()> + Send + 'static,
    {
        async_std::task::spawn(future);
    }
}

#[cfg(test)]
mod tests {
    extern crate std;
    use crate::{TaskCollection, Tracker};
    use core::panic;
    use smol::future::FutureExt;
    use std::time::Duration;

    #[test]
    #[cfg(feature = "smol")]
    fn test_smol() {
        let exec = smol::Executor::new();

        let f = async {
            let collection = TaskCollection::new(&exec);

            for i in &[5, 3, 1, 4, 2] {
                collection.spawn(async move {
                    smol::Timer::after(Duration::from_secs(*i)).await;
                });
            }

            collection.await;
        };

        let timeout = async {
            smol::Timer::after(Duration::from_secs(10)).await;
            panic!();
        };

        smol::block_on(exec.run(f.or(timeout)));
    }

    #[test]
    #[cfg(feature = "smol")]
    fn test_smol_static() {
        let exec = smol::Executor::new();
        static T: Tracker = Tracker::new();
        let f = async {
            let collection = TaskCollection::with_static_tracker(&exec, &T);

            for i in &[5, 3, 1, 4, 2] {
                collection.spawn(async move {
                    smol::Timer::after(Duration::from_secs(*i)).await;
                });
            }

            collection.await;
        };

        let timeout = async {
            smol::Timer::after(Duration::from_secs(10)).await;
            panic!();
        };

        smol::block_on(exec.run(f.or(timeout)));
    }

    #[test]
    #[cfg(feature = "tokio")]
    fn test_tokio() {
        let runtime = tokio::runtime::Runtime::new().unwrap();

        let f = async {
            let collection = TaskCollection::new(&runtime);

            for i in &[5, 3, 1, 4, 2] {
                collection.spawn(async move {
                    tokio::time::sleep(Duration::from_secs(*i)).await;
                });
            }

            collection.await;
        };

        runtime.block_on(async {
            tokio::select! {
                _ = f => (),
                _ = tokio::time::sleep(Duration::from_secs(10)) => panic!()
            }
        });
    }

    #[test]
    #[cfg(feature = "async-std")]
    fn test_async_std() {
        use crate::AsyncStdSpawner;
        let f = async {
            let collection = TaskCollection::new(AsyncStdSpawner);

            for i in &[5, 3, 1, 4, 2] {
                collection.spawn(async move {
                    async_std::task::sleep(Duration::from_secs(*i)).await;
                });
            }

            collection.await;
        };

        async_std::task::block_on(async_std::future::timeout(Duration::from_secs(10), f)).unwrap();
    }
}