Skip to content

Commit 7e6c097

Browse files
committed
Allow AtomicConsume to be used on targets that do not support atomic
1 parent 558c3ce commit 7e6c097

File tree

23 files changed

+97
-100
lines changed

23 files changed

+97
-100
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jobs:
8686
- name: dependency tree check
8787
run: ./ci/dependencies.sh
8888

89-
# When this job failed, run ci/no_atomic_cas.sh and commit result changes.
89+
# When this job failed, run ci/no_atomic.sh and commit result changes.
9090
# TODO(taiki-e): Ideally, this should be automated using a bot that creates
9191
# PR when failed, but there is no bandwidth to implement it
9292
# right now...
@@ -96,7 +96,7 @@ jobs:
9696
- uses: actions/checkout@v2
9797
- name: Install Rust
9898
run: rustup update nightly && rustup default nightly
99-
- run: ci/no_atomic_cas.sh
99+
- run: ci/no_atomic.sh
100100
- run: git diff --exit-code
101101

102102
# Check formatting.

build.rs

Lines changed: 0 additions & 32 deletions
This file was deleted.

ci/check-features.sh

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,19 @@ if [[ "$RUST_VERSION" != "nightly"* ]]; then
1313
# * `--no-dev-deps` - build without dev-dependencies to avoid https://github.com/rust-lang/cargo/issues/4866
1414
# * `--exclude benchmarks` - benchmarks doesn't published.
1515
# * `--skip nightly` - skip `nightly` feature as requires nightly compilers.
16-
cargo hack check --all --feature-powerset --no-dev-deps --exclude benchmarks --skip nightly
16+
cargo hack build --all --feature-powerset --no-dev-deps --exclude benchmarks --skip nightly
1717
else
1818
# On nightly, all feature combinations should work.
19-
cargo hack check --all --feature-powerset --no-dev-deps --exclude benchmarks
19+
cargo hack build --all --feature-powerset --no-dev-deps --exclude benchmarks
2020

21-
# Check for no_std environment.
21+
# Build for no_std environment.
22+
# thumbv7m-none-eabi supports atomic CAS.
23+
# thumbv6m-none-eabi supports atomic, but not atomic CAS.
24+
# riscv32i-unknown-none-elf does not support atomic at all.
2225
rustup target add thumbv7m-none-eabi
2326
rustup target add thumbv6m-none-eabi
24-
cargo hack check --all --feature-powerset --no-dev-deps --exclude benchmarks --target thumbv7m-none-eabi --skip std,default
25-
cargo hack check --all --feature-powerset --no-dev-deps --exclude benchmarks --target thumbv6m-none-eabi --skip std,default
27+
rustup target add riscv32i-unknown-none-elf
28+
cargo hack build --all --feature-powerset --no-dev-deps --exclude benchmarks --target thumbv7m-none-eabi --skip std,default
29+
cargo hack build --all --feature-powerset --no-dev-deps --exclude benchmarks --target thumbv6m-none-eabi --skip std,default
30+
cargo hack build --all --feature-powerset --no-dev-deps --exclude benchmarks --target riscv32i-unknown-none-elf --skip std,default
2631
fi

ci/no_atomic.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
# Update the list of targets that do not support atomic/CAS operations.
4+
#
5+
# Usage:
6+
# ./ci/no_atomic.sh
7+
8+
set -euo pipefail
9+
IFS=$'\n\t'
10+
11+
cd "$(cd "$(dirname "$0")" && pwd)"/..
12+
13+
file="no_atomic.rs"
14+
15+
{
16+
echo "// This file is @generated by $(basename "$0")."
17+
echo "// It is not intended for manual editing."
18+
echo ""
19+
} >"$file"
20+
21+
echo "const NO_ATOMIC_CAS: &[&str] = &[" >>"$file"
22+
for target in $(rustc --print target-list); do
23+
res=$(rustc --print target-spec-json -Z unstable-options --target "$target" \
24+
| jq -r "select(.\"atomic-cas\" == false)")
25+
[[ -z "$res" ]] || echo " \"$target\"," >>"$file"
26+
done
27+
echo "];" >>"$file"
28+
29+
# `"max-atomic-width" == 0` means that atomic is not supported at all.
30+
{
31+
# Only crossbeam-utils actually uses this const.
32+
echo "#[allow(dead_code)]"
33+
echo "const NO_ATOMIC: &[&str] = &["
34+
} >>"$file"
35+
for target in $(rustc --print target-list); do
36+
res=$(rustc --print target-spec-json -Z unstable-options --target "$target" \
37+
| jq -r "select(.\"max-atomic-width\" == 0)")
38+
[[ -z "$res" ]] || echo " \"$target\"," >>"$file"
39+
done
40+
echo "];" >>"$file"

ci/no_atomic_cas.sh

Lines changed: 0 additions & 27 deletions
This file was deleted.

crossbeam-epoch/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::env;
44

5-
include!("no_atomic_cas.rs");
5+
include!("no_atomic.rs");
66

