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

ArcCell: wait-free get() #81

Closed
wants to merge 2 commits 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
46 changes: 27 additions & 19 deletions src/sync/arc_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,56 @@ use std::sync::atomic::{AtomicUsize, Ordering};

/// A type providing atomic storage and retrieval of an `Arc<T>`.
#[derive(Debug)]
pub struct ArcCell<T>(AtomicUsize, PhantomData<Arc<T>>);
pub struct ArcCell<T> {
ptr: AtomicUsize,
sem: AtomicUsize,
_marker: PhantomData<Arc<T>>,
}

impl<T> Drop for ArcCell<T> {
fn drop(&mut self) {
self.take();
unsafe { mem::transmute::<_, Arc<T>>(self.ptr.load(Ordering::Relaxed)); }
}
}

impl<T> ArcCell<T> {
/// Creates a new `ArcCell`.
pub fn new(t: Arc<T>) -> ArcCell<T> {
ArcCell(AtomicUsize::new(unsafe { mem::transmute(t) }), PhantomData)
}

fn take(&self) -> Arc<T> {
loop {
match self.0.swap(0, Ordering::Acquire) {
0 => {}
n => return unsafe { mem::transmute(n) }
}
ArcCell {
ptr: AtomicUsize::new(unsafe { mem::transmute(t) }),
sem: AtomicUsize::new(0),
_marker: PhantomData,
}
}

fn put(&self, t: Arc<T>) {
debug_assert_eq!(self.0.load(Ordering::SeqCst), 0);
self.0.store(unsafe { mem::transmute(t) }, Ordering::Release);
/// Create a new `ArcCell` from the given `Arc` interior.
pub fn with_val(v: T) -> ArcCell<T> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no reason to shorten value to val.

ArcCell {
ptr: AtomicUsize::new(unsafe { mem::transmute(Arc::new(v)) }),
sem: AtomicUsize::new(0),
_marker: PhantomData,
}
}

/// Stores a new value in the `ArcCell`, returning the previous
/// value.
pub fn set(&self, t: Arc<T>) -> Arc<T> {
let old = self.take();
self.put(t);
old
unsafe {
let t: usize = mem::transmute(t);
let old: Arc<T> = mem::transmute(self.ptr.swap(t, Ordering::Acquire));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ordering is subtly incorrect, see my comment in the main thread for why. There's a crate atomic_utilities that has a fence/ordering combo that will create an optimal rmw setup that is ordered-before future loads and stores. I'm adding docs to it now but there's an example in the code.

while self.sem.load(Ordering::Relaxed) > 0 {}
old
}
}

/// Returns a copy of the value stored by the `ArcCell`.
pub fn get(&self) -> Arc<T> {
let t = self.take();
self.sem.fetch_add(1, Ordering::Relaxed);
let t: Arc<T> = unsafe { mem::transmute(self.ptr.load(Ordering::SeqCst)) };
// NB: correctness here depends on Arc's clone impl not panicking
let out = t.clone();
self.put(t);
self.sem.fetch_sub(1, Ordering::Relaxed);
mem::forget(t);
out
}
}
Expand Down