Struct Gutex

Source
pub struct Gutex<T> {
    group: Arc<GutexGroup>,
    active: UnsafeCell<usize>,
    value: UnsafeCell<T>,
}
Expand description

A mutex that grant exclusive access to a group of members.

The crate::lock::Mutex is prone to deadlock when using on a multiple struct fields like this:

use crate::lock::Mutex;

pub struct Foo {
    field1: Mutex<()>,
    field2: Mutex<()>,
}

The order to acquire the lock must be the same everywhere otherwise the deadlock is possible. Maintaining the lock order manually are cumbersome task so we introduce this type to handle this instead.

How this type are working is simple. Any locks on any member will lock the same mutex in the group, which mean there are only one mutex in the group. It have the same effect as the following code:

use crate::lock::Mutex;

pub struct Foo {
    data: Mutex<Data>,
}

struct Data {
    field1: (),
    field2: (),
}

The bonus point of this type is it will allow recursive lock for read-only access so you will never end up deadlock yourself. It will panic if you try to acquire write access while the readers are still active the same as core::cell::RefCell.

Fields§

§group: Arc<GutexGroup>§active: UnsafeCell<usize>§value: UnsafeCell<T>

Implementations§

Source§

impl<T> Gutex<T>

Source

pub fn read(&self) -> GutexRead<'_, T>

Locks this Gutex with read-only access.

Multiple read-only accesses can be taken out at the same time.

§Panics

If there are an active write access to this Gutex.

Source

pub fn write(&self) -> GutexWrite<'_, T>

§Panics

If there are any active reader or writer.

Trait Implementations§

Source§

impl<T: Send> Send for Gutex<T>

Source§

impl<T: Send> Sync for Gutex<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Gutex<T>

§

impl<T> !RefUnwindSafe for Gutex<T>

§

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

§

impl<T> !UnwindSafe for Gutex<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<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.