Debian Codegraph @

debian/rust-vendor/portable-atomic/src/imp/atomic128/macros.rs

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 macro_rules! atomic128 {
4 ($atomic_type:ident, $int_type:ident, $atomic_max:ident, $atomic_min:ident) => {
5 #[repr(C, align(16))]
6 pub(crate) struct $atomic_type {
7 v: core::cell::UnsafeCell<$int_type>,
8 }
9
10 // Send is implicitly implemented.
11 // SAFETY: any data races are prevented by atomic intrinsics.
12 unsafe impl Sync for $atomic_type {}
13
14 impl_default_no_fetch_ops!($atomic_type, $int_type);
15 impl_default_bit_opts!($atomic_type, $int_type);
16 impl $atomic_type {
17 #[inline]
18 pub(crate) const fn new(v: $int_type) -> Self {
19 Self { v: core::cell::UnsafeCell::new(v) }
20 }
21
22 #[inline]
23 pub(crate) fn is_lock_free() -> bool {
24 is_lock_free()
25 }
26 pub(crate) const IS_ALWAYS_LOCK_FREE: bool = IS_ALWAYS_LOCK_FREE;
27
28 #[inline]
29 #[cfg_attr(
30 any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri),
31 track_caller
32 )]
33 pub(crate) fn load(&self, order: Ordering) -> $int_type {
34 crate::utils::assert_load_ordering(order);
35 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
36 // SAFETY: any data races are prevented by atomic intrinsics and the raw
37 // pointer passed in is valid because we got it from a reference.
38 unsafe {
39 atomic_load(self.v.get().cast::<u128>(), order) as $int_type
40 }
41 }
42
43 #[inline]
44 #[cfg_attr(
45 any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri),
46 track_caller
47 )]
48 pub(crate) fn store(&self, val: $int_type, order: Ordering) {
49 crate::utils::assert_store_ordering(order);
50 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
51 // SAFETY: any data races are prevented by atomic intrinsics and the raw
52 // pointer passed in is valid because we got it from a reference.
53 unsafe {
54 atomic_store(self.v.get().cast::<u128>(), val as u128, order)
55 }
56 }
57
58 #[inline]
59 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
60 pub(crate) fn swap(&self, val: $int_type, order: Ordering) -> $int_type {
61 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
62 // SAFETY: any data races are prevented by atomic intrinsics and the raw
63 // pointer passed in is valid because we got it from a reference.
64 unsafe {
65 atomic_swap(self.v.get().cast::<u128>(), val as u128, order) as $int_type
66 }
67 }
68
69 #[inline]
70 #[cfg_attr(
71 any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri),
72 track_caller
73 )]
74 pub(crate) fn compare_exchange(
75 &self,
76 current: $int_type,
77 new: $int_type,
78 success: Ordering,
79 failure: Ordering,
80 ) -> Result<$int_type, $int_type> {
81 crate::utils::assert_compare_exchange_ordering(success, failure);
82 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
83 // SAFETY: any data races are prevented by atomic intrinsics and the raw
84 // pointer passed in is valid because we got it from a reference.
85 unsafe {
86 match atomic_compare_exchange(
87 self.v.get().cast::<u128>(),
88 current as u128,
89 new as u128,
90 success,
91 failure,
92 ) {
93 Ok(v) => Ok(v as $int_type),
94 Err(v) => Err(v as $int_type),
95 }
96 }
97 }
98
99 #[inline]
100 #[cfg_attr(
101 any(all(debug_assertions, not(portable_atomic_no_track_caller)), miri),
102 track_caller
103 )]
104 pub(crate) fn compare_exchange_weak(
105 &self,
106 current: $int_type,
107 new: $int_type,
108 success: Ordering,
109 failure: Ordering,
110 ) -> Result<$int_type, $int_type> {
111 crate::utils::assert_compare_exchange_ordering(success, failure);
112 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
113 // SAFETY: any data races are prevented by atomic intrinsics and the raw
114 // pointer passed in is valid because we got it from a reference.
115 unsafe {
116 match atomic_compare_exchange_weak(
117 self.v.get().cast::<u128>(),
118 current as u128,
119 new as u128,
120 success,
121 failure,
122 ) {
123 Ok(v) => Ok(v as $int_type),
124 Err(v) => Err(v as $int_type),
125 }
126 }
127 }
128
129 #[inline]
130 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
131 pub(crate) fn fetch_add(&self, val: $int_type, order: Ordering) -> $int_type {
132 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
133 // SAFETY: any data races are prevented by atomic intrinsics and the raw
134 // pointer passed in is valid because we got it from a reference.
135 unsafe {
136 atomic_add(self.v.get().cast::<u128>(), val as u128, order) as $int_type
137 }
138 }
139
140 #[inline]
141 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
142 pub(crate) fn fetch_sub(&self, val: $int_type, order: Ordering) -> $int_type {
143 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
144 // SAFETY: any data races are prevented by atomic intrinsics and the raw
145 // pointer passed in is valid because we got it from a reference.
146 unsafe {
147 atomic_sub(self.v.get().cast::<u128>(), val as u128, order) as $int_type
148 }
149 }
150
151 #[inline]
152 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
153 pub(crate) fn fetch_and(&self, val: $int_type, order: Ordering) -> $int_type {
154 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
155 // SAFETY: any data races are prevented by atomic intrinsics and the raw
156 // pointer passed in is valid because we got it from a reference.
157 unsafe {
158 atomic_and(self.v.get().cast::<u128>(), val as u128, order) as $int_type
159 }
160 }
161
162 #[inline]
163 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
164 pub(crate) fn fetch_nand(&self, val: $int_type, order: Ordering) -> $int_type {
165 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
166 // SAFETY: any data races are prevented by atomic intrinsics and the raw
167 // pointer passed in is valid because we got it from a reference.
168 unsafe {
169 atomic_nand(self.v.get().cast::<u128>(), val as u128, order) as $int_type
170 }
171 }
172
173 #[inline]
174 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
175 pub(crate) fn fetch_or(&self, val: $int_type, order: Ordering) -> $int_type {
176 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
177 // SAFETY: any data races are prevented by atomic intrinsics and the raw
178 // pointer passed in is valid because we got it from a reference.
179 unsafe {
180 atomic_or(self.v.get().cast::<u128>(), val as u128, order) as $int_type
181 }
182 }
183
184 #[inline]
185 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
186 pub(crate) fn fetch_xor(&self, val: $int_type, order: Ordering) -> $int_type {
187 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
188 // SAFETY: any data races are prevented by atomic intrinsics and the raw
189 // pointer passed in is valid because we got it from a reference.
190 unsafe {
191 atomic_xor(self.v.get().cast::<u128>(), val as u128, order) as $int_type
192 }
193 }
194
195 #[inline]
196 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
197 pub(crate) fn fetch_max(&self, val: $int_type, order: Ordering) -> $int_type {
198 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
199 // SAFETY: any data races are prevented by atomic intrinsics and the raw
200 // pointer passed in is valid because we got it from a reference.
201 unsafe {
202 $atomic_max(self.v.get().cast::<u128>(), val as u128, order) as $int_type
203 }
204 }
205
206 #[inline]
207 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
208 pub(crate) fn fetch_min(&self, val: $int_type, order: Ordering) -> $int_type {
209 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
210 // SAFETY: any data races are prevented by atomic intrinsics and the raw
211 // pointer passed in is valid because we got it from a reference.
212 unsafe {
213 $atomic_min(self.v.get().cast::<u128>(), val as u128, order) as $int_type
214 }
215 }
216
217 #[inline]
218 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
219 pub(crate) fn fetch_not(&self, order: Ordering) -> $int_type {
220 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
221 // SAFETY: any data races are prevented by atomic intrinsics and the raw
222 // pointer passed in is valid because we got it from a reference.
223 unsafe {
224 atomic_not(self.v.get().cast::<u128>(), order) as $int_type
225 }
226 }
227 #[inline]
228 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
229 pub(crate) fn not(&self, order: Ordering) {
230 self.fetch_not(order);
231 }
232
233 #[inline]
234 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
235 pub(crate) fn fetch_neg(&self, order: Ordering) -> $int_type {
236 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
237 // SAFETY: any data races are prevented by atomic intrinsics and the raw
238 // pointer passed in is valid because we got it from a reference.
239 unsafe {
240 atomic_neg(self.v.get().cast::<u128>(), order) as $int_type
241 }
242 }
243 #[inline]
244 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
245 pub(crate) fn neg(&self, order: Ordering) {
246 self.fetch_neg(order);
247 }
248
249 #[inline]
250 pub(crate) const fn as_ptr(&self) -> *mut $int_type {
251 self.v.get()
252 }
253 }
254 };
255 }
256
257 #[cfg(any(target_arch = "powerpc64", target_arch = "s390x", target_arch = "x86_64"))]
258 #[allow(unused_macros)] // also used by intrinsics.rs
259 macro_rules! atomic_rmw_by_atomic_update {
260 () => {
261 #[inline]
262 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
263 unsafe fn atomic_swap(dst: *mut u128, val: u128, order: Ordering) -> u128 {
264 // SAFETY: the caller must uphold the safety contract.
265 unsafe { atomic_update(dst, order, |_| val) }
266 }
267 #[inline]
268 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
269 unsafe fn atomic_add(dst: *mut u128, val: u128, order: Ordering) -> u128 {
270 // SAFETY: the caller must uphold the safety contract.
271 unsafe { atomic_update(dst, order, |x| x.wrapping_add(val)) }
272 }
273 #[inline]
274 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
275 unsafe fn atomic_sub(dst: *mut u128, val: u128, order: Ordering) -> u128 {
276 // SAFETY: the caller must uphold the safety contract.
277 unsafe { atomic_update(dst, order, |x| x.wrapping_sub(val)) }
278 }
279 #[inline]
280 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
281 unsafe fn atomic_and(dst: *mut u128, val: u128, order: Ordering) -> u128 {
282 // SAFETY: the caller must uphold the safety contract.
283 unsafe { atomic_update(dst, order, |x| x & val) }
284 }
285 #[inline]
286 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
287 unsafe fn atomic_nand(dst: *mut u128, val: u128, order: Ordering) -> u128 {
288 // SAFETY: the caller must uphold the safety contract.
289 unsafe { atomic_update(dst, order, |x| !(x & val)) }
290 }
291 #[inline]
292 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
293 unsafe fn atomic_or(dst: *mut u128, val: u128, order: Ordering) -> u128 {
294 // SAFETY: the caller must uphold the safety contract.
295 unsafe { atomic_update(dst, order, |x| x | val) }
296 }
297 #[inline]
298 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
299 unsafe fn atomic_xor(dst: *mut u128, val: u128, order: Ordering) -> u128 {
300 // SAFETY: the caller must uphold the safety contract.
301 unsafe { atomic_update(dst, order, |x| x ^ val) }
302 }
303 #[inline]
304 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
305 unsafe fn atomic_not(dst: *mut u128, order: Ordering) -> u128 {
306 // SAFETY: the caller must uphold the safety contract.
307 unsafe { atomic_update(dst, order, |x| !x) }
308 }
309 #[inline]
310 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
311 unsafe fn atomic_neg(dst: *mut u128, order: Ordering) -> u128 {
312 // SAFETY: the caller must uphold the safety contract.
313 unsafe { atomic_update(dst, order, u128::wrapping_neg) }
314 }
315 atomic_rmw_by_atomic_update!(cmp);
316 };
317 (cmp) => {
318 #[inline]
319 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
320 unsafe fn atomic_max(dst: *mut u128, val: u128, order: Ordering) -> u128 {
321 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
322 // SAFETY: the caller must uphold the safety contract.
323 unsafe {
324 atomic_update(dst, order, |x| core::cmp::max(x as i128, val as i128) as u128)
325 }
326 }
327 #[inline]
328 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
329 unsafe fn atomic_umax(dst: *mut u128, val: u128, order: Ordering) -> u128 {
330 // SAFETY: the caller must uphold the safety contract.
331 unsafe { atomic_update(dst, order, |x| core::cmp::max(x, val)) }
332 }
333 #[inline]
334 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
335 unsafe fn atomic_min(dst: *mut u128, val: u128, order: Ordering) -> u128 {
336 #[allow(clippy::cast_possible_wrap, clippy::cast_sign_loss)]
337 // SAFETY: the caller must uphold the safety contract.
338 unsafe {
339 atomic_update(dst, order, |x| core::cmp::min(x as i128, val as i128) as u128)
340 }
341 }
342 #[inline]
343 #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
344 unsafe fn atomic_umin(dst: *mut u128, val: u128, order: Ordering) -> u128 {
345 // SAFETY: the caller must uphold the safety contract.
346 unsafe { atomic_update(dst, order, |x| core::cmp::min(x, val)) }
347 }
348 };
349 }