From: Ralf Jung Date: Mon, 23 May 2022 08:28:46 +0000 (+0200) Subject: test that compare-exchange-weak-failure-rate=0.0 means what it says X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=5ed22b32a21101b3e4b33684e5e895e3bd441a5e;p=rust.git test that compare-exchange-weak-failure-rate=0.0 means what it says --- diff --git a/tests/run-pass/atomic-compare-exchange-weak-never-fail.rs b/tests/run-pass/atomic-compare-exchange-weak-never-fail.rs new file mode 100644 index 00000000000..2c2d4e61d9f --- /dev/null +++ b/tests/run-pass/atomic-compare-exchange-weak-never-fail.rs @@ -0,0 +1,17 @@ +// compile-flags: -Zmiri-compare-exchange-weak-failure-rate=0.0 +use std::sync::atomic::{AtomicBool, Ordering::*}; + +// Ensure that compare_exchange_weak never fails. +fn main() { + let atomic = AtomicBool::new(false); + let tries = 100; + for _ in 0..tries { + let cur = atomic.load(Relaxed); + // Try (weakly) to flip the flag. + if atomic.compare_exchange_weak(cur, !cur, Relaxed, Relaxed).is_err() { + // We failed. Avoid panic machinery as that uses atomics/locks. + eprintln!("compare_exchange_weak failed"); + std::process::abort(); + } + } +}