Skip to content

Commit

Permalink
Use ManuallyDrop in TreiberStack
Browse files Browse the repository at this point in the history
  • Loading branch information
Vtec234 committed Oct 24, 2018
1 parent acc8643 commit 7ba55fb
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/treiber_stack.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use std::{ptr, mem};
use std::mem::ManuallyDrop;
use std::ptr;

use epoch::{self, Atomic, Owned};

Expand All @@ -13,17 +14,10 @@ pub struct TreiberStack<T> {

#[derive(Debug)]
struct Node<T> {
data: T,
data: ManuallyDrop<T>,
next: Atomic<Node<T>>,
}

impl<T> Node<T> {
/// Deallocates the memory for this node without executing T's destructor.
fn finalize(n: Node<T>) {
mem::forget(n.data);
}
}

impl<T> TreiberStack<T> {
/// Create a new, empty stack.
pub fn new() -> TreiberStack<T> {
Expand All @@ -35,7 +29,7 @@ impl<T> TreiberStack<T> {
/// Push `t` on top of the stack.
pub fn push(&self, t: T) {
let mut n = Owned::new(Node {
data: t,
data: ManuallyDrop::new(t),
next: Atomic::null(),
});
let guard = epoch::pin();
Expand Down Expand Up @@ -64,10 +58,8 @@ impl<T> TreiberStack<T> {
.is_ok()
{
unsafe {
guard.defer(move || {
Node::<T>::finalize(*head_shared.into_owned().into_box())
} );
return Some(ptr::read(&(*head).data));
guard.defer(move || drop(head_shared.into_owned()) );
return Some(ManuallyDrop::into_inner(ptr::read(&(*head).data)));
}
}
}
Expand Down

0 comments on commit 7ba55fb

Please sign in to comment.