summaryrefslogtreecommitdiffstats
path: root/tests/ui/intrinsics/intrinsic-atomics.rs
blob: b17f4347be31cce7dd97c0f4614e021e64b4f0f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// run-pass
#![feature(intrinsics)]

mod rusti {
    extern "rust-intrinsic" {
        pub fn atomic_cxchg_seqcst_seqcst<T>(dst: *mut T, old: T, src: T) -> (T, bool);
        pub fn atomic_cxchg_acquire_acquire<T>(dst: *mut T, old: T, src: T) -> (T, bool);
        pub fn atomic_cxchg_release_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);

        pub fn atomic_cxchgweak_seqcst_seqcst<T>(dst: *mut T, old: T, src: T) -> (T, bool);
        pub fn atomic_cxchgweak_acquire_acquire<T>(dst: *mut T, old: T, src: T) -> (T, bool);
        pub fn atomic_cxchgweak_release_relaxed<T>(dst: *mut T, old: T, src: T) -> (T, bool);

        pub fn atomic_load_seqcst<T>(src: *const T) -> T;
        pub fn atomic_load_acquire<T>(src: *const T) -> T;

        pub fn atomic_store_seqcst<T>(dst: *mut T, val: T);
        pub fn atomic_store_release<T>(dst: *mut T, val: T);

        pub fn atomic_xchg_seqcst<T>(dst: *mut T, src: T) -> T;
        pub fn atomic_xchg_acquire<T>(dst: *mut T, src: T) -> T;
        pub fn atomic_xchg_release<T>(dst: *mut T, src: T) -> T;

        pub fn atomic_xadd_seqcst<T>(dst: *mut T, src: T) -> T;
        pub fn atomic_xadd_acquire<T>(dst: *mut T, src: T) -> T;
        pub fn atomic_xadd_release<T>(dst: *mut T, src: T) -> T;

        pub fn atomic_xsub_seqcst<T>(dst: *mut T, src: T) -> T;
        pub fn atomic_xsub_acquire<T>(dst: *mut T, src: T) -> T;
        pub fn atomic_xsub_release<T>(dst: *mut T, src: T) -> T;
    }
}

pub fn main() {
    unsafe {
        let mut x: Box<_> = Box::new(1);

        assert_eq!(rusti::atomic_load_seqcst(&*x), 1);
        *x = 5;
        assert_eq!(rusti::atomic_load_acquire(&*x), 5);

        rusti::atomic_store_seqcst(&mut *x,3);
        assert_eq!(*x, 3);
        rusti::atomic_store_release(&mut *x,1);
        assert_eq!(*x, 1);

        assert_eq!(rusti::atomic_cxchg_seqcst_seqcst(&mut *x, 1, 2), (1, true));
        assert_eq!(*x, 2);

        assert_eq!(rusti::atomic_cxchg_acquire_acquire(&mut *x, 1, 3), (2, false));
        assert_eq!(*x, 2);

        assert_eq!(rusti::atomic_cxchg_release_relaxed(&mut *x, 2, 1), (2, true));
        assert_eq!(*x, 1);

        assert_eq!(rusti::atomic_xchg_seqcst(&mut *x, 0), 1);
        assert_eq!(*x, 0);

        assert_eq!(rusti::atomic_xchg_acquire(&mut *x, 1), 0);
        assert_eq!(*x, 1);

        assert_eq!(rusti::atomic_xchg_release(&mut *x, 0), 1);
        assert_eq!(*x, 0);

        assert_eq!(rusti::atomic_xadd_seqcst(&mut *x, 1), 0);
        assert_eq!(rusti::atomic_xadd_acquire(&mut *x, 1), 1);
        assert_eq!(rusti::atomic_xadd_release(&mut *x, 1), 2);
        assert_eq!(*x, 3);

        assert_eq!(rusti::atomic_xsub_seqcst(&mut *x, 1), 3);
        assert_eq!(rusti::atomic_xsub_acquire(&mut *x, 1), 2);
        assert_eq!(rusti::atomic_xsub_release(&mut *x, 1), 1);
        assert_eq!(*x, 0);

        loop {
            let res = rusti::atomic_cxchgweak_seqcst_seqcst(&mut *x, 0, 1);
            assert_eq!(res.0, 0);
            if res.1 {
                break;
            }
        }
        assert_eq!(*x, 1);

        loop {
            let res = rusti::atomic_cxchgweak_acquire_acquire(&mut *x, 1, 2);
            assert_eq!(res.0, 1);
            if res.1 {
                break;
            }
        }
        assert_eq!(*x, 2);

        loop {
            let res = rusti::atomic_cxchgweak_release_relaxed(&mut *x, 2, 3);
            assert_eq!(res.0, 2);
            if res.1 {
                break;
            }
        }
        assert_eq!(*x, 3);
    }
}