use std::mem::{self, ManuallyDrop};
use std::ptr;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use std::thread::{self, Thread};
use epoch::{self, Atomic, Owned, Shared};
use utils::CachePadded;
#[derive(Debug)]
pub struct MsQueue<T> {
head: CachePadded<Atomic<Node<T>>>,
tail: CachePadded<Atomic<Node<T>>>,
}
#[derive(Debug)]
struct Node<T> {
payload: Payload<T>,
next: Atomic<Node<T>>,
}
#[derive(Debug)]
enum Payload<T> {
Data(ManuallyDrop<T>),
Blocked(*mut Signal<T>),
}
#[derive(Debug)]
struct Signal<T> {
thread: Thread,
data: Option<T>,
ready: AtomicBool,
}
impl<T> Node<T> {
fn is_data(&self) -> bool {
if let Payload::Data(_) = self.payload {
true
} else {
false
}
}
}
unsafe impl<T: Send> Sync for MsQueue<T> {}
unsafe impl<T: Send> Send for MsQueue<T> {}
impl<T> MsQueue<T> {
pub fn new() -> MsQueue<T> {
let q = MsQueue {
head: CachePadded::new(Atomic::null()),
tail: CachePadded::new(Atomic::null()),
};
let sentinel = Owned::new(Node {
payload: Payload::Data(unsafe { mem::uninitialized() }),
next: Atomic::null(),
});
let guard = epoch::pin();
let sentinel = sentinel.into_shared(&guard);
q.head.store(sentinel, Relaxed);
q.tail.store(sentinel, Relaxed);
q
}
#[inline(always)]
fn push_internal(
&self,
guard: &epoch::Guard,
onto: Shared<Node<T>>,
n: Owned<Node<T>>,
) -> Result<(), Owned<Node<T>>> {
let next_atomic = &unsafe { onto.as_ref() }.unwrap().next;
let next_shared = next_atomic.load(Acquire, guard);
if unsafe { next_shared.as_ref() }.is_some() {
let _ = self.tail.compare_and_set(onto, next_shared, Release, guard);
Err(n)
} else {
next_atomic
.compare_and_set(Shared::null(), n, Release, guard)
.map(|shared| {
let _ = self.tail.compare_and_set(onto, shared, Release, guard);
}).map_err(|e| e.new)
}
}
pub fn push(&self, t: T) {
enum Cache<T> {
Data(T),
Node(Owned<Node<T>>),
}
impl<T> Cache<T> {
fn into_node(self) -> Owned<Node<T>> {
match self {
Cache::Data(t) => Owned::new(Node {
payload: Payload::Data(ManuallyDrop::new(t)),
next: Atomic::null(),
}),
Cache::Node(n) => n,
}
}
fn into_data(self) -> T {
match self {
Cache::Data(t) => t,
Cache::Node(node) => match (*node.into_box()).payload {
Payload::Data(t) => ManuallyDrop::into_inner(t),
_ => unreachable!(),
},
}
}
}
let mut cache = Cache::Data(t);
let guard = epoch::pin();
loop {
let tail_shared = self.tail.load(Acquire, &guard);
let tail_ref = unsafe { tail_shared.as_ref() }.unwrap();
if tail_ref.is_data() || self.head.load(Relaxed, &guard) == tail_shared {
match self.push_internal(&guard, tail_shared, cache.into_node()) {
Ok(_) => return,
Err(n) => {
cache = Cache::Node(n)
}
}
} else {
let head_shared = self.head.load(Acquire, &guard);
let head = unsafe { head_shared.as_ref() }.unwrap();
let next_shared = head.next.load(Acquire, &guard);
let request = unsafe { next_shared.as_ref() }.and_then(|next| match next.payload {
Payload::Blocked(signal) => Some((next_shared, signal)),
Payload::Data(_) => None,
});
if let Some((blocked_node, signal)) = request {
if self
.head
.compare_and_set(head_shared, blocked_node, Release, &guard)
.is_ok()
{
unsafe {
(*signal).data = Some(cache.into_data());
let thread = (*signal).thread.clone();
(*signal).ready.store(true, Release);
thread.unpark();
guard.defer_destroy(head_shared);
return;
}
}
}
}
}
}
#[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();
let next_shared = head.next.load(Acquire, guard);
if let Some(next) = unsafe { next_shared.as_ref() } {
if let Payload::Data(ref t) = next.payload {
unsafe {
if self
.head
.compare_and_set(head_shared, next_shared, Release, guard)
.is_ok()
{
guard.defer_destroy(head_shared);
Ok(Some(ManuallyDrop::into_inner(ptr::read(t))))
} else {
Err(())
}
}
} else {
Ok(None)
}
} else {
Ok(None)
}
}
pub fn is_empty(&self) -> bool {
let guard = epoch::pin();
let head = unsafe { self.head.load(Acquire, &guard).as_ref() }.unwrap();
if let Some(next) = unsafe { head.next.load(Acquire, &guard).as_ref() } {
if let Payload::Data(_) = next.payload {
false
} else {
true
}
} else {
true
}
}
pub fn try_pop(&self) -> Option<T> {
let guard = epoch::pin();
loop {
if let Ok(r) = self.pop_internal(&guard) {
return r;
}
}
}
pub fn pop(&self) -> T {
let guard = epoch::pin();
loop {
match self.pop_internal(&guard) {
Ok(Some(r)) => {
return r;
}
Ok(None) => {
break;
}
Err(()) => {}
}
}
let mut signal = Signal {
thread: thread::current(),
data: None,
ready: AtomicBool::new(false),
};
let mut node = Owned::new(Node {
payload: Payload::Blocked(&mut signal),
next: Atomic::null(),
});
loop {
if let Ok(Some(r)) = self.pop_internal(&guard) {
return r;
}
let tail_shared = self.tail.load(Acquire, &guard);
let tail = unsafe { tail_shared.as_ref() }.unwrap();
if tail.is_data() {
let head_shared = self.head.load(Relaxed, &guard);
if tail.is_data() && tail_shared != head_shared {
continue;
}
}
match self.push_internal(&guard, tail_shared, node) {
Ok(()) => break,
Err(n) => {
node = n;
}
}
}
drop(guard);
while !signal.ready.load(Acquire) {
thread::park();
}
signal.data.unwrap()
}
}
impl<T> Drop for MsQueue<T> {
fn drop(&mut self) {
while self.try_pop().is_some() {}
let guard = epoch::pin();
let sentinel = self.head.load(Relaxed, &guard).as_raw() as *mut Node<T>;
unsafe {
drop(Vec::from_raw_parts(sentinel, 0, 1));
}
}
}
impl<T> Default for MsQueue<T> {
fn default() -> Self {
MsQueue::new()
}
}
#[cfg(test)]
mod test {
const CONC_COUNT: i64 = 1000000;
use super::*;
use scope;
#[test]
fn push_try_pop_1() {
let q: MsQueue<i64> = MsQueue::new();
assert!(q.is_empty());
q.push(37);
assert!(!q.is_empty());
assert_eq!(q.try_pop(), Some(37));
assert!(q.is_empty());
}
#[test]
fn push_try_pop_2() {
let q: MsQueue<i64> = MsQueue::new();
assert!(q.is_empty());
q.push(37);
q.push(48);
assert_eq!(q.try_pop(), Some(37));
assert!(!q.is_empty());
assert_eq!(q.try_pop(), Some(48));
assert!(q.is_empty());
}
#[test]
fn push_try_pop_many_seq() {
let q: MsQueue<i64> = MsQueue::new();
assert!(q.is_empty());
for i in 0..200 {
q.push(i)
}
assert!(!q.is_empty());
for i in 0..200 {
assert_eq!(q.try_pop(), Some(i));
}
assert!(q.is_empty());
}
#[test]
fn push_pop_1() {
let q: MsQueue<i64> = MsQueue::new();
assert!(q.is_empty());
q.push(37);
assert!(!q.is_empty());
assert_eq!(q.pop(), 37);
assert!(q.is_empty());
}
#[test]
fn push_pop_2() {
let q: MsQueue<i64> = MsQueue::new();
q.push(37);
q.push(48);
assert_eq!(q.pop(), 37);
assert_eq!(q.pop(), 48);
}
#[test]
fn push_pop_many_seq() {
let q: MsQueue<i64> = MsQueue::new();
assert!(q.is_empty());
for i in 0..200 {
q.push(i)
}
assert!(!q.is_empty());
for i in 0..200 {
assert_eq!(q.pop(), i);
}
assert!(q.is_empty());
}
#[test]
fn push_try_pop_many_spsc() {
let q: MsQueue<i64> = MsQueue::new();
assert!(q.is_empty());
scope(|scope| {
scope.spawn(|_| {
let mut next = 0;
while next < CONC_COUNT {
if let Some(elem) = q.try_pop() {
assert_eq!(elem, next);
next += 1;
}
}
});
for i in 0..CONC_COUNT {
q.push(i)
}
}).unwrap();
}
#[test]
fn push_try_pop_many_spmc() {
fn recv(_t: i32, q: &MsQueue<i64>) {
let mut cur = -1;
for _i in 0..CONC_COUNT {
if let Some(elem) = q.try_pop() {
assert!(elem > cur);
cur = elem;
if cur == CONC_COUNT - 1 {
break;
}
}
}
}
let q: MsQueue<i64> = MsQueue::new();
assert!(q.is_empty());
scope(|scope| {
for i in 0..3 {
let q = &q;
scope.spawn(move |_| recv(i, q));
}
scope.spawn(|_| {
for i in 0..CONC_COUNT {
q.push(i);
}
});
}).unwrap();
}
#[test]
fn push_try_pop_many_mpmc() {
enum LR {
Left(i64),
Right(i64),
}
let q: MsQueue<LR> = MsQueue::new();
assert!(q.is_empty());
scope(|scope| {
for _t in 0..2 {
scope.spawn(|_| {
for i in CONC_COUNT - 1..CONC_COUNT {
q.push(LR::Left(i))
}
});
scope.spawn(|_| {
for i in CONC_COUNT - 1..CONC_COUNT {
q.push(LR::Right(i))
}
});
scope.spawn(|_| {
let mut vl = vec![];
let mut vr = vec![];
for _i in 0..CONC_COUNT {
match q.try_pop() {
Some(LR::Left(x)) => vl.push(x),
Some(LR::Right(x)) => vr.push(x),
_ => {}
}
}
let mut vl2 = vl.clone();
let mut vr2 = vr.clone();
vl2.sort();
vr2.sort();
assert_eq!(vl, vl2);
assert_eq!(vr, vr2);
});
}
}).unwrap();
}
#[test]
fn push_pop_many_spsc() {
let q: MsQueue<i64> = MsQueue::new();
scope(|scope| {
scope.spawn(|_| {
let mut next = 0;
while next < CONC_COUNT {
assert_eq!(q.pop(), next);
next += 1;
}
});
for i in 0..CONC_COUNT {
q.push(i)
}
}).unwrap();
assert!(q.is_empty());
}
#[test]
fn is_empty_dont_pop() {
let q: MsQueue<i64> = MsQueue::new();
q.push(20);
q.push(20);
assert!(!q.is_empty());
assert!(!q.is_empty());
assert!(q.try_pop().is_some());
}
}