Skip to content

Commit b6eccc6

Browse files
committed
Test that _weak atomics sometimes fail
1 parent c678bd7 commit b6eccc6

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

tests/run-pass/atomic.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ fn main() {
55
atomic_isize();
66
atomic_u64();
77
atomic_fences();
8+
weak_sometimes_fails();
89
}
910

1011
fn atomic_bool() {
@@ -85,3 +86,17 @@ fn atomic_fences() {
8586
compiler_fence(Acquire);
8687
compiler_fence(AcqRel);
8788
}
89+
90+
fn weak_sometimes_fails() {
91+
static ATOMIC: AtomicBool = AtomicBool::new(false);
92+
let tries = 20;
93+
for _ in 0..tries {
94+
let cur = ATOMIC.load(Relaxed);
95+
// Try (weakly) to flip the flag.
96+
if ATOMIC.compare_exchange_weak(cur, !cur, Relaxed, Relaxed).is_err() {
97+
// We succeeded, so return and skip the panic.
98+
return;
99+
}
100+
}
101+
panic!("compare_exchange_weak succeeded {} tries in a row", tries);
102+
}

0 commit comments

Comments
 (0)