Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of treiber stacks for
conc
.
- Loading branch information
Showing
1 changed file
with
38 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!(), | ||
} | ||
} | ||
} | ||
} | ||
*/ |