Skip to content

Commit dc13108

Browse files
committed
add tests for protected RMW operations
1 parent e309290 commit dc13108

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

tests/lib.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,82 @@ fn reentrant() {
326326
assert_eq!(dropped.load(Ordering::Relaxed), 5);
327327
}
328328

329+
#[test]
330+
fn swap_stress() {
331+
for _ in 0..cfg::ITER {
332+
let collector = Collector::new();
333+
let entries = [const { AtomicPtr::new(ptr::null_mut()) }; cfg::ITEMS];
334+
335+
thread::scope(|s| {
336+
for _ in 0..cfg::THREADS {
337+
s.spawn(|| {
338+
for i in 0..cfg::ITEMS {
339+
let guard = collector.enter();
340+
let new = Box::into_raw(Box::new(i));
341+
let old = guard.swap(&entries[i], new, Ordering::AcqRel);
342+
if !old.is_null() {
343+
unsafe { assert_eq!(*old, i) }
344+
unsafe { guard.defer_retire(old, reclaim::boxed) }
345+
}
346+
}
347+
});
348+
}
349+
});
350+
351+
for i in 0..cfg::ITEMS {
352+
let val = entries[i].load(Ordering::Relaxed);
353+
let _ = unsafe { Box::from_raw(val) };
354+
}
355+
}
356+
}
357+
358+
#[test]
359+
fn cas_stress() {
360+
for _ in 0..cfg::ITER {
361+
let collector = Collector::new();
362+
let entries = [const { AtomicPtr::new(ptr::null_mut()) }; cfg::ITEMS];
363+
364+
thread::scope(|s| {
365+
for _ in 0..cfg::THREADS {
366+
s.spawn(|| {
367+
for i in 0..cfg::ITEMS {
368+
let guard = collector.enter();
369+
let new = Box::into_raw(Box::new(i));
370+
371+
loop {
372+
let old = entries[i].load(Ordering::Relaxed);
373+
374+
let result = guard.compare_exchange(
375+
&entries[i],
376+
old,
377+
new,
378+
Ordering::AcqRel,
379+
Ordering::Relaxed,
380+
);
381+
382+
let Ok(old) = result else {
383+
continue;
384+
};
385+
386+
if !old.is_null() {
387+
unsafe { assert_eq!(*old, i) }
388+
unsafe { guard.defer_retire(old, reclaim::boxed) }
389+
}
390+
391+
break;
392+
}
393+
}
394+
});
395+
}
396+
});
397+
398+
for i in 0..cfg::ITEMS {
399+
let val = entries[i].load(Ordering::Relaxed);
400+
let _ = unsafe { Box::from_raw(val) };
401+
}
402+
}
403+
}
404+
329405
#[test]
330406
fn owned_guard() {
331407
let collector = Collector::new().batch_size(5);

0 commit comments

Comments
 (0)