Crate priority_semaphore

Source
Expand description

Runtime-agnostic priority semaphore.

This crate provides PrioritySemaphore, an asynchronous semaphore where waiters supply a signed priority. Higher priorities are awakened before lower ones. The implementation is runtime agnostic and works with either Tokio or async-std when the corresponding feature is enabled.

use std::sync::Arc;
use priority_semaphore::PrioritySemaphore;

let sem = Arc::new(PrioritySemaphore::new(1));

let hi = sem.clone();
tokio::spawn(async move {
    let _permit = hi.acquire(10).await.unwrap();
    println!("high priority acquired");
});

let lo = sem.clone();
tokio::spawn(async move {
    let _permit = lo.acquire(1).await.unwrap();
    println!("low priority acquired");
});

Macros§

doc_cfg
Conditionally add #[doc(cfg(feature = $feat))].

Structs§

Permit
Returned by successful acquire; releases permit on Drop.
PrioritySemaphore
Async-aware priority semaphore.

Enums§

AcquireError
Returned by async acquire.
TryAcquireError
Returned by try_acquire when no permits are immediately available.

Type Aliases§

Priority
Priority value used by the semaphore.