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>