Skip to content

Use ManuallyDrop in queues #184

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

Merged
1 commit merged into from Jul 25, 2018
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
15 changes: 8 additions & 7 deletions src/ms_queue.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use std::mem::{self, ManuallyDrop};
use std::ptr;
use std::sync::atomic::AtomicBool;
use std::{mem, ptr};
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use std::thread::{self, Thread};

use epoch::{self, Atomic, Owned, Shared};
Expand Down Expand Up @@ -28,7 +29,7 @@ struct Node<T> {
#[derive(Debug)]
enum Payload<T> {
/// A node with actual data that can be popped.
Data(T),
Data(ManuallyDrop<T>),
/// A node representing a blocked request for data.
Blocked(*mut Signal<T>),
}
Expand Down Expand Up @@ -122,7 +123,7 @@ impl<T> MsQueue<T> {
fn into_node(self) -> Owned<Node<T>> {
match self {
Cache::Data(t) => Owned::new(Node {
payload: Payload::Data(t),
payload: Payload::Data(ManuallyDrop::new(t)),
next: Atomic::null(),
}),
Cache::Node(n) => n,
Expand All @@ -134,7 +135,7 @@ impl<T> MsQueue<T> {
match self {
Cache::Data(t) => t,
Cache::Node(node) => match (*node.into_box()).payload {
Payload::Data(t) => t,
Payload::Data(t) => ManuallyDrop::into_inner(t),
_ => unreachable!(),
},
}
Expand Down Expand Up @@ -195,9 +196,9 @@ impl<T> MsQueue<T> {
}
}

#[inline(always)]
// Attempt to pop a data node. `Ok(None)` if queue is empty or in blocking
// mode; `Err(())` if lost race to pop.
#[inline(always)]
fn pop_internal(&self, guard: &epoch::Guard) -> Result<Option<T>, ()> {
let head_shared = self.head.load(Acquire, guard);
let head = unsafe { head_shared.as_ref() }.unwrap();
Expand All @@ -210,7 +211,7 @@ impl<T> MsQueue<T> {
.is_ok()
{
guard.defer(move || head_shared.into_owned());
Ok(Some(ptr::read(t)))
Ok(Some(ManuallyDrop::into_inner(ptr::read(t))))
} else {
Err(())
}
Expand Down
13 changes: 7 additions & 6 deletions src/seg_queue.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::cell::UnsafeCell;
use std::cmp;
use std::fmt;
use std::mem::{self, ManuallyDrop};
use std::ptr;
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use std::sync::atomic::{AtomicBool, AtomicUsize};
use std::fmt;
use std::{mem, ptr};
use std::cmp;
use std::cell::UnsafeCell;

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

Expand All @@ -21,7 +22,7 @@ pub struct SegQueue<T> {

struct Segment<T> {
low: AtomicUsize,
data: [UnsafeCell<(T, AtomicBool)>; SEG_SIZE],
data: ManuallyDrop<[UnsafeCell<(T, AtomicBool)>; SEG_SIZE]>,
high: AtomicUsize,
next: Atomic<Segment<T>>,
}
Expand All @@ -37,7 +38,7 @@ unsafe impl<T: Send> Sync for Segment<T> {}
impl<T> Segment<T> {
fn new() -> Segment<T> {
let rqueue = Segment {
data: unsafe { mem::uninitialized() },
data: unsafe { ManuallyDrop::new(mem::uninitialized()) },
low: AtomicUsize::new(0),
high: AtomicUsize::new(0),
next: Atomic::null(),
Expand Down