Provide 32 bit atomic impls for emscripten

This commit is contained in:
David Tolnay 2019-07-17 09:11:35 -07:00
parent 3158bf9093
commit 7b0e06c825
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
6 changed files with 29 additions and 30 deletions

View File

@ -14,8 +14,6 @@ fn main() {
let target = env::var("TARGET").unwrap(); let target = env::var("TARGET").unwrap();
let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten"; 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 // std::collections::Bound was stabilized in Rust 1.17
// but it was moved to core::ops later in Rust 1.26: // but it was moved to core::ops later in Rust 1.26:
// https://doc.rust-lang.org/core/ops/enum.Bound.html // https://doc.rust-lang.org/core/ops/enum.Bound.html
@ -71,8 +69,23 @@ fn main() {
println!("cargo:rustc-cfg=num_nonzero"); println!("cargo:rustc-cfg=num_nonzero");
} }
if minor >= 34 && has_atomic_integers { if minor >= 34 {
println!("cargo:rustc-cfg=std_integer_atomics"); // 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> {
u32::from_str(next).ok() 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
}
}

View File

@ -2546,7 +2546,7 @@ where
} }
} }
#[cfg(all(feature = "std", std_integer_atomics))] #[cfg(all(feature = "std", std_atomic))]
macro_rules! atomic_impl { macro_rules! atomic_impl {
($($ty:ident)*) => { ($($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! { atomic_impl! {
AtomicBool AtomicBool
AtomicI8 AtomicI16 AtomicI32 AtomicIsize AtomicI8 AtomicI16 AtomicI32 AtomicIsize
AtomicU8 AtomicU16 AtomicU32 AtomicUsize AtomicU8 AtomicU16 AtomicU32 AtomicUsize
} }
#[cfg(all(feature = "std", std_integer_atomics, not(target_os = "emscripten")))] #[cfg(all(feature = "std", std_atomic64))]
atomic_impl! { atomic_impl! {
AtomicI64 AtomicU64 AtomicI64 AtomicU64
} }

View File

@ -212,12 +212,12 @@ mod lib {
#[cfg(range_inclusive)] #[cfg(range_inclusive)]
pub use self::core::ops::RangeInclusive; pub use self::core::ops::RangeInclusive;
#[cfg(all(feature = "std", std_integer_atomics))] #[cfg(all(feature = "std", std_atomic))]
pub use std::sync::atomic::{ pub use std::sync::atomic::{
AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8, AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8,
AtomicUsize, Ordering, 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}; pub use std::sync::atomic::{AtomicI64, AtomicU64};
#[cfg(any(core_duration, feature = "std"))] #[cfg(any(core_duration, feature = "std"))]

View File

@ -842,7 +842,7 @@ where
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#[cfg(all(feature = "std", std_integer_atomics))] #[cfg(all(feature = "std", std_atomic))]
macro_rules! atomic_impl { macro_rules! atomic_impl {
($($ty:ident)*) => { ($($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! { atomic_impl! {
AtomicBool AtomicBool
AtomicI8 AtomicI16 AtomicI32 AtomicIsize AtomicI8 AtomicI16 AtomicI32 AtomicIsize
AtomicU8 AtomicU16 AtomicU32 AtomicUsize AtomicU8 AtomicU16 AtomicU32 AtomicUsize
} }
#[cfg(all(feature = "std", std_integer_atomics, not(target_os = "emscripten")))] #[cfg(all(feature = "std", std_atomic64))]
atomic_impl! { atomic_impl! {
AtomicI64 AtomicU64 AtomicI64 AtomicU64
} }

View File

@ -17,7 +17,7 @@ use std::sync::atomic::{
use std::sync::{Arc, Weak as ArcWeak}; use std::sync::{Arc, Weak as ArcWeak};
use std::time::{Duration, UNIX_EPOCH}; use std::time::{Duration, UNIX_EPOCH};
#[cfg(not(target_os = "emscripten"))] #[cfg(target_arch = "x86_64")]
use std::sync::atomic::{AtomicI64, AtomicU64}; use std::sync::atomic::{AtomicI64, AtomicU64};
use fnv::FnvHasher; use fnv::FnvHasher;
@ -1181,7 +1181,7 @@ fn test_atomics() {
test(AtomicU32::load, 131072u32, Token::U32(131072u32)); test(AtomicU32::load, 131072u32, Token::U32(131072u32));
test(AtomicUsize::load, 131072usize, Token::U32(131072)); 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(AtomicI64::load, -8589934592, Token::I64(-8589934592));
test(AtomicU64::load, 8589934592u64, Token::U64(8589934592)); test(AtomicU64::load, 8589934592u64, Token::U64(8589934592));

View File

@ -19,7 +19,7 @@ use std::time::{Duration, UNIX_EPOCH};
#[cfg(unix)] #[cfg(unix)]
use std::str; use std::str;
#[cfg(not(target_os = "emscripten"))] #[cfg(target_arch = "x86_64")]
use std::sync::atomic::{AtomicI64, AtomicU64}; use std::sync::atomic::{AtomicI64, AtomicU64};
use fnv::FnvHasher; use fnv::FnvHasher;
@ -504,7 +504,7 @@ declare_tests! {
} }
} }
#[cfg(not(target_os = "emscripten"))] #[cfg(target_arch = "x86_64")]
declare_tests! { declare_tests! {
test_atomic64 { test_atomic64 {
AtomicI64::new(-4295032832i64) => &[Token::I64(-4295032832i64)], AtomicI64::new(-4295032832i64) => &[Token::I64(-4295032832i64)],