Latch

Struct Latch 

Source
pub struct Latch<S: RelaxStrategy> { /* private fields */ }
Expand description

A lightweight, memory-efficient synchronization primitive for asynchronous Rust code.

Latch is a synchronization primitive that allows multiple tasks to wait for a signal that unlocks them all simultaneously. It’s designed with minimal memory overhead, using only a single usize for its internal state.

When the Latch is open, calling all waiting methods is lock-free and wait-free

§Memory Layout

The Latch struct is marked with #[repr(transparent)] ensuring it has the same memory layout as a single AtomicUsize, making it extremely lightweight.

§Type Parameters

  • S - The relaxation strategy used when spinning on the internal lock. Must implement the RelaxStrategy trait from the spin crate.

§Examples

Basic usage:

use latch::{Latch, spin::Spin};
use std::sync::Arc;

let latch = Arc::new(Latch::<Spin>::new());
let latch_clone = latch.clone();

// Spawn a task to wait on the latch
let task = tokio::spawn(async move {
    println!("Waiting for latch...");
    latch_clone.wait().await;
    println!("Latch opened!");
});

// Open the latch, releasing any waiting tasks
latch.open();

Multiple waiters:

use latch::{Latch, spin::Spin};
use std::sync::Arc;

let latch = Arc::new(Latch::<Spin>::new());
let mut handles = vec![];

// Spawn multiple waiters
for i in 0..10 {
    let latch_clone = latch.clone();
    handles.push(tokio::spawn(async move {
        println!("Task {i} waiting");
        latch_clone.wait().await;
        println!("Task {i} resumed");
    }));
}

// Open the latch, releasing all waiters at once
latch.open();

// Wait for all tasks to complete
for handle in handles {
    handle.await.unwrap();
}

Synchronous waiting (with the std feature):

use latch::{Latch, spin::Spin};
use std::sync::Arc;
use std::thread;

let latch = Arc::new(Latch::<Spin>::new());
let latch_clone = latch.clone();

// Spawn a thread that will wait synchronously
let thread_handle = thread::spawn(move || {
    println!("Thread waiting");
    latch_clone.wait_sync();
    println!("Thread resumed");
});

// Open the latch, releasing the waiting thread
latch.open();

Implementations§

Source§

impl<S: RelaxStrategy> Latch<S>

Source

pub const fn new() -> Self

Creates a new instance of the Latch structure.

§Returns

A new instance of Latch.

§Example
let latch = Latch::<Spin>::new();
Source

pub fn opened(&self) -> bool

Checks if the Latch is in an “open” state.

Source

pub fn open(&self)

Opens the latch if it is currently locked.

Source

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

Waits for the latch to open, returning a LatchWait object that allows managing the latch wait state.

§Returns

A LatchWait<S> instance, which represents the state of the waiting node within the latch.

Trait Implementations§

Source§

impl<S: RelaxStrategy> Drop for Latch<S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<S: RelaxStrategy> Send for Latch<S>

Source§

impl<S: RelaxStrategy> Sync for Latch<S>

Auto Trait Implementations§

§

impl<S> !Freeze for Latch<S>

§

impl<S> RefUnwindSafe for Latch<S>
where S: RefUnwindSafe,

§

impl<S> Unpin for Latch<S>
where S: Unpin,

§

impl<S> UnwindSafe for Latch<S>
where S: UnwindSafe,

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.