pub struct ShardedMutexGuard<'a, T, TAG>(/* private fields */)
where
T: AssocObjects<TAG>;Expand description
The guard returned from locking a ShardedMutex. Dropping this will unlock the mutex.
Access to the underlying value is done by dereferencing this guard.
Implementations§
Source§impl<'a, T, TAG> ShardedMutexGuard<'a, T, TAG>where
T: AssocObjects<TAG>,
impl<'a, T, TAG> ShardedMutexGuard<'a, T, TAG>where
T: AssocObjects<TAG>,
Sourcepub fn then_lock<U, UTAG>(
self,
new: &'a ShardedMutex<U, UTAG>,
timeout: Duration,
) -> Result<ShardedMutexGuard<'_, U, UTAG>, ShardedMutexGuard<'_, T, TAG>>where
U: AssocObjects<UTAG>,
pub fn then_lock<U, UTAG>(
self,
new: &'a ShardedMutex<U, UTAG>,
timeout: Duration,
) -> Result<ShardedMutexGuard<'_, U, UTAG>, ShardedMutexGuard<'_, T, TAG>>where
U: AssocObjects<UTAG>,
Hand-over-hand locking, locks another ShardedMutex, then unlocks the mutex hold by
the self guard. This can result in a deadlock because the locking order of the
underlying mutexes is not known. To resolve this we use a timeout.
§Returns
Ok(ShardedMutexGuard<U, UTAG>) with the new acquired lock.
When new is the same mutex as self refers to, then Ok(self) is returned.
§Errors
Err(self) with the old lock still held, when the new lock was not acquired
When this function returns an ‘Err’ a deadlock could be the cause, it the
responsibility of the caller to resolve this. Usually this is done by undo any changes
in the object that is hold by self, then drop self and then retry the whole
operation. A error would also be returned when the new lock was not acquired within the
timeout.
§Example
use sharded_mutex::*;
use std::time::Duration;
let x = ShardedMutex::new(123);
let y = ShardedMutex::new(23.4);
let mut guard_x = x.lock();
let guard_y = guard_x.then_lock(&y, Duration::from_millis(100)).unwrap();
let guard_again = guard_y.then_lock(&y, Duration::from_millis(100)).unwrap();