pub struct GLock<T> { /* private fields */ }
Expand description
Represents a granular lock object. A GLock
is used to protect a data value of type T
, which
can only be accessed with a mutable reference after calling lock_exclusive()
,
try_lock_exclusive()
, lock_exclusive_using_parent()
or try_lock_exclusive_using_parent()
.
Each GLock
can have zero or more child GLock
s, which can be nested (i.e. placed inside the
protected data) or non-nested. The same locking rules apply in both cases.
When locking a child GLock
, first you need to lock its parent GLock
, then lock it by calling
lock_using_parent()
, try_lock_using_parent()
, lock_exclusive_using_parent()
or
lock_exclusive_using_parent()
, and passing a reference to the parent’s GLockGuard
.
If you do not lock the parent and proceed to lock the child GLock
directly using lock()
,
try_lock()
, lock_exclusive()
or try_lock_exclusive()
, an implicit lock will be acquired
for the parent GLock
that will be release when dropping this lock’s GLockGuard
.
Implementations§
source§impl<T> GLock<T>
impl<T> GLock<T>
sourcepub fn new_root_builder() -> GLockBuilder
pub fn new_root_builder() -> GLockBuilder
Creates a new root GLockBuilder
. This is similar to calling GLockBuilder::new_root_builder()
.
sourcepub fn new_root(data: T) -> LockResult<GLock<T>>
pub fn new_root(data: T) -> LockResult<GLock<T>>
Creates a new root GLock
protecting the specified data.
sourcepub fn new_child_builder(&self) -> LockResult<GLockBuilder>
pub fn new_child_builder(&self) -> LockResult<GLockBuilder>
Creates a GLockBuilder
for a lock that is a child of the current GLock
.
sourcepub fn new_child<T2>(&self, data: T2) -> LockResult<GLock<T2>>
pub fn new_child<T2>(&self, data: T2) -> LockResult<GLock<T2>>
Creates a GLock
that is a child of the current GLock
, protecting the specified data.
sourcepub fn lock(&self, lock_type: LockType) -> LockResult<GLockGuard<'_, T>>
pub fn lock(&self, lock_type: LockType) -> LockResult<GLockGuard<'_, T>>
Acquires a lock of the specified type on the current GLock
. If the lock is busy, it will
block until it is ready. If this is a child GLock
, it will implicitly acquire the
appropriate lock on its parent GLock
.
If you are trying to acquire an Exclusive
lock, it is better to use lock_exclusive()
,
because the GLockGuard
returned by lock()
will not allow mutation of protected data.
sourcepub fn try_lock(&self, lock_type: LockType) -> LockResult<GLockGuard<'_, T>>
pub fn try_lock(&self, lock_type: LockType) -> LockResult<GLockGuard<'_, T>>
Attempts to acquire a lock of the specified type on the current GLock
. If the lock is busy,
it will return a LockError::LockBusy
error. If this is a child GLock
, it will implicitly
attempt to acquire the appropriate lock on its parent GLock
.
If you are trying to acquire an Exclusive
lock, it is better to use try_lock_exclusive()
,
because the GLockGuard
returned by try_lock()
will not allow mutation of protected data.
sourcepub fn lock_using_parent<T2>(
&self,
lock_type: LockType,
parent: &GLockGuard<'_, T2>
) -> LockResult<GLockGuard<'_, T>>
pub fn lock_using_parent<T2>(
&self,
lock_type: LockType,
parent: &GLockGuard<'_, T2>
) -> LockResult<GLockGuard<'_, T>>
Acquires a lock of the specified type on the current child GLock
, using the specified
GLockGuard
of the parent GLock
. If the lock is busy, it will block until it is ready.
If you are trying to acquire an Exclusive
lock, it is better to use
lock_exclusive_using_parent()
, because the GLockGuard
returned by
lock_using_parent()
will not allow mutation of protected data.
sourcepub fn try_lock_using_parent<T2>(
&self,
lock_type: LockType,
parent: &GLockGuard<'_, T2>
) -> LockResult<GLockGuard<'_, T>>
pub fn try_lock_using_parent<T2>(
&self,
lock_type: LockType,
parent: &GLockGuard<'_, T2>
) -> LockResult<GLockGuard<'_, T>>
Attempts to acquire a lock of the specified type on the current child GLock
, using the
specified GLockGuard
of the parent GLock
. If the lock is busy, it will return a
LockError::LockBusy
error.
If you are trying to acquire an Exclusive
lock, it is better to use
try_lock_exclusive_using_parent()
, because the GLockGuard
returned by
try_lock_using_parent()
will not allow mutation of protected data.
sourcepub fn lock_exclusive(&self) -> LockResult<GLockGuardMut<'_, T>>
pub fn lock_exclusive(&self) -> LockResult<GLockGuardMut<'_, T>>
Acquires an Exclusive
lock on the current GLock
. If the lock is busy, it will block
until it is ready. If this is a child GLock
, it will implicitly acquire the appropriate
lock on its parent GLock
.
The returned GLockGuardMut
allows mutating the protected data.
sourcepub fn try_lock_exclusive(&self) -> LockResult<GLockGuardMut<'_, T>>
pub fn try_lock_exclusive(&self) -> LockResult<GLockGuardMut<'_, T>>
Attempts to acquire an Exclusive
lock on the current GLock
. If the lock is busy,
it will return a LockError::LockBusy
error. If this is a child GLock
, it will implicitly
attempt to acquire the appropriate lock on its parent GLock
.
The returned GLockGuardMut
allows mutating the protected data.
sourcepub fn lock_exclusive_using_parent<T2>(
&self,
parent: &GLockGuard<'_, T2>
) -> LockResult<GLockGuardMut<'_, T>>
pub fn lock_exclusive_using_parent<T2>(
&self,
parent: &GLockGuard<'_, T2>
) -> LockResult<GLockGuardMut<'_, T>>
Acquires an Exclusive
lock on the current child GLock
, using the specified GLockGuard
of the parent GLock
. If the lock is busy, it will block until it is ready.
The returned GLockGuardMut
allows mutating the protected data.
sourcepub fn try_lock_exclusive_using_parent<T2>(
&self,
parent: &GLockGuard<'_, T2>
) -> LockResult<GLockGuardMut<'_, T>>
pub fn try_lock_exclusive_using_parent<T2>(
&self,
parent: &GLockGuard<'_, T2>
) -> LockResult<GLockGuardMut<'_, T>>
Attempts to acquire an Exclusive
lock on the current child GLock
, using the
specified GLockGuard
of the parent GLock
. If the lock is busy, it will return a
LockError::LockBusy
error.
The returned GLockGuardMut
allows mutating the protected data.