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
use std::sync::Arc;
pub struct WaitChild(Arc<()>);
impl WaitChild {
pub fn done(self) {}
}
#[derive(Default)]
pub struct WaitGroup(Arc<()>);
impl WaitGroup {
pub fn new() -> Self {
Self::default()
}
pub fn add(&self) -> WaitChild {
WaitChild(self.0.clone())
}
pub fn adds<const N: usize>(&self) -> impl Iterator<Item = WaitChild> + '_ {
(0..N).map(|_| self.add())
}
pub fn wait(self) {
use std::{thread::sleep, time::Duration};
while Arc::strong_count(&self.0) > 1 {
sleep(Duration::from_millis(1));
}
}
pub async fn async_wait(self) {
WaitFuture(self.0).await
}
}
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
struct WaitFuture(Arc<()>);
impl Future for WaitFuture {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if Arc::strong_count(&self.0) > 1 {
cx.waker().wake_by_ref();
Poll::Pending
} else {
Poll::Ready(())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use rand::prelude::*;
use std::{thread, time::Duration};
#[test]
fn it_works() {
let wg = WaitGroup::new();
wg.adds::<10>().enumerate().for_each(|(i, child)| {
thread::spawn(move || {
let duration = rand::thread_rng().gen_range(0..500);
thread::sleep(Duration::from_millis(duration));
println!("sleep:{duration}, {i}");
child.done();
});
});
wg.wait();
}
#[tokio::test]
async fn it_works_async() {
let wg = WaitGroup::new();
wg.adds::<10>().enumerate().for_each(|(i, child)| {
tokio::spawn(async move {
let duration = rand::thread_rng().gen_range(0..500);
tokio::time::sleep(Duration::from_millis(duration)).await;
println!("async sleep:{duration}, {i}");
child.done();
});
});
wg.async_wait().await;
}
}