Latch

Struct Latch 

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

A synchronization primitive that can be released exactly once, notifying all associated Gates. This is intended for one-shot notifications or barriers in asynchronous contexts.

§Simple example

use strut_sync::{Gate, Latch};


// Make a latch
let latch = Latch::new();

// Derive a gate from it
let gate = latch.gate();

// Spawn an asynchronous task
tokio::spawn(async move {
    // Perform some asynchronous work
    println!("This will print first");

    // Signal completion
    latch.release();
});

// Wait for the completion signal
gate.opened().await;

println!("Asynchronous task completed!")

§Full example

use std::sync::Arc;
use std::sync::atomic::{AtomicU8, Ordering};
use strut_sync::{Gate, Latch};
use pretty_assertions::assert_eq;


// Make a latch
let latch = Latch::new();

// Derive any number of gates from it
let gate_a = latch.gate();
let gate_b = latch.gate();
let gate_c = gate_b.clone();

// Create a marker
let marker = Arc::new(AtomicU8::new(0));

// Spawn tasks that increment the marker
tokio::spawn(increment_marker(gate_a, marker.clone()));
tokio::spawn(increment_marker(gate_b, marker.clone()));
tokio::spawn(increment_marker(gate_c, marker.clone()));

// Give the tasks a chance to start waiting
tokio::task::yield_now().await;

// Nothing should have happened yet
assert_eq!(marker.load(Ordering::Relaxed), 0);

// Release the latch
latch.release();

// Give the tasks a chance to wake up
tokio::task::yield_now().await;

// Marker should have been increased three times by now
assert_eq!(marker.load(Ordering::Relaxed), 3);


// Helper function
async fn increment_marker(gate: Gate, marker: Arc<AtomicU8>) {
    // Wait for the gate to open
    gate.opened().await;

    // Increment the marker
    marker.fetch_add(1, Ordering::Relaxed);
}

Implementations§

Source§

impl Latch

Source

pub fn new() -> Self

Returns a brand new, unreleased Latch.

Source

pub fn gate(&self) -> Gate

Returns a new Gate handle associated with this Latch. Multiple gates can be created and awaited independently, all linked to the same single-release latch.

Source

pub fn release(&self)

Permanently releases this Latch, notifying all associated Gates. Subsequent calls have no additional effect.

Trait Implementations§

Source§

impl Clone for Latch

Source§

fn clone(&self) -> Latch

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Latch

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Latch

Source§

fn default() -> Latch

Returns the “default value” for a type. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.