Struct Mutex

Source
pub struct Mutex<T> { /* private fields */ }
Expand description

Wrapper around a std::sync::Mutex which uses a thread local variable in order to check for lock hierarchy violations in debug builds.

See the crate level documentation for more general information.

use lock_hierarchy::Mutex;

let mutex_a = Mutex::new(()); // Level 0
let mutex_b = Mutex::with_level((), 0); // also level 0
// Fine, first mutex in thread
let _guard_a = mutex_a.lock().unwrap();
// Would panic, lock hierarchy violation
// let _guard_b = mutex_b.lock().unwrap();

Implementations§

Source§

impl<T> Mutex<T>

Source

pub fn new(t: T) -> Self

Creates lock with level 0. Use this constructor if you want to get an error in debug builds every time you acquire another lock while holding this one.

Source

pub fn with_level(t: T, level: u32) -> Self

Creates a lock and assigns it a level in the lock hierarchy. Higher levels must be acquired first if locks are to be held simultaneously. This way we can ensure locks are always acquired in the same order. This prevents deadlocks.

Source

pub fn lock(&self) -> LockResult<MutexGuard<'_, T>>

Source

pub fn get_mut(&mut self) -> LockResult<&mut T>

Source

pub fn into_inner(self) -> LockResult<T>

Trait Implementations§

Source§

impl<T: 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: Default> Default for Mutex<T>

Source§

fn default() -> Mutex<T>

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

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

Source§

fn from(value: T) -> Self

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

Auto Trait Implementations§

§

impl<T> !Freeze for Mutex<T>

§

impl<T> RefUnwindSafe for Mutex<T>

§

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

§

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

§

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

§

impl<T> UnwindSafe for Mutex<T>

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