diff --git a/library/std/src/sync/mutex.rs b/library/std/src/sync/mutex.rs index b2fbb77204aa0..31342a8905452 100644 --- a/library/std/src/sync/mutex.rs +++ b/library/std/src/sync/mutex.rs @@ -423,7 +423,7 @@ impl Mutex { T: Sized, { let data = self.data.into_inner(); - poison::map_result(self.poison.borrow(), |_| data) + poison::map_result(self.poison.borrow(), |()| data) } /// Returns a mutable reference to the underlying data. @@ -448,7 +448,7 @@ impl Mutex { #[stable(feature = "mutex_get_mut", since = "1.6.0")] pub fn get_mut(&mut self) -> LockResult<&mut T> { let data = self.data.get_mut(); - poison::map_result(self.poison.borrow(), |_| data) + poison::map_result(self.poison.borrow(), |()| data) } } @@ -497,7 +497,7 @@ impl fmt::Debug for Mutex { impl<'mutex, T: ?Sized> MutexGuard<'mutex, T> { unsafe fn new(lock: &'mutex Mutex) -> LockResult> { - poison::map_result(lock.poison.borrow(), |guard| MutexGuard { lock, poison: guard }) + poison::map_result(lock.poison.guard(), |guard| MutexGuard { lock, poison: guard }) } } diff --git a/library/std/src/sync/poison.rs b/library/std/src/sync/poison.rs index ba91fb0499ff0..9c918be338794 100644 --- a/library/std/src/sync/poison.rs +++ b/library/std/src/sync/poison.rs @@ -23,8 +23,15 @@ impl Flag { Flag { failed: AtomicBool::new(false) } } + /// Check the flag for an unguarded borrow, where we only care about existing poison. #[inline] - pub fn borrow(&self) -> LockResult { + pub fn borrow(&self) -> LockResult<()> { + if self.get() { Err(PoisonError::new(())) } else { Ok(()) } + } + + /// Check the flag for a guarded borrow, where we may also set poison when `done`. + #[inline] + pub fn guard(&self) -> LockResult { let ret = Guard { panicking: thread::panicking() }; if self.get() { Err(PoisonError::new(ret)) } else { Ok(ret) } } diff --git a/library/std/src/sync/rwlock.rs b/library/std/src/sync/rwlock.rs index 4f1b4bedaab25..9517e7e1f0325 100644 --- a/library/std/src/sync/rwlock.rs +++ b/library/std/src/sync/rwlock.rs @@ -434,7 +434,7 @@ impl RwLock { T: Sized, { let data = self.data.into_inner(); - poison::map_result(self.poison.borrow(), |_| data) + poison::map_result(self.poison.borrow(), |()| data) } /// Returns a mutable reference to the underlying data. @@ -461,7 +461,7 @@ impl RwLock { #[stable(feature = "rwlock_get_mut", since = "1.6.0")] pub fn get_mut(&mut self) -> LockResult<&mut T> { let data = self.data.get_mut(); - poison::map_result(self.poison.borrow(), |_| data) + poison::map_result(self.poison.borrow(), |()| data) } } @@ -510,13 +510,13 @@ impl From for RwLock { impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> { unsafe fn new(lock: &'rwlock RwLock) -> LockResult> { - poison::map_result(lock.poison.borrow(), |_| RwLockReadGuard { lock }) + poison::map_result(lock.poison.borrow(), |()| RwLockReadGuard { lock }) } } impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> { unsafe fn new(lock: &'rwlock RwLock) -> LockResult> { - poison::map_result(lock.poison.borrow(), |guard| RwLockWriteGuard { lock, poison: guard }) + poison::map_result(lock.poison.guard(), |guard| RwLockWriteGuard { lock, poison: guard }) } }