Skip to content

Commit

Permalink
Initial implementation of treiber stacks for conc.
Browse files Browse the repository at this point in the history
  • Loading branch information
ticki committed Jun 4, 2017
1 parent 31c3229 commit 492717a
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions conc/src/sync/treiber.rs
@@ -1,37 +1,57 @@
pub struct Treiber;
use std::sync::atomic;
use Atomic;

/*
pub struct Treiber<T> {
head: ::Option<Node<T>>,
head: Atomic<Node<T>>,
}

struct Node<T> {
data: T,
next: ::Option<Node<T>>,
next: *mut T,
}

impl<T> Treiber<T> {
pub fn new() -> Stack<T> {
Stack {
head: ::Option::default(),
pub fn new() -> Treiber<T> {
Treiber {
head: Atomic::default(),
}
}

pub fn push(&self, item: T) -> Option<T> {
pub fn pop(&self, item: T) -> Option<::Guard<T>> {
// TODO: Use `catch {}` here when it lands.
let mut head = self.head.load(atomic::Ordering::Acquire);
loop {
if let Some(head) = {
let next = head.next
while let Some(node) = head {
// TODO: This should be something that ignores the guard creation when the CAS
// fails, because it's expensive to do and not used anyway. It should be easy
// enough to implement, but I am struggling to come up with a good name for
// the method.
match self.head.compare_and_swap_raw(head, node.next, atomic::Ordering::Release) {
Ok(_) => return node.map(|x| x.data),
Err(new_head) => head = new_head,
}
}
}

match self.head.compare_and_swap(head, next, atomic::Ordering::Release) {
Ok()
Err(new_head) => head = new_head,
}
} else {
return None;
pub fn push(&self, item: T) {
// TODO: Use `catch {}` here when it lands.
let mut head = Box::new(Node {
data: item,
next: self.head.load(atomic::Ordering::Acquire),
});

loop {
// TODO: This should be something that ignores the guard creation when the CAS
// succeeds, because it's expensive to do and not used anyway. It should be easy
// enough to implement, but I am struggling to come up with a good name for the
// method.
match self.head.compare_and_swap_raw(head.next, Some(head), atomic::Ordering::Release) {
Ok(_) => break,
Err((new_head, Some(node))) => {
head = node;
head.next = new_head;
},
_ => unreachable!(),
}
}
}
}
*/

0 comments on commit 492717a

Please sign in to comment.