Skip to content

Commit

Permalink
Add version detection for stabilized atomics and checked_add
Browse files Browse the repository at this point in the history
  • Loading branch information
faern committed Apr 12, 2019
1 parent 2302547 commit 8edd981
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ lock_api = { path = "lock_api", version = "0.2" }
rand = "0.6"
lazy_static = "1.0"

[build-dependencies]
rustc_version = "0.2"

[features]
default = []
owning_ref = ["lock_api/owning_ref"]
Expand Down
9 changes: 9 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use rustc_version::{version, Version};

fn main() {
if version().unwrap() >= Version::parse("1.34.0").unwrap() {
println!("cargo:rustc-cfg=has_sized_atomics");
println!("cargo:rustc-cfg=has_checked_instant");
}
}

3 changes: 3 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ petgraph = { version = "0.4.5", optional = true }
thread-id = { version = "3.2.0", optional = true }
backtrace = { version = "0.3.2", optional = true }

[build-dependencies]
rustc_version = "0.2"

[target.'cfg(unix)'.dependencies]
libc = "0.2.27"

Expand Down
8 changes: 8 additions & 0 deletions core/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use rustc_version::{version, Version};

fn main() {
if version().unwrap() >= Version::parse("1.34.0").unwrap() {
println!("cargo:rustc-cfg=has_sized_atomics");
}
}

4 changes: 2 additions & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mod libstd {
}

cfg_if! {
if #[cfg(all(any(feature = "i-am-libstd", feature = "nightly"), target_os = "linux"))] {
if #[cfg(all(any(feature = "i-am-libstd", has_sized_atomics), target_os = "linux"))] {
#[path = "thread_parker/linux.rs"]
mod thread_parker;
} else if #[cfg(unix)] {
Expand All @@ -81,7 +81,7 @@ cfg_if! {
} else if #[cfg(windows)] {
#[path = "thread_parker/windows/mod.rs"]
mod thread_parker;
} else if #[cfg(all(any(feature = "i-am-libstd", feature = "nightly"), target_os = "redox"))] {
} else if #[cfg(all(any(feature = "i-am-libstd", has_sized_atomics), target_os = "redox"))] {
#[path = "thread_parker/redox.rs"]
mod thread_parker;
} else if #[cfg(all(target_env = "sgx", target_vendor = "fortanix"))] {
Expand Down
8 changes: 4 additions & 4 deletions src/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

#[cfg(any(feature = "nightly", feature = "i-am-libstd"))]
#[cfg(any(has_sized_atomics, feature = "i-am-libstd"))]
use core::sync::atomic::AtomicU8;
#[cfg(any(feature = "nightly", feature = "i-am-libstd"))]
#[cfg(any(has_sized_atomics, feature = "i-am-libstd"))]
type U8 = u8;
#[cfg(not(any(feature = "nightly", feature = "i-am-libstd")))]
#[cfg(not(any(has_sized_atomics, feature = "i-am-libstd")))]
use core::sync::atomic::AtomicUsize as AtomicU8;
#[cfg(not(any(feature = "nightly", feature = "i-am-libstd")))]
#[cfg(not(any(has_sized_atomics, feature = "i-am-libstd")))]
type U8 = usize;
use super::parking_lot_core::{self, SpinWait, DEFAULT_PARK_TOKEN, DEFAULT_UNPARK_TOKEN};
use super::util::UncheckedOptionExt;
Expand Down
8 changes: 4 additions & 4 deletions src/raw_mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

#[cfg(any(feature = "nightly", feature = "i-am-libstd"))]
#[cfg(any(has_sized_atomics, feature = "i-am-libstd"))]
use core::sync::atomic::AtomicU8;
use core::sync::atomic::Ordering;
#[cfg(any(feature = "nightly", feature = "i-am-libstd"))]
#[cfg(any(has_sized_atomics, feature = "i-am-libstd"))]
type U8 = u8;
#[cfg(not(any(feature = "nightly", feature = "i-am-libstd")))]
#[cfg(not(any(has_sized_atomics, feature = "i-am-libstd")))]
use core::sync::atomic::AtomicUsize as AtomicU8;
#[cfg(not(any(feature = "nightly", feature = "i-am-libstd")))]
#[cfg(not(any(has_sized_atomics, feature = "i-am-libstd")))]
type U8 = usize;
use super::libstd::time::{Duration, Instant};
use super::lock_api::{GuardNoSend, RawMutex as RawMutexTrait, RawMutexFair, RawMutexTimed};
Expand Down
4 changes: 2 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ unsafe fn unreachable() -> ! {

#[inline]
pub fn to_deadline(timeout: Duration) -> Option<Instant> {
#[cfg(any(feature = "nightly", feature = "i-am-libstd"))]
#[cfg(any(has_checked_instant, feature = "i-am-libstd"))]
let deadline = Instant::now().checked_add(timeout);
#[cfg(not(any(feature = "nightly", feature = "i-am-libstd")))]
#[cfg(not(any(has_checked_instant, feature = "i-am-libstd")))]
let deadline = Some(Instant::now() + timeout);

deadline
Expand Down

0 comments on commit 8edd981

Please sign in to comment.