Skip to content

Commit

Permalink
Auto merge of rust-lang#96422 - tmccombs:mutex-unpoison, r=m-ou-se
Browse files Browse the repository at this point in the history
Add functions to un-poison Mutex and RwLock

See discussion at https://internals.rust-lang.org/t/unpoisoning-a-mutex/16521/3
  • Loading branch information
bors committed May 20, 2022
2 parents 52cc779 + a65afd8 commit cd73afa
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
39 changes: 39 additions & 0 deletions library/std/src/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,45 @@ impl<T: ?Sized> Mutex<T> {
self.poison.get()
}

/// Clear the poisoned state from a mutex
///
/// If the mutex is poisoned, it will remain poisoned until this function is called. This
/// allows recovering from a poisoned state and marking that it has recovered. For example, if
/// the value is overwritten by a known-good value, then the mutex can be marked as
/// un-poisoned. Or possibly, the value could be inspected to determine if it is in a
/// consistent state, and if so the poison is removed.
///
/// # Examples
///
/// ```
/// #![feature(mutex_unpoison)]
///
/// use std::sync::{Arc, Mutex};
/// use std::thread;
///
/// let mutex = Arc::new(Mutex::new(0));
/// let c_mutex = Arc::clone(&mutex);
///
/// let _ = thread::spawn(move || {
/// let _lock = c_mutex.lock().unwrap();
/// panic!(); // the mutex gets poisoned
/// }).join();
///
/// assert_eq!(mutex.is_poisoned(), true);
/// let x = mutex.lock().unwrap_or_else(|mut e| {
/// **e.get_mut() = 1;
/// mutex.clear_poison();
/// e.into_inner()
/// });
/// assert_eq!(mutex.is_poisoned(), false);
/// assert_eq!(*x, 1);
/// ```
#[inline]
#[unstable(feature = "mutex_unpoison", issue = "96469")]
pub fn clear_poison(&self) {
self.poison.clear();
}

/// Consumes this mutex, returning the underlying data.
///
/// # Errors
Expand Down
5 changes: 5 additions & 0 deletions library/std/src/sync/poison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ impl Flag {
pub fn get(&self) -> bool {
self.failed.load(Ordering::Relaxed)
}

#[inline]
pub fn clear(&self) {
self.failed.store(false, Ordering::Relaxed)
}
}

pub struct Guard {
Expand Down
39 changes: 39 additions & 0 deletions library/std/src/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,45 @@ impl<T: ?Sized> RwLock<T> {
self.poison.get()
}

/// Clear the poisoned state from a lock
///
/// If the lock is poisoned, it will remain poisoned until this function is called. This allows
/// recovering from a poisoned state and marking that it has recovered. For example, if the
/// value is overwritten by a known-good value, then the mutex can be marked as un-poisoned. Or
/// possibly, the value could be inspected to determine if it is in a consistent state, and if
/// so the poison is removed.
///
/// # Examples
///
/// ```
/// #![feature(mutex_unpoison)]
///
/// use std::sync::{Arc, RwLock};
/// use std::thread;
///
/// let lock = Arc::new(RwLock::new(0));
/// let c_lock = Arc::clone(&lock);
///
/// let _ = thread::spawn(move || {
/// let _lock = c_lock.write().unwrap();
/// panic!(); // the mutex gets poisoned
/// }).join();
///
/// assert_eq!(lock.is_poisoned(), true);
/// let guard = lock.write().unwrap_or_else(|mut e| {
/// **e.get_mut() = 1;
/// lock.clear_poison();
/// e.into_inner()
/// });
/// assert_eq!(lock.is_poisoned(), false);
/// assert_eq!(*guard, 1);
/// ```
#[inline]
#[unstable(feature = "mutex_unpoison", issue = "96469")]
pub fn clear_poison(&self) {
self.poison.clear();
}

/// Consumes this `RwLock`, returning the underlying data.
///
/// # Errors
Expand Down

0 comments on commit cd73afa

Please sign in to comment.