#wait-group #async

atomic-waitgroup

A waitgroup implementation supports async with advanced features

4 releases

0.1.3 May 10, 2024
0.1.2 May 10, 2024
0.1.1 May 10, 2024
0.1.0 May 10, 2024

#403 in Concurrency

BSD-1-Clause

13KB
244 lines

atomic-waitgroup

A waitgroup support async with advanced features, implemented with atomic operations to reduce locking in mind.

Features

  • wait_to() is supported to wait for a value larger than zero.

  • wait() & wait_to() can be canceled by tokio::time::timeout or futures::select!.

  • Assumes only one thread calls wait(). If multiple concurrent wait() is detected, will panic for this invalid usage.

  • done() can be called by multiple coroutines other than the one calls wait().


lib.rs:

A waitgroup support async with advanced features, implemented with atomic operations to reduce locking in mind.

Features & restrictions

  • wait_to() is supported to wait for a value larger than zero.

  • wait() & wait_to() can be canceled by tokio::time::timeout or futures::select!.

  • Assumes only one thread calls wait(). If multiple concurrent wait() is detected, will panic for this invalid usage.

  • done() & wait() is allowed to called concurrently.

  • add() & done() is allowed to called concurrently.

  • add() & wait() will not conflict, but concurrent calls are not a good pattern.

Example

extern crate atomic_waitgroup;
use atomic_waitgroup::WaitGroup;
use tokio::runtime::Runtime;

let rt = Runtime::new().unwrap();
let wg = WaitGroup::new();
rt.block_on(async move {
    for i in 0..2 {
        let _guard = wg.add_guard();
        tokio::spawn(async move {
           // Do something
            drop(_guard);
        });
    }
    match tokio::time::timeout(
        tokio::time::Duration::from_secs(1),
        wg.wait_to(1)).await {
        Ok(_) => {
            assert!(wg.left() <= 1);
        }
        Err(_) => {
            println!("wg.wait_to(1) timeouted");
        }
    }
});


Dependencies

~0.4–5MB
~13K SLoC