Skip to content

Commit

Permalink
Merge pull request #33 from usbalbin/use__target_has_atomic
Browse files Browse the repository at this point in the history
Fixes #32
  • Loading branch information
Amanieu authored Jan 12, 2023
2 parents 319aa96 + 5989a43 commit d6ca640
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 46 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ keywords = ["atomic", "no_std"]
default = ["fallback"]
std = []
fallback = []

[build-dependencies]
autocfg = "1"
nightly = []
16 changes: 0 additions & 16 deletions build.rs

This file was deleted.

49 changes: 37 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#![warn(missing_docs)]
#![warn(rust_2018_idioms)]
#![no_std]
#![cfg_attr(feature = "nightly", feature(integer_atomics))]

#[cfg(any(test, feature = "std"))]
#[macro_use]
Expand Down Expand Up @@ -403,7 +404,10 @@ mod tests {
#[test]
fn atomic_bool() {
let a = Atomic::new(false);
assert_eq!(Atomic::<bool>::is_lock_free(), cfg!(has_atomic_u8),);
assert_eq!(
Atomic::<bool>::is_lock_free(),
cfg!(target_has_atomic = "8"),
);
assert_eq!(format!("{:?}", a), "Atomic(false)");
assert_eq!(a.load(SeqCst), false);
a.store(true, SeqCst);
Expand All @@ -419,7 +423,7 @@ mod tests {
#[test]
fn atomic_i8() {
let a = Atomic::new(0i8);
assert_eq!(Atomic::<i8>::is_lock_free(), cfg!(has_atomic_u8));
assert_eq!(Atomic::<i8>::is_lock_free(), cfg!(target_has_atomic = "8"));
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -440,7 +444,10 @@ mod tests {
#[test]
fn atomic_i16() {
let a = Atomic::new(0i16);
assert_eq!(Atomic::<i16>::is_lock_free(), cfg!(has_atomic_u16));
assert_eq!(
Atomic::<i16>::is_lock_free(),
cfg!(target_has_atomic = "16")
);
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -460,7 +467,10 @@ mod tests {
#[test]
fn atomic_i32() {
let a = Atomic::new(0i32);
assert_eq!(Atomic::<i32>::is_lock_free(), cfg!(has_atomic_u32));
assert_eq!(
Atomic::<i32>::is_lock_free(),
cfg!(target_has_atomic = "32")
);
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -482,7 +492,7 @@ mod tests {
let a = Atomic::new(0i64);
assert_eq!(
Atomic::<i64>::is_lock_free(),
cfg!(has_atomic_u64) && mem::align_of::<i64>() == 8
cfg!(target_has_atomic = "64") && mem::align_of::<i64>() == 8
);
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
Expand All @@ -503,7 +513,10 @@ mod tests {
#[test]
fn atomic_i128() {
let a = Atomic::new(0i128);
assert_eq!(Atomic::<i128>::is_lock_free(), cfg!(has_atomic_u128));
assert_eq!(
Atomic::<i128>::is_lock_free(),
cfg!(feature = "nightly") & cfg!(target_has_atomic = "128")
);
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand Down Expand Up @@ -542,7 +555,7 @@ mod tests {
#[test]
fn atomic_u8() {
let a = Atomic::new(0u8);
assert_eq!(Atomic::<u8>::is_lock_free(), cfg!(has_atomic_u8));
assert_eq!(Atomic::<u8>::is_lock_free(), cfg!(target_has_atomic = "8"));
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -562,7 +575,10 @@ mod tests {
#[test]
fn atomic_u16() {
let a = Atomic::new(0u16);
assert_eq!(Atomic::<u16>::is_lock_free(), cfg!(has_atomic_u16));
assert_eq!(
Atomic::<u16>::is_lock_free(),
cfg!(target_has_atomic = "16")
);
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -582,7 +598,10 @@ mod tests {
#[test]
fn atomic_u32() {
let a = Atomic::new(0u32);
assert_eq!(Atomic::<u32>::is_lock_free(), cfg!(has_atomic_u32));
assert_eq!(
Atomic::<u32>::is_lock_free(),
cfg!(target_has_atomic = "32")
);
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -604,7 +623,7 @@ mod tests {
let a = Atomic::new(0u64);
assert_eq!(
Atomic::<u64>::is_lock_free(),
cfg!(has_atomic_u64) && mem::align_of::<u64>() == 8
cfg!(target_has_atomic = "64") && mem::align_of::<u64>() == 8
);
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
Expand All @@ -625,7 +644,10 @@ mod tests {
#[test]
fn atomic_u128() {
let a = Atomic::new(0u128);
assert_eq!(Atomic::<u128>::is_lock_free(), cfg!(has_atomic_u128));
assert_eq!(
Atomic::<u128>::is_lock_free(),
cfg!(feature = "nightly") & cfg!(target_has_atomic = "128")
);
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand Down Expand Up @@ -702,7 +724,10 @@ mod tests {
#[test]
fn atomic_quxx() {
let a = Atomic::default();
assert_eq!(Atomic::<Quux>::is_lock_free(), cfg!(has_atomic_u32));
assert_eq!(
Atomic::<Quux>::is_lock_free(),
cfg!(target_has_atomic = "32")
);
assert_eq!(format!("{:?}", a), "Atomic(Quux(0))");
assert_eq!(a.load(SeqCst), Quux(0));
a.store(Quux(1), SeqCst);
Expand Down
33 changes: 18 additions & 15 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@ use core::sync::atomic::Ordering;
macro_rules! match_atomic {
($type:ident, $atomic:ident, $impl:expr, $fallback_impl:expr) => {
match mem::size_of::<$type>() {
#[cfg(has_atomic_u8)]
#[cfg(target_has_atomic = "8")]
1 if mem::align_of::<$type>() >= 1 => {
type $atomic = core::sync::atomic::AtomicU8;

$impl
}
#[cfg(has_atomic_u16)]
#[cfg(target_has_atomic = "16")]
2 if mem::align_of::<$type>() >= 2 => {
type $atomic = core::sync::atomic::AtomicU16;

$impl
}
#[cfg(has_atomic_u32)]
#[cfg(target_has_atomic = "32")]
4 if mem::align_of::<$type>() >= 4 => {
type $atomic = core::sync::atomic::AtomicU32;

$impl
}
#[cfg(has_atomic_u64)]
#[cfg(target_has_atomic = "64")]
8 if mem::align_of::<$type>() >= 8 => {
type $atomic = core::sync::atomic::AtomicU64;

$impl
}
#[cfg(has_atomic_u128)]
#[cfg(all(feature = "nightly", target_has_atomic = "128"))]
16 if mem::align_of::<$type>() >= 16 => {
type $atomic = core::sync::atomic::AtomicU128;

Expand All @@ -57,31 +57,31 @@ macro_rules! match_atomic {
macro_rules! match_signed_atomic {
($type:ident, $atomic:ident, $impl:expr, $fallback_impl:expr) => {
match mem::size_of::<$type>() {
#[cfg(has_atomic_i8)]
#[cfg(target_has_atomic = "8")]
1 if mem::align_of::<$type>() >= 1 => {
type $atomic = core::sync::atomic::AtomicI8;

$impl
}
#[cfg(has_atomic_i16)]
#[cfg(target_has_atomic = "16")]
2 if mem::align_of::<$type>() >= 2 => {
type $atomic = core::sync::atomic::AtomicI16;

$impl
}
#[cfg(has_atomic_i32)]
#[cfg(target_has_atomic = "32")]
4 if mem::align_of::<$type>() >= 4 => {
type $atomic = core::sync::atomic::AtomicI32;

$impl
}
#[cfg(has_atomic_i64)]
#[cfg(target_has_atomic = "64")]
8 if mem::align_of::<$type>() >= 8 => {
type $atomic = core::sync::atomic::AtomicI64;

$impl
}
#[cfg(has_atomic_u128)]
#[cfg(all(feature = "nightly", target_has_atomic = "128"))]
16 if mem::align_of::<$type>() >= 16 => {
type $atomic = core::sync::atomic::AtomicI128;

Expand All @@ -100,11 +100,14 @@ pub const fn atomic_is_lock_free<T>() -> bool {
let size = mem::size_of::<T>();
let align = mem::align_of::<T>();

(cfg!(has_atomic_u8) & (size == 1) & (align >= 1))
| (cfg!(has_atomic_u16) & (size == 2) & (align >= 2))
| (cfg!(has_atomic_u32) & (size == 4) & (align >= 4))
| (cfg!(has_atomic_u64) & (size == 8) & (align >= 8))
| (cfg!(has_atomic_u128) & (size == 16) & (align >= 16))
(cfg!(target_has_atomic = "8") & (size == 1) & (align >= 1))
| (cfg!(target_has_atomic = "16") & (size == 2) & (align >= 2))
| (cfg!(target_has_atomic = "32") & (size == 4) & (align >= 4))
| (cfg!(target_has_atomic = "64") & (size == 8) & (align >= 8))
| (cfg!(feature = "nightly")
& cfg!(target_has_atomic = "128")
& (size == 16)
& (align >= 16))
}

#[inline]
Expand Down

0 comments on commit d6ca640

Please sign in to comment.