From 6c2a3016608bc71cc6d8124c92056f4b26e5c603 Mon Sep 17 00:00:00 2001 From: Bjorn <75190918+BjornTheProgrammer@users.noreply.github.com> Date: Sat, 3 Aug 2024 23:09:57 -0700 Subject: [PATCH] Implement cfg portable_atomic_critical_section --- README.md | 3 +++ build.rs | 4 +++- src/cfgs.rs | 32 ++++++++++++++++---------------- src/imp/interrupt/mod.rs | 38 +++++++++++++++++++------------------- src/imp/mod.rs | 22 +++++++++++----------- src/imp/msp430.rs | 12 ++++++------ 6 files changed, 58 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 7e925f834..a924e5a74 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,9 @@ RUSTFLAGS="--cfg portable_atomic_no_outline_atomics" cargo ... Originally, we were providing these as cfgs instead of features, but based on a strong request from the embedded ecosystem, we have agreed to provide them as features as well. See [#94](https://github.com/taiki-e/portable-atomic/pull/94) for more. +- **`--cfg portable_atomic_critical_section`**
+ Since 1.7.1, this cfg is an alias of [`critical-section` feature](#optional-features-critical-section). + - **`--cfg portable_atomic_no_outline_atomics`**
Disable dynamic dispatching by run-time CPU feature detection. diff --git a/build.rs b/build.rs index 8b0607a7d..651778de8 100644 --- a/build.rs +++ b/build.rs @@ -19,6 +19,8 @@ fn main() { #[cfg(feature = "unsafe-assume-single-core")] println!("cargo:rustc-cfg=portable_atomic_unsafe_assume_single_core"); + #[cfg(feature = "critical-section")] + println!("cargo:rustc-cfg=portable_atomic_critical_section"); #[cfg(feature = "s-mode")] println!("cargo:rustc-cfg=portable_atomic_s_mode"); #[cfg(feature = "force-amo")] @@ -53,7 +55,7 @@ fn main() { // Custom cfgs set by build script. Not public API. // grep -E 'cargo:rustc-cfg=' build.rs | grep -v '=//' | sed -E 's/^.*cargo:rustc-cfg=//; s/(=\\)?".*$//' | LC_ALL=C sort -u | tr '\n' ',' println!( - "cargo:rustc-check-cfg=cfg(portable_atomic_disable_fiq,portable_atomic_force_amo,portable_atomic_ll_sc_rmw,portable_atomic_llvm_15,portable_atomic_llvm_16,portable_atomic_llvm_18,portable_atomic_new_atomic_intrinsics,portable_atomic_no_asm,portable_atomic_no_asm_maybe_uninit,portable_atomic_no_atomic_64,portable_atomic_no_atomic_cas,portable_atomic_no_atomic_load_store,portable_atomic_no_atomic_min_max,portable_atomic_no_cfg_target_has_atomic,portable_atomic_no_cmpxchg16b_intrinsic,portable_atomic_no_cmpxchg16b_target_feature,portable_atomic_no_const_raw_ptr_deref,portable_atomic_no_const_transmute,portable_atomic_no_core_unwind_safe,portable_atomic_no_stronger_failure_ordering,portable_atomic_no_track_caller,portable_atomic_no_unsafe_op_in_unsafe_fn,portable_atomic_s_mode,portable_atomic_sanitize_thread,portable_atomic_target_feature,portable_atomic_unsafe_assume_single_core,portable_atomic_unstable_asm,portable_atomic_unstable_asm_experimental_arch,portable_atomic_unstable_cfg_target_has_atomic,portable_atomic_unstable_isa_attribute)" + "cargo:rustc-check-cfg=cfg(portable_atomic_disable_fiq,portable_atomic_force_amo,portable_atomic_ll_sc_rmw,portable_atomic_llvm_15,portable_atomic_llvm_16,portable_atomic_llvm_18,portable_atomic_new_atomic_intrinsics,portable_atomic_no_asm,portable_atomic_no_asm_maybe_uninit,portable_atomic_no_atomic_64,portable_atomic_no_atomic_cas,portable_atomic_no_atomic_load_store,portable_atomic_no_atomic_min_max,portable_atomic_no_cfg_target_has_atomic,portable_atomic_no_cmpxchg16b_intrinsic,portable_atomic_no_cmpxchg16b_target_feature,portable_atomic_no_const_raw_ptr_deref,portable_atomic_no_const_transmute,portable_atomic_no_core_unwind_safe,portable_atomic_no_stronger_failure_ordering,portable_atomic_no_track_caller,portable_atomic_no_unsafe_op_in_unsafe_fn,portable_atomic_s_mode,portable_atomic_sanitize_thread,portable_atomic_target_feature,portable_atomic_unsafe_assume_single_core,portable_atomic_unstable_asm,portable_atomic_unstable_asm_experimental_arch,portable_atomic_unstable_cfg_target_has_atomic,portable_atomic_unstable_isa_attribute,portable_atomic_critical_section)" ); // TODO: handle multi-line target_feature_fallback // grep -E 'target_feature_fallback\("' build.rs | sed -E 's/^.*target_feature_fallback\(//; s/",.*$/"/' | LC_ALL=C sort -u | tr '\n' ',' diff --git a/src/cfgs.rs b/src/cfgs.rs index e6f0b3ff9..1ff346640 100644 --- a/src/cfgs.rs +++ b/src/cfgs.rs @@ -9,7 +9,7 @@ target_arch = "msp430", target_arch = "riscv32", target_arch = "riscv64", - feature = "critical-section", + portable_atomic_critical_section, )), )))] #[macro_use] @@ -42,7 +42,7 @@ mod atomic_8_16_macros { target_arch = "msp430", target_arch = "riscv32", target_arch = "riscv64", - feature = "critical-section", + portable_atomic_critical_section, )), ))] #[macro_use] @@ -78,7 +78,7 @@ mod atomic_8_16_macros { target_arch = "msp430", target_arch = "riscv32", target_arch = "riscv64", - feature = "critical-section", + portable_atomic_critical_section, )), )), ))] @@ -104,7 +104,7 @@ mod atomic_32_macros { target_arch = "msp430", target_arch = "riscv32", target_arch = "riscv64", - feature = "critical-section", + portable_atomic_critical_section, )), )), )))] @@ -130,7 +130,7 @@ mod atomic_32_macros { any( not(portable_atomic_no_atomic_cas), portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", ), @@ -147,7 +147,7 @@ mod atomic_32_macros { any( target_has_atomic = "ptr", portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", ), @@ -177,7 +177,7 @@ mod atomic_64_macros { any( not(portable_atomic_no_atomic_cas), portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", ), @@ -194,7 +194,7 @@ mod atomic_64_macros { any( target_has_atomic = "ptr", portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", ), @@ -278,7 +278,7 @@ mod atomic_64_macros { cfg(any( not(portable_atomic_no_atomic_cas), portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", )) @@ -288,7 +288,7 @@ mod atomic_64_macros { cfg(any( target_has_atomic = "ptr", portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", )) @@ -367,7 +367,7 @@ mod atomic_128_macros { cfg(not(any( not(portable_atomic_no_atomic_cas), portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", ))) @@ -377,7 +377,7 @@ mod atomic_128_macros { cfg(not(any( target_has_atomic = "ptr", portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", ))) @@ -401,7 +401,7 @@ mod atomic_128_macros { cfg(any( not(portable_atomic_no_atomic_cas), portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", )) @@ -411,7 +411,7 @@ mod atomic_128_macros { cfg(any( target_has_atomic = "ptr", portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", )) @@ -434,7 +434,7 @@ mod atomic_cas_macros { cfg(not(any( not(portable_atomic_no_atomic_cas), portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", ))) @@ -444,7 +444,7 @@ mod atomic_cas_macros { cfg(not(any( target_has_atomic = "ptr", portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", ))) diff --git a/src/imp/interrupt/mod.rs b/src/imp/interrupt/mod.rs index cccd9ef24..b9515d4ff 100644 --- a/src/imp/interrupt/mod.rs +++ b/src/imp/interrupt/mod.rs @@ -39,10 +39,10 @@ // CAS together with atomic load/store. The load/store will not be // called while interrupts are disabled, and since the load/store is // atomic, it is not affected by interrupts even if interrupts are enabled. -#[cfg(not(any(target_arch = "avr", feature = "critical-section")))] +#[cfg(not(any(target_arch = "avr", portable_atomic_critical_section)))] use arch::atomic; -#[cfg(not(feature = "critical-section"))] +#[cfg(not(portable_atomic_critical_section))] #[cfg_attr( all( target_arch = "arm", @@ -66,16 +66,16 @@ mod arch; use core::{cell::UnsafeCell, sync::atomic::Ordering}; // Critical section implementations might use locks internally. -#[cfg(feature = "critical-section")] +#[cfg(portable_atomic_critical_section)] const IS_ALWAYS_LOCK_FREE: bool = false; // Consider atomic operations based on disabling interrupts on single-core // systems are lock-free. (We consider the pre-v6 ARM Linux's atomic operations // provided in a similar way by the Linux kernel to be lock-free.) -#[cfg(not(feature = "critical-section"))] +#[cfg(not(portable_atomic_critical_section))] const IS_ALWAYS_LOCK_FREE: bool = true; -#[cfg(feature = "critical-section")] +#[cfg(portable_atomic_critical_section)] #[inline] fn with(f: F) -> R where @@ -84,7 +84,7 @@ where critical_section::with(|_| f()) } -#[cfg(not(feature = "critical-section"))] +#[cfg(not(portable_atomic_critical_section))] #[inline] fn with(f: F) -> R where @@ -143,11 +143,11 @@ impl AtomicPtr { #[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)] pub(crate) fn load(&self, order: Ordering) -> *mut T { crate::utils::assert_load_ordering(order); - #[cfg(not(any(target_arch = "avr", feature = "critical-section")))] + #[cfg(not(any(target_arch = "avr", portable_atomic_critical_section)))] { self.as_native().load(order) } - #[cfg(any(target_arch = "avr", feature = "critical-section"))] + #[cfg(any(target_arch = "avr", portable_atomic_critical_section))] // SAFETY: any data races are prevented by disabling interrupts (see // module-level comments) and the raw pointer is valid because we got it // from a reference. @@ -158,11 +158,11 @@ impl AtomicPtr { #[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)] pub(crate) fn store(&self, ptr: *mut T, order: Ordering) { crate::utils::assert_store_ordering(order); - #[cfg(not(any(target_arch = "avr", feature = "critical-section")))] + #[cfg(not(any(target_arch = "avr", portable_atomic_critical_section)))] { self.as_native().store(ptr, order); } - #[cfg(any(target_arch = "avr", feature = "critical-section"))] + #[cfg(any(target_arch = "avr", portable_atomic_critical_section))] // SAFETY: any data races are prevented by disabling interrupts (see // module-level comments) and the raw pointer is valid because we got it // from a reference. @@ -228,7 +228,7 @@ impl AtomicPtr { self.p.get() } - #[cfg(not(any(target_arch = "avr", feature = "critical-section")))] + #[cfg(not(any(target_arch = "avr", portable_atomic_critical_section)))] #[inline] fn as_native(&self) -> &atomic::AtomicPtr { // SAFETY: AtomicPtr and atomic::AtomicPtr have the same layout and @@ -288,11 +288,11 @@ macro_rules! atomic_int { #[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)] pub(crate) fn load(&self, order: Ordering) -> $int_type { crate::utils::assert_load_ordering(order); - #[cfg(not(any(target_arch = "avr", feature = "critical-section")))] + #[cfg(not(any(target_arch = "avr", portable_atomic_critical_section)))] { self.as_native().load(order) } - #[cfg(any(target_arch = "avr", feature = "critical-section"))] + #[cfg(any(target_arch = "avr", portable_atomic_critical_section))] // SAFETY: any data races are prevented by disabling interrupts (see // module-level comments) and the raw pointer is valid because we got it // from a reference. @@ -303,18 +303,18 @@ macro_rules! atomic_int { #[cfg_attr(all(debug_assertions, not(portable_atomic_no_track_caller)), track_caller)] pub(crate) fn store(&self, val: $int_type, order: Ordering) { crate::utils::assert_store_ordering(order); - #[cfg(not(any(target_arch = "avr", feature = "critical-section")))] + #[cfg(not(any(target_arch = "avr", portable_atomic_critical_section)))] { self.as_native().store(val, order); } - #[cfg(any(target_arch = "avr", feature = "critical-section"))] + #[cfg(any(target_arch = "avr", portable_atomic_critical_section))] // SAFETY: any data races are prevented by disabling interrupts (see // module-level comments) and the raw pointer is valid because we got it // from a reference. with(|| unsafe { self.v.get().write(val) }); } - #[cfg(not(any(target_arch = "avr", feature = "critical-section")))] + #[cfg(not(any(target_arch = "avr", portable_atomic_critical_section)))] #[inline] fn as_native(&self) -> &atomic::$atomic_type { // SAFETY: $atomic_type and atomic::$atomic_type have the same layout and @@ -323,17 +323,17 @@ macro_rules! atomic_int { } } - #[cfg(not(all(target_arch = "msp430", not(feature = "critical-section"))))] + #[cfg(not(all(target_arch = "msp430", not(portable_atomic_critical_section))))] impl_default_no_fetch_ops!($atomic_type, $int_type); impl_default_bit_opts!($atomic_type, $int_type); - #[cfg(not(all(target_arch = "msp430", not(feature = "critical-section"))))] + #[cfg(not(all(target_arch = "msp430", not(portable_atomic_critical_section))))] impl $atomic_type { #[inline] pub(crate) fn not(&self, order: Ordering) { self.fetch_not(order); } } - #[cfg(all(target_arch = "msp430", not(feature = "critical-section")))] + #[cfg(all(target_arch = "msp430", not(portable_atomic_critical_section)))] impl $atomic_type { #[inline] pub(crate) fn add(&self, val: $int_type, order: Ordering) { diff --git a/src/imp/mod.rs b/src/imp/mod.rs index 4e7715954..905e19175 100644 --- a/src/imp/mod.rs +++ b/src/imp/mod.rs @@ -6,7 +6,7 @@ #[cfg(not(any( all( portable_atomic_no_atomic_load_store, - not(all(target_arch = "bpf", not(feature = "critical-section"))), + not(all(target_arch = "bpf", not(portable_atomic_critical_section))), ), portable_atomic_unsafe_assume_single_core, target_arch = "avr", @@ -15,14 +15,14 @@ #[cfg_attr( portable_atomic_no_cfg_target_has_atomic, cfg(not(all( - any(target_arch = "riscv32", target_arch = "riscv64", feature = "critical-section"), + any(target_arch = "riscv32", target_arch = "riscv64", portable_atomic_critical_section), portable_atomic_no_atomic_cas, ))) )] #[cfg_attr( not(portable_atomic_no_cfg_target_has_atomic), cfg(not(all( - any(target_arch = "riscv32", target_arch = "riscv64", feature = "critical-section"), + any(target_arch = "riscv32", target_arch = "riscv64", portable_atomic_critical_section), not(target_has_atomic = "ptr"), ))) )] @@ -132,7 +132,7 @@ mod arm_linux; pub(crate) mod msp430; // atomic load/store for RISC-V without A-extension -#[cfg(any(test, not(feature = "critical-section")))] +#[cfg(any(test, not(portable_atomic_critical_section)))] #[cfg_attr(portable_atomic_no_cfg_target_has_atomic, cfg(any(test, portable_atomic_no_atomic_cas)))] #[cfg_attr( not(portable_atomic_no_cfg_target_has_atomic), @@ -191,7 +191,7 @@ mod fallback; #[cfg(any( all(test, target_os = "none"), portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", ))] @@ -207,7 +207,7 @@ mod fallback; target_arch = "riscv32", target_arch = "riscv64", target_arch = "xtensa", - feature = "critical-section", + portable_atomic_critical_section, ))] mod interrupt; @@ -228,14 +228,14 @@ pub(crate) mod float; #[cfg_attr( portable_atomic_no_cfg_target_has_atomic, cfg(not(all( - any(target_arch = "riscv32", target_arch = "riscv64", feature = "critical-section"), + any(target_arch = "riscv32", target_arch = "riscv64", portable_atomic_critical_section), portable_atomic_no_atomic_cas, ))) )] #[cfg_attr( not(portable_atomic_no_cfg_target_has_atomic), cfg(not(all( - any(target_arch = "riscv32", target_arch = "riscv64", feature = "critical-section"), + any(target_arch = "riscv32", target_arch = "riscv64", portable_atomic_critical_section), not(target_has_atomic = "ptr"), ))) )] @@ -264,12 +264,12 @@ items! { #[cfg(all( target_arch = "bpf", portable_atomic_no_atomic_load_store, - not(feature = "critical-section"), + not(portable_atomic_critical_section), ))] pub(crate) use self::core_atomic::{AtomicI64, AtomicIsize, AtomicPtr, AtomicU64, AtomicUsize}; // RISC-V without A-extension & !(assume single core | critical section) -#[cfg(not(any(portable_atomic_unsafe_assume_single_core, feature = "critical-section")))] +#[cfg(not(any(portable_atomic_unsafe_assume_single_core, portable_atomic_critical_section)))] #[cfg_attr(portable_atomic_no_cfg_target_has_atomic, cfg(portable_atomic_no_atomic_cas))] #[cfg_attr(not(portable_atomic_no_cfg_target_has_atomic), cfg(not(target_has_atomic = "ptr")))] #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] @@ -285,7 +285,7 @@ items! { // no core atomic CAS & (assume single core | critical section) => critical section based fallback #[cfg(any( portable_atomic_unsafe_assume_single_core, - feature = "critical-section", + portable_atomic_critical_section, target_arch = "avr", target_arch = "msp430", ))] diff --git a/src/imp/msp430.rs b/src/imp/msp430.rs index caf95ba14..075b39e79 100644 --- a/src/imp/msp430.rs +++ b/src/imp/msp430.rs @@ -15,7 +15,7 @@ #[cfg(not(portable_atomic_no_asm))] use core::arch::asm; -#[cfg(any(test, not(feature = "critical-section")))] +#[cfg(any(test, not(portable_atomic_critical_section)))] use core::cell::UnsafeCell; use core::sync::atomic::Ordering; @@ -58,21 +58,21 @@ pub fn compiler_fence(order: Ordering) { macro_rules! atomic { (load_store, $([$($generics:tt)*])? $atomic_type:ident, $value_type:ty, $asm_suffix:tt) => { - #[cfg(any(test, not(feature = "critical-section")))] + #[cfg(any(test, not(portable_atomic_critical_section)))] #[repr(transparent)] pub(crate) struct $atomic_type $(<$($generics)*>)? { v: UnsafeCell<$value_type>, } - #[cfg(any(test, not(feature = "critical-section")))] + #[cfg(any(test, not(portable_atomic_critical_section)))] // Send is implicitly implemented for atomic integers, but not for atomic pointers. // SAFETY: any data races are prevented by atomic operations. unsafe impl $(<$($generics)*>)? Send for $atomic_type $(<$($generics)*>)? {} - #[cfg(any(test, not(feature = "critical-section")))] + #[cfg(any(test, not(portable_atomic_critical_section)))] // SAFETY: any data races are prevented by atomic operations. unsafe impl $(<$($generics)*>)? Sync for $atomic_type $(<$($generics)*>)? {} - #[cfg(any(test, not(feature = "critical-section")))] + #[cfg(any(test, not(portable_atomic_critical_section)))] impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { #[cfg(test)] #[inline] @@ -150,7 +150,7 @@ macro_rules! atomic { }; ($([$($generics:tt)*])? $atomic_type:ident, $value_type:ty, $asm_suffix:tt) => { atomic!(load_store, $([$($generics)*])? $atomic_type, $value_type, $asm_suffix); - #[cfg(any(test, not(feature = "critical-section")))] + #[cfg(any(test, not(portable_atomic_critical_section)))] impl $(<$($generics)*>)? $atomic_type $(<$($generics)*>)? { #[inline] pub(crate) fn add(&self, val: $value_type, _order: Ordering) {