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
//! A compact asynchronous `WaitGroup` synchronization primitive.
//!
//! This crate is designed to be lightweight and executor-agnostic. It works
//! with any `async` runtime and supports `no_std` environments (requires
//! `alloc`).
//!
//! # Usage
//!
//! ## [`MonoWaitGroup`]
//!
//! ```rust
//! use compact_waitgroup::MonoWaitGroup;
//!
//! let (wg, token) = MonoWaitGroup::new();
//! assert!(!wg.is_done());
//!
//! std::thread::spawn(move || {
//! // Long-running task
//! token.release();
//! });
//!
//! // Wait for the task to complete
//! # futures_executor::block_on(async {
//! wg.await;
//! # });
//! ```
//!
//! ## [`WaitGroup`]
//!
//! ```rust
//! use compact_waitgroup::WaitGroup;
//!
//! let (wg, factory) = WaitGroup::new();
//!
//! factory.scope(|token| {
//! let token_cloned = token.clone();
//! std::thread::spawn(move || {
//! // Long-running task
//! token_cloned.release();
//! });
//! std::thread::spawn(move || {
//! // Another long-running task
//! token.release();
//! });
//! });
//!
//! // Wait for all tasks to complete
//! # futures_executor::block_on(async {
//! wg.await;
//! # });
//! ```
//!
//! ## With `async` Runtime
//!
//! ```rust
//! use compact_waitgroup::{GroupTokenExt, WaitGroup};
//! # let spawn = |_| {};
//! # let sleep = |_| async {};
//!
//! let (wg, factory) = WaitGroup::new();
//!
//! for (i, token) in std::iter::repeat_n(factory.into_token(), 8).enumerate() {
//! let task = async move {
//! println!("Task {i} started");
//! // Long-running task...
//! sleep(std::time::Duration::from_secs(1)).await;
//! println!("Task {i} finished");
//! };
//! spawn(task.release_on_ready(token));
//! }
//!
//! // Wait for all tasks to complete
//! # futures_executor::block_on(async {
//! wg.await;
//! # });
//! ```
//!
//! # Memory Layout
//!
//! This crate is designed to be extremely lightweight. The memory footprint
//! depends on the architecture and the enabled features.
//!
//! By default, [`MonoWaitGroup`] shares the same underlying memory structure as
//! [`WaitGroup`]. However, this means [`MonoWaitGroup`] carries a `usize` field
//! for reference counting of workers, which is redundant for the singly-owned
//! [`MonoGroupToken`].
//!
//! Enabling the `compact-mono` feature changes the internal definition of
//! [`MonoWaitGroup`]. It switches to a dedicated, stripped-down layout that
//! removes the reference counter.
//!
//! | Component | Default (64-bit) | With `compact-mono` | Saving |
//! | ------------------- | ---------------- | ------------------- | ----------- |
//! | **[`WaitGroup`]** | 32 bytes | 32 bytes | 0 bytes |
//! | **[`MonoWaitGroup`]** | **32 bytes** | **24 bytes** | **8 bytes** |
extern crate alloc;
pub use crate;
pub use crate;