[go: up one dir, main page]

Crate gmtx

Crate gmtx 

Source
Expand description

Mutex that grant exclusive access to a group of members.

The std::sync::Mutex and the related types are prone to deadlock when using on a multiple struct fields like this:

use std::sync::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 invent this crate to handle this instead.

How this crate are working is simple. Any locks on any Gutex 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 std::sync::Mutex;

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

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

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

Structsยง

Gutex
Member of a GutexGroup.
GutexGroup
Group of Gutex.
GutexReadGuard
RAII structure used to release the shared read access of a lock when dropped.
GutexWriteGuard
RAII structure used to release the exclusive write access of a lock when dropped.