Skip to content

Commit d19e334

Browse files
committed
deal with poison
1 parent 907250d commit d19e334

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

library/std/src/sync/rwlock.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::marker::PhantomData;
77
use crate::mem::{forget, ManuallyDrop};
88
use crate::ops::{Deref, DerefMut};
99
use crate::ptr::NonNull;
10-
use crate::sync::{poison, LockResult, TryLockError, TryLockResult};
10+
use crate::sync::{poison, LockResult, PoisonError, TryLockError, TryLockResult};
1111
use crate::sys::sync as sys;
1212

1313
/// A reader-writer lock
@@ -966,23 +966,20 @@ impl<'a, T: ?Sized> RwLockWriteGuard<'a, T> {
966966
///
967967
/// Atomically changes the state of the [`RwLock`] from exclusive mode into shared mode.
968968
///
969-
/// MORE DOCS COMING SOON.
969+
/// FIXME MORE DOCS COMING SOON.
970970
#[unstable(feature = "rwlock_downgrade", issue = "128203")]
971971
pub fn downgrade(s: Self) -> RwLockReadGuard<'a, T> {
972972
let lock = s.lock;
973973

974+
// We don't want to call the destructor since that calls `write_unlock`.
975+
forget(s);
976+
974977
// SAFETY: We take ownership of a write guard, so we must already have the `RwLock` in write
975978
// mode, satisfying the `downgrade` contract.
976979
unsafe { lock.inner.downgrade() };
977980

978-
// We don't want to call the destructor since that calls `write_unlock`.
979-
forget(s);
980-
981981
// SAFETY: We have just successfully called `downgrade`, so we fulfill the safety contract.
982-
unsafe {
983-
RwLockReadGuard::new(lock)
984-
.expect("We had the exclusive lock so we can't have panicked while holding it")
985-
}
982+
unsafe { RwLockReadGuard::new(lock).unwrap_or_else(PoisonError::into_inner) }
986983
}
987984
}
988985

0 commit comments

Comments
 (0)