From a6253ed05a1e0d14bc64915f5937c29092df9497 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Mon, 4 Nov 2019 22:22:40 -0800 Subject: [PATCH] chore: unify all mocked `loom` files (#1732) When the crates were merged, each component kept its own `loom` file containing mocked types it needed. This patch unifies them all in one location. --- tokio/src/executor/blocking/builder.rs | 2 +- tokio/src/executor/blocking/mod.rs | 4 +- tokio/src/executor/loom/mod.rs | 30 ------------ tokio/src/executor/mod.rs | 2 - tokio/src/executor/park/thread.rs | 6 +-- tokio/src/executor/task/core.rs | 4 +- tokio/src/executor/task/harness.rs | 4 +- tokio/src/executor/task/join.rs | 2 +- tokio/src/executor/task/raw.rs | 2 +- tokio/src/executor/task/stack.rs | 2 +- tokio/src/executor/task/state.rs | 10 ++-- tokio/src/executor/thread_pool/builder.rs | 7 ++- tokio/src/executor/thread_pool/current.rs | 2 +- tokio/src/executor/thread_pool/idle.rs | 4 +- .../src/executor/thread_pool/queue/global.rs | 4 +- .../src/executor/thread_pool/queue/inject.rs | 2 +- tokio/src/executor/thread_pool/queue/local.rs | 4 +- tokio/src/executor/thread_pool/queue/mod.rs | 2 +- .../src/executor/thread_pool/queue/worker.rs | 2 +- tokio/src/executor/thread_pool/set.rs | 4 +- tokio/src/executor/thread_pool/shutdown.rs | 2 +- tokio/src/executor/thread_pool/spawner.rs | 2 +- .../executor/thread_pool/tests/loom_pool.rs | 9 ++-- tokio/src/executor/thread_pool/worker.rs | 2 +- tokio/src/lib.rs | 2 +- tokio/src/loom.rs | 45 ----------------- tokio/src/loom/mocked.rs | 13 +++++ tokio/src/loom/mod.rs | 12 +++++ tokio/src/loom/std/alloc.rs | 18 +++++++ .../src/{executor => }/loom/std/atomic_u32.rs | 0 .../{executor => }/loom/std/atomic_usize.rs | 19 +++++--- .../{executor => }/loom/std/causal_cell.rs | 2 +- tokio/src/{executor => }/loom/std/mod.rs | 45 ++++------------- .../net/driver/reactor/dispatch/page/mod.rs | 2 +- .../reactor/dispatch/page/scheduled_io.rs | 7 ++- .../net/driver/reactor/dispatch/page/stack.rs | 6 ++- .../driver/reactor/dispatch/sharded_slab.rs | 2 +- tokio/src/net/driver/reactor/mod.rs | 6 ++- tokio/src/runtime/builder.rs | 2 +- tokio/src/sync/loom.rs | 48 ------------------- tokio/src/sync/mod.rs | 2 - tokio/src/sync/mpsc/block.rs | 4 +- tokio/src/sync/mpsc/chan.rs | 6 +-- tokio/src/sync/mpsc/list.rs | 2 +- tokio/src/sync/mpsc/unbounded.rs | 2 +- tokio/src/sync/oneshot.rs | 4 +- tokio/src/sync/semaphore.rs | 8 ++-- tokio/src/sync/task/atomic_waker.rs | 4 +- 48 files changed, 134 insertions(+), 241 deletions(-) delete mode 100644 tokio/src/executor/loom/mod.rs delete mode 100644 tokio/src/loom.rs create mode 100644 tokio/src/loom/mocked.rs create mode 100644 tokio/src/loom/mod.rs create mode 100644 tokio/src/loom/std/alloc.rs rename tokio/src/{executor => }/loom/std/atomic_u32.rs (100%) rename tokio/src/{executor => }/loom/std/atomic_usize.rs (81%) rename tokio/src/{executor => }/loom/std/causal_cell.rs (96%) rename tokio/src/{executor => }/loom/std/mod.rs (52%) delete mode 100644 tokio/src/sync/loom.rs diff --git a/tokio/src/executor/blocking/builder.rs b/tokio/src/executor/blocking/builder.rs index 05ea28c2b..e755ae238 100644 --- a/tokio/src/executor/blocking/builder.rs +++ b/tokio/src/executor/blocking/builder.rs @@ -1,5 +1,5 @@ use crate::executor::blocking::Pool; -use crate::executor::loom::thread; +use crate::loom::thread; use std::usize; diff --git a/tokio/src/executor/blocking/mod.rs b/tokio/src/executor/blocking/mod.rs index 2947062b1..d105fe22a 100644 --- a/tokio/src/executor/blocking/mod.rs +++ b/tokio/src/executor/blocking/mod.rs @@ -1,7 +1,7 @@ //! Thread pool for blocking operations -use crate::executor::loom::sync::{Arc, Condvar, Mutex}; -use crate::executor::loom::thread; +use crate::loom::sync::{Arc, Condvar, Mutex}; +use crate::loom::thread; #[cfg(feature = "blocking")] use crate::sync::oneshot; diff --git a/tokio/src/executor/loom/mod.rs b/tokio/src/executor/loom/mod.rs deleted file mode 100644 index f6f9d23c5..000000000 --- a/tokio/src/executor/loom/mod.rs +++ /dev/null @@ -1,30 +0,0 @@ -//! Stub out the necessary APIs to model with loom. - -#[cfg(not(all(test, loom)))] -pub(crate) mod std; - -#[cfg(all(test, loom))] -pub(crate) mod std { - pub(crate) use loom::{alloc, cell, sync, thread}; - - pub(crate) mod rand { - pub(crate) fn seed() -> u64 { - 1 - } - } - - pub(crate) mod sys { - pub(crate) fn num_cpus() -> usize { - 2 - } - } -} - -#[cfg(feature = "rt-full")] -pub(crate) use self::std::rand; -#[cfg(any(feature = "blocking", feature = "rt-current-thread"))] -pub(crate) use self::std::sync; -#[cfg(any(feature = "blocking", feature = "rt-full"))] -pub(crate) use self::std::thread; -#[cfg(feature = "rt-current-thread")] -pub(crate) use self::std::{alloc, cell, sys}; diff --git a/tokio/src/executor/mod.rs b/tokio/src/executor/mod.rs index fa2b29e4f..6134ea3b8 100644 --- a/tokio/src/executor/mod.rs +++ b/tokio/src/executor/mod.rs @@ -52,8 +52,6 @@ pub(crate) use self::enter::enter; mod global; pub use self::global::spawn; -pub(crate) mod loom; - pub mod park; #[cfg(feature = "rt-current-thread")] diff --git a/tokio/src/executor/park/thread.rs b/tokio/src/executor/park/thread.rs index c48b418d3..880dfb494 100644 --- a/tokio/src/executor/park/thread.rs +++ b/tokio/src/executor/park/thread.rs @@ -1,6 +1,6 @@ -use crate::executor::loom::sync::atomic::AtomicUsize; -use crate::executor::loom::sync::{Arc, Condvar, Mutex}; use crate::executor::park::{Park, Unpark}; +use crate::loom::sync::atomic::AtomicUsize; +use crate::loom::sync::{Arc, Condvar, Mutex}; use std::marker::PhantomData; use std::rc::Rc; @@ -213,7 +213,7 @@ impl Unpark for UnparkThread { #[cfg(feature = "rt-full")] mod waker { use super::{Inner, UnparkThread}; - use crate::executor::loom::sync::Arc; + use crate::loom::sync::Arc; use std::mem; use std::task::{RawWaker, RawWakerVTable, Waker}; diff --git a/tokio/src/executor/task/core.rs b/tokio/src/executor/task/core.rs index d0e1e3dd5..7a3016ee4 100644 --- a/tokio/src/executor/task/core.rs +++ b/tokio/src/executor/task/core.rs @@ -1,9 +1,9 @@ -use crate::executor::loom::alloc::Track; -use crate::executor::loom::cell::CausalCell; use crate::executor::task::raw::{self, Vtable}; use crate::executor::task::state::State; use crate::executor::task::waker::waker_ref; use crate::executor::task::Schedule; +use crate::loom::alloc::Track; +use crate::loom::cell::CausalCell; use std::cell::UnsafeCell; use std::future::Future; diff --git a/tokio/src/executor/task/harness.rs b/tokio/src/executor/task/harness.rs index e71e9a645..56b3cfbb4 100644 --- a/tokio/src/executor/task/harness.rs +++ b/tokio/src/executor/task/harness.rs @@ -1,8 +1,8 @@ -use crate::executor::loom::alloc::Track; -use crate::executor::loom::cell::CausalCheck; use crate::executor::task::core::{Cell, Core, Header, Trailer}; use crate::executor::task::state::Snapshot; use crate::executor::task::{JoinError, Schedule, Task}; +use crate::loom::alloc::Track; +use crate::loom::cell::CausalCheck; use std::future::Future; use std::marker::PhantomData; diff --git a/tokio/src/executor/task/join.rs b/tokio/src/executor/task/join.rs index b395b0ec3..faf6eb56c 100644 --- a/tokio/src/executor/task/join.rs +++ b/tokio/src/executor/task/join.rs @@ -1,5 +1,5 @@ -use crate::executor::loom::alloc::Track; use crate::executor::task::raw::RawTask; +use crate::loom::alloc::Track; use std::fmt; use std::future::Future; diff --git a/tokio/src/executor/task/raw.rs b/tokio/src/executor/task/raw.rs index 033bd91bd..6a34a3d41 100644 --- a/tokio/src/executor/task/raw.rs +++ b/tokio/src/executor/task/raw.rs @@ -1,8 +1,8 @@ -use crate::executor::loom::alloc::Track; use crate::executor::task::core::Cell; use crate::executor::task::harness::Harness; use crate::executor::task::state::{Snapshot, State}; use crate::executor::task::{Header, Schedule}; +use crate::loom::alloc::Track; use std::future::Future; use std::ptr::NonNull; diff --git a/tokio/src/executor/task/stack.rs b/tokio/src/executor/task/stack.rs index 20ac5c46e..cd675e10d 100644 --- a/tokio/src/executor/task/stack.rs +++ b/tokio/src/executor/task/stack.rs @@ -1,5 +1,5 @@ -use crate::executor::loom::sync::atomic::AtomicPtr; use crate::executor::task::{Header, Task}; +use crate::loom::sync::atomic::AtomicPtr; use std::marker::PhantomData; use std::ptr::{self, NonNull}; diff --git a/tokio/src/executor/task/state.rs b/tokio/src/executor/task/state.rs index 09b18f37f..3adfea91e 100644 --- a/tokio/src/executor/task/state.rs +++ b/tokio/src/executor/task/state.rs @@ -1,4 +1,4 @@ -use crate::executor::loom::sync::atomic::AtomicUsize; +use crate::loom::sync::atomic::AtomicUsize; use std::fmt; use std::sync::atomic::Ordering::{AcqRel, Acquire, Release}; @@ -224,7 +224,7 @@ impl State { /// /// Returns a snapshot of the state **after** the transition. pub(super) fn release_task(&self) -> Snapshot { - use crate::executor::loom::sync::atomic; + use crate::loom::sync::atomic; const DELTA: usize = RELEASED; @@ -283,7 +283,7 @@ impl State { /// /// Returns a snapshot of the state **after** the transition. pub(super) fn complete_join_handle(&self) -> Snapshot { - use crate::executor::loom::sync::atomic; + use crate::loom::sync::atomic; const DELTA: usize = JOIN_INTEREST; @@ -337,7 +337,7 @@ impl State { /// Store the join waker. pub(super) fn store_join_waker(&self) -> Snapshot { - use crate::executor::loom::sync::atomic; + use crate::loom::sync::atomic; const DELTA: usize = JOIN_WAKER; @@ -407,7 +407,7 @@ impl State { /// Returns `true` if the task should be released. pub(super) fn ref_dec(&self) -> bool { - use crate::executor::loom::sync::atomic; + use crate::loom::sync::atomic; let prev = self.val.fetch_sub(WAKER_ONE, Release); let next = Snapshot(prev - WAKER_ONE); diff --git a/tokio/src/executor/thread_pool/builder.rs b/tokio/src/executor/thread_pool/builder.rs index f9d40350b..c1b1ed19a 100644 --- a/tokio/src/executor/thread_pool/builder.rs +++ b/tokio/src/executor/thread_pool/builder.rs @@ -1,8 +1,7 @@ -use crate::executor::loom::sync::Arc; -use crate::executor::loom::sys::num_cpus; -use crate::executor::loom::thread; use crate::executor::park::Park; use crate::executor::thread_pool::{shutdown, worker, worker::Worker, Spawner, ThreadPool}; +use crate::loom::sync::Arc; +use crate::loom::sys::num_cpus; use std::{fmt, usize}; @@ -109,7 +108,7 @@ impl Builder { impl Drop for AbortOnPanic { fn drop(&mut self) { - if thread::panicking() { + if std::thread::panicking() { eprintln!("[ERROR] unhandled panic in Tokio scheduler. This is a bug and should be reported."); std::process::abort(); } diff --git a/tokio/src/executor/thread_pool/current.rs b/tokio/src/executor/thread_pool/current.rs index f02be1018..7bbe3e34a 100644 --- a/tokio/src/executor/thread_pool/current.rs +++ b/tokio/src/executor/thread_pool/current.rs @@ -1,6 +1,6 @@ -use crate::executor::loom::sync::Arc; use crate::executor::park::Unpark; use crate::executor::thread_pool::{worker, Owned}; +use crate::loom::sync::Arc; use std::cell::Cell; use std::ptr; diff --git a/tokio/src/executor/thread_pool/idle.rs b/tokio/src/executor/thread_pool/idle.rs index 0fae29bf5..acf80df87 100644 --- a/tokio/src/executor/thread_pool/idle.rs +++ b/tokio/src/executor/thread_pool/idle.rs @@ -1,7 +1,7 @@ //! Coordinates idling workers -use crate::executor::loom::sync::atomic::AtomicUsize; -use crate::executor::loom::sync::Mutex; +use crate::loom::sync::atomic::AtomicUsize; +use crate::loom::sync::Mutex; use std::fmt; use std::sync::atomic::Ordering::{self, AcqRel, Relaxed, SeqCst}; diff --git a/tokio/src/executor/thread_pool/queue/global.rs b/tokio/src/executor/thread_pool/queue/global.rs index 6bcd62162..20a9d36f8 100644 --- a/tokio/src/executor/thread_pool/queue/global.rs +++ b/tokio/src/executor/thread_pool/queue/global.rs @@ -1,6 +1,6 @@ -use crate::executor::loom::sync::atomic::AtomicUsize; -use crate::executor::loom::sync::Mutex; use crate::executor::task::{Header, Task}; +use crate::loom::sync::atomic::AtomicUsize; +use crate::loom::sync::Mutex; use std::marker::PhantomData; use std::ptr::{self, NonNull}; diff --git a/tokio/src/executor/thread_pool/queue/inject.rs b/tokio/src/executor/thread_pool/queue/inject.rs index 5f8caf1ac..045ce9227 100644 --- a/tokio/src/executor/thread_pool/queue/inject.rs +++ b/tokio/src/executor/thread_pool/queue/inject.rs @@ -1,6 +1,6 @@ -use crate::executor::loom::sync::Arc; use crate::executor::task::Task; use crate::executor::thread_pool::queue::Cluster; +use crate::loom::sync::Arc; pub(crate) struct Inject { cluster: Arc>, diff --git a/tokio/src/executor/thread_pool/queue/local.rs b/tokio/src/executor/thread_pool/queue/local.rs index e3323cb48..62472b0bf 100644 --- a/tokio/src/executor/thread_pool/queue/local.rs +++ b/tokio/src/executor/thread_pool/queue/local.rs @@ -1,8 +1,8 @@ -use crate::executor::loom::cell::{CausalCell, CausalCheck}; -use crate::executor::loom::sync::atomic::{self, AtomicU32}; use crate::executor::task::Task; use crate::executor::thread_pool::queue::global; use crate::executor::thread_pool::LOCAL_QUEUE_CAPACITY; +use crate::loom::cell::{CausalCell, CausalCheck}; +use crate::loom::sync::atomic::{self, AtomicU32}; use std::fmt; use std::mem::MaybeUninit; diff --git a/tokio/src/executor/thread_pool/queue/mod.rs b/tokio/src/executor/thread_pool/queue/mod.rs index e873c31a9..88633ee39 100644 --- a/tokio/src/executor/thread_pool/queue/mod.rs +++ b/tokio/src/executor/thread_pool/queue/mod.rs @@ -8,7 +8,7 @@ mod worker; pub(crate) use self::inject::Inject; pub(crate) use self::worker::Worker; -use crate::executor::loom::sync::Arc; +use crate::loom::sync::Arc; pub(crate) fn build(workers: usize) -> Vec> { let local: Vec<_> = (0..workers).map(|_| local::Queue::new()).collect(); diff --git a/tokio/src/executor/thread_pool/queue/worker.rs b/tokio/src/executor/thread_pool/queue/worker.rs index 2cd34a97f..be2552252 100644 --- a/tokio/src/executor/thread_pool/queue/worker.rs +++ b/tokio/src/executor/thread_pool/queue/worker.rs @@ -1,6 +1,6 @@ -use crate::executor::loom::sync::Arc; use crate::executor::task::Task; use crate::executor::thread_pool::queue::{local, Cluster, Inject}; +use crate::loom::sync::Arc; use std::cell::Cell; use std::fmt; diff --git a/tokio/src/executor/thread_pool/set.rs b/tokio/src/executor/thread_pool/set.rs index e878cad5f..c55f5d7d3 100644 --- a/tokio/src/executor/thread_pool/set.rs +++ b/tokio/src/executor/thread_pool/set.rs @@ -2,12 +2,12 @@ //! //! - Attempt to spin. -use crate::executor::loom::rand::seed; -use crate::executor::loom::sync::Arc; use crate::executor::park::Unpark; use crate::executor::task::{self, JoinHandle, Task}; use crate::executor::thread_pool::{current, queue, Idle, Owned, Shared}; use crate::executor::util::{CachePadded, FastRand}; +use crate::loom::rand::seed; +use crate::loom::sync::Arc; use std::cell::UnsafeCell; use std::future::Future; diff --git a/tokio/src/executor/thread_pool/shutdown.rs b/tokio/src/executor/thread_pool/shutdown.rs index b7c4177f0..9a0ee479d 100644 --- a/tokio/src/executor/thread_pool/shutdown.rs +++ b/tokio/src/executor/thread_pool/shutdown.rs @@ -3,7 +3,7 @@ //! Each worker holds the `Sender` half. When all the `Sender` halves are //! dropped, the `Receiver` receives a notification. -use crate::executor::loom::sync::Arc; +use crate::loom::sync::Arc; use crate::sync::oneshot; #[derive(Debug, Clone)] diff --git a/tokio/src/executor/thread_pool/spawner.rs b/tokio/src/executor/thread_pool/spawner.rs index 4fdf9594c..28ebe1646 100644 --- a/tokio/src/executor/thread_pool/spawner.rs +++ b/tokio/src/executor/thread_pool/spawner.rs @@ -1,7 +1,7 @@ -use crate::executor::loom::sync::Arc; use crate::executor::park::Unpark; use crate::executor::task::JoinHandle; use crate::executor::thread_pool::worker; +use crate::loom::sync::Arc; use std::fmt; use std::future::Future; diff --git a/tokio/src/executor/thread_pool/tests/loom_pool.rs b/tokio/src/executor/thread_pool/tests/loom_pool.rs index 6b2b5c307..fff00d677 100644 --- a/tokio/src/executor/thread_pool/tests/loom_pool.rs +++ b/tokio/src/executor/thread_pool/tests/loom_pool.rs @@ -1,14 +1,13 @@ -use crate::executor::loom::sync::atomic::Ordering::{Acquire, Relaxed, Release}; -use crate::executor::loom::sync::atomic::{AtomicBool, AtomicUsize}; -use crate::executor::loom::sync::{Arc, Mutex}; use crate::executor::park::{Park, Unpark}; use crate::executor::tests::loom_oneshot as oneshot; use crate::executor::thread_pool::{self, Builder}; use crate::spawn; -use loom::sync::Notify; +use loom::sync::atomic::{AtomicBool, AtomicUsize}; +use loom::sync::{Arc, Mutex, Notify}; use std::future::Future; +use std::sync::atomic::Ordering::{Acquire, Relaxed, Release}; use std::time::Duration; #[test] @@ -190,7 +189,7 @@ fn gated() -> impl Future { } fn gated2(thread: bool) -> impl Future { - use crate::executor::loom::thread; + use loom::thread; use std::sync::Arc; let gate = Arc::new(AtomicBool::new(false)); diff --git a/tokio/src/executor/thread_pool/worker.rs b/tokio/src/executor/thread_pool/worker.rs index 25d259666..10aeeae4e 100644 --- a/tokio/src/executor/thread_pool/worker.rs +++ b/tokio/src/executor/thread_pool/worker.rs @@ -1,7 +1,7 @@ -use crate::executor::loom::sync::Arc; use crate::executor::park::{Park, Unpark}; use crate::executor::task::Task; use crate::executor::thread_pool::{current, Owned, Shared, Spawner}; +use crate::loom::sync::Arc; use std::cell::Cell; use std::ops::{Deref, DerefMut}; diff --git a/tokio/src/lib.rs b/tokio/src/lib.rs index 5d18e97ce..89547c1d8 100644 --- a/tokio/src/lib.rs +++ b/tokio/src/lib.rs @@ -98,7 +98,7 @@ pub mod io; #[cfg(feature = "net-driver")] pub mod net; -#[cfg(feature = "net-driver")] +#[cfg(any(feature = "sync", feature = "blocking", feature = "rt-current-thread"))] mod loom; pub mod prelude; diff --git a/tokio/src/loom.rs b/tokio/src/loom.rs deleted file mode 100644 index 57ce2df6c..000000000 --- a/tokio/src/loom.rs +++ /dev/null @@ -1,45 +0,0 @@ -//! This module abstracts over `loom` and `std::sync` depending on whether we -//! are running tests or not. -pub(crate) use self::inner::*; - -#[cfg(all(test, loom))] -mod inner { - pub(crate) use loom::sync::CausalCell; - pub(crate) use loom::sync::Mutex; - pub(crate) mod atomic { - pub(crate) use loom::sync::atomic::*; - pub(crate) use std::sync::atomic::Ordering; - } -} - -#[cfg(not(all(test, loom)))] -mod inner { - use std::cell::UnsafeCell; - pub(crate) use std::sync::atomic; - pub(crate) use std::sync::Mutex; - - #[derive(Debug)] - pub(crate) struct CausalCell(UnsafeCell); - - impl CausalCell { - pub(crate) fn new(data: T) -> CausalCell { - CausalCell(UnsafeCell::new(data)) - } - - #[inline(always)] - pub(crate) fn with(&self, f: F) -> R - where - F: FnOnce(*const T) -> R, - { - f(self.0.get()) - } - - #[inline(always)] - pub(crate) fn with_mut(&self, f: F) -> R - where - F: FnOnce(*mut T) -> R, - { - f(self.0.get()) - } - } -} diff --git a/tokio/src/loom/mocked.rs b/tokio/src/loom/mocked.rs new file mode 100644 index 000000000..789139522 --- /dev/null +++ b/tokio/src/loom/mocked.rs @@ -0,0 +1,13 @@ +pub(crate) use loom::*; + +pub(crate) mod rand { + pub(crate) fn seed() -> u64 { + 1 + } +} + +pub(crate) mod sys { + pub(crate) fn num_cpus() -> usize { + 2 + } +} diff --git a/tokio/src/loom/mod.rs b/tokio/src/loom/mod.rs new file mode 100644 index 000000000..56a41f25a --- /dev/null +++ b/tokio/src/loom/mod.rs @@ -0,0 +1,12 @@ +//! This module abstracts over `loom` and `std::sync` depending on whether we +//! are running tests or not. + +#[cfg(not(all(test, loom)))] +mod std; +#[cfg(not(all(test, loom)))] +pub(crate) use self::std::*; + +#[cfg(all(test, loom))] +mod mocked; +#[cfg(all(test, loom))] +pub(crate) use self::mocked::*; diff --git a/tokio/src/loom/std/alloc.rs b/tokio/src/loom/std/alloc.rs new file mode 100644 index 000000000..25b199b1b --- /dev/null +++ b/tokio/src/loom/std/alloc.rs @@ -0,0 +1,18 @@ +#[derive(Debug)] +pub(crate) struct Track { + value: T, +} + +impl Track { + pub(crate) fn new(value: T) -> Track { + Track { value } + } + + pub(crate) fn get_mut(&mut self) -> &mut T { + &mut self.value + } + + pub(crate) fn into_inner(self) -> T { + self.value + } +} diff --git a/tokio/src/executor/loom/std/atomic_u32.rs b/tokio/src/loom/std/atomic_u32.rs similarity index 100% rename from tokio/src/executor/loom/std/atomic_u32.rs rename to tokio/src/loom/std/atomic_u32.rs diff --git a/tokio/src/executor/loom/std/atomic_usize.rs b/tokio/src/loom/std/atomic_usize.rs similarity index 81% rename from tokio/src/executor/loom/std/atomic_usize.rs rename to tokio/src/loom/std/atomic_usize.rs index 4a424b1ea..d255d087b 100644 --- a/tokio/src/executor/loom/std/atomic_usize.rs +++ b/tokio/src/loom/std/atomic_usize.rs @@ -1,6 +1,6 @@ use std::cell::UnsafeCell; use std::fmt; -use std::ops::Deref; +use std::ops; /// `AtomicUsize` providing an additional `load_unsync` function. pub(crate) struct AtomicUsize { @@ -11,7 +11,6 @@ unsafe impl Send for AtomicUsize {} unsafe impl Sync for AtomicUsize {} impl AtomicUsize { - #[cfg(feature = "rt-current-thread")] pub(crate) fn new(val: usize) -> AtomicUsize { let inner = UnsafeCell::new(std::sync::atomic::AtomicUsize::new(val)); AtomicUsize { inner } @@ -23,13 +22,12 @@ impl AtomicUsize { /// /// All mutations must have happened before the unsynchronized load. /// Additionally, there must be no concurrent mutations. - #[cfg(feature = "rt-full")] pub(crate) unsafe fn unsync_load(&self) -> usize { *(*self.inner.get()).get_mut() } } -impl Deref for AtomicUsize { +impl ops::Deref for AtomicUsize { type Target = std::sync::atomic::AtomicUsize; fn deref(&self) -> &Self::Target { @@ -39,8 +37,15 @@ impl Deref for AtomicUsize { } } -impl fmt::Debug for AtomicUsize { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - self.deref().fmt(fmt) +impl ops::DerefMut for AtomicUsize { + fn deref_mut(&mut self) -> &mut Self::Target { + // safety: we hold `&mut self` + unsafe { &mut *self.inner.get() } + } +} + +impl fmt::Debug for AtomicUsize { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + (**self).fmt(fmt) } } diff --git a/tokio/src/executor/loom/std/causal_cell.rs b/tokio/src/loom/std/causal_cell.rs similarity index 96% rename from tokio/src/executor/loom/std/causal_cell.rs rename to tokio/src/loom/std/causal_cell.rs index bb9b31e5a..c4917e5f8 100644 --- a/tokio/src/executor/loom/std/causal_cell.rs +++ b/tokio/src/loom/std/causal_cell.rs @@ -1,5 +1,6 @@ use std::cell::UnsafeCell; +#[derive(Debug)] pub(crate) struct CausalCell(UnsafeCell); #[derive(Default)] @@ -44,6 +45,5 @@ impl CausalCell { impl CausalCheck { pub(crate) fn check(self) {} - #[cfg(feature = "rt-full")] pub(crate) fn join(&mut self, _other: CausalCheck) {} } diff --git a/tokio/src/executor/loom/std/mod.rs b/tokio/src/loom/std/mod.rs similarity index 52% rename from tokio/src/executor/loom/std/mod.rs rename to tokio/src/loom/std/mod.rs index 3293f4405..5bbf15319 100644 --- a/tokio/src/executor/loom/std/mod.rs +++ b/tokio/src/loom/std/mod.rs @@ -1,39 +1,21 @@ // rt-full implies rt-current-thread -#[cfg(feature = "rt-full")] +#![cfg_attr(not(feature = "rt-full"), allow(unused_imports, dead_code))] + mod atomic_u32; mod atomic_usize; -#[cfg(feature = "rt-current-thread")] mod causal_cell; -#[cfg(feature = "rt-current-thread")] -pub(crate) mod alloc { - #[derive(Debug)] - pub(crate) struct Track { - value: T, - } +pub(crate) mod alloc; - impl Track { - pub(crate) fn new(value: T) -> Track { - Track { value } - } - - pub(crate) fn get_mut(&mut self) -> &mut T { - &mut self.value - } - - pub(crate) fn into_inner(self) -> T { - self.value - } - } -} - -#[cfg(feature = "rt-current-thread")] pub(crate) mod cell { pub(crate) use super::causal_cell::{CausalCell, CausalCheck}; } -#[cfg(feature = "rt-full")] +pub(crate) mod future { + pub(crate) use crate::sync::AtomicWaker; +} + pub(crate) mod rand { use std::collections::hash_map::RandomState; use std::hash::{BuildHasher, Hash, Hasher}; @@ -56,23 +38,17 @@ pub(crate) mod rand { } pub(crate) mod sync { - #[cfg(any(feature = "blocking", feature = "rt-current-thread"))] - pub(crate) use std::sync::{Arc, Condvar, Mutex}; + pub(crate) use std::sync::*; pub(crate) mod atomic { - #[cfg(feature = "rt-full")] - pub(crate) use crate::executor::loom::std::atomic_u32::AtomicU32; - #[cfg(feature = "rt-current-thread")] - pub(crate) use crate::executor::loom::std::atomic_usize::AtomicUsize; + pub(crate) use crate::loom::std::atomic_u32::AtomicU32; + pub(crate) use crate::loom::std::atomic_usize::AtomicUsize; - #[cfg(feature = "rt-full")] pub(crate) use std::sync::atomic::spin_loop_hint; - #[cfg(feature = "rt-current-thread")] pub(crate) use std::sync::atomic::{fence, AtomicPtr}; } } -#[cfg(feature = "rt-current-thread")] pub(crate) mod sys { #[cfg(feature = "rt-full")] pub(crate) fn num_cpus() -> usize { @@ -85,5 +61,4 @@ pub(crate) mod sys { } } -#[cfg(any(feature = "blocking", feature = "rt-full"))] pub(crate) use std::thread; diff --git a/tokio/src/net/driver/reactor/dispatch/page/mod.rs b/tokio/src/net/driver/reactor/dispatch/page/mod.rs index 0b8d3c4c2..9bef94f31 100644 --- a/tokio/src/net/driver/reactor/dispatch/page/mod.rs +++ b/tokio/src/net/driver/reactor/dispatch/page/mod.rs @@ -1,5 +1,5 @@ use super::{Pack, INITIAL_PAGE_SIZE, WIDTH}; -use crate::loom::CausalCell; +use crate::loom::cell::CausalCell; pub(crate) mod scheduled_io; mod stack; diff --git a/tokio/src/net/driver/reactor/dispatch/page/scheduled_io.rs b/tokio/src/net/driver/reactor/dispatch/page/scheduled_io.rs index 9cd99a869..aa0441220 100644 --- a/tokio/src/net/driver/reactor/dispatch/page/scheduled_io.rs +++ b/tokio/src/net/driver/reactor/dispatch/page/scheduled_io.rs @@ -1,10 +1,9 @@ use super::super::{Pack, Tid, RESERVED_BITS, WIDTH}; -use crate::loom::{ - atomic::{AtomicUsize, Ordering}, - CausalCell, -}; +use crate::loom::{cell::CausalCell, sync::atomic::AtomicUsize}; use crate::sync::AtomicWaker; +use std::sync::atomic::Ordering; + #[derive(Debug)] pub(crate) struct ScheduledIo { /// The offset of the next item on the free list. diff --git a/tokio/src/net/driver/reactor/dispatch/page/stack.rs b/tokio/src/net/driver/reactor/dispatch/page/stack.rs index 26597dc2d..d98f124f5 100644 --- a/tokio/src/net/driver/reactor/dispatch/page/stack.rs +++ b/tokio/src/net/driver/reactor/dispatch/page/stack.rs @@ -1,5 +1,7 @@ -use crate::loom::atomic::{AtomicUsize, Ordering}; +use crate::loom::sync::atomic::AtomicUsize; + use std::fmt; +use std::sync::atomic::Ordering; pub(super) struct TransferStack { head: AtomicUsize, @@ -55,7 +57,7 @@ impl fmt::Debug for TransferStack { mod test { use super::super::super::test_util; use super::*; - use crate::loom::CausalCell; + use loom::cell::CausalCell; use loom::thread; use std::sync::Arc; diff --git a/tokio/src/net/driver/reactor/dispatch/sharded_slab.rs b/tokio/src/net/driver/reactor/dispatch/sharded_slab.rs index 09f6e1117..b85f1bddf 100644 --- a/tokio/src/net/driver/reactor/dispatch/sharded_slab.rs +++ b/tokio/src/net/driver/reactor/dispatch/sharded_slab.rs @@ -1,7 +1,7 @@ use super::*; use std::fmt; -use crate::loom::Mutex; +use crate::loom::sync::Mutex; /// A sharded slab. pub(crate) struct Slab { diff --git a/tokio/src/net/driver/reactor/mod.rs b/tokio/src/net/driver/reactor/mod.rs index a5325f7dd..3a2f35150 100644 --- a/tokio/src/net/driver/reactor/mod.rs +++ b/tokio/src/net/driver/reactor/mod.rs @@ -1,6 +1,8 @@ -use super::platform; use crate::executor::park::{Park, Unpark}; -use crate::loom::atomic::{AtomicUsize, Ordering::SeqCst}; +use crate::loom::sync::atomic::AtomicUsize; +use crate::net::driver::platform; + +use std::sync::atomic::Ordering::SeqCst; mod dispatch; use dispatch::SingleShard; diff --git a/tokio/src/runtime/builder.rs b/tokio/src/runtime/builder.rs index 3efb96a08..1ed38c8a0 100644 --- a/tokio/src/runtime/builder.rs +++ b/tokio/src/runtime/builder.rs @@ -78,7 +78,7 @@ impl Builder { current_thread: false, // Default to use an equal number of threads to number of CPU cores - num_threads: crate::executor::loom::sys::num_cpus(), + num_threads: crate::loom::sys::num_cpus(), // Default thread name thread_name: "tokio-runtime-worker".into(), diff --git a/tokio/src/sync/loom.rs b/tokio/src/sync/loom.rs deleted file mode 100644 index 1b5a5c9d7..000000000 --- a/tokio/src/sync/loom.rs +++ /dev/null @@ -1,48 +0,0 @@ -#[cfg(not(all(test, loom)))] -mod imp { - pub(crate) mod future { - pub(crate) use crate::sync::task::AtomicWaker; - } - - pub(crate) mod sync { - pub(crate) use std::sync::atomic; - pub(crate) use std::sync::Arc; - - use std::cell::UnsafeCell; - - pub(crate) struct CausalCell(UnsafeCell); - - impl CausalCell { - pub(crate) fn new(data: T) -> CausalCell { - CausalCell(UnsafeCell::new(data)) - } - - pub(crate) fn with(&self, f: F) -> R - where - F: FnOnce(*const T) -> R, - { - f(self.0.get()) - } - - pub(crate) fn with_mut(&self, f: F) -> R - where - F: FnOnce(*mut T) -> R, - { - f(self.0.get()) - } - } - } - - pub(crate) mod thread { - pub(crate) fn yield_now() { - ::std::sync::atomic::spin_loop_hint(); - } - } -} - -#[cfg(all(test, loom))] -mod imp { - pub(crate) use loom::*; -} - -pub(crate) use self::imp::*; diff --git a/tokio/src/sync/mod.rs b/tokio/src/sync/mod.rs index 84f6bd98b..25f58ffdd 100644 --- a/tokio/src/sync/mod.rs +++ b/tokio/src/sync/mod.rs @@ -37,8 +37,6 @@ macro_rules! if_loom { mod barrier; pub use barrier::{Barrier, BarrierWaitResult}; -mod loom; - pub mod mpsc; mod mutex; diff --git a/tokio/src/sync/mpsc/block.rs b/tokio/src/sync/mpsc/block.rs index aea693849..f03648bab 100644 --- a/tokio/src/sync/mpsc/block.rs +++ b/tokio/src/sync/mpsc/block.rs @@ -1,6 +1,6 @@ -use crate::sync::loom::{ +use crate::loom::{ + cell::CausalCell, sync::atomic::{AtomicPtr, AtomicUsize}, - sync::CausalCell, thread, }; diff --git a/tokio/src/sync/mpsc/chan.rs b/tokio/src/sync/mpsc/chan.rs index fe5ff9044..ad0d99a8f 100644 --- a/tokio/src/sync/mpsc/chan.rs +++ b/tokio/src/sync/mpsc/chan.rs @@ -1,8 +1,4 @@ -use crate::sync::loom::{ - future::AtomicWaker, - sync::atomic::AtomicUsize, - sync::{Arc, CausalCell}, -}; +use crate::loom::{cell::CausalCell, future::AtomicWaker, sync::atomic::AtomicUsize, sync::Arc}; use crate::sync::mpsc::list; use std::fmt; diff --git a/tokio/src/sync/mpsc/list.rs b/tokio/src/sync/mpsc/list.rs index a1295bb05..eecc4da33 100644 --- a/tokio/src/sync/mpsc/list.rs +++ b/tokio/src/sync/mpsc/list.rs @@ -1,6 +1,6 @@ //! A concurrent, lock-free, FIFO list. -use crate::sync::loom::{ +use crate::loom::{ sync::atomic::{AtomicPtr, AtomicUsize}, thread, }; diff --git a/tokio/src/sync/mpsc/unbounded.rs b/tokio/src/sync/mpsc/unbounded.rs index 5a73771e8..4eb750efb 100644 --- a/tokio/src/sync/mpsc/unbounded.rs +++ b/tokio/src/sync/mpsc/unbounded.rs @@ -1,4 +1,4 @@ -use crate::sync::loom::sync::atomic::AtomicUsize; +use crate::loom::sync::atomic::AtomicUsize; use crate::sync::mpsc::chan; use std::fmt; diff --git a/tokio/src/sync/oneshot.rs b/tokio/src/sync/oneshot.rs index 7cf4eb8f1..3c757e9e5 100644 --- a/tokio/src/sync/oneshot.rs +++ b/tokio/src/sync/oneshot.rs @@ -1,6 +1,8 @@ //! A channel for sending a single message between asynchronous tasks. -use crate::sync::loom::sync::{atomic::AtomicUsize, Arc, CausalCell}; +use crate::loom::cell::CausalCell; +use crate::loom::sync::atomic::AtomicUsize; +use crate::loom::sync::Arc; use futures_core::ready; use std::fmt; diff --git a/tokio/src/sync/semaphore.rs b/tokio/src/sync/semaphore.rs index 1120be077..b4a093f85 100644 --- a/tokio/src/sync/semaphore.rs +++ b/tokio/src/sync/semaphore.rs @@ -8,12 +8,10 @@ //! section. If no permits are available, then acquiring the semaphore returns //! `Pending`. The task is woken once a permit becomes available. -use crate::sync::loom::{ +use crate::loom::{ + cell::CausalCell, future::AtomicWaker, - sync::{ - atomic::{AtomicPtr, AtomicUsize}, - CausalCell, - }, + sync::atomic::{AtomicPtr, AtomicUsize}, thread, }; diff --git a/tokio/src/sync/task/atomic_waker.rs b/tokio/src/sync/task/atomic_waker.rs index ff952a186..88d083d6f 100644 --- a/tokio/src/sync/task/atomic_waker.rs +++ b/tokio/src/sync/task/atomic_waker.rs @@ -1,5 +1,5 @@ -use crate::sync::loom::sync::atomic::{self, AtomicUsize}; -use crate::sync::loom::sync::CausalCell; +use crate::loom::cell::CausalCell; +use crate::loom::sync::atomic::{self, AtomicUsize}; use std::fmt; use std::sync::atomic::Ordering::{AcqRel, Acquire, Release};