2020-01-25 13:48:26 -06:00
|
|
|
use std::sync::atomic::{compiler_fence, fence, AtomicBool, AtomicIsize, AtomicU64, Ordering::*};
|
2017-03-14 07:05:51 -05:00
|
|
|
|
|
|
|
fn main() {
|
2019-08-02 12:50:54 -05:00
|
|
|
atomic_bool();
|
|
|
|
atomic_isize();
|
2019-08-02 13:08:04 -05:00
|
|
|
atomic_u64();
|
2019-10-05 06:09:23 -05:00
|
|
|
atomic_fences();
|
2019-08-02 12:50:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn atomic_bool() {
|
|
|
|
static mut ATOMIC: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(*ATOMIC.get_mut(), false);
|
|
|
|
ATOMIC.store(true, SeqCst);
|
|
|
|
assert_eq!(*ATOMIC.get_mut(), true);
|
|
|
|
ATOMIC.fetch_or(false, SeqCst);
|
|
|
|
assert_eq!(*ATOMIC.get_mut(), true);
|
|
|
|
ATOMIC.fetch_and(false, SeqCst);
|
|
|
|
assert_eq!(*ATOMIC.get_mut(), false);
|
|
|
|
ATOMIC.fetch_nand(true, SeqCst);
|
|
|
|
assert_eq!(*ATOMIC.get_mut(), true);
|
|
|
|
ATOMIC.fetch_xor(true, SeqCst);
|
|
|
|
assert_eq!(*ATOMIC.get_mut(), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn atomic_isize() {
|
|
|
|
static ATOMIC: AtomicIsize = AtomicIsize::new(0);
|
|
|
|
|
2017-03-14 07:05:51 -05:00
|
|
|
// Make sure trans can emit all the intrinsics correctly
|
2018-08-15 14:01:40 -05:00
|
|
|
assert_eq!(ATOMIC.compare_exchange(0, 1, Relaxed, Relaxed), Ok(0));
|
|
|
|
assert_eq!(ATOMIC.compare_exchange(0, 2, Acquire, Relaxed), Err(1));
|
|
|
|
assert_eq!(ATOMIC.compare_exchange(0, 1, Release, Relaxed), Err(1));
|
|
|
|
assert_eq!(ATOMIC.compare_exchange(1, 0, AcqRel, Relaxed), Ok(1));
|
2017-03-14 07:05:51 -05:00
|
|
|
ATOMIC.compare_exchange(0, 1, SeqCst, Relaxed).ok();
|
|
|
|
ATOMIC.compare_exchange(0, 1, Acquire, Acquire).ok();
|
|
|
|
ATOMIC.compare_exchange(0, 1, AcqRel, Acquire).ok();
|
|
|
|
ATOMIC.compare_exchange(0, 1, SeqCst, Acquire).ok();
|
|
|
|
ATOMIC.compare_exchange(0, 1, SeqCst, SeqCst).ok();
|
2018-08-15 14:01:40 -05:00
|
|
|
|
|
|
|
ATOMIC.store(0, SeqCst);
|
|
|
|
|
|
|
|
assert_eq!(ATOMIC.compare_exchange_weak(0, 1, Relaxed, Relaxed), Ok(0));
|
|
|
|
assert_eq!(ATOMIC.compare_exchange_weak(0, 2, Acquire, Relaxed), Err(1));
|
|
|
|
assert_eq!(ATOMIC.compare_exchange_weak(0, 1, Release, Relaxed), Err(1));
|
|
|
|
assert_eq!(ATOMIC.compare_exchange_weak(1, 0, AcqRel, Relaxed), Ok(1));
|
2017-03-14 07:05:51 -05:00
|
|
|
ATOMIC.compare_exchange_weak(0, 1, AcqRel, Relaxed).ok();
|
|
|
|
ATOMIC.compare_exchange_weak(0, 1, SeqCst, Relaxed).ok();
|
|
|
|
ATOMIC.compare_exchange_weak(0, 1, Acquire, Acquire).ok();
|
|
|
|
ATOMIC.compare_exchange_weak(0, 1, AcqRel, Acquire).ok();
|
|
|
|
ATOMIC.compare_exchange_weak(0, 1, SeqCst, Acquire).ok();
|
|
|
|
ATOMIC.compare_exchange_weak(0, 1, SeqCst, SeqCst).ok();
|
|
|
|
}
|
2019-08-02 13:08:04 -05:00
|
|
|
|
|
|
|
fn atomic_u64() {
|
|
|
|
static ATOMIC: AtomicU64 = AtomicU64::new(0);
|
|
|
|
|
|
|
|
ATOMIC.store(1, SeqCst);
|
|
|
|
assert_eq!(ATOMIC.compare_exchange(0, 0x100, AcqRel, Acquire), Err(1));
|
2019-10-05 06:09:23 -05:00
|
|
|
assert_eq!(
|
|
|
|
ATOMIC.compare_exchange_weak(1, 0x100, AcqRel, Acquire),
|
|
|
|
Ok(1)
|
|
|
|
);
|
2019-08-02 13:08:04 -05:00
|
|
|
assert_eq!(ATOMIC.load(Relaxed), 0x100);
|
|
|
|
}
|
2019-10-05 06:09:23 -05:00
|
|
|
|
|
|
|
fn atomic_fences() {
|
|
|
|
fence(SeqCst);
|
|
|
|
fence(Release);
|
|
|
|
fence(Acquire);
|
2019-10-09 03:29:08 -05:00
|
|
|
fence(AcqRel);
|
2020-01-25 13:48:26 -06:00
|
|
|
compiler_fence(SeqCst);
|
|
|
|
compiler_fence(Release);
|
|
|
|
compiler_fence(Acquire);
|
|
|
|
compiler_fence(AcqRel);
|
2019-10-05 06:09:23 -05:00
|
|
|
}
|