From 7b0e06c82569bdb72554e8ee61f1afab23a5a3e9 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 17 Jul 2019 09:11:35 -0700 Subject: [PATCH] Provide 32 bit atomic impls for emscripten --- serde/build.rs | 35 +++++++++++++++++------------------ serde/src/de/impls.rs | 6 +++--- serde/src/lib.rs | 4 ++-- serde/src/ser/impls.rs | 6 +++--- test_suite/tests/test_de.rs | 4 ++-- test_suite/tests/test_ser.rs | 4 ++-- 6 files changed, 29 insertions(+), 30 deletions(-) diff --git a/serde/build.rs b/serde/build.rs index 3069010f..c9fdaf54 100644 --- a/serde/build.rs +++ b/serde/build.rs @@ -14,8 +14,6 @@ fn main() { let target = env::var("TARGET").unwrap(); let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten"; - let has_atomic_integers = target_has_at_least_atomic_u64(&target); - // std::collections::Bound was stabilized in Rust 1.17 // but it was moved to core::ops later in Rust 1.26: // https://doc.rust-lang.org/core/ops/enum.Bound.html @@ -71,8 +69,23 @@ fn main() { println!("cargo:rustc-cfg=num_nonzero"); } - if minor >= 34 && has_atomic_integers { - println!("cargo:rustc-cfg=std_integer_atomics"); + if minor >= 34 { + // Whitelist of archs that support std::sync::atomic module. Ideally we + // would use #[cfg(target_has_atomic = "...")] but it is not stable yet. + // Instead this is based on rustc's src/librustc_target/spec/*.rs. + let has_atomic64 = target.starts_with("x86_64") + || target.starts_with("i686") + || target.starts_with("aarch64") + || target.starts_with("powerpc64") + || target.starts_with("sparc64") + || target.starts_with("mips64el"); + let has_atomic32 = has_atomic64 || emscripten; + if has_atomic64 { + println!("cargo:rustc-cfg=std_atomic64"); + } + if has_atomic32 { + println!("cargo:rustc-cfg=std_atomic"); + } } } @@ -104,17 +117,3 @@ fn rustc_minor_version() -> Option { u32::from_str(next).ok() } - -fn target_has_at_least_atomic_u64(target: &str) -> bool { - // The cfg variable target_has_atomic is unstable - // so this data comes from the src/librustc_target/spec/*.rs - // files in the rust source. Generally, it's 64-bit platforms - // plus i686. - if target.starts_with("x86_64") || target.starts_with("i686") || - target.starts_with("aarch64") || target.starts_with("powerpc64") || - target.starts_with("sparc64") || target.starts_with("mips64el") { - true - } else { - false - } -} diff --git a/serde/src/de/impls.rs b/serde/src/de/impls.rs index 141629a5..9e72aad2 100644 --- a/serde/src/de/impls.rs +++ b/serde/src/de/impls.rs @@ -2546,7 +2546,7 @@ where } } -#[cfg(all(feature = "std", std_integer_atomics))] +#[cfg(all(feature = "std", std_atomic))] macro_rules! atomic_impl { ($($ty:ident)*) => { $( @@ -2562,14 +2562,14 @@ macro_rules! atomic_impl { }; } -#[cfg(all(feature = "std", std_integer_atomics))] +#[cfg(all(feature = "std", std_atomic))] atomic_impl! { AtomicBool AtomicI8 AtomicI16 AtomicI32 AtomicIsize AtomicU8 AtomicU16 AtomicU32 AtomicUsize } -#[cfg(all(feature = "std", std_integer_atomics, not(target_os = "emscripten")))] +#[cfg(all(feature = "std", std_atomic64))] atomic_impl! { AtomicI64 AtomicU64 } diff --git a/serde/src/lib.rs b/serde/src/lib.rs index 35b5ca53..5723c674 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -212,12 +212,12 @@ mod lib { #[cfg(range_inclusive)] pub use self::core::ops::RangeInclusive; - #[cfg(all(feature = "std", std_integer_atomics))] + #[cfg(all(feature = "std", std_atomic))] pub use std::sync::atomic::{ AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8, AtomicUsize, Ordering, }; - #[cfg(all(feature = "std", std_integer_atomics, not(target_os = "emscripten")))] + #[cfg(all(feature = "std", std_atomic64))] pub use std::sync::atomic::{AtomicI64, AtomicU64}; #[cfg(any(core_duration, feature = "std"))] diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index 619be8b2..caf48e89 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -842,7 +842,7 @@ where //////////////////////////////////////////////////////////////////////////////// -#[cfg(all(feature = "std", std_integer_atomics))] +#[cfg(all(feature = "std", std_atomic))] macro_rules! atomic_impl { ($($ty:ident)*) => { $( @@ -858,14 +858,14 @@ macro_rules! atomic_impl { } } -#[cfg(all(feature = "std", std_integer_atomics))] +#[cfg(all(feature = "std", std_atomic))] atomic_impl! { AtomicBool AtomicI8 AtomicI16 AtomicI32 AtomicIsize AtomicU8 AtomicU16 AtomicU32 AtomicUsize } -#[cfg(all(feature = "std", std_integer_atomics, not(target_os = "emscripten")))] +#[cfg(all(feature = "std", std_atomic64))] atomic_impl! { AtomicI64 AtomicU64 } diff --git a/test_suite/tests/test_de.rs b/test_suite/tests/test_de.rs index 9d1d308d..3946865b 100644 --- a/test_suite/tests/test_de.rs +++ b/test_suite/tests/test_de.rs @@ -17,7 +17,7 @@ use std::sync::atomic::{ use std::sync::{Arc, Weak as ArcWeak}; use std::time::{Duration, UNIX_EPOCH}; -#[cfg(not(target_os = "emscripten"))] +#[cfg(target_arch = "x86_64")] use std::sync::atomic::{AtomicI64, AtomicU64}; use fnv::FnvHasher; @@ -1181,7 +1181,7 @@ fn test_atomics() { test(AtomicU32::load, 131072u32, Token::U32(131072u32)); test(AtomicUsize::load, 131072usize, Token::U32(131072)); - #[cfg(not(target_os = "emscripten"))] + #[cfg(target_arch = "x86_64")] { test(AtomicI64::load, -8589934592, Token::I64(-8589934592)); test(AtomicU64::load, 8589934592u64, Token::U64(8589934592)); diff --git a/test_suite/tests/test_ser.rs b/test_suite/tests/test_ser.rs index 4c1412c4..719e987d 100644 --- a/test_suite/tests/test_ser.rs +++ b/test_suite/tests/test_ser.rs @@ -19,7 +19,7 @@ use std::time::{Duration, UNIX_EPOCH}; #[cfg(unix)] use std::str; -#[cfg(not(target_os = "emscripten"))] +#[cfg(target_arch = "x86_64")] use std::sync::atomic::{AtomicI64, AtomicU64}; use fnv::FnvHasher; @@ -504,7 +504,7 @@ declare_tests! { } } -#[cfg(not(target_os = "emscripten"))] +#[cfg(target_arch = "x86_64")] declare_tests! { test_atomic64 { AtomicI64::new(-4295032832i64) => &[Token::I64(-4295032832i64)],