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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
//! Lock-free circular buffer for single-producer, single-consumer scenarios.
//!
//! This module provides `BufferWheel`, a fixed-size circular buffer that enables
//! lock-free communication between one producer and one consumer thread using
//! atomic operations. Ideal for high-performance message passing and streaming
//! data processing.
use std::ptr;
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use std::sync::atomic::{AtomicPtr, AtomicUsize};
use crate::MesoError;
/// A lock-free circular buffer designed for single-producer, single-consumer (SPSC) scenarios.
#[derive(Debug)]
pub struct BufferWheel<const N: usize, T> {
/// Array of atomic pointers to stored data. Each slot can hold one item.
buffers: [AtomicPtr<T>; N],
/// Current read position in the circular buffer
read: AtomicUsize,
/// Current write position in the circular buffer
write: AtomicUsize,
}
impl<const N: usize, T> Default for BufferWheel<N, T> {
/// Creates a new empty `BufferWheel` with default initialization.
fn default() -> Self {
BufferWheel::new()
}
}
impl<const N: usize, T> BufferWheel<N, T> {
/// Creates a new empty `BufferWheel`.
///
/// Initializes all buffer slots to null pointers and sets read/write positions to 0.
/// The buffer starts in an empty state.
pub fn new() -> Self {
let buffers = array_init::array_init(|_| AtomicPtr::new(ptr::null_mut()));
Self {
buffers,
read: AtomicUsize::new(0),
write: AtomicUsize::new(0),
}
}
/// Writes data to the buffer.
///
/// Attempts to write the provided data to the next available slot in the circular buffer.
/// The data is moved into the buffer (not copied). If the buffer is full, returns an error
/// without consuming the data.
pub fn write(&self, data: T) -> Result<(), MesoError> {
let write = self.write.load(Relaxed);
let next_write = (write + 1) % N;
// The buffer is full if the next write position would be the same as the read position.
if next_write == self.read.load(Acquire) {
return Err(MesoError::BuffersFull);
}
// The old pointer should be null, as we never overwrite unread data.
let new_ptr = Box::into_raw(Box::new(data));
let old_ptr = self.buffers[write].swap(new_ptr, Release);
assert!(
old_ptr.is_null(),
"BufferWheel write overwrote unread data, indicates a bug."
);
// Make the new data available by updating the write index.
self.write.store(next_write, Release);
Ok(())
}
/// Reads data from the buffer.
///
/// This will fail if the buffer is empty.
pub fn read(&self) -> Result<T, MesoError> {
let read = self.read.load(Relaxed);
// The buffer is empty if the read and write positions are the same.
if read == self.write.load(Acquire) {
return Err(MesoError::NoPendingUpdates);
}
// Take the pointer from the buffer, replacing it with null.
let null = ptr::null_mut();
let data_ptr = self.buffers[read].swap(null, Acquire);
// This should never happen in a correct SPSC scenario.
if data_ptr.is_null() {
// This indicates the producer hasn't finished writing the data before updating the write index,
// which points to a memory ordering bug. With correct Release/Acquire, this is a safeguard.
return Err(MesoError::ExpectedUpdate);
}
let data = unsafe { *Box::from_raw(data_ptr) };
// Make the slot available for writing by updating the read index.
let next_read = (read + 1) % N;
self.read.store(next_read, Release);
Ok(data)
}
}
unsafe impl<const N: usize, T: Clone> Send for BufferWheel<N, T> {}
unsafe impl<const N: usize, T: Clone> Sync for BufferWheel<N, T> {}
#[cfg(test)]
mod spsc_tests {
use super::*;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
//use std::time::Duration;
#[test]
fn sequential_write_read() {
let buf = BufferWheel::<3, i32>::default();
// reading from an empty buffer should complain
assert_eq!(buf.read().unwrap_err(), MesoError::NoPendingUpdates);
// write two items
buf.write(42).expect("first write okay");
buf.write(1337).expect("second write okay");
// read them back in order
assert_eq!(buf.read().unwrap(), 42);
assert_eq!(buf.read().unwrap(), 1337);
// and now it's empty again
assert_eq!(buf.read().unwrap_err(), MesoError::NoPendingUpdates);
}
#[test]
fn capacity_full_and_recover() {
// capacity 2 -> on the 2nd write 'full' flips on,
// 3rd write errors until we read something.
let buf = BufferWheel::<3, u8>::default();
assert!(buf.write(10).is_ok());
assert!(buf.write(20).is_ok());
// buffer is full now
let e = buf.write(30).unwrap_err();
assert_eq!(e, MesoError::BuffersFull);
// read one slot, which should clear `full`
assert_eq!(buf.read().unwrap(), 10);
// now we can write again
buf.write(30).expect("recovered after read");
// drain the rest
assert_eq!(buf.read().unwrap(), 20);
assert_eq!(buf.read().unwrap(), 30);
// finally empty
assert_eq!(buf.read().unwrap_err(), MesoError::NoPendingUpdates);
}
#[test]
fn spsc_concurrent_spinning() {
// tiny buffer of size 1; writer must spin until reader catches up
let buf = Arc::new(BufferWheel::<2, usize>::default());
let prod = Arc::clone(&buf);
let cons = Arc::clone(&buf);
let writer = thread::spawn(move || {
for i in 0..100 {
// spin on full
loop {
match prod.write(i) {
Ok(_) => break,
Err(MesoError::BuffersFull) => continue,
Err(e) => panic!("unexpected write error: {e:?}"),
}
}
}
});
let reader = thread::spawn(move || {
for expected in 0..100 {
// spin on empty
loop {
match cons.read() {
Ok(v) => {
assert_eq!(v, expected);
break;
}
Err(MesoError::NoPendingUpdates) => continue,
Err(e) => panic!("unexpected read error: {e:?}"),
}
}
}
});
writer.join().unwrap();
reader.join().unwrap();
}
#[test]
fn spsc_concurrent_large_buffer() {
const BUFFER_SIZE: usize = 1024;
const NUM_MESSAGES: usize = 100_000;
let buf = Arc::new(BufferWheel::<BUFFER_SIZE, usize>::default());
let prod = Arc::clone(&buf);
let cons = Arc::clone(&buf);
let writer = thread::spawn(move || {
for i in 0..NUM_MESSAGES {
loop {
match prod.write(i) {
Ok(_) => break,
Err(MesoError::BuffersFull) => {
//println!("Buffers full");
thread::sleep(Duration::from_nanos(2))
} // Yield if full
Err(e) => panic!("unexpected write error: {e:?}"),
}
}
}
});
let reader = thread::spawn(move || {
for expected in 0..NUM_MESSAGES {
loop {
match cons.read() {
Ok(v) => {
//println!("managed a read");
assert_eq!(
v, expected,
"Data integrity check failed at message {expected}"
);
break;
}
Err(MesoError::NoPendingUpdates) => {
//println!("Buffers empty");
thread::sleep(Duration::from_nanos(2))
} // Yield if empty
Err(e) => panic!("unexpected read error: {e:?}"),
}
}
}
});
writer.join().unwrap();
reader.join().unwrap();
}
#[test]
fn spsc_concurrent_alternating_write_read() {
const BUFFER_SIZE: usize = 4;
const ITERATIONS: usize = 1000;
let buf = Arc::new(BufferWheel::<BUFFER_SIZE, usize>::default());
let prod = Arc::clone(&buf);
let cons = Arc::clone(&buf);
let writer = thread::spawn(move || {
for i in 0..ITERATIONS {
loop {
if prod.write(i).is_ok() {
break;
}
thread::yield_now();
}
// lil randomness
if i % 50 == 0 {
thread::sleep(Duration::from_nanos(1));
}
}
});
let reader = thread::spawn(move || {
for expected in 0..ITERATIONS {
loop {
match cons.read() {
Ok(v) => {
assert_eq!(
v, expected,
"Alternating check: expected {expected}, got {v}"
);
break;
}
Err(MesoError::NoPendingUpdates) => thread::yield_now(),
Err(e) => panic!("unexpected read error: {e:?}"),
}
}
// lil randomness
if expected % 75 == 0 {
thread::sleep(Duration::from_nanos(1));
}
}
});
writer.join().unwrap();
reader.join().unwrap();
}
#[test]
fn spsc_concurrent_producer_faster() {
const BUFFER_SIZE: usize = 4;
const NUM_MESSAGES: usize = 1000;
let buf = Arc::new(BufferWheel::<BUFFER_SIZE, usize>::default());
let prod = Arc::clone(&buf);
let cons = Arc::clone(&buf);
let writer = thread::spawn(move || {
for i in 0..NUM_MESSAGES {
loop {
match prod.write(i) {
Ok(_) => break,
Err(MesoError::BuffersFull) => thread::yield_now(),
Err(e) => panic!("unexpected write error: {e:?}"),
}
}
}
});
let reader = thread::spawn(move || {
// Give producer a head start
thread::sleep(Duration::from_millis(10));
for expected in 0..NUM_MESSAGES {
loop {
match cons.read() {
Ok(v) => {
assert_eq!(
v, expected,
"Producer faster: expected {expected}, got {v}"
);
break;
}
Err(MesoError::NoPendingUpdates) => thread::yield_now(),
Err(e) => panic!("unexpected read error: {e:?}"),
}
}
}
});
writer.join().unwrap();
reader.join().unwrap();
}
#[test]
fn spsc_concurrent_consumer_faster() {
const BUFFER_SIZE: usize = 4;
const NUM_MESSAGES: usize = 1000;
let buf = Arc::new(BufferWheel::<BUFFER_SIZE, usize>::default());
let prod = Arc::clone(&buf);
let cons = Arc::clone(&buf);
let writer = thread::spawn(move || {
// Give consumer a head start
thread::sleep(Duration::from_millis(10));
for i in 0..NUM_MESSAGES {
loop {
match prod.write(i) {
Ok(_) => break,
Err(MesoError::BuffersFull) => thread::yield_now(),
Err(e) => panic!("unexpected write error: {e:?}"),
}
}
}
});
let reader = thread::spawn(move || {
for expected in 0..NUM_MESSAGES {
loop {
match cons.read() {
Ok(v) => {
assert_eq!(
v, expected,
"Consumer faster: expected {expected}, got {v}"
);
break;
}
Err(MesoError::NoPendingUpdates) => thread::yield_now(),
Err(e) => panic!("unexpected read error: {e:?}"),
}
}
}
});
writer.join().unwrap();
reader.join().unwrap();
}
}
#[cfg(test)]
mod bufferwheel_brutal_stress_tests {
use super::*;
use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Barrier};
use std::thread;
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct StressPayload {
id: u64,
data: Vec<u8>,
checksum: u64,
}
impl StressPayload {
fn new(id: u64, size: usize) -> Self {
let data: Vec<u8> = (0..size)
.map(|i| (id as u8).wrapping_add(i as u8))
.collect();
let checksum = data.iter().map(|&b| b as u64).sum::<u64>().wrapping_add(id);
Self { id, data, checksum }
}
fn verify(&self) -> bool {
let expected_checksum = self
.data
.iter()
.map(|&b| b as u64)
.sum::<u64>()
.wrapping_add(self.id);
self.checksum == expected_checksum
}
}
/// This test uses a tiny buffer to maximize contention. The producer aggressively writes,
/// while the consumer validates every message. The final assertion ensures that the
/// number of messages successfully written equals the number of messages received and validated.
#[test]
fn test_tiny_buffer_high_contention() {
const BUFFER_SIZE: usize = 2;
const NUM_MESSAGES: u64 = 10_000;
let buffer = Arc::new(BufferWheel::<BUFFER_SIZE, StressPayload>::new());
let barrier = Arc::new(Barrier::new(2));
// --- Producer Thread ---
let prod_buffer = Arc::clone(&buffer);
let prod_barrier = Arc::clone(&barrier);
let producer = thread::spawn(move || {
let mut writes_succeeded = 0;
prod_barrier.wait();
for i in 0..NUM_MESSAGES {
let payload = StressPayload::new(i, 8 + (i % 128) as usize);
loop {
match prod_buffer.write(payload.clone()) {
Ok(()) => {
writes_succeeded += 1;
break;
}
Err(MesoError::BuffersFull) => {
thread::sleep(Duration::from_nanos(1)); // Sleep briefly when full
continue;
}
Err(e) => panic!("Unexpected producer error: {:?}", e),
}
}
}
writes_succeeded
});
// --- Consumer Thread ---
let cons_buffer = Arc::clone(&buffer);
let cons_barrier = Arc::clone(&barrier);
let consumer = thread::spawn(move || {
let mut received_payloads = HashSet::new();
cons_barrier.wait();
while received_payloads.len() < NUM_MESSAGES as usize {
match cons_buffer.read() {
Ok(payload) => {
assert!(payload.verify(), "Payload {} corrupted!", payload.id);
// Assert that we haven't seen this message before
assert!(
received_payloads.insert(payload),
"Duplicate payload received!"
);
}
Err(MesoError::NoPendingUpdates) => {
thread::sleep(Duration::from_nanos(1)); // Sleep briefly when empty
continue;
}
Err(e) => panic!("Unexpected consumer error: {:?}", e),
}
}
received_payloads.len() as u64
});
let writes = producer.join().expect("Producer panicked");
let reads = consumer.join().expect("Consumer panicked");
println!("\n=== High Contention Test Results ===");
println!("Messages to send: {}", NUM_MESSAGES);
println!("Successful writes: {}", writes);
println!("Verified reads: {}", reads);
assert_eq!(
writes, NUM_MESSAGES,
"Producer failed to write all messages!"
);
assert_eq!(
reads, NUM_MESSAGES,
"Consumer failed to receive all messages!"
);
}
/// This test hammers multiple independent SPSC buffers concurrently. This helps
/// ensure that there is no cross-talk or shared state issues between buffer instances.
/// It verifies that the total messages sent across all buffers match the total received.
#[test]
fn test_multiple_isolated_buffers() {
const NUM_BUFFERS: usize = 4;
const MESSAGES_PER_BUFFER: u64 = 5_000;
let mut handles = vec![];
let total_writes = Arc::new(AtomicU64::new(0));
let total_reads = Arc::new(AtomicU64::new(0));
let barrier = Arc::new(Barrier::new(NUM_BUFFERS * 2));
for buffer_id in 0..NUM_BUFFERS {
let buffer = Arc::new(BufferWheel::<3, StressPayload>::new());
// --- Producer for this buffer ---
let prod_buffer = Arc::clone(&buffer);
let prod_barrier = Arc::clone(&barrier);
let prod_writes = Arc::clone(&total_writes);
handles.push(thread::spawn(move || {
prod_barrier.wait();
for i in 0..MESSAGES_PER_BUFFER {
let msg_id = (buffer_id as u64 * MESSAGES_PER_BUFFER) + i;
let payload = StressPayload::new(msg_id, 32);
loop {
if prod_buffer.write(payload.clone()).is_ok() {
prod_writes.fetch_add(1, Ordering::Relaxed);
break;
}
thread::sleep(Duration::from_nanos(1));
}
}
}));
// --- Consumer for this buffer ---
let cons_buffer = Arc::clone(&buffer);
let cons_barrier = Arc::clone(&barrier);
let cons_reads = Arc::clone(&total_reads);
handles.push(thread::spawn(move || {
let mut received_count = 0;
cons_barrier.wait();
while received_count < MESSAGES_PER_BUFFER {
if let Ok(payload) = cons_buffer.read() {
assert!(
payload.verify(),
"Corrupted payload in buffer {}",
buffer_id
);
let expected_buffer_id = payload.id / MESSAGES_PER_BUFFER;
assert_eq!(
expected_buffer_id, buffer_id as u64,
"Message received in wrong buffer!"
);
cons_reads.fetch_add(1, Ordering::Relaxed);
received_count += 1;
} else {
thread::sleep(Duration::from_nanos(1));
}
}
}));
}
for handle in handles {
handle.join().expect("Thread panicked");
}
let final_writes = total_writes.load(Ordering::SeqCst);
let final_reads = total_reads.load(Ordering::SeqCst);
let expected_total = NUM_BUFFERS as u64 * MESSAGES_PER_BUFFER;
println!("\n=== Multi-Buffer Test Results ===");
println!("Total expected: {}", expected_total);
println!("Total writes: {}", final_writes);
println!("Total reads: {}", final_reads);
assert_eq!(final_writes, expected_total, "Lost writes across buffers!");
assert_eq!(final_reads, expected_total, "Lost reads across buffers!");
}
/// This test is designed to provoke memory ordering issues and false sharing.
/// It uses multiple threads operating on independent buffers that might share CPU
/// cache lines. The consumer verifies that it only receives data from its
/// designated producer, checking for data corruption.
#[test]
fn test_cache_line_contention() {
const BUFFER_SIZE: usize = 8;
const NUM_ITERATIONS: u64 = 10_000;
const NUM_PAIRS: usize = 4;
let corruption_detected = Arc::new(AtomicBool::new(false));
let barrier = Arc::new(Barrier::new(NUM_PAIRS * 2));
let mut handles = vec![];
for i in 0..NUM_PAIRS {
let buffer = Arc::new(BufferWheel::<BUFFER_SIZE, u64>::new());
// --- Producer ---
let prod_buffer = Arc::clone(&buffer);
let prod_barrier = Arc::clone(&barrier);
handles.push(thread::spawn(move || {
prod_barrier.wait();
for j in 0..NUM_ITERATIONS {
let value = (i as u64) << 32 | j;
loop {
if prod_buffer.write(value).is_ok() {
break;
}
std::hint::spin_loop(); // Aggressive spin to maximize cache pressure
}
}
}));
// --- Consumer ---
let cons_buffer = Arc::clone(&buffer);
let cons_barrier = Arc::clone(&barrier);
let cons_corruption_flag = Arc::clone(&corruption_detected);
handles.push(thread::spawn(move || {
cons_barrier.wait();
for _ in 0..NUM_ITERATIONS {
loop {
if let Ok(value) = cons_buffer.read() {
let producer_id = (value >> 32) as usize;
if producer_id != i {
eprintln!(
"CORRUPTION: Consumer {} got value from producer {}",
i, producer_id
);
cons_corruption_flag.store(true, Ordering::Relaxed);
}
break;
}
std::hint::spin_loop(); // Aggressive spin
}
}
}));
}
for handle in handles {
handle.join().expect("Thread panicked");
}
assert!(
!corruption_detected.load(Ordering::Relaxed),
"Data corruption detected, possibly due to cache line/memory ordering issues!"
);
}
}