#![feature(placement_in_syntax, placement_new_protocol, unique)]
use std::mem;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::ops::{Place, InPlace, Placer};
use std::ops::Deref;
use std::marker::PhantomData;
use std::ptr::Unique;
use std::cell::Cell;
extern crate libc;
use libc::{MAP_SHARED, MAP_ANONYMOUS};
extern crate mmap;
pub struct Reader<'a, T: 'static> {
wfbuf: &'a MMBox<WfBuf<T>>
}
impl<'a, T> Reader<'a, T> {
fn new<'b>(wfbuf: &'b MMBox<WfBuf<T>>) -> Reader<'a ,T> where 'b: 'a {
Reader {
wfbuf: wfbuf
}
}
pub fn read(&self) -> T where T: Copy {
let mut last: usize = 0;
while last == 0 {
last = self.wfbuf.last.load(Ordering::SeqCst);
}
self.wfbuf.read.store(last, Ordering::SeqCst);
let value: T = self.wfbuf.buf[last - 1].get();
self.wfbuf.read.store(0, Ordering::SeqCst);
value
}
}
pub struct Writer<'a, T: 'static> {
wfbuf: &'a MMBox<WfBuf<T>>
}
impl<'a, T: Copy> Writer<'a, T> {
fn new<'b>(wfbuf: &'b MMBox<WfBuf<T>>) -> Writer<'a, T> where 'b: 'a {
Writer {
wfbuf: wfbuf
}
}
pub fn write(&self, t: T) {
let last: usize = self.wfbuf.last.load(Ordering::SeqCst);
let read: usize = self.wfbuf.read.load(Ordering::SeqCst);
for i in 1..3 {
if last != i && read != i {
self.wfbuf.buf[i - 1].set(t);
self.wfbuf.last.store(i, Ordering::SeqCst);
return;
}
}
unreachable!();
}
}
pub struct WfBuf<T> {
read: AtomicUsize,
last: AtomicUsize,
buf: [Cell<T>; 3]
}
impl<T: Default + Copy> WfBuf<T> {
pub fn new() -> Self {
WfBuf {
read: AtomicUsize::new(0),
last: AtomicUsize::new(0),
buf: [Default::default(), Default::default(), Default::default()]
}
}
}
pub struct MMBox<T> {
ptr: Unique<T>
}
impl<T: Default + Copy> MMBox<WfBuf<T>> {
pub fn into_reader(&self) -> Reader<T> {
Reader::new(self)
}
pub fn into_writer(&self) -> Writer<T> {
Writer::new(self)
}
pub fn new() -> Self {
in MMSHARED { WfBuf::<T>::new() }
}
}
impl<T> Deref for MMBox<WfBuf<T>> {
type Target = WfBuf<T>;
fn deref(&self) -> &Self::Target { unsafe { self.ptr.get() } }
}
pub const MMSHARED: MMSharedSingleton = MMSharedSingleton { _force_singleton: () };
#[derive(Clone, Copy)]
pub struct MMSharedSingleton { _force_singleton: () }
pub struct WfBufPlace<T> {
mm: mmap::MemoryMap,
marker: PhantomData<*mut T>
}
impl<T> Placer<T> for MMSharedSingleton {
type Place = WfBufPlace<T>;
fn make_place(self) -> Self::Place {
let size = mem::size_of::<WfBuf<T>>();
let mm = if size == 0 {
panic!("Wait free buffer implementation cannot have zero size.");
} else {
let mm = match mmap::MemoryMap::new(size, &[
mmap::MapOption::MapReadable,
mmap::MapOption::MapWritable,
mmap::MapOption::MapNonStandardFlags(MAP_SHARED | MAP_ANONYMOUS)
]) {
Ok(mm) => mm,
Err(msg) => panic!("{:?}", msg)
};
mm
};
WfBufPlace { mm: mm, marker: PhantomData }
}
}
impl<T> InPlace<T> for WfBufPlace<T> {
type Owner = MMBox<T>;
unsafe fn finalize(self) -> Self::Owner {
let p = self.mm.data() as *mut WfBuf<T>;
mem::forget(self);
mem::transmute(p)
}
}
impl<T> Place<T> for WfBufPlace<T> {
fn pointer(&mut self) -> *mut T {
unsafe { mem::transmute(self.mm.data()) }
}
}
#[cfg(test)]
mod tests {
use ::WfBuf;
use ::MMBox;
use ::libc::fork;
use ::std::thread::sleep_ms;
#[test]
fn write_sleep() {
let wfbuf = MMBox::<WfBuf<i64>>::new();
match unsafe { fork() } {
-1 => panic!("Fork failed."),
0 => {
let writer = wfbuf.into_writer();
sleep_ms(500);
writer.write(1);
writer.write(2);
},
_ => {
let reader = wfbuf.into_reader();
assert_eq!(1, reader.read());
}
}
}
#[test]
fn read_sleep() {
let wfbuf = MMBox::<WfBuf<i64>>::new();
match unsafe { fork() } {
-1 => panic!("Fork failed."),
0 => {
let writer = wfbuf.into_writer();
writer.write(1);
writer.write(2);
},
_ => {
let reader = wfbuf.into_reader();
sleep_ms(500);
assert_eq!(2, reader.read());
}
}
}
}