77
// The rustc-cfg strings below are *not* public API. Please let us know by
88
// opening a GitHub issue if your build environment requires some way to enable
@@ -28,5 +28,5 @@ fn main() {
2828
println!("cargo:rustc-cfg=crossbeam_no_atomic_cas");
2929
}
3030

31-
println!("cargo:rerun-if-changed=no_atomic_cas.rs");
31+
println!("cargo:rerun-if-changed=no_atomic.rs");
3232
}

crossbeam-epoch/no_atomic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../no_atomic.rs

crossbeam-epoch/no_atomic_cas.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

crossbeam-epoch/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ mod primitive {
104104
pub(crate) use loom::lazy_static;
105105
pub(crate) use loom::thread_local;
106106
}
107+
#[cfg(not(crossbeam_no_atomic_cas))]
107108
#[cfg(not(crossbeam_loom))]
108109
#[allow(unused_imports, dead_code)]
109110
mod primitive {
110-
#[cfg(any(feature = "alloc", feature = "std"))]
111+
#[cfg(feature = "alloc")]
111112
pub(crate) mod cell {
112113
#[derive(Debug)]
113114
#[repr(transparent)]
@@ -135,14 +136,13 @@ mod primitive {
135136
}
136137
}
137138
}
138-
#[cfg(any(feature = "alloc", feature = "std"))]
139+
#[cfg(feature = "alloc")]
139140
pub(crate) mod sync {
140141
pub(crate) mod atomic {
141142
pub(crate) use core::sync::atomic::compiler_fence;
142143
pub(crate) use core::sync::atomic::fence;
143144
pub(crate) use core::sync::atomic::AtomicUsize;
144145
}
145-
#[cfg(not(crossbeam_no_atomic_cas))]
146146
pub(crate) use alloc::sync::Arc;
147147
}
148148

crossbeam-queue/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::env;
44

5-
include!("no_atomic_cas.rs");
5+
include!("no_atomic.rs");
66

77
// The rustc-cfg strings below are *not* public API. Please let us know by
88
// opening a GitHub issue if your build environment requires some way to enable
@@ -28,5 +28,5 @@ fn main() {
2828
println!("cargo:rustc-cfg=crossbeam_no_atomic_cas");
2929
}
3030

31-
println!("cargo:rerun-if-changed=no_atomic_cas.rs");
31+
println!("cargo:rerun-if-changed=no_atomic.rs");
3232
}

crossbeam-queue/no_atomic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../no_atomic.rs

crossbeam-queue/no_atomic_cas.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

crossbeam-skiplist/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::env;
44

5-
include!("no_atomic_cas.rs");
5+
include!("no_atomic.rs");
66

77
// The rustc-cfg strings below are *not* public API. Please let us know by
88
// opening a GitHub issue if your build environment requires some way to enable
@@ -28,5 +28,5 @@ fn main() {
2828
println!("cargo:rustc-cfg=crossbeam_no_atomic_cas");
2929
}
3030

31-
println!("cargo:rerun-if-changed=no_atomic_cas.rs");
31+
println!("cargo:rerun-if-changed=no_atomic.rs");
3232
}

crossbeam-skiplist/no_atomic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../no_atomic.rs

crossbeam-skiplist/no_atomic_cas.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

crossbeam-utils/build.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::env;
44

55
use autocfg::AutoCfg;
66

7-
include!("no_atomic_cas.rs");
7+
include!("no_atomic.rs");
88

