use loom::cell::UnsafeCell;
use loom::sync::atomic::{AtomicU32, Ordering, fence};
use loom::sync::{Arc, Mutex};
use loom::thread;
const SLOTS: usize = 3;
const SEAL: u32 = u32::MAX;
struct Journal {
commit: [AtomicU32; SLOTS],
body: [UnsafeCell<u32>; SLOTS],
}
impl Journal {
fn new() -> Journal {
Journal {
commit: std::array::from_fn(|_| AtomicU32::new(0)),
body: std::array::from_fn(|_| UnsafeCell::new(0)),
}
}
fn append(&self, slot: usize, value: u32) {
self.body[slot].with_mut(|p| unsafe { *p = value });
self.commit[slot].store(value, Ordering::Release);
}
fn seal(&self, slot: usize) {
self.commit[slot].store(SEAL, Ordering::Release);
}
fn read(&self, slot: usize) -> Option<u32> {
let word = self.commit[slot].load(Ordering::Relaxed);
if word == 0 {
return None;
}
fence(Ordering::Acquire);
if word == SEAL {
return Some(SEAL);
}
let body = self.body[slot].with(|p| unsafe { *p });
assert_eq!(body, word, "reader saw a commit word without its body");
Some(body)
}
}
#[test]
fn reader_never_sees_unpublished_bodies() {
loom::model(|| {
let j = Arc::new(Journal::new());
let w = {
let j = j.clone();
thread::spawn(move || {
j.append(0, 11);
j.append(1, 22);
j.seal(2);
})
};
let mut slot = 0;
let mut seen = Vec::new();
while slot < SLOTS {
match j.read(slot) {
Some(SEAL) => break,
Some(v) => {
seen.push(v);
slot += 1;
}
None => thread::yield_now(),
}
}
assert_eq!(seen, [11, 22]);
w.join().unwrap();
});
}
#[test]
fn two_readers_agree() {
loom::model(|| {
let j = Arc::new(Journal::new());
let w = {
let j = j.clone();
thread::spawn(move || {
j.append(0, 7);
j.append(1, 8);
})
};
let r = {
let j = j.clone();
thread::spawn(move || [j.read(0), j.read(1)])
};
let mine = [j.read(0), j.read(1)];
let theirs = r.join().unwrap();
w.join().unwrap();
for got in [mine, theirs] {
assert!(got[0].is_none_or(|v| v == 7));
assert!(got[1].is_none_or(|v| v == 8));
}
});
}
struct WordRecord {
commit: AtomicU32,
words: [UnsafeCell<u64>; 3],
}
fn check(len: u32, a: u64, b: u64) -> u64 {
(u64::from(len) ^ a.rotate_left(7) ^ b.rotate_left(29)).wrapping_mul(0x9e37_79b9_7f4a_7c15)
}
#[test]
fn single_pass_validation_after_the_fence_always_passes() {
loom::model(|| {
let r = Arc::new(WordRecord {
commit: AtomicU32::new(0),
words: std::array::from_fn(|_| UnsafeCell::new(0)),
});
let w = {
let r = r.clone();
thread::spawn(move || {
let (a, b) = (0x1111_2222_3333_4444, 0x5555_6666_7777_8888);
r.words[1].with_mut(|p| unsafe { *p = a });
r.words[2].with_mut(|p| unsafe { *p = b });
r.words[0].with_mut(|p| unsafe { *p = check(24, a, b) });
r.commit.store(24, Ordering::Release);
})
};
let word = r.commit.load(Ordering::Relaxed);
if word != 0 {
fence(Ordering::Acquire);
let stored = r.words[0].with(|p| unsafe { *p });
let a = r.words[1].with(|p| unsafe { *p });
let b = r.words[2].with(|p| unsafe { *p });
assert_eq!(
stored,
check(word, a, b),
"a published record failed validation"
);
}
w.join().unwrap();
});
}
#[test]
fn marker_stored_by_the_checkpoint_thread_covers_only_committed_records() {
loom::model(|| {
let j = Arc::new(Journal::new());
let requested = Arc::new(Mutex::new(None::<u32>));
let marker = Arc::new(AtomicU32::new(0));
let writer = {
let (j, requested) = (j.clone(), requested.clone());
thread::spawn(move || {
j.append(0, 11);
*requested.lock().unwrap() = Some(1);
j.append(1, 22);
j.append(2, 33);
})
};
let checkpointer = {
let (requested, marker) = (requested.clone(), marker.clone());
thread::spawn(move || {
loop {
if let Some(next) = *requested.lock().unwrap() {
marker.store(next, Ordering::Release);
break;
}
thread::yield_now();
}
})
};
let covered = marker.load(Ordering::Acquire);
for slot in 0..covered as usize {
assert!(
j.read(slot).is_some(),
"the marker covers slot {slot}, which the reader found empty"
);
}
writer.join().unwrap();
checkpointer.join().unwrap();
assert_eq!(marker.load(Ordering::Acquire), 1);
});
}