Struct Mutex

Source
pub struct Mutex<L: Level, T> { /* private fields */ }
Expand description

A mutual exclusion primitive useful for protecting shared data

This mutex will block threads waiting for the lock to become available. The mutex can also be statically initialized or created via a new constructor. Each mutex has a type parameter which represents the data that it is protecting. The data can only be accessed through the RAII guards returned from lock and try_lock, which guarantees that the data is only ever accessed when the mutex is locked.

Implementations§

Source§

impl<L: Level, T> Mutex<L, T>

Source

pub fn new(val: T) -> Self

Creates a new mutex in an unlocked state ready for use

Source

pub fn lock<'a, LP: Lower<L> + 'a>( &'a self, lock_token: LockToken<'a, LP>, ) -> MutexGuard<'a, L, T>

Acquires a mutex, blocking the current thread until it is able to do so.

This function will block the local thread until it is available to acquire the mutex. Upon returning, the thread is the only thread with the mutex held. An RAII guard is returned to allow scoped unlock of the lock. When the guard goes out of scope, the mutex will be unlocked.

Source

pub fn try_lock<'a, LP: Lower<L> + 'a>( &'a self, lock_token: LockToken<'a, LP>, ) -> Option<MutexGuard<'a, L, 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 block.

Source

pub fn try_lock_for<'a, LP: Lower<L> + 'a>( &'a self, lock_token: LockToken<'a, LP>, duration: Duration, ) -> Option<MutexGuard<'a, L, T>>

Attempts to acquire this lock. Timeout after duration has expired

Since this operation is not supported by a std::sync::mutex this this is implemented using try_lock and waits

Source

pub fn into_inner(self) -> T

Consumes this Mutex, returning the underlying data.

Trait Implementations§

Source§

impl<L: Debug + Level, T: Debug> Debug for Mutex<L, T>

Source§

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

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

impl<L: Level, T: Default> Default for Mutex<L, T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<L, T> !Freeze for Mutex<L, T>

§

impl<L, T> RefUnwindSafe for Mutex<L, T>
where L: RefUnwindSafe,

§

impl<L, T> Send for Mutex<L, T>
where T: Send, L: Send,

§

impl<L, T> Sync for Mutex<L, T>
where T: Send, L: Sync,

§

impl<L, T> Unpin for Mutex<L, T>
where L: Unpin, T: Unpin,

§

impl<L, T> UnwindSafe for Mutex<L, T>
where L: 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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.