Struct spin::RwLockUpgradeableGuard
source · pub struct RwLockUpgradeableGuard<'a, T: 'a + ?Sized> { /* private fields */ }
Expand description
A guard from which the protected data can be read, and can be upgraded to a writable guard if needed
No writers or other upgradeable guards can exist while this is in scope. New reader creation is prevented (to alleviate writer starvation) but there may be existing readers when the lock is acquired.
When the guard falls out of scope it will release the lock.
Implementations§
source§impl<'rwlock, T: ?Sized> RwLockUpgradeableGuard<'rwlock, T>
impl<'rwlock, T: ?Sized> RwLockUpgradeableGuard<'rwlock, T>
sourcepub fn upgrade(self) -> RwLockWriteGuard<'rwlock, T>
pub fn upgrade(self) -> RwLockWriteGuard<'rwlock, T>
Upgrades an upgradeable lock guard to a writable lock guard.
let mylock = spin::RwLock::new(0);
let upgradeable = mylock.upgradeable_read(); // Readable, but not yet writable
let writable = upgradeable.upgrade();
sourcepub fn try_upgrade(self) -> Result<RwLockWriteGuard<'rwlock, T>, Self>
pub fn try_upgrade(self) -> Result<RwLockWriteGuard<'rwlock, T>, Self>
Tries to upgrade an upgradeable lock guard to a writable lock guard.
let mylock = spin::RwLock::new(0);
let upgradeable = mylock.upgradeable_read(); // Readable, but not yet writable
match upgradeable.try_upgrade() {
Ok(writable) => /* upgrade successful - use writable lock guard */ (),
Err(upgradeable) => /* upgrade unsuccessful */ (),
};
sourcepub fn downgrade(self) -> RwLockReadGuard<'rwlock, T>
pub fn downgrade(self) -> RwLockReadGuard<'rwlock, T>
Downgrades the upgradeable lock guard to a readable, shared lock guard. Cannot fail and is guaranteed not to spin.
let mylock = spin::RwLock::new(1);
let upgradeable = mylock.upgradeable_read();
assert!(mylock.try_read().is_none());
assert_eq!(*upgradeable, 1);
let readable = upgradeable.downgrade(); // This is guaranteed not to spin
assert!(mylock.try_read().is_some());
assert_eq!(*readable, 1);