Skip to content

Commit

Permalink
Enable portable-atomic's require-cas feature (#152)
Browse files Browse the repository at this point in the history
This provides a better error message if the end user forgets to use the
cfg or feature.

This also reverts the following commits (partially):
- 94cd9c5
- b1ee815
- 0e0ddca
- 3d061b8
  • Loading branch information
taiki-e committed May 11, 2023
1 parent 77754ce commit 5be251f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 114 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ jobs:
- name: Install Rust
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- name: Run Tests
run: cargo test --features _test_atomic --verbose
run: cargo test --verbose
- name: Build crate
run: cargo build --all --features _test_atomic --all-targets
run: cargo build --all --all-features --all-targets
- name: Catch missing feature flags
if: startsWith(matrix.rust, 'nightly')
run: cargo check -Z features=dev_dep
Expand All @@ -46,7 +46,7 @@ jobs:
- name: Install Rust
run: rustup update ${{ matrix.version }} && rustup default ${{ matrix.version }}
- name: Check MSRV
run: cargo check --all --features _test_atomic
run: cargo check --all --all-features

miri:
runs-on: ubuntu-latest
Expand Down
8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ rust-version = "1.38"

[dependencies]
lock_api_crate = { package = "lock_api", version = "0.4", optional = true }
portable-atomic = { version = "1", optional = true, default-features = false }
# 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"] }

[features]
default = ["lock_api", "mutex", "spin_mutex", "rwlock", "once", "lazy", "barrier"]
Expand Down Expand Up @@ -52,11 +53,6 @@ lock_api = ["lock_api_crate"]
# Enables std-only features such as yield-relaxing.
std = []

# An alias of all features *except* `portable_atomic`. Used only for internal crate tests.
# Do not use this feature, its removal is not considered a breaking change and its behaviour may change.
# If you're working on `spin` and you're adding a feature, please add it to this list.
_test_atomic = ["mutex", "spin_mutex", "ticket_mutex", "fair_mutex", "rwlock", "once", "lazy", "barrier", "lock_api", "std"]

# Use the portable_atomic crate to support platforms without native atomic operations.
# The `portable_atomic_unsafe_assume_single_core` cfg or `critical-section` feature
# of `portable-atomic` crate must also be set by the final binary crate.
Expand Down
120 changes: 15 additions & 105 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,82 +68,34 @@ extern crate portable_atomic;

#[cfg(not(feature = "portable_atomic"))]
use core::sync::atomic;
#[cfg(all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core))]
#[cfg(feature = "portable_atomic")]
use portable_atomic as atomic;

#[cfg(all(
feature = "portable_atomic",
not(portable_atomic_unsafe_assume_single_core)
))]
core::compile_error!("The feature \"portable_atomic\" requires the \"portable_atomic_unsafe_assume_single_core\" cfg flag to be enabled. See https://docs.rs/portable-atomic/latest/portable_atomic/#optional-cfg.");

#[cfg(all(
feature = "barrier",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "barrier")]
#[cfg_attr(docsrs, doc(cfg(feature = "barrier")))]
pub mod barrier;
#[cfg(all(
feature = "lazy",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "lazy")]
#[cfg_attr(docsrs, doc(cfg(feature = "lazy")))]
pub mod lazy;
#[cfg(all(
feature = "mutex",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "mutex")]
#[cfg_attr(docsrs, doc(cfg(feature = "mutex")))]
pub mod mutex;
#[cfg(all(
feature = "once",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "once")]
#[cfg_attr(docsrs, doc(cfg(feature = "once")))]
pub mod once;
pub mod relax;
#[cfg(all(
feature = "rwlock",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "rwlock")]
#[cfg_attr(docsrs, doc(cfg(feature = "rwlock")))]
pub mod rwlock;

#[cfg(all(
feature = "mutex",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "mutex")]
#[cfg_attr(docsrs, doc(cfg(feature = "mutex")))]
pub use mutex::MutexGuard;
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub use relax::Yield;
pub use relax::{RelaxStrategy, Spin};
#[cfg(all(
feature = "rwlock",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "rwlock")]
#[cfg_attr(docsrs, doc(cfg(feature = "rwlock")))]
pub use rwlock::RwLockReadGuard;

Expand All @@ -155,69 +107,39 @@ pub use rwlock::RwLockReadGuard;
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(
feature = "barrier",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "barrier")]
#[cfg_attr(docsrs, doc(cfg(feature = "barrier")))]
pub type Barrier = crate::barrier::Barrier;

/// A value which is initialized on the first access. See [`lazy::Lazy`] for documentation.
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(
feature = "lazy",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "lazy")]
#[cfg_attr(docsrs, doc(cfg(feature = "lazy")))]
pub type Lazy<T, F = fn() -> T> = crate::lazy::Lazy<T, F>;

/// A primitive that synchronizes the execution of multiple threads. See [`mutex::Mutex`] for documentation.
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(
feature = "mutex",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "mutex")]
#[cfg_attr(docsrs, doc(cfg(feature = "mutex")))]
pub type Mutex<T> = crate::mutex::Mutex<T>;

/// A primitive that provides lazy one-time initialization. See [`once::Once`] for documentation.
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(
feature = "once",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "once")]
#[cfg_attr(docsrs, doc(cfg(feature = "once")))]
pub type Once<T = ()> = crate::once::Once<T>;

/// A lock that provides data access to either one writer or many readers. See [`rwlock::RwLock`] for documentation.
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(
feature = "rwlock",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "rwlock")]
#[cfg_attr(docsrs, doc(cfg(feature = "rwlock")))]
pub type RwLock<T> = crate::rwlock::RwLock<T>;

Expand All @@ -226,27 +148,15 @@ pub type RwLock<T> = crate::rwlock::RwLock<T>;
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(
feature = "rwlock",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "rwlock")]
#[cfg_attr(docsrs, doc(cfg(feature = "rwlock")))]
pub type RwLockUpgradableGuard<'a, T> = crate::rwlock::RwLockUpgradableGuard<'a, T>;

/// A guard that provides mutable data access. See [`rwlock::RwLockWriteGuard`] for documentation.
///
/// A note for advanced users: this alias exists to avoid subtle type inference errors due to the default relax
/// strategy type parameter. If you need a non-default relax strategy, use the fully-qualified path.
#[cfg(all(
feature = "rwlock",
any(
not(feature = "portable_atomic"),
all(feature = "portable_atomic", portable_atomic_unsafe_assume_single_core)
)
))]
#[cfg(feature = "rwlock")]
#[cfg_attr(docsrs, doc(cfg(feature = "rwlock")))]
pub type RwLockWriteGuard<'a, T> = crate::rwlock::RwLockWriteGuard<'a, T>;

Expand Down

0 comments on commit 5be251f

Please sign in to comment.