Skip to content

Added method to unpoison poisoned RWARCs #7392

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

Closed
wants to merge 1 commit into from
Closed
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
41 changes: 41 additions & 0 deletions src/libextra/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use core::cast;
use core::unstable::sync::UnsafeAtomicRcBox;
use core::task;
use core::borrow;
use core::util;

/// As sync::condvar, a mechanism for unlock-and-descheduling and signaling.
pub struct Condvar<'self> {
Expand Down Expand Up @@ -332,6 +333,28 @@ impl<T:Const + Owned> RWARC<T> {
}
}

/// As write(), but unpoisons poisoned ARCs.
#[inline]
pub fn unpoison<U>(&self, blk: &fn() -> (T, U)) -> U {
unsafe {
let state = self.x.get();
do (*borrow_rwlock(state)).write {
let _z = PoisonOnFail(&mut (*state).failed);

let mut (writevalue, result) = blk();

util::swap(&mut (*state).data, &mut writevalue);
if (*state).failed {
// Might be uninitialized so is unsafe to destroy
cast::forget(writevalue)
}
(*state).failed = false;

result
}
}
}

/// As write(), but with a condvar, as sync::rwlock.write_cond().
#[inline]
pub fn write_cond<'x, 'c, U>(&self,
Expand Down Expand Up @@ -736,6 +759,24 @@ mod tests {
}
}
#[test]
fn test_rw_arc_unpoison() {
let arc = RWARC(0);

// Poison the arc
let arc_clone = arc.clone();
do task::spawn_unlinked {
do arc_clone.write |_| {
fail!()
}
}

do arc.unpoison { (0, ()) }

do arc.read |_| {
// Should succeed at reading the now unpoisoned value
}
}
#[test]
fn test_rw_downgrade() {
// (1) A downgrader gets in write mode and does cond.wait.
// (2) A writer gets in write mode, sets state to 42, and does signal.
Expand Down