pub struct Mutex<T: ?Sized> { /* private fields */ }
Expand description
An asynchronous Mutex
-like type.
This type acts similarly to std::sync::Mutex
, with two major
differences: Mutex::lock
is an async method so does not block, and the lock
guard is designed to be held across .await
points.
Prefer this type to crate::fiber::mutex::Mutex
if used in async contexts.
This Mutex
makes fiber yielding calls to be explicit with .await
syntax and
will help avoid deadlocks in case of multiple futures used in join_all
or similar combinators.
Implementations§
source§impl<T: ?Sized> Mutex<T>
impl<T: ?Sized> Mutex<T>
sourcepub fn new(t: T) -> Mutex<T>where
T: Sized,
pub fn new(t: T) -> Mutex<T>where
T: Sized,
Creates a new mutex in an unlocked state ready for use.
Examples
use tarantool::fiber::r#async::Mutex;
let mutex = Mutex::new(0);
sourcepub async fn lock(&self) -> MutexGuard<'_, T>
pub async fn lock(&self) -> MutexGuard<'_, T>
Locks this mutex, causing the current future/fiber to yield until the lock has
been acquired. When the lock has been acquired, function returns a
MutexGuard
. The lock will be unlocked when the
guard is dropped.
Examples
use std::rc::Rc;
use tarantool::fiber::{start_async, block_on, r#async::Mutex};
let mutex = Rc::new(Mutex::new(0));
let c_mutex = Rc::clone(&mutex);
start_async(async move {
*c_mutex.lock().await = 10;
}).join();
block_on(async { assert_eq!(*mutex.lock().await, 10) });
sourcepub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>>
Attempts to acquire this lock.
If the lock could not be acquired at this time, then None
is returned.
Otherwise, a MutexGuard
is returned. The lock will be unlocked when the
guard is dropped.
This function does not yield.
Examples
use std::rc::Rc;
use tarantool::fiber::{start_proc, r#async::Mutex};
let mutex = Rc::new(Mutex::new(0));
let c_mutex = Rc::clone(&mutex);
start_proc(move || {
let mut lock = c_mutex.try_lock();
if let Some(ref mut mutex) = lock {
**mutex = 10;
} else {
println!("try_lock failed");
}
}).join();
assert_eq!(*mutex.try_lock().unwrap(), 10);
sourcepub fn unlock(guard: MutexGuard<'_, T>)
pub fn unlock(guard: MutexGuard<'_, T>)
Immediately drops the guard, and consequently unlocks the mutex.
This function is equivalent to calling drop
on the guard but is more
self-documenting. Alternately, the guard will be automatically dropped
when it goes out of scope.
use tarantool::fiber::r#async::Mutex;
let mutex = Mutex::new(0);
let mut guard = mutex.try_lock().unwrap();
*guard += 20;
Mutex::unlock(guard);
sourcepub fn into_inner(self) -> Twhere
T: Sized,
pub fn into_inner(self) -> Twhere
T: Sized,
Consumes this mutex, returning the underlying data.
Examples
use tarantool::fiber::r#async::Mutex;
let mutex = Mutex::new(0);
assert_eq!(mutex.into_inner(), 0);
sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Returns a mutable reference to the underlying data.
Since this call borrows the Mutex
mutably, no actual locking needs to
take place – the mutable borrow statically guarantees no locks exist.
Examples
use tarantool::fiber::r#async::Mutex;
let mut mutex = Mutex::new(0);
*mutex.get_mut() = 10;
assert_eq!(*mutex.try_lock().unwrap(), 10);