diff --git a/Cargo.toml b/Cargo.toml index 94b63b6e5..4caf9f040 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,8 +47,6 @@ crossbeam-epoch = { version = "0.9.17", path = "crossbeam-epoch", default-featur crossbeam-queue = { version = "0.3.10", path = "crossbeam-queue", default-features = false, optional = true } crossbeam-utils = { version = "0.8.18", path = "crossbeam-utils", default-features = false } -cfg-if = "1" - [dev-dependencies] rand = "0.8" diff --git a/crossbeam-channel/Cargo.toml b/crossbeam-channel/Cargo.toml index 804600ddd..38684ddca 100644 --- a/crossbeam-channel/Cargo.toml +++ b/crossbeam-channel/Cargo.toml @@ -26,8 +26,6 @@ std = ["crossbeam-utils/std"] [dependencies] crossbeam-utils = { version = "0.8.18", path = "../crossbeam-utils", default-features = false } -cfg-if = "1" - [dev-dependencies] num_cpus = "1.13.0" rand = "0.8" diff --git a/crossbeam-channel/src/lib.rs b/crossbeam-channel/src/lib.rs index cc1ef112f..0bb98a282 100644 --- a/crossbeam-channel/src/lib.rs +++ b/crossbeam-channel/src/lib.rs @@ -336,36 +336,40 @@ )] #![cfg_attr(not(feature = "std"), no_std)] -use cfg_if::cfg_if; +#[cfg(feature = "std")] +mod channel; +#[cfg(feature = "std")] +mod context; +#[cfg(feature = "std")] +mod counter; +#[cfg(feature = "std")] +mod err; +#[cfg(feature = "std")] +mod flavors; +#[cfg(feature = "std")] +mod select; +#[cfg(feature = "std")] +mod select_macro; +#[cfg(feature = "std")] +mod utils; +#[cfg(feature = "std")] +mod waker; -cfg_if! { - if #[cfg(feature = "std")] { - mod channel; - mod context; - mod counter; - mod err; - mod flavors; - mod select; - mod select_macro; - mod utils; - mod waker; - - /// Crate internals used by the `select!` macro. - #[doc(hidden)] - pub mod internal { - pub use crate::select::SelectHandle; - pub use crate::select::{select, select_timeout, try_select}; - } - - pub use crate::channel::{after, at, never, tick}; - pub use crate::channel::{bounded, unbounded}; - pub use crate::channel::{IntoIter, Iter, TryIter}; - pub use crate::channel::{Receiver, Sender}; - - pub use crate::select::{Select, SelectedOperation}; - - pub use crate::err::{ReadyTimeoutError, SelectTimeoutError, TryReadyError, TrySelectError}; - pub use crate::err::{RecvError, RecvTimeoutError, TryRecvError}; - pub use crate::err::{SendError, SendTimeoutError, TrySendError}; - } +/// Crate internals used by the `select!` macro. +#[doc(hidden)] +#[cfg(feature = "std")] +pub mod internal { + pub use crate::select::{select, select_timeout, try_select, SelectHandle}; } + +#[cfg(feature = "std")] +pub use crate::{ + channel::{ + after, at, bounded, never, tick, unbounded, IntoIter, Iter, Receiver, Sender, TryIter, + }, + err::{ + ReadyTimeoutError, RecvError, RecvTimeoutError, SelectTimeoutError, SendError, + SendTimeoutError, TryReadyError, TryRecvError, TrySelectError, TrySendError, + }, + select::{Select, SelectedOperation}, +}; diff --git a/crossbeam-deque/Cargo.toml b/crossbeam-deque/Cargo.toml index d20ee0233..2bf525df1 100644 --- a/crossbeam-deque/Cargo.toml +++ b/crossbeam-deque/Cargo.toml @@ -27,7 +27,5 @@ std = ["crossbeam-epoch/std", "crossbeam-utils/std"] crossbeam-epoch = { version = "0.9.17", path = "../crossbeam-epoch", default-features = false } crossbeam-utils = { version = "0.8.18", path = "../crossbeam-utils", default-features = false } -cfg-if = "1" - [dev-dependencies] rand = "0.8" diff --git a/crossbeam-deque/src/deque.rs b/crossbeam-deque/src/deque.rs index ff19c7a75..f02b03d4d 100644 --- a/crossbeam-deque/src/deque.rs +++ b/crossbeam-deque/src/deque.rs @@ -8,8 +8,8 @@ use std::slice; use std::sync::atomic::{self, AtomicIsize, AtomicPtr, AtomicUsize, Ordering}; use std::sync::Arc; -use crate::epoch::{self, Atomic, Owned}; -use crate::utils::{Backoff, CachePadded}; +use crossbeam_epoch::{self as epoch, Atomic, Owned}; +use crossbeam_utils::{Backoff, CachePadded}; // Minimum buffer capacity. const MIN_CAP: usize = 64; diff --git a/crossbeam-deque/src/lib.rs b/crossbeam-deque/src/lib.rs index 16bc728b3..a5745d904 100644 --- a/crossbeam-deque/src/lib.rs +++ b/crossbeam-deque/src/lib.rs @@ -97,14 +97,7 @@ )] #![cfg_attr(not(feature = "std"), no_std)] -use cfg_if::cfg_if; - -cfg_if! { - if #[cfg(feature = "std")] { - use crossbeam_epoch as epoch; - use crossbeam_utils as utils; - - mod deque; - pub use crate::deque::{Injector, Steal, Stealer, Worker}; - } -} +#[cfg(feature = "std")] +mod deque; +#[cfg(feature = "std")] +pub use crate::deque::{Injector, Steal, Stealer, Worker}; diff --git a/crossbeam-epoch/Cargo.toml b/crossbeam-epoch/Cargo.toml index 2943561b0..e2b339d31 100644 --- a/crossbeam-epoch/Cargo.toml +++ b/crossbeam-epoch/Cargo.toml @@ -45,8 +45,6 @@ loom = ["loom-crate", "crossbeam-utils/loom"] [dependencies] crossbeam-utils = { version = "0.8.18", path = "../crossbeam-utils", default-features = false } -cfg-if = "1" - # Enable the use of loom for concurrency testing. # # NOTE: This feature is outside of the normal semver guarantees and minor or diff --git a/crossbeam-epoch/src/lib.rs b/crossbeam-epoch/src/lib.rs index 96374edde..fd4d74bed 100644 --- a/crossbeam-epoch/src/lib.rs +++ b/crossbeam-epoch/src/lib.rs @@ -66,8 +66,6 @@ #[cfg(crossbeam_loom)] extern crate loom_crate as loom; -use cfg_if::cfg_if; - #[cfg(crossbeam_loom)] #[allow(unused_imports, dead_code)] mod primitive { @@ -134,34 +132,35 @@ mod primitive { pub(crate) use std::thread_local; } -#[cfg(target_has_atomic = "ptr")] -cfg_if! { - if #[cfg(feature = "alloc")] { - extern crate alloc; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +extern crate alloc; - mod atomic; - mod collector; - mod deferred; - mod epoch; - mod guard; - mod internal; - mod sync; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +mod atomic; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +mod collector; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +mod deferred; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +mod epoch; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +mod guard; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +mod internal; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +mod sync; - pub use self::atomic::{ - Pointable, Atomic, CompareExchangeError, - Owned, Pointer, Shared, - }; - pub use self::collector::{Collector, LocalHandle}; - pub use self::guard::{unprotected, Guard}; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +#[allow(deprecated)] +pub use crate::atomic::{CompareAndSetError, CompareAndSetOrdering}; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +pub use crate::{ + atomic::{Atomic, CompareExchangeError, Owned, Pointable, Pointer, Shared}, + collector::{Collector, LocalHandle}, + guard::{unprotected, Guard}, +}; - #[allow(deprecated)] - pub use self::atomic::{CompareAndSetError, CompareAndSetOrdering}; - } -} - -cfg_if! { - if #[cfg(feature = "std")] { - mod default; - pub use self::default::{default_collector, is_pinned, pin}; - } -} +#[cfg(feature = "std")] +mod default; +#[cfg(feature = "std")] +pub use crate::default::{default_collector, is_pinned, pin}; diff --git a/crossbeam-queue/Cargo.toml b/crossbeam-queue/Cargo.toml index ad211d996..fc71b8280 100644 --- a/crossbeam-queue/Cargo.toml +++ b/crossbeam-queue/Cargo.toml @@ -39,7 +39,5 @@ nightly = ["crossbeam-utils/nightly"] [dependencies] crossbeam-utils = { version = "0.8.18", path = "../crossbeam-utils", default-features = false } -cfg-if = "1" - [dev-dependencies] rand = "0.8" diff --git a/crossbeam-queue/src/lib.rs b/crossbeam-queue/src/lib.rs index 36687282a..4d95f54e5 100644 --- a/crossbeam-queue/src/lib.rs +++ b/crossbeam-queue/src/lib.rs @@ -20,15 +20,13 @@ )] #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(target_has_atomic = "ptr")] -cfg_if::cfg_if! { - if #[cfg(feature = "alloc")] { - extern crate alloc; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +extern crate alloc; - mod array_queue; - mod seg_queue; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +mod array_queue; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +mod seg_queue; - pub use self::array_queue::ArrayQueue; - pub use self::seg_queue::SegQueue; - } -} +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +pub use crate::{array_queue::ArrayQueue, seg_queue::SegQueue}; diff --git a/crossbeam-skiplist/Cargo.toml b/crossbeam-skiplist/Cargo.toml index 1e8d86a42..62feb350d 100644 --- a/crossbeam-skiplist/Cargo.toml +++ b/crossbeam-skiplist/Cargo.toml @@ -31,7 +31,5 @@ alloc = ["crossbeam-epoch/alloc"] crossbeam-epoch = { version = "0.9.17", path = "../crossbeam-epoch", default-features = false } crossbeam-utils = { version = "0.8.18", path = "../crossbeam-utils", default-features = false } -cfg-if = "1" - [dev-dependencies] rand = "0.8" diff --git a/crossbeam-skiplist/src/base.rs b/crossbeam-skiplist/src/base.rs index 44ee07332..efa0a1c7f 100644 --- a/crossbeam-skiplist/src/base.rs +++ b/crossbeam-skiplist/src/base.rs @@ -10,8 +10,8 @@ use core::ops::{Bound, Deref, Index, RangeBounds}; use core::ptr; use core::sync::atomic::{fence, AtomicUsize, Ordering}; -use crate::epoch::{self, Atomic, Collector, Guard, Shared}; -use crate::utils::CachePadded; +use crossbeam_epoch::{self as epoch, Atomic, Collector, Guard, Shared}; +use crossbeam_utils::CachePadded; /// Number of bits needed to store height. const HEIGHT_BITS: usize = 5; diff --git a/crossbeam-skiplist/src/lib.rs b/crossbeam-skiplist/src/lib.rs index 194823800..2abbc1f84 100644 --- a/crossbeam-skiplist/src/lib.rs +++ b/crossbeam-skiplist/src/lib.rs @@ -243,30 +243,20 @@ )] #![cfg_attr(not(feature = "std"), no_std)] -use cfg_if::cfg_if; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +extern crate alloc; -#[cfg(target_has_atomic = "ptr")] -cfg_if! { - if #[cfg(feature = "alloc")] { - extern crate alloc; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +pub mod base; +#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))] +#[doc(inline)] +pub use crate::base::SkipList; - use crossbeam_epoch as epoch; - use crossbeam_utils as utils; +#[cfg(feature = "std")] +pub mod map; +#[cfg(feature = "std")] +pub mod set; - pub mod base; - #[doc(inline)] - pub use crate::base::SkipList; - } -} - -cfg_if! { - if #[cfg(feature = "std")] { - pub mod map; - #[doc(inline)] - pub use crate::map::SkipMap; - - pub mod set; - #[doc(inline)] - pub use crate::set::SkipSet; - } -} +#[cfg(feature = "std")] +#[doc(inline)] +pub use crate::{map::SkipMap, set::SkipSet}; diff --git a/crossbeam-skiplist/src/map.rs b/crossbeam-skiplist/src/map.rs index 3b60eec9c..b1fa21e82 100644 --- a/crossbeam-skiplist/src/map.rs +++ b/crossbeam-skiplist/src/map.rs @@ -7,7 +7,7 @@ use std::ops::{Bound, RangeBounds}; use std::ptr; use crate::base::{self, try_pin_loop}; -use crate::epoch; +use crossbeam_epoch as epoch; /// An ordered map based on a lock-free skip list. /// diff --git a/crossbeam-utils/Cargo.toml b/crossbeam-utils/Cargo.toml index 66d8201c4..b4267d471 100644 --- a/crossbeam-utils/Cargo.toml +++ b/crossbeam-utils/Cargo.toml @@ -31,7 +31,6 @@ std = [] nightly = [] [dependencies] -cfg-if = "1" # Enable the use of loom for concurrency testing. # diff --git a/crossbeam-utils/src/atomic/mod.rs b/crossbeam-utils/src/atomic/mod.rs index 4332cc3bd..7b39fe474 100644 --- a/crossbeam-utils/src/atomic/mod.rs +++ b/crossbeam-utils/src/atomic/mod.rs @@ -5,23 +5,18 @@ #[cfg(target_has_atomic = "ptr")] #[cfg(not(crossbeam_loom))] -cfg_if::cfg_if! { - // Use "wide" sequence lock if the pointer width <= 32 for preventing its counter against wrap - // around. - // - // We are ignoring too wide architectures (pointer width >= 256), since such a system will not - // appear in a conceivable future. - // - // In narrow architectures (pointer width <= 16), the counter is still <= 32-bit and may be - // vulnerable to wrap around. But it's mostly okay, since in such a primitive hardware, the - // counter will not be increased that fast. - if #[cfg(any(target_pointer_width = "64", target_pointer_width = "128"))] { - mod seq_lock; - } else { - #[path = "seq_lock_wide.rs"] - mod seq_lock; - } -} +// Use "wide" sequence lock if the pointer width <= 32 for preventing its counter against wrap +// around. +// +// In narrow architectures (pointer width <= 16), the counter is still <= 32-bit and may be +// vulnerable to wrap around. But it's mostly okay, since in such a primitive hardware, the +// counter will not be increased that fast. +// Note that Rust (and C99) pointers must be at least 16-bits: https://github.com/rust-lang/rust/pull/49305 +#[cfg_attr( + any(target_pointer_width = "16", target_pointer_width = "32"), + path = "seq_lock_wide.rs" +)] +mod seq_lock; #[cfg(target_has_atomic = "ptr")] // We cannot provide AtomicCell under cfg(crossbeam_loom) because loom's atomic @@ -29,9 +24,9 @@ cfg_if::cfg_if! { // TODO: The latest loom supports fences, so fallback using seqlock may be available. #[cfg(not(crossbeam_loom))] mod atomic_cell; -mod consume; - #[cfg(target_has_atomic = "ptr")] #[cfg(not(crossbeam_loom))] -pub use self::atomic_cell::AtomicCell; -pub use self::consume::AtomicConsume; +pub use atomic_cell::AtomicCell; + +mod consume; +pub use consume::AtomicConsume; diff --git a/crossbeam-utils/src/lib.rs b/crossbeam-utils/src/lib.rs index 6ab748f34..7206c1ee2 100644 --- a/crossbeam-utils/src/lib.rs +++ b/crossbeam-utils/src/lib.rs @@ -99,13 +99,9 @@ pub use crate::cache_padded::CachePadded; mod backoff; pub use crate::backoff::Backoff; -use cfg_if::cfg_if; +#[cfg(feature = "std")] +pub mod sync; -cfg_if! { - if #[cfg(feature = "std")] { - pub mod sync; - - #[cfg(not(crossbeam_loom))] - pub mod thread; - } -} +#[cfg(feature = "std")] +#[cfg(not(crossbeam_loom))] +pub mod thread; diff --git a/crossbeam-utils/src/thread.rs b/crossbeam-utils/src/thread.rs index 2d4805e12..b2e063ae6 100644 --- a/crossbeam-utils/src/thread.rs +++ b/crossbeam-utils/src/thread.rs @@ -120,7 +120,6 @@ use std::sync::{Arc, Mutex}; use std::thread; use crate::sync::WaitGroup; -use cfg_if::cfg_if; type SharedVec = Arc>>; type SharedOption = Arc>>; @@ -562,37 +561,42 @@ impl ScopedJoinHandle<'_, T> { } } -cfg_if! { - if #[cfg(unix)] { - use std::os::unix::thread::{JoinHandleExt, RawPthread}; - - impl JoinHandleExt for ScopedJoinHandle<'_, T> { - fn as_pthread_t(&self) -> RawPthread { - // Borrow the handle. The handle will surely be available because the root scope waits - // for nested scopes before joining remaining threads. - let handle = self.handle.lock().unwrap(); - handle.as_ref().unwrap().as_pthread_t() - } - fn into_pthread_t(self) -> RawPthread { - self.as_pthread_t() - } +/// Unix-specific extensions. +#[cfg(unix)] +mod unix { + use super::ScopedJoinHandle; + use std::os::unix::thread::{JoinHandleExt, RawPthread}; + + impl JoinHandleExt for ScopedJoinHandle<'_, T> { + fn as_pthread_t(&self) -> RawPthread { + // Borrow the handle. The handle will surely be available because the root scope waits + // for nested scopes before joining remaining threads. + let handle = self.handle.lock().unwrap(); + handle.as_ref().unwrap().as_pthread_t() } - } else if #[cfg(windows)] { - use std::os::windows::io::{AsRawHandle, IntoRawHandle, RawHandle}; - - impl AsRawHandle for ScopedJoinHandle<'_, T> { - fn as_raw_handle(&self) -> RawHandle { - // Borrow the handle. The handle will surely be available because the root scope waits - // for nested scopes before joining remaining threads. - let handle = self.handle.lock().unwrap(); - handle.as_ref().unwrap().as_raw_handle() - } + fn into_pthread_t(self) -> RawPthread { + self.as_pthread_t() } + } +} +/// Windows-specific extensions. +#[cfg(windows)] +mod windows { + use super::ScopedJoinHandle; + use std::os::windows::io::{AsRawHandle, IntoRawHandle, RawHandle}; + + impl AsRawHandle for ScopedJoinHandle<'_, T> { + fn as_raw_handle(&self) -> RawHandle { + // Borrow the handle. The handle will surely be available because the root scope waits + // for nested scopes before joining remaining threads. + let handle = self.handle.lock().unwrap(); + handle.as_ref().unwrap().as_raw_handle() + } + } - impl IntoRawHandle for ScopedJoinHandle<'_, T> { - fn into_raw_handle(self) -> RawHandle { - self.as_raw_handle() - } + impl IntoRawHandle for ScopedJoinHandle<'_, T> { + fn into_raw_handle(self) -> RawHandle { + self.as_raw_handle() } } } diff --git a/src/lib.rs b/src/lib.rs index 9bc3ec111..1230e5dce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,32 +65,17 @@ pub mod utils { pub use crossbeam_utils::CachePadded; } -use cfg_if::cfg_if; +#[cfg(feature = "alloc")] +#[doc(inline)] +pub use {crossbeam_epoch as epoch, crossbeam_queue as queue}; -cfg_if! { - if #[cfg(feature = "alloc")] { - #[doc(inline)] - pub use crossbeam_epoch as epoch; +#[cfg(feature = "std")] +#[doc(inline)] +pub use { + crossbeam_channel as channel, crossbeam_channel::select, crossbeam_deque as deque, + crossbeam_utils::sync, +}; - #[doc(inline)] - pub use crossbeam_queue as queue; - } -} - -cfg_if! { - if #[cfg(feature = "std")] { - #[doc(inline)] - pub use crossbeam_deque as deque; - - #[doc(inline)] - pub use crossbeam_channel as channel; - pub use crossbeam_channel::select; - - pub use crossbeam_utils::sync; - - #[cfg(not(crossbeam_loom))] - pub use crossbeam_utils::thread; - #[cfg(not(crossbeam_loom))] - pub use crossbeam_utils::thread::scope; - } -} +#[cfg(feature = "std")] +#[cfg(not(crossbeam_loom))] +pub use crossbeam_utils::thread::{self, scope};