pub struct MutexTicket<'a, T> { /* private fields */ }
Expand description
Helper to poll a mutex.
Since Mutex::lock futures keep track of where in queue the lock request is,
this is different to mutex.lock().poll(ctx)
as that construction will create
a new lock request at each poll.
This can often be useful when we need to poll something that is contained under
this mutex.
Typical usage: let mut ticket = MutexTicket::new(); poll_fn(|ctx| { let mutex_guard = ready!(ticket.poll(ctx)); mutex_guard.some_child_future.poll(ctx) }).await;
What this means: Attempt to acquire the mutex. If it’s not available, wait until it’s available. With the mutex acquired, check some_child_future. If it’s completed, complete the poll_fn. If it’s not completed drop the mutex guard (unblock other tasks) and wait for some_child_future to be awoken.
Implementations§
Source§impl<'a, T> MutexTicket<'a, T>
impl<'a, T> MutexTicket<'a, T>
Sourcepub fn new(mutex: &'a Mutex<T>) -> MutexTicket<'a, T>
pub fn new(mutex: &'a Mutex<T>) -> MutexTicket<'a, T>
Create a new MutexTicket
Sourcepub fn poll(&mut self, ctx: &mut Context<'_>) -> Poll<MutexGuard<'a, T>>
pub fn poll(&mut self, ctx: &mut Context<'_>) -> Poll<MutexGuard<'a, T>>
Poll once to see if the lock has been acquired. This is not Future::poll because it’s intended to be a helper used during a Future::poll implementation, but never as a Future itself – one can simply call Mutex::lock.await in that case!
Sourcepub async fn lock(&mut self) -> MutexGuard<'a, T>
pub async fn lock(&mut self) -> MutexGuard<'a, T>
Finish locking. This should be used instead of the Mutex.lock function if there
is a MutexTicket
constructed already - it may be that said MutexTicket
has already been
granted ownership of the Mutex - if this is the case, the Mutex.lock call will never succeed.