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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#[cfg(test)]
use stdarch_test::assert_instr;
extern "C" {
#[link_name = "llvm.x86.xbegin"]
fn x86_xbegin() -> i32;
#[link_name = "llvm.x86.xend"]
fn x86_xend() -> ();
#[link_name = "llvm.x86.xabort"]
fn x86_xabort(imm8: i8) -> ();
#[link_name = "llvm.x86.xtest"]
fn x86_xtest() -> i32;
}
pub const _XBEGIN_STARTED: u32 = !0;
#[allow(clippy::identity_op)]
pub const _XABORT_EXPLICIT: u32 = 1 << 0;
pub const _XABORT_RETRY: u32 = 1 << 1;
pub const _XABORT_CONFLICT: u32 = 1 << 2;
pub const _XABORT_CAPACITY: u32 = 1 << 3;
pub const _XABORT_DEBUG: u32 = 1 << 4;
pub const _XABORT_NESTED: u32 = 1 << 5;
#[inline]
#[target_feature(enable = "rtm")]
#[cfg_attr(test, assert_instr(xbegin))]
pub unsafe fn _xbegin() -> u32 {
x86_xbegin() as _
}
#[inline]
#[target_feature(enable = "rtm")]
#[cfg_attr(test, assert_instr(xend))]
pub unsafe fn _xend() {
x86_xend()
}
#[inline]
#[target_feature(enable = "rtm")]
#[cfg_attr(test, assert_instr(xabort, imm8 = 0x0))]
#[rustc_args_required_const(0)]
pub unsafe fn _xabort(imm8: u32) {
macro_rules! call {
($imm8:expr) => {
x86_xabort($imm8)
};
}
constify_imm8!(imm8, call)
}
#[inline]
#[target_feature(enable = "rtm")]
#[cfg_attr(test, assert_instr(xtest))]
pub unsafe fn _xtest() -> u8 {
x86_xtest() as _
}
#[inline]
pub const fn _xabort_code(status: u32) -> u32 {
(status >> 24) & 0xFF
}
#[cfg(test)]
mod tests {
use stdarch_test::simd_test;
use crate::core_arch::x86::*;
#[simd_test(enable = "rtm")]
unsafe fn test_xbegin_xend() {
let mut x = 0;
for _ in 0..10 {
let code = rtm::_xbegin();
if code == _XBEGIN_STARTED {
x += 1;
rtm::_xend();
assert_eq!(x, 1);
break;
}
assert_eq!(x, 0);
}
}
#[simd_test(enable = "rtm")]
unsafe fn test_xabort() {
const ABORT_CODE: u32 = 42;
_xabort(ABORT_CODE);
for _ in 0..10 {
let mut x = 0;
let code = rtm::_xbegin();
if code == _XBEGIN_STARTED {
x += 1;
rtm::_xabort(ABORT_CODE);
} else if code & _XABORT_EXPLICIT != 0 {
let test_abort_code = rtm::_xabort_code(code);
assert_eq!(test_abort_code, ABORT_CODE);
}
assert_eq!(x, 0);
}
}
#[simd_test(enable = "rtm")]
unsafe fn test_xtest() {
assert_eq!(_xtest(), 0);
for _ in 0..10 {
let code = rtm::_xbegin();
if code == _XBEGIN_STARTED {
let in_tx = _xtest();
rtm::_xend();
assert_eq!(in_tx, 1);
break;
}
}
}
}