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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//! Lock-free single-producer single-consumer (SPSC) ring buffer.
//!
//! Implements a bounded SPSC ring buffer using `std::sync::atomic` for the
//! head/tail indices and a `Box<[std::cell::UnsafeCell<std::mem::MaybeUninit<T>>]>`
//! for storage — but because we cannot use `unsafe_code` in this crate we
//! build on `crossbeam::queue::ArrayQueue`, which is a proven lock-free
//! bounded queue implemented in pure safe Rust (its `unsafe` is inside
//! the `crossbeam-queue` crate itself, not ours).
//!
//! The `SpscRing<T: Copy>` wrapper presents the exact API required by the
//! task specification:
//!
//! * `push(val: T) -> bool` — returns `false` when full.
//! * `pop() -> Option<T>` — returns `None` when empty.
//! * `len() -> usize`
//! * `is_empty() -> bool`
//!
//! Because `ArrayQueue` is MPMC it is also trivially SPSC-safe. The wrapper
//! exposes `&self` receivers so a producer and consumer can share an
//! `Arc<SpscRing<T>>` without needing `&mut self`.
//!
//! # Usage
//!
//! ```rust
//! use std::sync::Arc;
//! use oximedia_videoip::spsc_ring::SpscRing;
//!
//! let ring = Arc::new(SpscRing::<u32>::new(64));
//! assert!(ring.push(42));
//! assert_eq!(ring.pop(), Some(42));
//! ```
use crossbeam::queue::ArrayQueue;
use std::sync::Arc;
/// Lock-free SPSC ring buffer backed by `crossbeam::queue::ArrayQueue`.
///
/// `T` must be `Copy` to match the specification (values are stored by value,
/// not behind a pointer).
pub struct SpscRing<T: Copy + Send> {
inner: ArrayQueue<T>,
}
impl<T: Copy + Send> SpscRing<T> {
/// Creates a new ring buffer that can hold at most `capacity` elements.
///
/// Panics if `capacity == 0`.
#[must_use]
pub fn new(capacity: usize) -> Self {
assert!(capacity > 0, "SpscRing capacity must be > 0");
Self {
inner: ArrayQueue::new(capacity),
}
}
/// Attempts to push a value into the ring.
///
/// Returns `true` on success, `false` if the ring is full.
pub fn push(&self, val: T) -> bool {
self.inner.push(val).is_ok()
}
/// Attempts to pop a value from the ring.
///
/// Returns `Some(val)` on success, `None` if the ring is empty.
pub fn pop(&self) -> Option<T> {
self.inner.pop()
}
/// Returns the number of elements currently in the ring.
#[must_use]
pub fn len(&self) -> usize {
self.inner.len()
}
/// Returns `true` if the ring is empty.
#[must_use]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
/// Returns the maximum number of elements the ring can hold.
#[must_use]
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
}
/// Convenience type alias for sharing a `SpscRing` across threads.
pub type SharedSpscRing<T> = Arc<SpscRing<T>>;
// ===========================================================================
// Tests
// ===========================================================================
#[cfg(test)]
mod tests {
use super::*;
use std::thread;
// ── Item 4 required tests ─────────────────────────────────────────────────
#[test]
fn test_ring_buffer_basic_push_pop() {
let ring = SpscRing::<u32>::new(8);
assert!(ring.is_empty());
assert_eq!(ring.len(), 0);
assert!(ring.push(10));
assert!(ring.push(20));
assert!(ring.push(30));
assert_eq!(ring.len(), 3);
assert!(!ring.is_empty());
assert_eq!(ring.pop(), Some(10));
assert_eq!(ring.pop(), Some(20));
assert_eq!(ring.pop(), Some(30));
assert_eq!(ring.pop(), None);
assert!(ring.is_empty());
}
#[test]
fn test_ring_buffer_full_returns_false() {
let ring = SpscRing::<u8>::new(3);
assert!(ring.push(1));
assert!(ring.push(2));
assert!(ring.push(3));
// Ring now full (capacity = 3).
assert!(!ring.push(4), "push on full ring should return false");
}
#[test]
fn test_ring_buffer_spsc_concurrent() {
const N: usize = 10_000;
let ring = Arc::new(SpscRing::<u64>::new(128));
let prod_ring = Arc::clone(&ring);
let producer = thread::spawn(move || {
let mut sent = 0u64;
while sent < N as u64 {
if prod_ring.push(sent) {
sent += 1;
} else {
// Ring full — yield.
thread::yield_now();
}
}
});
let cons_ring = Arc::clone(&ring);
let consumer = thread::spawn(move || {
let mut received = 0u64;
let mut last = u64::MAX;
while received < N as u64 {
if let Some(val) = cons_ring.pop() {
// Values must be strictly increasing (FIFO order).
assert!(
last == u64::MAX || val == last + 1,
"ordering violation: got {val} after {last}"
);
last = val;
received += 1;
} else {
thread::yield_now();
}
}
});
producer.join().expect("producer thread panicked");
consumer.join().expect("consumer thread panicked");
}
// ── Additional correctness tests ──────────────────────────────────────────
#[test]
fn test_ring_buffer_wrap_around() {
let ring = SpscRing::<i32>::new(4);
assert!(ring.push(1));
assert!(ring.push(2));
assert!(ring.push(3));
assert!(ring.push(4));
assert_eq!(ring.pop(), Some(1));
assert_eq!(ring.pop(), Some(2));
assert!(ring.push(5));
assert!(ring.push(6));
assert_eq!(ring.pop(), Some(3));
assert_eq!(ring.pop(), Some(4));
assert_eq!(ring.pop(), Some(5));
assert_eq!(ring.pop(), Some(6));
assert_eq!(ring.pop(), None);
}
#[test]
fn test_ring_buffer_capacity_reported() {
let ring = SpscRing::<u32>::new(16);
assert_eq!(ring.capacity(), 16);
}
#[test]
fn test_ring_buffer_pop_empty() {
let ring = SpscRing::<f64>::new(4);
assert_eq!(ring.pop(), None);
}
}