Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid thread::panicking() in non-poisoning methods of Mutex and RwLock #97924

Merged
merged 1 commit into from
Jun 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions library/std/src/sync/mutex.rs
Original file line number Diff line number Diff line change
@@ -423,7 +423,7 @@ impl<T: ?Sized> Mutex<T> {
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<T: ?Sized> Mutex<T> {
#[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<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {

impl<'mutex, T: ?Sized> MutexGuard<'mutex, T> {
unsafe fn new(lock: &'mutex Mutex<T>) -> LockResult<MutexGuard<'mutex, T>> {
poison::map_result(lock.poison.borrow(), |guard| MutexGuard { lock, poison: guard })
poison::map_result(lock.poison.guard(), |guard| MutexGuard { lock, poison: guard })
}
}

9 changes: 8 additions & 1 deletion library/std/src/sync/poison.rs
Original file line number Diff line number Diff line change
@@ -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<Guard> {
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<Guard> {
let ret = Guard { panicking: thread::panicking() };
if self.get() { Err(PoisonError::new(ret)) } else { Ok(ret) }
}
8 changes: 4 additions & 4 deletions library/std/src/sync/rwlock.rs
Original file line number Diff line number Diff line change
@@ -434,7 +434,7 @@ impl<T: ?Sized> RwLock<T> {
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<T: ?Sized> RwLock<T> {
#[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<T> From<T> for RwLock<T> {

impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> {
unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockReadGuard<'rwlock, T>> {
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<T>) -> LockResult<RwLockWriteGuard<'rwlock, T>> {
poison::map_result(lock.poison.borrow(), |guard| RwLockWriteGuard { lock, poison: guard })
poison::map_result(lock.poison.guard(), |guard| RwLockWriteGuard { lock, poison: guard })
}
}