Struct tarantool::fiber::mutex::Mutex

source ·
pub struct Mutex<T: ?Sized> { /* private fields */ }

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::mutex::Mutex;

let mutex = Mutex::new(0);
source

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);
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, 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);
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::mutex::Mutex;
let mutex = Mutex::new(0);

let mut guard = mutex.lock();
*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::mutex::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::mutex::Mutex;

let mut mutex = Mutex::new(0);
*mutex.get_mut() = 10;
assert_eq!(*mutex.lock(), 10);

Trait Implementations§

source§

impl<T: ?Sized + Debug> 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> !Send for Mutex<T>

§

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.