Debian Codegraph @

debian/rust-vendor/portable-atomic/src/imp/mod.rs

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 // -----------------------------------------------------------------------------
4 // Lock-free implementations
5
6 cfg_core_atomic!({
7 mod core_atomic;
8 });
9
10 // AVR
11 #[cfg(target_arch = "avr")]
12 #[cfg(not(portable_atomic_no_asm))]
13 #[cfg(not(feature = "critical-section"))]
14 mod avr;
15
16 // pre-v6 Arm Linux
17 #[cfg(all(
18 target_arch = "arm",
19 not(any(miri, portable_atomic_sanitize_thread)),
20 any(target_os = "linux", target_os = "android"),
21 not(any(target_feature = "v6", portable_atomic_target_feature = "v6")),
22 ))]
23 pub(crate) mod arm_linux;
24
25 // MSP430
26 #[cfg(target_arch = "msp430")]
27 pub(crate) mod msp430;
28
29 // RISC-V without A-extension
30 #[cfg(any(test, not(feature = "critical-section")))]
31 #[cfg_attr(
32 portable_atomic_no_cfg_target_has_atomic,
33 cfg(any(
34 all(test, not(any(miri, portable_atomic_sanitize_thread))),
35 portable_atomic_no_atomic_cas,
36 ))
37 )]
38 #[cfg_attr(
39 not(portable_atomic_no_cfg_target_has_atomic),
40 cfg(any(
41 all(test, not(any(miri, portable_atomic_sanitize_thread))),
42 not(target_has_atomic = "ptr"),
43 ))
44 )]
45 #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
46 mod riscv;
47
48 // x86-specific optimizations
49 // Miri and Sanitizer do not support inline assembly.
50 #[cfg(all(
51 any(target_arch = "x86", target_arch = "x86_64"),
52 not(any(miri, portable_atomic_sanitize_thread)),
53 any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
54 ))]
55 pub(crate) mod x86;
56
57 // 64-bit atomic implementations on 32-bit architectures
58 #[cfg(any(target_arch = "arm", target_arch = "riscv32"))]
59 mod atomic64;
60
61 // 128-bit atomic implementations on 64-bit architectures
62 #[cfg(any(
63 target_arch = "aarch64",
64 target_arch = "arm64ec",
65 target_arch = "powerpc64",
66 target_arch = "riscv64",
67 target_arch = "s390x",
68 target_arch = "x86_64",
69 ))]
70 mod atomic128;
71
72 // -----------------------------------------------------------------------------
73 // Lock-based fallback implementations
74
75 #[cfg(feature = "fallback")]
76 #[cfg_attr(portable_atomic_no_cfg_target_has_atomic, cfg(not(portable_atomic_no_atomic_cas)))]
77 #[cfg_attr(not(portable_atomic_no_cfg_target_has_atomic), cfg(target_has_atomic = "ptr"))]
78 #[cfg_attr(
79 not(test),
80 cfg(not(any(
81 // ---------------
82 // Use other ways.
83 target_arch = "avr",
84 target_arch = "msp430",
85 portable_atomic_unsafe_assume_single_core,
86 // --------------------
87 // Has 128-bit atomics.
88 all(
89 target_arch = "aarch64",
90 not(all(
91 any(miri, portable_atomic_sanitize_thread),
92 not(portable_atomic_atomic_intrinsics),
93 )),
94 any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
95 ),
96 all(
97 target_arch = "arm64ec",
98 not(all(
99 any(miri, portable_atomic_sanitize_thread),
100 not(portable_atomic_atomic_intrinsics),
101 )),
102 not(portable_atomic_no_asm),
103 ),
104 all(
105 target_arch = "x86_64",
106 any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
107 any(target_feature = "cmpxchg16b", portable_atomic_target_feature = "cmpxchg16b"),
108 ),
109 all(
110 target_arch = "riscv64",
111 not(any(miri, portable_atomic_sanitize_thread)),
112 any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
113 any(target_feature = "zacas", portable_atomic_target_feature = "zacas"),
114 ),
115 all(
116 target_arch = "powerpc64",
117 not(all(
118 any(miri, portable_atomic_sanitize_thread),
119 not(portable_atomic_atomic_intrinsics),
120 )),
121 not(portable_atomic_no_asm),
122 any(
123 target_feature = "quadword-atomics",
124 portable_atomic_target_feature = "quadword-atomics",
125 ),
126 ),
127 all(
128 target_arch = "s390x",
129 not(all(
130 any(miri, portable_atomic_sanitize_thread),
131 not(portable_atomic_atomic_intrinsics),
132 )),
133 not(portable_atomic_no_asm),
134 ),
135 ))
136 ))]
137 mod fallback;
138
139 // -----------------------------------------------------------------------------
140 // Fallback implementation based on disabling interrupts or critical-section
141
142 // On AVR, we always use critical section based fallback implementation.
143 // AVR can be safely assumed to be single-core, so this is sound.
144 // MSP430 as well.
145 // See the module-level comments of interrupt module for more.
146 #[cfg_attr(
147 portable_atomic_no_cfg_target_has_atomic,
148 cfg(any(
149 target_arch = "avr",
150 target_arch = "msp430",
151 all(feature = "critical-section", any(test, portable_atomic_no_atomic_cas)),
152 portable_atomic_unsafe_assume_single_core,
153 portable_atomic_unsafe_assume_privileged,
154 ))
155 )]
156 #[cfg_attr(
157 not(portable_atomic_no_cfg_target_has_atomic),
158 cfg(any(
159 target_arch = "avr",
160 target_arch = "msp430",
161 all(feature = "critical-section", any(test, not(target_has_atomic = "ptr"))),
162 portable_atomic_unsafe_assume_single_core,
163 portable_atomic_unsafe_assume_privileged,
164 ))
165 )]
166 #[cfg(any(
167 target_arch = "arm",
168 target_arch = "avr",
169 target_arch = "msp430",
170 target_arch = "riscv32",
171 target_arch = "riscv64",
172 target_arch = "xtensa",
173 feature = "critical-section",
174 ))]
175 mod interrupt;
176
177 // -----------------------------------------------------------------------------
178 // Atomic float implementations
179
180 #[cfg(feature = "float")]
181 pub(crate) mod float;
182
183 // -----------------------------------------------------------------------------
184
185 // {8,16,32}-bit & ptr-sized atomics
186 cfg_sel!({
187 // has CAS | (has core atomic & !(avr | msp430 | critical section)) => core atomic
188 #[cfg_attr(
189 portable_atomic_no_cfg_target_has_atomic,
190 cfg(not(any(
191 target_arch = "avr",
192 target_arch = "msp430",
193 portable_atomic_no_atomic_load_store,
194 all(
195 any(
196 target_arch = "riscv32",
197 target_arch = "riscv64",
198 feature = "critical-section",
199 portable_atomic_unsafe_assume_single_core,
200 ),
201 portable_atomic_no_atomic_cas,
202 ),
203 )))
204 )]
205 #[cfg_attr(
206 not(portable_atomic_no_cfg_target_has_atomic),
207 cfg(not(any(
208 target_arch = "avr",
209 target_arch = "msp430",
210 portable_atomic_no_atomic_load_store,
211 all(
212 any(
213 target_arch = "riscv32",
214 target_arch = "riscv64",
215 feature = "critical-section",
216 portable_atomic_unsafe_assume_single_core,
217 ),
218 not(target_has_atomic = "ptr"),
219 ),
220 )))
221 )]
222 {
223 pub(crate) use self::core_atomic::{
224 AtomicI8, AtomicI16, AtomicI32, AtomicIsize, AtomicPtr, AtomicU8, AtomicU16, AtomicU32,
225 AtomicUsize,
226 };
227 }
228 // no native atomic CAS & (assume single core | critical section) => critical section based fallback
229 #[cfg(any(
230 target_arch = "avr",
231 target_arch = "msp430",
232 feature = "critical-section",
233 portable_atomic_unsafe_assume_single_core,
234 ))]
235 {
236 pub(crate) use self::interrupt::{
237 AtomicI8, AtomicI16, AtomicIsize, AtomicPtr, AtomicU8, AtomicU16, AtomicUsize,
238 };
239 #[cfg(any(not(target_pointer_width = "16"), feature = "fallback"))]
240 pub(crate) use self::interrupt::{AtomicI32, AtomicU32};
241 }
242 // RISC-V without A-extension & !(assume single core | critical section)
243 #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
244 {
245 pub(crate) use self::riscv::{
246 AtomicI8, AtomicI16, AtomicI32, AtomicIsize, AtomicPtr, AtomicU8, AtomicU16, AtomicU32,
247 AtomicUsize,
248 };
249 }
250 // bpf & !(critical section) => core atomic
251 #[cfg(target_arch = "bpf")]
252 {
253 pub(crate) use self::core_atomic::{AtomicIsize, AtomicPtr, AtomicUsize};
254 }
255 #[cfg(else)]
256 {
257 // no atomic
258 }
259 });
260
261 // 64-bit atomics
262 cfg_sel!({
263 // has CAS | (has core atomic & !(avr | msp430 | critical section)) => core atomic
264 #[cfg_attr(
265 portable_atomic_no_cfg_target_has_atomic,
266 cfg(all(
267 not(any(
268 target_arch = "avr",
269 target_arch = "msp430",
270 portable_atomic_no_atomic_load_store,
271 all(
272 any(
273 target_arch = "riscv32",
274 target_arch = "riscv64",
275 feature = "critical-section",
276 portable_atomic_unsafe_assume_single_core,
277 ),
278 portable_atomic_no_atomic_cas,
279 )
280 )),
281 any(
282 not(portable_atomic_no_atomic_64),
283 not(any(target_pointer_width = "16", target_pointer_width = "32")),
284 ),
285 ))
286 )]
287 #[cfg_attr(
288 not(portable_atomic_no_cfg_target_has_atomic),
289 cfg(all(
290 not(any(
291 target_arch = "avr",
292 target_arch = "msp430",
293 portable_atomic_no_atomic_load_store,
294 all(
295 any(
296 target_arch = "riscv32",
297 target_arch = "riscv64",
298 feature = "critical-section",
299 portable_atomic_unsafe_assume_single_core,
300 ),
301 not(target_has_atomic = "ptr"),
302 )
303 )),
304 any(
305 target_has_atomic = "64",
306 not(any(target_pointer_width = "16", target_pointer_width = "32")),
307 ),
308 ))
309 )]
310 {
311 pub(crate) use self::core_atomic::{AtomicI64, AtomicU64};
312 }
313 // pre-v6 Arm Linux
314 #[cfg(all(
315 feature = "fallback",
316 target_arch = "arm",
317 not(any(miri, portable_atomic_sanitize_thread)),
318 any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
319 any(target_os = "linux", target_os = "android"),
320 not(any(target_feature = "v6", portable_atomic_target_feature = "v6")),
321 not(portable_atomic_no_outline_atomics),
322 ))]
323 {
324 pub(crate) use self::atomic64::arm_linux::{AtomicI64, AtomicU64};
325 }
326 // riscv32 & (zacas | outline-atomics)
327 #[cfg(all(
328 target_arch = "riscv32",
329 not(any(miri, portable_atomic_sanitize_thread)),
330 any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
331 any(
332 target_feature = "zacas",
333 portable_atomic_target_feature = "zacas",
334 all(
335 feature = "fallback",
336 not(portable_atomic_no_outline_atomics),
337 any(target_os = "linux", target_os = "android"),
338 ),
339 ),
340 ))]
341 {
342 pub(crate) use self::atomic64::riscv32::{AtomicI64, AtomicU64};
343 }
344 // no native atomic CAS & (assume single core | critical section) => critical section based fallback
345 #[cfg_attr(
346 portable_atomic_no_cfg_target_has_atomic,
347 cfg(any(
348 target_arch = "avr",
349 target_arch = "msp430",
350 all(feature = "critical-section", portable_atomic_no_atomic_cas),
351 portable_atomic_unsafe_assume_single_core,
352 ))
353 )]
354 #[cfg_attr(
355 not(portable_atomic_no_cfg_target_has_atomic),
356 cfg(any(
357 target_arch = "avr",
358 target_arch = "msp430",
359 all(feature = "critical-section", not(target_has_atomic = "ptr")),
360 portable_atomic_unsafe_assume_single_core,
361 ))
362 )]
363 {
364 #[cfg(any(
365 not(any(target_pointer_width = "16", target_pointer_width = "32")),
366 feature = "fallback",
367 ))]
368 pub(crate) use self::interrupt::{AtomicI64, AtomicU64};
369 }
370 // no core 64-bit atomic & has CAS => use lock-base fallback
371 #[cfg_attr(
372 portable_atomic_no_cfg_target_has_atomic,
373 cfg(all(feature = "fallback", not(portable_atomic_no_atomic_cas)))
374 )]
375 #[cfg_attr(
376 not(portable_atomic_no_cfg_target_has_atomic),
377 cfg(all(feature = "fallback", target_has_atomic = "ptr"))
378 )]
379 {
380 pub(crate) use self::fallback::{AtomicI64, AtomicU64};
381 }
382 // RISC-V without A-extension & !(assume single core | critical section)
383 #[cfg(target_arch = "riscv64")]
384 {
385 pub(crate) use self::riscv::{AtomicI64, AtomicU64};
386 }
387 // bpf & !(critical section) => core atomic
388 #[cfg(target_arch = "bpf")]
389 {
390 pub(crate) use self::core_atomic::{AtomicI64, AtomicU64};
391 }
392 #[cfg(else)]
393 {
394 // no atomic
395 }
396 });
397
398 // 128-bit atomics
399 cfg_sel!({
400 // AArch64
401 #[cfg(any(
402 all(
403 target_arch = "aarch64",
404 not(all(
405 any(miri, portable_atomic_sanitize_thread),
406 not(portable_atomic_atomic_intrinsics),
407 )),
408 any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
409 ),
410 all(
411 target_arch = "arm64ec",
412 not(all(
413 any(miri, portable_atomic_sanitize_thread),
414 not(portable_atomic_atomic_intrinsics),
415 )),
416 not(portable_atomic_no_asm),
417 ),
418 ))]
419 {
420 pub(crate) use self::atomic128::aarch64::{AtomicI128, AtomicU128};
421 }
422 // x86_64 & (cmpxchg16b | outline-atomics)
423 #[cfg(all(
424 target_arch = "x86_64",
425 not(all(
426 any(miri, portable_atomic_sanitize_thread),
427 portable_atomic_no_cmpxchg16b_intrinsic,
428 )),
429 any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
430 any(
431 target_feature = "cmpxchg16b",
432 portable_atomic_target_feature = "cmpxchg16b",
433 all(
434 feature = "fallback",
435 not(portable_atomic_no_outline_atomics),
436 not(any(target_env = "sgx", miri)),
437 ),
438 ),
439 ))]
440 {
441 pub(crate) use self::atomic128::x86_64::{AtomicI128, AtomicU128};
442 }
443 // riscv64 & (zacas | outline-atomics)
444 #[cfg(all(
445 target_arch = "riscv64",
446 not(any(miri, portable_atomic_sanitize_thread)),
447 any(not(portable_atomic_no_asm), portable_atomic_unstable_asm),
448 any(
449 target_feature = "zacas",
450 portable_atomic_target_feature = "zacas",
451 all(
452 feature = "fallback",
453 not(portable_atomic_no_outline_atomics),
454 any(target_os = "linux", target_os = "android"),
455 ),
456 ),
457 ))]
458 {
459 pub(crate) use self::atomic128::riscv64::{AtomicI128, AtomicU128};
460 }
461 // powerpc64 & (pwr8 | outline-atomics)
462 #[cfg(all(
463 target_arch = "powerpc64",
464 not(all(
465 any(miri, portable_atomic_sanitize_thread),
466 not(portable_atomic_atomic_intrinsics),
467 )),
468 not(portable_atomic_no_asm),
469 any(
470 target_feature = "quadword-atomics",
471 portable_atomic_target_feature = "quadword-atomics",
472 all(
473 feature = "fallback",
474 not(portable_atomic_no_outline_atomics),
475 any(
476 all(
477 target_os = "linux",
478 any(
479 all(
480 target_env = "gnu",
481 any(target_endian = "little", not(target_feature = "crt-static")),
482 ),
483 all(
484 target_env = "musl",
485 any(not(target_feature = "crt-static"), feature = "std"),
486 ),
487 target_env = "ohos",
488 all(target_env = "uclibc", not(target_feature = "crt-static")),
489 portable_atomic_outline_atomics,
490 ),
491 ),
492 target_os = "android",
493 all(
494 target_os = "freebsd",
495 any(
496 target_endian = "little",
497 not(target_feature = "crt-static"),
498 portable_atomic_outline_atomics,
499 ),
500 ),
501 target_os = "openbsd",
502 all(
503 target_os = "aix",
504 not(portable_atomic_pre_llvm_20),
505 portable_atomic_outline_atomics,
506 ), // TODO(aix): currently disabled by default
507 ),
508 not(any(miri, portable_atomic_sanitize_thread)),
509 ),
510 ),
511 ))]
512 {
513 pub(crate) use self::atomic128::powerpc64::{AtomicI128, AtomicU128};
514 }
515 // s390x
516 #[cfg(all(
517 target_arch = "s390x",
518 not(all(
519 any(miri, portable_atomic_sanitize_thread),
520 not(portable_atomic_atomic_intrinsics),
521 )),
522 not(portable_atomic_no_asm),
523 ))]
524 {
525 pub(crate) use self::atomic128::s390x::{AtomicI128, AtomicU128};
526 }
527 // no native atomic CAS & (assume single core | critical section) => critical section based fallback
528 #[cfg_attr(
529 portable_atomic_no_cfg_target_has_atomic,
530 cfg(any(
531 target_arch = "avr",
532 target_arch = "msp430",
533 all(feature = "critical-section", portable_atomic_no_atomic_cas),
534 portable_atomic_unsafe_assume_single_core,
535 ))
536 )]
537 #[cfg_attr(
538 not(portable_atomic_no_cfg_target_has_atomic),
539 cfg(any(
540 target_arch = "avr",
541 target_arch = "msp430",
542 all(feature = "critical-section", not(target_has_atomic = "ptr")),
543 portable_atomic_unsafe_assume_single_core,
544 ))
545 )]
546 {
547 #[cfg(feature = "fallback")]
548 pub(crate) use self::interrupt::{AtomicI128, AtomicU128};
549 }
550 // no core 128-bit atomic & has CAS => use lock-base fallback
551 #[cfg_attr(
552 portable_atomic_no_cfg_target_has_atomic,
553 cfg(all(feature = "fallback", not(portable_atomic_no_atomic_cas)))
554 )]
555 #[cfg_attr(
556 not(portable_atomic_no_cfg_target_has_atomic),
557 cfg(all(feature = "fallback", target_has_atomic = "ptr"))
558 )]
559 {
560 pub(crate) use self::fallback::{AtomicI128, AtomicU128};
561 }
562 #[cfg(else)]
563 {
564 // no atomic
565 }
566 });