Skip to content
This repository has been archived by the owner on Nov 5, 2018. It is now read-only.

Remove Atomic's _owned methods #35

Merged
1 commit merged into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
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
383 changes: 153 additions & 230 deletions src/atomic.rs

Large diffs are not rendered by default.

8 changes: 2 additions & 6 deletions src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ unsafe impl Sync for Collector {}
impl Collector {
/// Creates a new collector.
pub fn new() -> Self {
Collector {
global: Arc::new(Global::new()),
}
Collector { global: Arc::new(Global::new()) }
}

/// Creates a new handle for the collector.
Expand All @@ -43,9 +41,7 @@ impl Collector {
impl Clone for Collector {
/// Creates another reference to the same garbage collector.
fn clone(&self) -> Self {
Collector {
global: self.global.clone(),
}
Collector { global: self.global.clone() }
}
}

Expand Down
16 changes: 4 additions & 12 deletions src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,21 @@ impl Epoch {
/// Returns the same epoch, but marked as pinned.
#[inline]
pub fn pinned(&self) -> Epoch {
Epoch {
data: self.data | 1,
}
Epoch { data: self.data | 1 }
}

/// Returns the same epoch, but marked as unpinned.
#[inline]
pub fn unpinned(&self) -> Epoch {
Epoch {
data: self.data & !1,
}
Epoch { data: self.data & !1 }
}

/// Returns the successor epoch.
///
/// The returned epoch will be marked as pinned only if the previous one was as well.
#[inline]
pub fn successor(&self) -> Epoch {
Epoch {
data: self.data.wrapping_add(2),
}
Epoch { data: self.data.wrapping_add(2) }
}

/// Decomposes the internal data into the epoch and the pin state.
Expand Down Expand Up @@ -97,9 +91,7 @@ impl AtomicEpoch {
/// Loads a value from the atomic epoch.
#[inline]
pub fn load(&self, ord: Ordering) -> Epoch {
Epoch {
data: self.data.load(ord),
}
Epoch { data: self.data.load(ord) }
}

/// Stores a value into the atomic epoch.
Expand Down
4 changes: 1 addition & 3 deletions src/garbage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ impl fmt::Debug for Garbage {
impl Garbage {
/// Make a closure that will later be called.
pub fn new<F: FnOnce() + Send>(f: F) -> Self {
Garbage {
func: Deferred::new(move || f()),
}
Garbage { func: Deferred::new(move || f()) }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Guard {
/// [`unprotected`]: fn.unprotected.html
pub unsafe fn defer<F, R>(&self, f: F)
where
F: FnOnce() -> R + Send
F: FnOnce() -> R + Send,
{
let garbage = Garbage::new(|| drop(f()));

Expand Down
6 changes: 1 addition & 5 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,7 @@ impl Local {
// that the second one makes pinning faster in this particular case.
let current = Epoch::starting();
let previous = self.epoch.compare_and_swap(current, new_epoch, SeqCst);
debug_assert_eq!(
current,
previous,
"participant was expected to be unpinned"
);
debug_assert_eq!(current, previous, "participant was expected to be unpinned");
} else {
self.epoch.store(new_epoch, Relaxed);
atomic::fence(SeqCst);
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
//!
//! Concurrent collections are built using atomic pointers. This module provides [`Atomic`], which
//! is just a shared atomic pointer to a heap-allocated object. Loading an [`Atomic`] yields a
//! [`Ptr`], which is an epoch-protected pointer through which the loaded object can be safely read.
//! [`Shared`], which is an epoch-protected pointer through which the loaded object can be safely
//! read.
//!
//! # Pinning
//!
Expand All @@ -49,7 +50,7 @@
//!
//! [`Atomic`]: struct.Atomic.html
//! [`Collector`]: struct.Collector.html
//! [`Ptr`]: struct.Ptr.html
//! [`Shared`]: struct.Shared.html
//! [`pin`]: fn.pin.html
//! [`defer`]: fn.defer.html

Expand All @@ -72,7 +73,7 @@ mod guard;
mod internal;
mod sync;

pub use self::atomic::{Atomic, CompareAndSetOrdering, Owned, Ptr};
pub use self::atomic::{Atomic, CompareAndSetOrdering, Owned, Shared};
pub use self::guard::{unprotected, Guard};
pub use self::default::{default_handle, is_pinned, pin};
pub use self::collector::{Collector, Handle};
20 changes: 10 additions & 10 deletions src/sync/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use std::marker::PhantomData;
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};

use {Atomic, Ptr, Guard, unprotected};
use {Atomic, Shared, Guard, unprotected};

/// An entry in a linked list.
///
Expand Down Expand Up @@ -92,7 +92,7 @@ pub struct Iter<'g, T: 'g, C: Container<T>> {
pred: &'g Atomic<Entry>,

/// The current entry.
curr: Ptr<'g, Entry>,
curr: Shared<'g, Entry>,

/// The phantom data for container.
_marker: PhantomData<(&'g T, C)>,
Expand Down Expand Up @@ -146,17 +146,17 @@ impl<T, C: Container<T>> List<T, C> {
/// - `container` is immovable, e.g. inside a `Box`;
/// - An entry is not inserted twice; and
/// - The inserted object will be removed before the list is dropped.
pub unsafe fn insert<'g>(&'g self, container: Ptr<'g, T>, guard: &'g Guard) {
pub unsafe fn insert<'g>(&'g self, container: Shared<'g, T>, guard: &'g Guard) {
let to = &self.head;
let entry = &*C::entry_of(container.as_raw());
let entry_ptr = Ptr::from_raw(entry);
let entry_ptr = Shared::from_raw(entry);
let mut next = to.load(Relaxed, guard);

loop {
entry.next.store(next, Relaxed);
match to.compare_and_set_weak(next, entry_ptr, Release, guard) {
Ok(_) => break,
Err(n) => next = n,
Err((_, n)) => next = n,
}
}
}
Expand Down Expand Up @@ -227,7 +227,7 @@ impl<'g, T: 'g, C: Container<T>> Iterator for Iter<'g, T, C> {
}
self.curr = succ;
}
Err(succ) => {
Err((_, succ)) => {
// We lost the race to delete the entry by a concurrent iterator. Set
// `self.curr` to the updated pointer, and report that we are stalled.
self.curr = succ;
Expand Down Expand Up @@ -273,9 +273,7 @@ mod tests {
}

fn finalize(entry: *const Entry) {
unsafe {
drop(Box::from_raw(entry as *mut Entry))
}
unsafe { drop(Box::from_raw(entry as *mut Entry)) }
}
}

Expand All @@ -297,7 +295,9 @@ mod tests {
assert!(iter.next().is_some());
assert!(iter.next().is_none());

unsafe { n2.as_ref().unwrap().delete(&guard); }
unsafe {
n2.as_ref().unwrap().delete(&guard);
}

let mut iter = l.iter(&guard);
assert!(iter.next().is_some());
Expand Down
10 changes: 4 additions & 6 deletions src/sync/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};

use crossbeam_utils::cache_padded::CachePadded;

use {unprotected, Atomic, Guard, Owned, Ptr};
use {unprotected, Atomic, Guard, Owned, Shared};

// The representation here is a singly-linked list, with a sentinel node at the front. In general
// the `tail` pointer may lag behind the actual tail. Non-sentinel nodes are either all `Data` or
Expand Down Expand Up @@ -65,7 +65,7 @@ impl<T> Queue<T> {
/// Attempts to atomically place `n` into the `next` pointer of `onto`, and returns `true` on
/// success. The queue's `tail` pointer may be updated.
#[inline(always)]
fn push_internal(&self, onto: Ptr<Node<T>>, new: Ptr<Node<T>>, guard: &Guard) -> bool {
fn push_internal(&self, onto: Shared<Node<T>>, new: Shared<Node<T>>, guard: &Guard) -> bool {
// is `onto` the actual tail?
let o = unsafe { onto.deref() };
let next = o.next.load(Acquire, guard);
Expand All @@ -76,7 +76,7 @@ impl<T> Queue<T> {
} else {
// looks like the actual tail; attempt to link in `n`
let result = o.next
.compare_and_set(Ptr::null(), new, Release, guard)
.compare_and_set(Shared::null(), new, Release, guard)
.is_ok();
if result {
// try to move the tail pointer forward
Expand Down Expand Up @@ -205,9 +205,7 @@ mod test {

impl<T> Queue<T> {
pub fn new() -> Queue<T> {
Queue {
queue: super::Queue::new(),
}
Queue { queue: super::Queue::new() }
}

pub fn push(&self, t: T) {
Expand Down