pub struct Mutex<T: ?Sized> { /* private fields */ }
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::mutex::Mutex;
let mutex = Mutex::new(0);
sourcepub fn lock(&self) -> MutexGuard<'_, T>
pub fn lock(&self) -> MutexGuard<'_, T>
Acquires a mutex, yielding the current fiber until it is able to do so.
This function will yield the current fiber until it is available to acquire the mutex. Upon returning, the fiber is the only fiber with the lock held. A RAII guard is returned to allow scoped unlock of the lock. When the guard goes out of scope, the mutex will be unlocked.
The exact behavior on locking a mutex in the fiber which already holds the lock is left unspecified.
Abortions
This function might abort when called if the lock is already held by the current fiber.
Examples
use std::rc::Rc;
use tarantool::fiber::{start_proc, mutex::Mutex};
let mutex = Rc::new(Mutex::new(0));
let c_mutex = Rc::clone(&mutex);
start_proc(move || {
*c_mutex.lock() = 10;
}).join();
assert_eq!(*mutex.lock(), 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, an RAII guard is returned. The lock will be unlocked when the
guard is dropped.
This function does not yield.
Abortions
This function might abort when called if the lock is already held by the current fiber.
Examples
use std::rc::Rc;
use tarantool::fiber::{start_proc, mutex::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.lock(), 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::mutex::Mutex;
let mutex = Mutex::new(0);
let mut guard = mutex.lock();
*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::mutex::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::mutex::Mutex;
let mut mutex = Mutex::new(0);
*mutex.get_mut() = 10;
assert_eq!(*mutex.lock(), 10);