core/stdarch/crates/core_arch/src/x86/
rtm.rs1#[cfg(test)]
17use stdarch_test::assert_instr;
18
19unsafe extern "C" {
20    #[link_name = "llvm.x86.xbegin"]
21    fn x86_xbegin() -> i32;
22    #[link_name = "llvm.x86.xend"]
23    fn x86_xend();
24    #[link_name = "llvm.x86.xabort"]
25    fn x86_xabort(imm8: i8);
26    #[link_name = "llvm.x86.xtest"]
27    fn x86_xtest() -> i32;
28}
29
30#[unstable(feature = "stdarch_x86_rtm", issue = "111138")]
32pub const _XBEGIN_STARTED: u32 = !0;
33
34#[allow(clippy::identity_op)]
37#[unstable(feature = "stdarch_x86_rtm", issue = "111138")]
38pub const _XABORT_EXPLICIT: u32 = 1 << 0;
39
40#[unstable(feature = "stdarch_x86_rtm", issue = "111138")]
42pub const _XABORT_RETRY: u32 = 1 << 1;
43
44#[unstable(feature = "stdarch_x86_rtm", issue = "111138")]
46pub const _XABORT_CONFLICT: u32 = 1 << 2;
47
48#[unstable(feature = "stdarch_x86_rtm", issue = "111138")]
50pub const _XABORT_CAPACITY: u32 = 1 << 3;
51
52#[unstable(feature = "stdarch_x86_rtm", issue = "111138")]
54pub const _XABORT_DEBUG: u32 = 1 << 4;
55
56#[unstable(feature = "stdarch_x86_rtm", issue = "111138")]
58pub const _XABORT_NESTED: u32 = 1 << 5;
59
60#[inline]
65#[target_feature(enable = "rtm")]
66#[cfg_attr(test, assert_instr(xbegin))]
67#[unstable(feature = "stdarch_x86_rtm", issue = "111138")]
68pub unsafe fn _xbegin() -> u32 {
69    x86_xbegin() as _
70}
71
72#[inline]
76#[target_feature(enable = "rtm")]
77#[cfg_attr(test, assert_instr(xend))]
78#[unstable(feature = "stdarch_x86_rtm", issue = "111138")]
79pub unsafe fn _xend() {
80    x86_xend()
81}
82
83#[inline]
87#[target_feature(enable = "rtm")]
88#[cfg_attr(test, assert_instr(xabort, IMM8 = 0x0))]
89#[rustc_legacy_const_generics(0)]
90#[unstable(feature = "stdarch_x86_rtm", issue = "111138")]
91pub unsafe fn _xabort<const IMM8: u32>() {
92    static_assert_uimm_bits!(IMM8, 8);
93    x86_xabort(IMM8 as i8)
94}
95
96#[inline]
101#[target_feature(enable = "rtm")]
102#[cfg_attr(test, assert_instr(xtest))]
103#[unstable(feature = "stdarch_x86_rtm", issue = "111138")]
104pub unsafe fn _xtest() -> u8 {
105    x86_xtest() as _
106}
107
108#[inline]
111#[unstable(feature = "stdarch_x86_rtm", issue = "111138")]
112pub const fn _xabort_code(status: u32) -> u32 {
113    (status >> 24) & 0xFF
114}
115
116#[cfg(test)]
117mod tests {
118    use stdarch_test::simd_test;
119
120    use crate::core_arch::x86::*;
121
122    #[simd_test(enable = "rtm")]
123    unsafe fn test_xbegin() {
124        let mut x = 0;
125        for _ in 0..10 {
126            let code = _xbegin();
127            if code == _XBEGIN_STARTED {
128                x += 1;
129                _xend();
130                assert_eq!(x, 1);
131                break;
132            }
133            assert_eq!(x, 0);
134        }
135    }
136
137    #[simd_test(enable = "rtm")]
138    unsafe fn test_xabort() {
139        const ABORT_CODE: u32 = 42;
140        _xabort::<ABORT_CODE>();
142
143        for _ in 0..10 {
144            let mut x = 0;
145            let code = rtm::_xbegin();
146            if code == _XBEGIN_STARTED {
147                x += 1;
148                rtm::_xabort::<ABORT_CODE>();
149            } else if code & _XABORT_EXPLICIT != 0 {
150                let test_abort_code = rtm::_xabort_code(code);
151                assert_eq!(test_abort_code, ABORT_CODE);
152            }
153            assert_eq!(x, 0);
154        }
155    }
156
157    #[simd_test(enable = "rtm")]
158    unsafe fn test_xtest() {
159        assert_eq!(_xtest(), 0);
160
161        for _ in 0..10 {
162            let code = rtm::_xbegin();
163            if code == _XBEGIN_STARTED {
164                let in_tx = _xtest();
165                rtm::_xend();
166
167                assert_eq!(in_tx, 1);
170                break;
171            }
172        }
173    }
174}