mea::latch

Struct Latch

Source
pub struct Latch { /* private fields */ }
Expand description

A synchronization primitive that can be used to coordinate multiple tasks.

A latch starts with an initial count and tasks can wait for this count to reach zero. The count can be decremented by calling count_down() or arrive(). Once the count reaches zero, all waiting tasks are unblocked.

§Examples

use std::sync::Arc;

use mea::latch::Latch;

let latch = Arc::new(Latch::new(3));
let mut handles = Vec::new();

// Spawn tasks that will count down the latch
for i in 0..3 {
    let latch = latch.clone();
    handles.push(tokio::spawn(async move {
        // Do some work
        println!("Task {} completing", i);
        latch.count_down();
    }));
}

// Wait for all tasks to complete
latch.wait().await;
println!("All tasks completed!");

for handle in handles {
    handle.await.unwrap();
}

Implementations§

Source§

impl Latch

Source

pub fn new(count: u32) -> Self

Creates a new latch initialized with the given count.

§Arguments
  • count - The initial count value. Tasks will wait until this count reaches zero.
§Examples
use mea::latch::Latch;

let latch = Latch::new(3); // Creates a latch with count of 3
Source

pub fn count(&self) -> u32

Returns the current count.

This method is typically used for debugging and testing purposes.

§Examples
use mea::latch::Latch;

let latch = Latch::new(5);
assert_eq!(latch.count(), 5);
Source

pub fn count_down(&self)

Decrements the latch count by one, waking up all pending tasks if the counter reaches zero.

If the current count is zero, this method has no effect.

§Examples
use mea::latch::Latch;

let latch = Latch::new(2);
latch.count_down(); // Count is now 1
latch.count_down(); // Count is now 0, all waiting tasks are woken
Source

pub fn arrive(&self, n: u32)

Decrements the latch count by n, waking up all waiting tasks if the counter reaches zero.

This method provides a way to decrement the counter by more than one at a time. It will not cause an overflow when decrementing the counter.

§Arguments
  • n - The amount to decrement the counter by
§Behavior
  • If n is zero or the counter has already reached zero, nothing happens
  • If the current count is greater than n, it is decremented by n
  • If the current count is greater than 0 but less than or equal to n, the count becomes zero and all waiting tasks are woken
§Examples
use mea::latch::Latch;

let latch = Latch::new(5);
latch.arrive(3); // Count is now 2
latch.arrive(2); // Count is now 0, all waiting tasks are woken
Source

pub fn try_wait(&self) -> Result<(), u32>

Attempts to wait for the latch count to reach zero without blocking.

§Returns
  • Ok(()) if the count is zero
  • Err(count) if the count is not zero, where count is the current count
§Examples
use mea::latch::Latch;

let latch = Latch::new(2);
assert_eq!(latch.try_wait(), Err(2));
latch.count_down();
assert_eq!(latch.try_wait(), Err(1));
latch.count_down();
assert_eq!(latch.try_wait(), Ok(()));
Source

pub fn wait(&self) -> LatchWait<'_>

Returns a future that will complete when the latch count reaches zero.

§Examples
use std::sync::Arc;

use mea::latch::Latch;

let latch = Arc::new(Latch::new(1));
let latch2 = latch.clone();

// Spawn a task that will wait for the latch
let handle = tokio::spawn(async move {
    latch2.wait().await;
    println!("Latch reached zero!");
});

// Count down the latch
latch.count_down();
handle.await.unwrap();

Trait Implementations§

Source§

impl Debug for Latch

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !Freeze for Latch

§

impl RefUnwindSafe for Latch

§

impl Send for Latch

§

impl Sync for Latch

§

impl Unpin for Latch

§

impl UnwindSafe for Latch

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.