From 64b711461ed528de7c05868e5024f21e602cd4e0 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Fri, 30 Jun 2023 06:51:59 +0100 Subject: [PATCH] Fix `RwLockUpgradeableReadGuard::with_upgraded` Fixes #392 --- lock_api/src/rwlock.rs | 4 ++-- tests/issue_392.rs | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 tests/issue_392.rs diff --git a/lock_api/src/rwlock.rs b/lock_api/src/rwlock.rs index cfec59e1..275e6375 100644 --- a/lock_api/src/rwlock.rs +++ b/lock_api/src/rwlock.rs @@ -2025,7 +2025,7 @@ impl<'a, R: RawRwLockUpgradeDowngrade + 'a, T: ?Sized + 'a> RwLockUpgradableRead // Safety: We just upgraded the lock, so we have mutable access to the data. // This will restore the state the lock was in at the start of the function. - defer!(unsafe { self.rwlock.raw.downgrade_upgradable() }); + defer!(unsafe { self.rwlock.raw.downgrade_to_upgradable() }); // Safety: We upgraded the lock, so we have mutable access to the data. // When this function returns, whether by drop or panic, @@ -2047,7 +2047,7 @@ impl<'a, R: RawRwLockUpgradeDowngrade + 'a, T: ?Sized + 'a> RwLockUpgradableRead if unsafe { self.rwlock.raw.try_upgrade() } { // Safety: We just upgraded the lock, so we have mutable access to the data. // This will restore the state the lock was in at the start of the function. - defer!(unsafe { self.rwlock.raw.downgrade_upgradable() }); + defer!(unsafe { self.rwlock.raw.downgrade_to_upgradable() }); // Safety: We upgraded the lock, so we have mutable access to the data. // When this function returns, whether by drop or panic, diff --git a/tests/issue_392.rs b/tests/issue_392.rs new file mode 100644 index 00000000..f2bb5a1b --- /dev/null +++ b/tests/issue_392.rs @@ -0,0 +1,15 @@ +use parking_lot::RwLock; + +struct Lock(RwLock); + +#[test] +fn issue_392() { + let lock = Lock(RwLock::new(0)); + let mut rl = lock.0.upgradable_read(); + rl.with_upgraded(|_| { + println!("lock upgrade"); + }); + rl.with_upgraded(|_| { + println!("lock upgrade"); + }); +}