Skip to content
/ bytes Public
forked from tokio-rs/bytes

Commit

Permalink
Use portable-atomic and remove our own logic for now
Browse files Browse the repository at this point in the history
Adopt the third idea of tokio-rs#573 (comment).
  • Loading branch information
taiki-e committed Mar 30, 2023
1 parent b02b594 commit 0a2e648
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 110 deletions.
37 changes: 10 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,40 +113,23 @@ jobs:

# Build for no_std environment.
no-std:
strategy:
fail-fast: false
matrix:
# thumbv7m-none-eabi supports atomic CAS.
# thumbv6m-none-eabi supports atomic, but not atomic CAS.
# riscv32i-unknown-none-elf does not support atomic at all.
target:
- thumbv7m-none-eabi
- thumbv6m-none-eabi
- riscv32i-unknown-none-elf
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Rust
run: rustup update stable
- name: Install cargo-hack
run: cargo install cargo-hack
- run: rustup target add ${{ matrix.target }}
# * --optional-deps is needed for serde feature
# * --no-dev-deps is needed to avoid https://github.com/rust-lang/cargo/issues/4866
- run: cargo hack build --target ${{ matrix.target }} --feature-powerset --skip std,default --optional-deps --no-dev-deps

# When this job failed, run ci/no_atomic_cas.sh and commit result changes.
# TODO(taiki-e): Ideally, this should be automated using a bot that creates
# PR when failed, but there is no bandwidth to implement it
# right now...
codegen:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Rust
run: rustup update nightly && rustup default nightly
- run: ci/no_atomic_cas.sh
- run: git diff --exit-code
# thumbv7m-none-eabi supports atomic CAS.
# thumbv6m-none-eabi supports atomic, but not atomic CAS.
- run: rustup target add thumbv7m-none-eabi
- run: rustup target add thumbv6m-none-eabi
# * --optional-deps is needed for serde feature
# * --no-dev-deps is needed to avoid https://github.com/rust-lang/cargo/issues/4866
- run: cargo hack build --target thumbv7m-none-eabi --feature-powerset --skip std,default --optional-deps --no-dev-deps
# A sound way to provide atomic CAS on platforms without native atomic CAS is system-dependent.
# portable-atomic provides major ways via cfgs and accepts user-defined implementations via critical-section features.
- 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

# Sanitizers
tsan:
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ edition = "2018"
[features]
default = ["std"]
std = []
# Use portable-atomic crate to support platforms without atomic CAS.
# See https://docs.rs/portable-atomic for more information.
extra-platforms = ["portable-atomic"]

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

[dev-dependencies]
serde_test = "1.0"
Expand Down
31 changes: 0 additions & 31 deletions build.rs

This file was deleted.

27 changes: 0 additions & 27 deletions ci/no_atomic_cas.sh

This file was deleted.

13 changes: 0 additions & 13 deletions no_atomic_cas.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/buf/buf_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,6 @@ pub trait Buf {
/// let bytes = (&b"hello world"[..]).copy_to_bytes(5);
/// assert_eq!(&bytes[..], &b"hello"[..]);
/// ```
#[cfg(not(bytes_no_atomic_cas))]
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
use super::BufMut;

Expand Down Expand Up @@ -1325,7 +1324,6 @@ macro_rules! deref_forward_buf {
(**self).get_int_ne(nbytes)
}

#[cfg(not(bytes_no_atomic_cas))]
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
(**self).copy_to_bytes(len)
}
Expand Down
1 change: 0 additions & 1 deletion src/buf/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ where
n
}

#[cfg(not(bytes_no_atomic_cas))]
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
let a_rem = self.a.remaining();
if a_rem >= len {
Expand Down
1 change: 0 additions & 1 deletion src/buf/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ impl<T: Buf> Buf for Take<T> {
self.limit -= cnt;
}

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

Expand Down
7 changes: 0 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,17 @@ extern crate std;
pub mod buf;
pub use crate::buf::{Buf, BufMut};

#[cfg(not(bytes_no_atomic_cas))]
mod bytes;
#[cfg(not(bytes_no_atomic_cas))]
mod bytes_mut;
#[cfg(not(bytes_no_atomic_cas))]
mod fmt;
mod loom;
#[cfg(not(bytes_no_atomic_cas))]
pub use crate::bytes::Bytes;
#[cfg(not(bytes_no_atomic_cas))]
pub use crate::bytes_mut::BytesMut;

// Optional Serde support
#[cfg(not(bytes_no_atomic_cas))]
#[cfg(feature = "serde")]
mod serde;

#[cfg(not(bytes_no_atomic_cas))]
#[inline(never)]
#[cold]
fn abort() -> ! {
Expand Down
4 changes: 3 additions & 1 deletion src/loom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

#[cfg(not(all(test, loom)))]
pub(crate) mod sync {
#[cfg(not(bytes_no_atomic_cas))]
pub(crate) mod atomic {
#[cfg(not(feature = "extra-platforms"))]
pub(crate) use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
#[cfg(feature = "extra-platforms")]
pub(crate) use portable_atomic::{AtomicPtr, AtomicUsize, Ordering};

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

0 comments on commit 0a2e648

Please sign in to comment.