Skip to content

Commit

Permalink
Remove fallback to word-sized atomics as we explicitly detect the ava…
Browse files Browse the repository at this point in the history
…ilable atomics now.
  • Loading branch information
adamreichold committed Aug 4, 2020
1 parent ceba876 commit 833996f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 106 deletions.
17 changes: 4 additions & 13 deletions build.rs
Expand Up @@ -4,22 +4,13 @@ fn main() {
let ac = autocfg::new();

for root in &["core", "std"] {
ac.emit_path_cfg(
&format!("{}::sync::atomic::AtomicUsize", root),
"has_atomic_usize",
);
ac.emit_path_cfg(
&format!("{}::sync::atomic::AtomicIsize", root),
"has_atomic_isize",
);

for size in &[8, 16, 32, 64] {
ac.emit_path_cfg(
&format!("{}::sync::atomic::AtomicU{}", root, size),
ac.emit_expression_cfg(
&format!("{}::sync::atomic::AtomicU{}::compare_exchange", root, size),
&format!("has_atomic_u{}", size),
);
ac.emit_path_cfg(
&format!("{}::sync::atomic::AtomicI{}", root, size),
ac.emit_expression_cfg(
&format!("{}::sync::atomic::AtomicI{}::compare_exchange", root, size),
&format!("has_atomic_i{}", size),
);
}
Expand Down
86 changes: 12 additions & 74 deletions src/lib.rs
Expand Up @@ -392,13 +392,7 @@ mod tests {
#[test]
fn atomic_bool() {
let a = Atomic::new(false);
assert_eq!(
Atomic::<bool>::is_lock_free(),
cfg!(any(
has_atomic_u8,
all(target_pointer_width = "8", has_atomic_usize)
))
);
assert_eq!(Atomic::<bool>::is_lock_free(), cfg!(has_atomic_u8),);
assert_eq!(format!("{:?}", a), "Atomic(false)");
assert_eq!(a.load(SeqCst), false);
a.store(true, SeqCst);
Expand All @@ -414,13 +408,7 @@ mod tests {
#[test]
fn atomic_i8() {
let a = Atomic::new(0i8);
assert_eq!(
Atomic::<i8>::is_lock_free(),
cfg!(any(
has_atomic_u8,
all(target_pointer_width = "8", has_atomic_usize)
))
);
assert_eq!(Atomic::<i8>::is_lock_free(), cfg!(has_atomic_u8));
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -441,13 +429,7 @@ mod tests {
#[test]
fn atomic_i16() {
let a = Atomic::new(0i16);
assert_eq!(
Atomic::<i16>::is_lock_free(),
cfg!(any(
has_atomic_u16,
all(target_pointer_width = "16", has_atomic_usize)
))
);
assert_eq!(Atomic::<i16>::is_lock_free(), cfg!(has_atomic_u16));
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -467,13 +449,7 @@ mod tests {
#[test]
fn atomic_i32() {
let a = Atomic::new(0i32);
assert_eq!(
Atomic::<i32>::is_lock_free(),
cfg!(any(
has_atomic_u32,
all(target_pointer_width = "32", has_atomic_usize)
))
);
assert_eq!(Atomic::<i32>::is_lock_free(), cfg!(has_atomic_u32));
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -495,10 +471,7 @@ mod tests {
let a = Atomic::new(0i64);
assert_eq!(
Atomic::<i64>::is_lock_free(),
cfg!(any(
has_atomic_u64,
all(target_pointer_width = "64", has_atomic_usize)
)) && mem::align_of::<i64>() == 8
cfg!(has_atomic_u64) && mem::align_of::<i64>() == 8
);
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
Expand All @@ -519,10 +492,7 @@ mod tests {
#[test]
fn atomic_i128() {
let a = Atomic::new(0i128);
assert_eq!(
Atomic::<i128>::is_lock_free(),
cfg!(all(target_pointer_width = "128", has_atomic_usize))
);
assert_eq!(Atomic::<i128>::is_lock_free(), cfg!(has_atomic_u128));
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -542,7 +512,6 @@ mod tests {
#[test]
fn atomic_isize() {
let a = Atomic::new(0isize);
assert_eq!(Atomic::<isize>::is_lock_free(), cfg!(has_atomic_usize));
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -562,13 +531,7 @@ mod tests {
#[test]
fn atomic_u8() {
let a = Atomic::new(0u8);
assert_eq!(
Atomic::<u8>::is_lock_free(),
cfg!(any(
has_atomic_u8,
all(target_pointer_width = "8", has_atomic_usize)
))
);
assert_eq!(Atomic::<u8>::is_lock_free(), cfg!(has_atomic_u8));
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -588,13 +551,7 @@ mod tests {
#[test]
fn atomic_u16() {
let a = Atomic::new(0u16);
assert_eq!(
Atomic::<u16>::is_lock_free(),
cfg!(any(
has_atomic_u16,
all(target_pointer_width = "16", has_atomic_usize)
))
);
assert_eq!(Atomic::<u16>::is_lock_free(), cfg!(has_atomic_u16));
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -614,13 +571,7 @@ mod tests {
#[test]
fn atomic_u32() {
let a = Atomic::new(0u32);
assert_eq!(
Atomic::<u32>::is_lock_free(),
cfg!(any(
has_atomic_u32,
all(target_pointer_width = "32", has_atomic_usize)
))
);
assert_eq!(Atomic::<u32>::is_lock_free(), cfg!(has_atomic_u32));
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -642,10 +593,7 @@ mod tests {
let a = Atomic::new(0u64);
assert_eq!(
Atomic::<u64>::is_lock_free(),
cfg!(any(
has_atomic_u64,
all(target_pointer_width = "64", has_atomic_usize)
)) && mem::align_of::<u64>() == 8
cfg!(has_atomic_u64) && mem::align_of::<u64>() == 8
);
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
Expand All @@ -666,10 +614,7 @@ mod tests {
#[test]
fn atomic_u128() {
let a = Atomic::new(0u128);
assert_eq!(
Atomic::<u128>::is_lock_free(),
cfg!(all(target_pointer_width = "128", has_atomic_usize))
);
assert_eq!(Atomic::<u128>::is_lock_free(), cfg!(has_atomic_u128));
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand All @@ -689,7 +634,6 @@ mod tests {
#[test]
fn atomic_usize() {
let a = Atomic::new(0usize);
assert_eq!(Atomic::<usize>::is_lock_free(), cfg!(has_atomic_usize));
assert_eq!(format!("{:?}", a), "Atomic(0)");
assert_eq!(a.load(SeqCst), 0);
a.store(1, SeqCst);
Expand Down Expand Up @@ -747,13 +691,7 @@ mod tests {
#[test]
fn atomic_quxx() {
let a = Atomic::default();
assert_eq!(
Atomic::<Quux>::is_lock_free(),
cfg!(any(
has_atomic_u32,
all(target_pointer_width = "4", has_atomic_usize)
))
);
assert_eq!(Atomic::<Quux>::is_lock_free(), cfg!(has_atomic_u32));
assert_eq!(format!("{:?}", a), "Atomic(Quux(0))");
assert_eq!(a.load(SeqCst), Quux(0));
a.store(Quux(1), SeqCst);
Expand Down
19 changes: 0 additions & 19 deletions src/ops.rs
Expand Up @@ -12,9 +12,6 @@ use core::ops;
use core::sync::atomic::Ordering;
use fallback;

const SIZEOF_USIZE: usize = mem::size_of::<usize>();
const ALIGNOF_USIZE: usize = mem::align_of::<usize>();

macro_rules! match_atomic {
($type:ident, $atomic:ident, $impl:expr, $fallback_impl:expr) => {
match mem::size_of::<$type>() {
Expand Down Expand Up @@ -42,20 +39,11 @@ macro_rules! match_atomic {

$impl
}
#[cfg(has_atomic_usize)]
SIZEOF_USIZE if mem::align_of::<$type>() >= ALIGNOF_USIZE => {
type $atomic = core::sync::atomic::AtomicUsize;

$impl
}
_ => $fallback_impl,
}
};
}

const SIZEOF_ISIZE: usize = mem::size_of::<isize>();
const ALIGNOF_ISIZE: usize = mem::align_of::<isize>();

macro_rules! match_signed_atomic {
($type:ident, $atomic:ident, $impl:expr, $fallback_impl:expr) => {
match mem::size_of::<$type>() {
Expand Down Expand Up @@ -83,12 +71,6 @@ macro_rules! match_signed_atomic {

$impl
}
#[cfg(has_atomic_isize)]
SIZEOF_ISIZE if mem::align_of::<$type>() >= ALIGNOF_ISIZE => {
type $atomic = core::sync::atomic::AtomicIsize;

$impl
}
_ => $fallback_impl,
}
};
Expand All @@ -103,7 +85,6 @@ pub const fn atomic_is_lock_free<T>() -> bool {
| (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_usize) & (size == SIZEOF_USIZE) & (align >= ALIGNOF_USIZE))
}

#[inline]
Expand Down

0 comments on commit 833996f

Please sign in to comment.