Skip to content

Commit

Permalink
Provide 32 bit atomic impls for emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jul 17, 2019
1 parent 3158bf9 commit 7b0e06c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
35 changes: 17 additions & 18 deletions serde/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
}
}
}

Expand Down Expand Up @@ -104,17 +117,3 @@ fn rustc_minor_version() -> Option<u32> {

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
}
}
6 changes: 3 additions & 3 deletions serde/src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2546,7 +2546,7 @@ where
}
}

#[cfg(all(feature = "std", std_integer_atomics))]
#[cfg(all(feature = "std", std_atomic))]
macro_rules! atomic_impl {
($($ty:ident)*) => {
$(
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down
6 changes: 3 additions & 3 deletions serde/src/ser/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ where

////////////////////////////////////////////////////////////////////////////////

#[cfg(all(feature = "std", std_integer_atomics))]
#[cfg(all(feature = "std", std_atomic))]
macro_rules! atomic_impl {
($($ty:ident)*) => {
$(
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions test_suite/tests/test_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions test_suite/tests/test_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)],
Expand Down

0 comments on commit 7b0e06c

Please sign in to comment.