priority_semaphore/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]
2#![forbid(unsafe_code)]
3#![deny(missing_docs)]
4
5//! Runtime-agnostic priority semaphore.
6//!
7//! This crate provides [`PrioritySemaphore`], an asynchronous semaphore where
8//! waiters supply a signed priority. Higher priorities are awakened before
9//! lower ones. The implementation is runtime agnostic and works with either
10//! **Tokio** or **async-std** when the corresponding feature is enabled.
11//!
12//! ```rust
13//! use std::sync::Arc;
14//! use priority_semaphore::PrioritySemaphore;
15//!
16//! # #[tokio::main]
17//! # async fn main() {
18//! let sem = Arc::new(PrioritySemaphore::new(1));
19//!
20//! let hi = sem.clone();
21//! tokio::spawn(async move {
22//! let _permit = hi.acquire(10).await.unwrap();
23//! println!("high priority acquired");
24//! });
25//!
26//! let lo = sem.clone();
27//! tokio::spawn(async move {
28//! let _permit = lo.acquire(1).await.unwrap();
29//! println!("low priority acquired");
30//! });
31//! # }
32//! ```
33
34extern crate alloc;
35#[cfg(feature = "std")]
36extern crate std;
37
38mod error;
39mod lock;
40mod permit;
41mod queue;
42mod semaphore;
43mod util;
44mod waiter;
45
46pub use crate::error::{AcquireError, TryAcquireError};
47pub use crate::permit::Permit;
48pub use crate::semaphore::{Priority, PrioritySemaphore};