Struct tarantool::fiber::async::mutex::Mutex

source ·
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>

source

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);
source

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) });
source

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);
source

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);
source

pub fn into_inner(self) -> T
where 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);
source

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);

Trait Implementations§

source§

impl<T: Debug + ?Sized> Debug for Mutex<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: ?Sized + Default> Default for Mutex<T>

source§

fn default() -> Mutex<T>

Creates a Mutex<T>, with the Default value for T.

source§

impl<T> From<T> for Mutex<T>

source§

fn from(t: T) -> Self

Creates a new mutex in an unlocked state ready for use. This is equivalent to Mutex::new.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Mutex<T>

§

impl<T: ?Sized> Send for Mutex<T>
where T: Send,

§

impl<T> !Sync for Mutex<T>

§

impl<T: ?Sized> Unpin for Mutex<T>
where T: Unpin,

§

impl<T: ?Sized> UnwindSafe for Mutex<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.