pub struct Mutex<P: Priority, T: ?Sized, Q: MutexQueue<P> = DefaultMutexQueue<P>> { /* private fields */ }Expand description
A mutex that queues waiters by priority. Higher priority requesters will receive access first.
If the evict feature is enabled, the current holder of the lock will be notified if a higher
priority waiter is queued.
Note that requesters with the same priority may receive their access in an arbitrary priority - if this is non-desireable, FIFO and LIFO may be used.
Example:
use async_priority_lock::{FIFO, HighestFirst, Mutex, MutexGuard};
static MY_LOCK: Mutex<FIFO<HighestFirst<usize>>, Vec<usize>> =
Mutex::const_new(vec![]);
async fn push_num(num: usize, priority: usize) {
// Pushes our number to the queue. If multiple writers are waiting on this, the waiters
// will acquire access by order of priority. As the priority is FIFO, rquesters with the
// same priority will execute oldest first.
MY_LOCK.lock_from(priority).await.push(num);
}
async fn wait_and_push(num: usize, priority: usize) {
loop {
let mut guard = MY_LOCK.lock_from(priority).await;
// wait a second or abort and retry if the resource is requested with a higher
// priority in the meantime
tokio::select! {
_ = tokio::time::sleep(tokio::time::Duration::from_secs(1)) => {},
_ = MutexGuard::evicted(&mut guard) => {
// drop the guard and try again
continue;
}
}
guard.push(num)
}
}Implementations§
Source§impl<P: Priority, T: ?Sized, Q: MutexQueue<P>> Mutex<P, T, Q>
impl<P: Priority, T: ?Sized, Q: MutexQueue<P>> Mutex<P, T, Q>
Sourcepub fn try_lock(
&self,
priority: impl Into<P>,
) -> Result<MutexGuard<'_, P, T, Q>, TryLockError>
pub fn try_lock( &self, priority: impl Into<P>, ) -> Result<MutexGuard<'_, P, T, Q>, TryLockError>
Try to acquire the lock without blocking or requesting eviction of the current holder.
Priority will be stored in guard; higher priority requesters will try to evict the returned
guard if the evict flag is enabled.
i.e. the holder may wait / check for eviction via MutexGuard::evicted.
Sourcepub async fn lock(&self, priority: P) -> MutexGuard<'_, P, T, Q>
pub async fn lock(&self, priority: P) -> MutexGuard<'_, P, T, Q>
Acquire exclusive access to the locked resource, waiting until after higher priority requesters acquire and release the lock.
If the evict feature is enabled, this will also notify the current holder to request it
to release the lock if the current holder is lower priority.
i.e. the holder may wait / check for eviction via MutexGuard::evicted.
Cancel Safety: This function is cancel safe.
Sourcepub fn lock_from(
&self,
priority: impl Into<P>,
) -> impl Future<Output = MutexGuard<'_, P, T, Q>>
pub fn lock_from( &self, priority: impl Into<P>, ) -> impl Future<Output = MutexGuard<'_, P, T, Q>>
Acquire exclusive access to the locked resource, waiting until higher priority requesters release the lock.
Shorthand for self.lock(priority.into()).
Cancel Safety: This function is cancel safe.
Sourcepub fn lock_default(&self) -> impl Future<Output = MutexGuard<'_, P, T, Q>>where
P: Default,
pub fn lock_default(&self) -> impl Future<Output = MutexGuard<'_, P, T, Q>>where
P: Default,
Acquire exclusive access to the locked resource, waiting until higher priority requesters release the lock.
Shorthand for self.lock(Default::default()).
Cancel Safety: This function is cancel safe.
Sourcepub const fn const_new(val: T) -> Selfwhere
Q: ConstDefault,
T: Sized,
pub const fn const_new(val: T) -> Selfwhere
Q: ConstDefault,
T: Sized,
Create a new Mutex, unlocked and ready for use.
Available when the queue is ConstDefault if the const-default feature is enabled.
All builtin queues are ConstDefault.
Trait Implementations§
Source§impl<P: Priority, T: ConstDefault, Q: ConstDefault + MutexQueue<P>> ConstDefault for Mutex<P, T, Q>
Available on crate feature const-default only.
impl<P: Priority, T: ConstDefault, Q: ConstDefault + MutexQueue<P>> ConstDefault for Mutex<P, T, Q>
const-default only.Source§impl<P, T, Q: MutexQueue<P>> Debug for Mutex<P, T, Q>
impl<P, T, Q: MutexQueue<P>> Debug for Mutex<P, T, Q>
Source§impl<'de, P: Priority, T, Q: MutexQueue<P>> Deserialize<'de> for Mutex<P, T, Q>where
T: Deserialize<'de>,
Available on crate feature serde only.
impl<'de, P: Priority, T, Q: MutexQueue<P>> Deserialize<'de> for Mutex<P, T, Q>where
T: Deserialize<'de>,
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
impl<P: Priority, T: ?Sized + Send, Q: Send + MutexQueue<P>> Send for Mutex<P, T, Q>
impl<P: Priority, T: ?Sized + Send, Q: Send + Sync + MutexQueue<P>> Sync for Mutex<P, T, Q>
For the mutex to be Sync, T must be Send (Sync is not needed).
Also see rust Mutex docs
(however, both Sync and Send are required for the queue; usually these are implemented if P
is Send + Sync)