async-priority-lock
A priority-based mutex where access is granted by order of priority.
Example
use async_priority_lock::{Mutex, MutexGuard};
use tokio::time::{Duration, sleep};
static MY_LOCK: Mutex<usize, Vec<usize>> = Mutex::const_new(vec![]);
async fn append(priority: usize, value: usize) {
loop {
let mut guard = MY_LOCK.lock_from(priority).await;
tokio::select! {
_ = MutexGuard::evicted(&mut guard) => { continue },
_ = sleep(Duration::from_millis(10)) => {
guard.push(value);
break;
}
}
}
}
#[tokio::main]
async fn main() {
tokio::join!(append(0, 1), append(3, 3), append(2, 2));
println!("data: {:?}", MY_LOCK); }
Features
evict - enables eviction of current lock holders PriorityMutexGuard::evicted
no-std - allows compiling for no_std (note: alloc still required)
serde - allows serde to deserialize into mutexes (but not serialize)