Skip to content

Commit 1e0640f

Browse files
committed
Use portable-atomic and remove our own logic for now
Adopt the third idea of #573 (comment).
1 parent b02b594 commit 1e0640f

File tree

10 files changed

+17
-110
lines changed

10 files changed

+17
-110
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -113,40 +113,23 @@ jobs:
113113

114114
# Build for no_std environment.
115115
no-std:
116-
strategy:
117-
fail-fast: false
118-
matrix:
119-
# thumbv7m-none-eabi supports atomic CAS.
120-
# thumbv6m-none-eabi supports atomic, but not atomic CAS.
121-
# riscv32i-unknown-none-elf does not support atomic at all.
122-
target:
123-
- thumbv7m-none-eabi
124-
- thumbv6m-none-eabi
125-
- riscv32i-unknown-none-elf
126116
runs-on: ubuntu-latest
127117
steps:
128118
- uses: actions/checkout@v2
129119
- name: Install Rust
130120
run: rustup update stable
131121
- name: Install cargo-hack
132122
run: cargo install cargo-hack
133-
- run: rustup target add ${{ matrix.target }}
134-
# * --optional-deps is needed for serde feature
135-
# * --no-dev-deps is needed to avoid https://github.com/rust-lang/cargo/issues/4866
136-
- run: cargo hack build --target ${{ matrix.target }} --feature-powerset --skip std,default --optional-deps --no-dev-deps
137-
138-
# When this job failed, run ci/no_atomic_cas.sh and commit result changes.
139-
# TODO(taiki-e): Ideally, this should be automated using a bot that creates
140-
# PR when failed, but there is no bandwidth to implement it
141-
# right now...
142-
codegen:
143-
runs-on: ubuntu-latest
144-
steps:
145-
- uses: actions/checkout@v2
146-
- name: Install Rust
147-
run: rustup update nightly && rustup default nightly
148-
- run: ci/no_atomic_cas.sh
149-
- run: git diff --exit-code
123+
# thumbv7m-none-eabi supports atomic CAS.
124+
# thumbv6m-none-eabi supports atomic, but not atomic CAS.
125+
- run: rustup target add thumbv7m-none-eabi
126+
- run: rustup target add thumbv6m-none-eabi
127+
# * --optional-deps is needed for serde feature
128+
# * --no-dev-deps is needed to avoid https://github.com/rust-lang/cargo/issues/4866
129+
- run: cargo hack build --target thumbv7m-none-eabi --feature-powerset --skip std,default --optional-deps --no-dev-deps
130+
# A sound way to provide atomic CAS on platforms without native atomic CAS is system-dependent.
131+
# portable-atomic provides major ways via cfgs and accepts user-defined implementations via critical-section feature.
132+
- 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
150133

151134
# Sanitizers
152135
tsan:

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ edition = "2018"
2020
[features]
2121
default = ["std"]
2222
std = []
23+
# Use portable-atomic crate to support platforms without atomic CAS.
24+
# See https://docs.rs/portable-atomic for more information.
25+
extra-platforms = ["portable-atomic"]
2326

2427
[dependencies]
2528
serde = { version = "1.0.60", optional = true, default-features = false, features = ["alloc"] }
29+
portable-atomic = { version = "1", optional = true, default-features = false }
2630

2731
[dev-dependencies]
2832
serde_test = "1.0"

build.rs

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

ci/no_atomic_cas.sh

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

no_atomic_cas.rs

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

src/buf/buf_impl.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,6 @@ pub trait Buf {
11001100
/// let bytes = (&b"hello world"[..]).copy_to_bytes(5);
11011101
/// assert_eq!(&bytes[..], &b"hello"[..]);
11021102
/// ```
1103-
#[cfg(not(bytes_no_atomic_cas))]
11041103
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
11051104
use super::BufMut;
11061105

@@ -1325,7 +1324,6 @@ macro_rules! deref_forward_buf {
13251324
(**self).get_int_ne(nbytes)
13261325
}
13271326

1328-
#[cfg(not(bytes_no_atomic_cas))]
13291327
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
13301328
(**self).copy_to_bytes(len)
13311329
}

src/buf/chain.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ where
171171
n
172172
}
173173

174-
#[cfg(not(bytes_no_atomic_cas))]
175174
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
176175
let a_rem = self.a.remaining();
177176
if a_rem >= len {

src/buf/take.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ impl<T: Buf> Buf for Take<T> {
145145
self.limit -= cnt;
146146
}
147147

148-
#[cfg(not(bytes_no_atomic_cas))]
149148
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
150149
assert!(len <= self.remaining(), "`len` greater than remaining");
151150

src/lib.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,17 @@ extern crate std;
8484
pub mod buf;
8585
pub use crate::buf::{Buf, BufMut};
8686

87-
#[cfg(not(bytes_no_atomic_cas))]
8887
mod bytes;
89-
#[cfg(not(bytes_no_atomic_cas))]
9088
mod bytes_mut;
91-
#[cfg(not(bytes_no_atomic_cas))]
9289
mod fmt;
9390
mod loom;
94-
#[cfg(not(bytes_no_atomic_cas))]
9591
pub use crate::bytes::Bytes;
96-
#[cfg(not(bytes_no_atomic_cas))]
9792
pub use crate::bytes_mut::BytesMut;
9893

9994
// Optional Serde support
100-
#[cfg(not(bytes_no_atomic_cas))]
10195
#[cfg(feature = "serde")]
10296
mod serde;
10397

104-
#[cfg(not(bytes_no_atomic_cas))]
10598
#[inline(never)]
10699
#[cold]
107100
fn abort() -> ! {

src/loom.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
#[cfg(not(all(test, loom)))]
44
pub(crate) mod sync {
5-
#[cfg(not(bytes_no_atomic_cas))]
65
pub(crate) mod atomic {
6+
#[cfg(not(feature = "extra-platforms"))]
77
pub(crate) use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
8+
#[cfg(feature = "extra-platforms")]
9+
pub(crate) use portable_atomic::{AtomicPtr, AtomicUsize, Ordering};
810

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

0 commit comments

Comments
 (0)