-
Notifications
You must be signed in to change notification settings - Fork 120
/
ring.rs
74 lines (59 loc) · 1.89 KB
/
ring.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::mem::MaybeUninit;
use loom::cell::UnsafeCell;
use loom::sync::{ atomic, Mutex };
const LENGTH: u32 = 8;
const MASK: u32 = LENGTH - 1;
pub struct Ring<T> {
buf: Box<[UnsafeCell<MaybeUninit<T>>]>,
head: atomic::AtomicU32,
tail: atomic::AtomicU32,
push_lock: Mutex<()>
}
impl<T: Copy> Ring<T> {
pub fn new() -> Ring<T> {
let mut buf = Vec::with_capacity(LENGTH as usize);
for _ in 0..LENGTH {
buf.push(UnsafeCell::new(MaybeUninit::uninit()));
}
Ring {
buf: buf.into_boxed_slice(),
head: atomic::AtomicU32::new(0),
tail: atomic::AtomicU32::new(0),
push_lock: Mutex::new(())
}
}
pub fn push(&self, t: T) -> Result<(), T> {
let _lock = self.push_lock.lock().unwrap();
let head = self.head.load(atomic::Ordering::Acquire);
let tail = unsafe { self.tail.unsync_load() };
if tail.wrapping_sub(head) == LENGTH {
return Err(t);
}
unsafe {
self.buf[(tail & MASK) as usize]
.with_mut(|p| (*p).as_mut_ptr().write(t));
}
self.tail.store(tail.wrapping_add(1), atomic::Ordering::Release);
Ok(())
}
pub fn pop(&self) -> Option<T> {
loop {
let head = self.head.load(atomic::Ordering::Acquire);
let tail = self.tail.load(atomic::Ordering::Acquire);
if head == tail {
return None;
}
let t = self.buf[(head & MASK) as usize]
.with(|p| unsafe { (*p).as_ptr().read() });
match self.head.compare_exchange_weak(
head,
head.wrapping_add(1),
atomic::Ordering::Release,
atomic::Ordering::Relaxed
) {
Ok(_) => return Some(t),
Err(_) => ()
}
}
}
}