Skip to content

Commit 4ab579f

Browse files
committed
Support non-atomic targets by using portable-atomic
squashed commits from upstream tokio-rs#467 and rebased with updated deps
1 parent f7072c9 commit 4ab579f

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

.github/workflows/ci.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@ jobs:
128128
cargo build --target ${{ matrix.target }}
129129
if: matrix.target == 'wasm32-unknown-unknown'
130130

131+
# Build for no_std environment.
132+
no-std:
133+
runs-on: ubuntu-latest
134+
steps:
135+
- uses: actions/checkout@v4
136+
- name: Install Rust
137+
run: rustup update stable
138+
- name: Install cargo-hack
139+
run: cargo install cargo-hack
140+
# thumbv7m-none-eabi supports atomic CAS.
141+
# thumbv6m-none-eabi supports atomic, but not atomic CAS.
142+
- run: rustup target add thumbv7m-none-eabi
143+
- run: rustup target add thumbv6m-none-eabi
144+
# * --optional-deps is needed for serde feature
145+
# * --no-dev-deps is needed to avoid https://github.com/rust-lang/cargo/issues/4866
146+
- run: cargo hack build --target thumbv7m-none-eabi --feature-powerset --skip std,default --optional-deps --no-dev-deps
147+
# A sound way to provide atomic CAS on platforms without native atomic CAS is system-dependent.
148+
# portable-atomic provides major ways via cfgs and accepts user-defined implementations via critical-section feature.
149+
- run: cargo hack build --target thumbv6m-none-eabi --feature-powerset --skip std,default --optional-deps --no-dev-deps --features extra-platforms,portable-atomic/critical-section
150+
131151
# Sanitizers
132152
tsan:
133153
name: tsan

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ categories = ["network-programming", "data-structures"]
2121
[features]
2222
default = ["std"]
2323
std = []
24+
# Use portable-atomic crate to support platforms without atomic CAS.
25+
# See https://docs.rs/portable-atomic for more information.
26+
extra-platforms = ["portable-atomic"]
2427

2528
[dependencies]
2629
serde = { version = "1.0.60", optional = true, default-features = false, features = ["alloc"] }
30+
# Enable require-cas feature to provide a better error message if the end user forgets to use the cfg or feature.
31+
portable-atomic = { version = "1.10", optional = true, default-features = false, features = ["require-cas"] }
2732

2833
[dev-dependencies]
2934
serde_test = "1.0"

ci/test-stable.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ if [[ "${RUST_VERSION}" == "nightly"* ]]; then
1616
cargo check --benches
1717

1818
# Check minimal versions
19-
cargo clean
20-
cargo update -Zminimal-versions
19+
# Remove dev-dependencies from Cargo.toml to prevent the next `cargo update`
20+
# from determining minimal versions based on dev-dependencies.
21+
cargo hack --remove-dev-deps --workspace
22+
# Update Cargo.lock to minimal version dependencies.
23+
cargo update -Z minimal-versions
2124
cargo check --all-features
2225
fi

src/loom.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#[cfg(not(all(test, loom)))]
22
pub(crate) mod sync {
33
pub(crate) mod atomic {
4+
#[cfg(not(feature = "extra-platforms"))]
45
pub(crate) use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
6+
#[cfg(feature = "extra-platforms")]
7+
pub(crate) use portable_atomic::{AtomicPtr, AtomicUsize, Ordering};
58

69
pub(crate) trait AtomicMut<T> {
710
fn with_mut<F, R>(&mut self, f: F) -> R

0 commit comments

Comments
 (0)