99
// The rustc-cfg strings below are *not* public API. Please let us know by
1010
// opening a GitHub issue if your build environment requires some way to enable
@@ -29,6 +29,9 @@ fn main() {
2929
if NO_ATOMIC_CAS.contains(&&*target) {
3030
println!("cargo:rustc-cfg=crossbeam_no_atomic_cas");
3131
}
32+
if NO_ATOMIC.contains(&&*target) {
33+
println!("cargo:rustc-cfg=crossbeam_no_atomic");
34+
}
3235

3336
let cfg = match AutoCfg::new() {
3437
Ok(cfg) => cfg,
@@ -47,4 +50,6 @@ fn main() {
4750
cfg.emit_type_cfg("core::sync::atomic::AtomicU32", "has_atomic_u32");
4851
cfg.emit_type_cfg("core::sync::atomic::AtomicU64", "has_atomic_u64");
4952
cfg.emit_type_cfg("core::sync::atomic::AtomicU128", "has_atomic_u128");
53+
54+
println!("cargo:rerun-if-changed=no_atomic.rs");
5055
}

crossbeam-utils/no_atomic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../no_atomic.rs

crossbeam-utils/no_atomic_cas.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

crossbeam-utils/src/atomic/consume.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
22
use crate::primitive::sync::atomic::compiler_fence;
3+
#[cfg(not(crossbeam_no_atomic))]
34
use core::sync::atomic::Ordering;
45

56
/// Trait which allows reading from primitive atomic types with "consume" ordering.
@@ -25,6 +26,7 @@ pub trait AtomicConsume {
2526
fn load_consume(&self) -> Self::Val;
2627
}
2728

29+
#[cfg(not(crossbeam_no_atomic))]
2830
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
2931
macro_rules! impl_consume {
3032
() => {
@@ -37,6 +39,7 @@ macro_rules! impl_consume {
3739
};
3840
}
3941

42+
#[cfg(not(crossbeam_no_atomic))]
4043
#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
4144
macro_rules! impl_consume {
4245
() => {
@@ -49,12 +52,13 @@ macro_rules! impl_consume {
4952

5053
macro_rules! impl_atomic {
5154
($atomic:ident, $val:ty) => {
52-
impl AtomicConsume for ::core::sync::atomic::$atomic {
55+
#[cfg(not(crossbeam_no_atomic))]
56+
impl AtomicConsume for core::sync::atomic::$atomic {
5357
type Val = $val;
5458
impl_consume!();
5559
}
5660
#[cfg(crossbeam_loom)]
57-
impl AtomicConsume for ::loom::sync::atomic::$atomic {
61+
impl AtomicConsume for loom::sync::atomic::$atomic {
5862
type Val = $val;
5963
impl_consume!();
6064
}
@@ -63,7 +67,6 @@ macro_rules! impl_atomic {
6367

6468
impl_atomic!(AtomicBool, bool);
6569
impl_atomic!(AtomicUsize, usize);
66-
#[cfg(not(crossbeam_loom))]
6770
impl_atomic!(AtomicIsize, isize);
6871
#[cfg(has_atomic_u8)]
6972
impl_atomic!(AtomicU8, u8);
@@ -82,13 +85,14 @@ impl_atomic!(AtomicU64, u64);
8285
#[cfg(has_atomic_u64)]
8386
impl_atomic!(AtomicI64, i64);
8487

85-
impl<T> AtomicConsume for ::core::sync::atomic::AtomicPtr<T> {
88+
#[cfg(not(crossbeam_no_atomic))]
89+
impl<T> AtomicConsume for core::sync::atomic::AtomicPtr<T> {
8690
type Val = *mut T;
8791
impl_consume!();
8892
}
8993

9094
#[cfg(crossbeam_loom)]
91-
impl<T> AtomicConsume for ::loom::sync::atomic::AtomicPtr<T> {
95+
impl<T> AtomicConsume for loom::sync::atomic::AtomicPtr<T> {
9296
type Val = *mut T;
9397
impl_consume!();
9498
}

crossbeam-utils/src/atomic/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
//! * [`AtomicCell`], a thread-safe mutable memory location.
44
//! * [`AtomicConsume`], for reading from primitive atomic types with "consume" ordering.
55
6+
#[cfg(not(crossbeam_no_atomic_cas))]
67
#[cfg(not(crossbeam_loom))]
7-
use cfg_if::cfg_if;
8-
9-
#[cfg(not(crossbeam_loom))]
10-
cfg_if! {
8+
cfg_if::cfg_if! {
119
// Use "wide" sequence lock if the pointer width <= 32 for preventing its counter against wrap
1210
// around.
1311
//
@@ -25,8 +23,10 @@ cfg_if! {
2523
}
2624
}
2725

26+
#[cfg(not(crossbeam_no_atomic_cas))]
2827
mod atomic_cell;
2928
mod consume;
3029

30+
#[cfg(not(crossbeam_no_atomic_cas))]
3131
pub use self::atomic_cell::AtomicCell;
3232
pub use self::consume::AtomicConsume;

crossbeam-utils/src/lib.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,18 @@ mod primitive {
6969
// use [`core::hint::spin_loop`] instead.
7070
#[allow(deprecated)]
7171
pub(crate) use core::sync::atomic::spin_loop_hint;
72+
#[cfg(not(crossbeam_no_atomic))]
7273
pub(crate) use core::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize};
74+
#[cfg(not(crossbeam_no_atomic))]
7375
#[cfg(has_atomic_u16)]
7476
pub(crate) use core::sync::atomic::{AtomicI16, AtomicU16};
77+
#[cfg(not(crossbeam_no_atomic))]
7578
#[cfg(has_atomic_u32)]
7679
pub(crate) use core::sync::atomic::{AtomicI32, AtomicU32};
80+
#[cfg(not(crossbeam_no_atomic))]
7781
#[cfg(has_atomic_u64)]
7882
pub(crate) use core::sync::atomic::{AtomicI64, AtomicU64};
83+
#[cfg(not(crossbeam_no_atomic))]
7984
#[cfg(has_atomic_u8)]
8085
pub(crate) use core::sync::atomic::{AtomicI8, AtomicU8};
8186
}
@@ -85,15 +90,6 @@ mod primitive {
8590
}
8691
}
8792

88-
cfg_if! {
89-
if #[cfg(feature = "alloc")] {
90-
extern crate alloc;
91-
} else if #[cfg(feature = "std")] {
92-
extern crate std as alloc;
93-
}
94-
}
95-
96-
#[cfg(not(crossbeam_no_atomic_cas))]
9793
pub mod atomic;
9894

9995
mod cache_padded;

0 commit comments

Comments
 (0)