Skip to content

Commit

Permalink
Support non-atomic targets
Browse files Browse the repository at this point in the history
Remove uses of unstable feature(cfg_target_has_atomic)

fix

setup codegen ci

add comment

update list

Use portable-atomic and remove our own logic for now

Adopt the third idea of tokio-rs#573 (comment).

Enable portable-atomic's require-cas feature

This provides a better error message if the end user forgets to use the
cfg or feature.
  • Loading branch information
taiki-e authored and XLPhere committed Jun 1, 2024
1 parent fa1daac commit e0140d5
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 6 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ jobs:
cargo build --target ${{ matrix.target }}
if: matrix.target == 'wasm32-unknown-unknown'

# Build for no_std environment.
no-std:
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
# 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 feature.
- 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:
name: tsan
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ categories = ["network-programming", "data-structures"]
[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"] }
# Enable require-cas feature to provide a better error message if the end user forgets to use the cfg or feature.
portable-atomic = { version = "1.3", optional = true, default-features = false, features = ["require-cas"] }

[dev-dependencies]
serde_test = "1.0"
Expand Down
7 changes: 5 additions & 2 deletions ci/test-stable.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ if [[ "${RUST_VERSION}" == "nightly"* ]]; then
cargo check --benches

# Check minimal versions
cargo clean
cargo update -Zminimal-versions
# Remove dev-dependencies from Cargo.toml to prevent the next `cargo update`
# from determining minimal versions based on dev-dependencies.
cargo hack --remove-dev-deps --workspace
# Update Cargo.lock to minimal version dependencies.
cargo update -Z minimal-versions
cargo check --all-features
fi
4 changes: 2 additions & 2 deletions src/buf/chain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::buf::{IntoIter, UninitSlice};
use crate::{Buf, BufMut, Bytes};
use crate::{Buf, BufMut};

#[cfg(feature = "std")]
use std::io::IoSlice;
Expand Down Expand Up @@ -169,7 +169,7 @@ where
n
}

fn copy_to_bytes(&mut self, len: usize) -> Bytes {
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
let a_rem = self.a.remaining();
if a_rem >= len {
self.a.copy_to_bytes(len)
Expand Down
4 changes: 2 additions & 2 deletions src/buf/take.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Buf, Bytes};
use crate::Buf;

use core::cmp;

Expand Down Expand Up @@ -145,7 +145,7 @@ impl<T: Buf> Buf for Take<T> {
self.limit -= cnt;
}

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

let r = self.inner.copy_to_bytes(len);
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
))]
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(bytes_unstable, feature(cfg_target_has_atomic))]

//! Provides abstractions for working with bytes.
//!
Expand Down
3 changes: 3 additions & 0 deletions src/loom.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#[cfg(not(all(test, loom)))]
pub(crate) mod sync {
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 e0140d5

Please sign in to comment.