From 334320064a047e9855d789decba36ebb984c2c54 Mon Sep 17 00:00:00 2001 From: jtnunley Date: Sat, 10 Dec 2022 21:53:43 -0800 Subject: [PATCH 1/3] Change flags for having an atomic u64 --- tokio/build.rs | 31 +++++++++++++++++++++++++++++ tokio/src/macros/cfg.rs | 44 +++++++++++++++++++++++++++-------------- 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/tokio/build.rs b/tokio/build.rs index 93b05092bf9..28ebc206390 100644 --- a/tokio/build.rs +++ b/tokio/build.rs @@ -24,9 +24,17 @@ const CONST_MUTEX_NEW_PROBE: &str = r#" } "#; +const TARGET_HAS_ATOMIC_PROBE: &str = r#" +{ + #[cfg(target_has_atomic = "ptr")] + let _ = (); +} +"#; + fn main() { let mut enable_const_thread_local = false; let mut enable_addr_of = false; + let mut enable_target_has_atomic = false; let mut enable_const_mutex_new = false; match AutoCfg::new() { @@ -66,6 +74,21 @@ fn main() { } } + // The `target_has_atomic` cfg was stabilized in 1.60. + if ac.probe_rustc_version(1, 61) { + enable_target_has_atomic = true; + } else if ac.probe_rustc_version(1, 60) { + // This compiler claims to be 1.60, but there are some nightly + // compilers that claim to be 1.60 without supporting the + // feature. Explicitly probe to check if code using them + // compiles. + // + // The oldest nightly that supports the feature is 2022-02-11. + if ac.probe_expression(TARGET_HAS_ATOMIC_PROBE) { + enable_target_has_atomic = true; + } + } + // The `Mutex::new` method was made const in 1.63. if ac.probe_rustc_version(1, 64) { enable_const_mutex_new = true; @@ -109,6 +132,14 @@ fn main() { autocfg::emit("tokio_no_addr_of") } + if !enable_target_has_atomic { + // To disable this feature on compilers that support it, you can + // explicitly pass this flag with the following environment variable: + // + // RUSTFLAGS="--cfg tokio_no_target_has_atomic" + autocfg::emit("tokio_no_target_has_atomic") + } + if !enable_const_mutex_new { // To disable this feature on compilers that support it, you can // explicitly pass this flag with the following environment variable: diff --git a/tokio/src/macros/cfg.rs b/tokio/src/macros/cfg.rs index 2eea344b516..08772642121 100644 --- a/tokio/src/macros/cfg.rs +++ b/tokio/src/macros/cfg.rs @@ -461,14 +461,21 @@ macro_rules! cfg_not_coop { macro_rules! cfg_has_atomic_u64 { ($($item:item)*) => { $( - #[cfg(not(any( - target_arch = "arm", - target_arch = "mips", - target_arch = "powerpc", - target_arch = "riscv32", - tokio_wasm, - tokio_no_atomic_u64, - )))] + #[cfg_attr( + not(tokio_no_target_has_atomic), + cfg(all(target_has_atomic = "64", not(tokio_no_atomic_u64)) + ))] + #[cfg_attr( + tokio_no_target_has_atomic, + cfg(not(any( + target_arch = "arm", + target_arch = "mips", + target_arch = "powerpc", + target_arch = "riscv32", + tokio_wasm, + tokio_no_atomic_u64, + ))) + )] $item )* } @@ -477,14 +484,21 @@ macro_rules! cfg_has_atomic_u64 { macro_rules! cfg_not_has_atomic_u64 { ($($item:item)*) => { $( - #[cfg(any( - target_arch = "arm", - target_arch = "mips", - target_arch = "powerpc", - target_arch = "riscv32", - tokio_wasm, - tokio_no_atomic_u64, + #[cfg_attr( + not(tokio_no_target_has_atomic), + cfg(any(not(target_has_atomic = "64"), tokio_no_atomic_u64) ))] + #[cfg_attr( + tokio_no_target_has_atomic, + cfg(any( + target_arch = "arm", + target_arch = "mips", + target_arch = "powerpc", + target_arch = "riscv32", + tokio_wasm, + tokio_no_atomic_u64, + )) + )] $item )* } From 4003c26f2e2c4a94fc3778ab98591ff9ff722bcb Mon Sep 17 00:00:00 2001 From: jtnunley Date: Sun, 11 Dec 2022 14:18:25 -0800 Subject: [PATCH 2/3] Use probes instead of guesses --- tokio/build.rs | 21 +++++++++++++++++++++ tokio/src/macros/cfg.rs | 18 ++---------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/tokio/build.rs b/tokio/build.rs index 28ebc206390..503c0242fc8 100644 --- a/tokio/build.rs +++ b/tokio/build.rs @@ -31,11 +31,18 @@ const TARGET_HAS_ATOMIC_PROBE: &str = r#" } "#; +const TARGET_ATOMIC_U64_PROBE: &str = r#" +{ + use std::sync::atomic::AtomicU64 as _; +} +"#; + fn main() { let mut enable_const_thread_local = false; let mut enable_addr_of = false; let mut enable_target_has_atomic = false; let mut enable_const_mutex_new = false; + let mut target_needs_atomic_u64_fallback = false; match AutoCfg::new() { Ok(ac) => { @@ -89,6 +96,12 @@ fn main() { } } + // If we can't tell using `target_has_atomic`, tell if the target + // has `AtomicU64` by trying to use it. + if !enable_target_has_atomic && !ac.probe_expression(TARGET_ATOMIC_U64_PROBE) { + target_needs_atomic_u64_fallback = true; + } + // The `Mutex::new` method was made const in 1.63. if ac.probe_rustc_version(1, 64) { enable_const_mutex_new = true; @@ -148,6 +161,14 @@ fn main() { autocfg::emit("tokio_no_const_mutex_new") } + if target_needs_atomic_u64_fallback { + // To disable this feature on compilers that support it, you can + // explicitly pass this flag with the following environment variable: + // + // RUSTFLAGS="--cfg tokio_no_atomic_u64" + autocfg::emit("tokio_no_atomic_u64") + } + let target = ::std::env::var("TARGET").unwrap_or_default(); // We emit cfgs instead of using `target_family = "wasm"` that requires Rust 1.54. diff --git a/tokio/src/macros/cfg.rs b/tokio/src/macros/cfg.rs index 08772642121..1c66d24147a 100644 --- a/tokio/src/macros/cfg.rs +++ b/tokio/src/macros/cfg.rs @@ -467,14 +467,7 @@ macro_rules! cfg_has_atomic_u64 { ))] #[cfg_attr( tokio_no_target_has_atomic, - cfg(not(any( - target_arch = "arm", - target_arch = "mips", - target_arch = "powerpc", - target_arch = "riscv32", - tokio_wasm, - tokio_no_atomic_u64, - ))) + cfg(not(tokio_no_atomic_u64)) )] $item )* @@ -490,14 +483,7 @@ macro_rules! cfg_not_has_atomic_u64 { ))] #[cfg_attr( tokio_no_target_has_atomic, - cfg(any( - target_arch = "arm", - target_arch = "mips", - target_arch = "powerpc", - target_arch = "riscv32", - tokio_wasm, - tokio_no_atomic_u64, - )) + cfg(tokio_no_atomic_u64) )] $item )* From 6fa46c5e377aa6cad670b45416a1f85cedb2e466 Mon Sep 17 00:00:00 2001 From: jtnunley Date: Tue, 13 Dec 2022 14:10:50 -0800 Subject: [PATCH 3/3] Mailbox