From d1b0a187b8de383cc9b67ceb044dbbb6c3c9f000 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 24 Aug 2022 14:51:43 -0700 Subject: [PATCH 1/4] io: move public types out of io::driver. The `Interest` and `Ready` types are part of the public API and should not be defined in the driver module, which is intended to be internal. This is in preparation to move the I/O driver into the runtime module. --- tokio/src/io/async_fd.rs | 3 ++- tokio/src/io/driver/mod.rs | 9 +-------- tokio/src/io/driver/registration.rs | 3 ++- tokio/src/io/driver/scheduled_io.rs | 3 ++- tokio/src/io/{driver => }/interest.rs | 2 +- tokio/src/io/mod.rs | 6 +++++- tokio/src/io/poll_evented.rs | 3 ++- tokio/src/io/{driver => }/ready.rs | 0 tokio/src/signal/unix/driver.rs | 4 ++-- 9 files changed, 17 insertions(+), 16 deletions(-) rename tokio/src/io/{driver => }/interest.rs (99%) rename tokio/src/io/{driver => }/ready.rs (100%) diff --git a/tokio/src/io/async_fd.rs b/tokio/src/io/async_fd.rs index d91710a40b9..1e2ca340a0d 100644 --- a/tokio/src/io/async_fd.rs +++ b/tokio/src/io/async_fd.rs @@ -1,4 +1,5 @@ -use crate::io::driver::{Handle, Interest, ReadyEvent, Registration}; +use crate::io::driver::{Handle, ReadyEvent, Registration}; +use crate::io::Interest; use mio::unix::SourceFd; use std::io; diff --git a/tokio/src/io/driver/mod.rs b/tokio/src/io/driver/mod.rs index 9801a22c4a9..f3e98fec067 100644 --- a/tokio/src/io/driver/mod.rs +++ b/tokio/src/io/driver/mod.rs @@ -1,13 +1,5 @@ #![cfg_attr(not(feature = "rt"), allow(dead_code))] -mod interest; -#[allow(unreachable_pub)] -pub use interest::Interest; - -mod ready; -#[allow(unreachable_pub)] -pub use ready::Ready; - mod registration; pub(crate) use registration::Registration; @@ -16,6 +8,7 @@ use scheduled_io::ScheduledIo; mod metrics; +use crate::io::{Interest, Ready}; use crate::park::{Park, Unpark}; use crate::util::slab::{self, Slab}; use crate::{loom::sync::RwLock, util::bit}; diff --git a/tokio/src/io/driver/registration.rs b/tokio/src/io/driver/registration.rs index b765976bbb1..4867ad8dddc 100644 --- a/tokio/src/io/driver/registration.rs +++ b/tokio/src/io/driver/registration.rs @@ -1,6 +1,7 @@ #![cfg_attr(not(feature = "net"), allow(dead_code))] -use crate::io::driver::{Direction, Handle, Interest, ReadyEvent, ScheduledIo}; +use crate::io::driver::{Direction, Handle, ReadyEvent, ScheduledIo}; +use crate::io::Interest; use crate::util::slab; use mio::event::Source; diff --git a/tokio/src/io/driver/scheduled_io.rs b/tokio/src/io/driver/scheduled_io.rs index 229fe7b1c13..8aafdad1cdc 100644 --- a/tokio/src/io/driver/scheduled_io.rs +++ b/tokio/src/io/driver/scheduled_io.rs @@ -1,4 +1,5 @@ -use super::{Interest, Ready, ReadyEvent, Tick}; +use super::{ReadyEvent, Tick}; +use crate::io::{Interest, Ready}; use crate::loom::sync::atomic::AtomicUsize; use crate::loom::sync::Mutex; use crate::util::bit; diff --git a/tokio/src/io/driver/interest.rs b/tokio/src/io/interest.rs similarity index 99% rename from tokio/src/io/driver/interest.rs rename to tokio/src/io/interest.rs index d6b46dfb7c6..945c4de1419 100644 --- a/tokio/src/io/driver/interest.rs +++ b/tokio/src/io/interest.rs @@ -1,6 +1,6 @@ #![cfg_attr(not(feature = "net"), allow(dead_code, unreachable_pub))] -use crate::io::driver::Ready; +use crate::io::Ready; use std::fmt; use std::ops; diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index 3f6cc6b720d..899c9cdf148 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -208,7 +208,11 @@ cfg_io_driver_impl! { pub(crate) mod driver; cfg_net! { - pub use driver::{Interest, Ready}; + mod interest; + pub use interest::Interest; + + mod ready; + pub use ready::Ready; } #[cfg_attr(tokio_wasi, allow(unused_imports))] diff --git a/tokio/src/io/poll_evented.rs b/tokio/src/io/poll_evented.rs index f37d4dab92d..0edc88800b3 100644 --- a/tokio/src/io/poll_evented.rs +++ b/tokio/src/io/poll_evented.rs @@ -1,4 +1,5 @@ -use crate::io::driver::{Handle, Interest, Registration}; +use crate::io::driver::{Handle, Registration}; +use crate::io::Interest; use mio::event::Source; use std::fmt; diff --git a/tokio/src/io/driver/ready.rs b/tokio/src/io/ready.rs similarity index 100% rename from tokio/src/io/driver/ready.rs rename to tokio/src/io/ready.rs diff --git a/tokio/src/signal/unix/driver.rs b/tokio/src/signal/unix/driver.rs index ba2edc35167..1b057936f84 100644 --- a/tokio/src/signal/unix/driver.rs +++ b/tokio/src/signal/unix/driver.rs @@ -2,8 +2,8 @@ //! Signal driver -use crate::io::driver::{Driver as IoDriver, Interest}; -use crate::io::PollEvented; +use crate::io::driver::Driver as IoDriver; +use crate::io::{Interest, PollEvented}; use crate::park::Park; use crate::signal::registry::globals; From 9ca5170f5e078e48bd27e581c0e4b9ac14b36aa0 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Wed, 24 Aug 2022 16:48:58 -0700 Subject: [PATCH 2/4] rt: move I/O driver into `runtime` module This patch moves the I/O driver into the runtime module. The I/O driver is a runtime concern and is only used by the runtime. Moving the driver is a first step to clean up some Tokio internals. There will be follow up patches that integrate the I/O driver and other runtime concerns more closely. This is an internal refactor and should not impact any public APIs. --- tokio/src/io/async_fd.rs | 2 +- tokio/src/io/bsd/poll_aio.rs | 2 +- tokio/src/io/interest.rs | 4 ++-- tokio/src/io/mod.rs | 6 ++---- tokio/src/io/poll_evented.rs | 4 ++-- tokio/src/process/unix/orphan.rs | 2 +- tokio/src/runtime/driver.rs | 6 +++--- tokio/src/{io/driver => runtime/io}/metrics.rs | 0 tokio/src/{io/driver => runtime/io}/mod.rs | 7 ++++--- tokio/src/{io/driver => runtime/io}/platform.rs | 0 tokio/src/{io/driver => runtime/io}/registration.rs | 4 ++-- tokio/src/{io/driver => runtime/io}/scheduled_io.rs | 3 ++- tokio/src/runtime/mod.rs | 4 ++++ tokio/src/signal/unix/driver.rs | 5 +++-- 14 files changed, 27 insertions(+), 22 deletions(-) rename tokio/src/{io/driver => runtime/io}/metrics.rs (100%) rename tokio/src/{io/driver => runtime/io}/mod.rs (98%) rename tokio/src/{io/driver => runtime/io}/platform.rs (100%) rename tokio/src/{io/driver => runtime/io}/registration.rs (98%) rename tokio/src/{io/driver => runtime/io}/scheduled_io.rs (99%) diff --git a/tokio/src/io/async_fd.rs b/tokio/src/io/async_fd.rs index 1e2ca340a0d..eb544b95a0b 100644 --- a/tokio/src/io/async_fd.rs +++ b/tokio/src/io/async_fd.rs @@ -1,5 +1,5 @@ -use crate::io::driver::{Handle, ReadyEvent, Registration}; use crate::io::Interest; +use crate::runtime::io::{Handle, ReadyEvent, Registration}; use mio::unix::SourceFd; use std::io; diff --git a/tokio/src/io/bsd/poll_aio.rs b/tokio/src/io/bsd/poll_aio.rs index f1ac4b2d77a..8b6aa9010f2 100644 --- a/tokio/src/io/bsd/poll_aio.rs +++ b/tokio/src/io/bsd/poll_aio.rs @@ -1,6 +1,6 @@ //! Use POSIX AIO futures with Tokio. -use crate::io::driver::{Handle, Interest, ReadyEvent, Registration}; +use crate::runtime::io::{Handle, Interest, ReadyEvent, Registration}; use mio::event::Source; use mio::Registry; use mio::Token; diff --git a/tokio/src/io/interest.rs b/tokio/src/io/interest.rs index 945c4de1419..013c1140590 100644 --- a/tokio/src/io/interest.rs +++ b/tokio/src/io/interest.rs @@ -1,6 +1,6 @@ #![cfg_attr(not(feature = "net"), allow(dead_code, unreachable_pub))] -use crate::io::Ready; +use crate::io::ready::Ready; use std::fmt; use std::ops; @@ -100,7 +100,7 @@ impl Interest { self.0 } - pub(super) fn mask(self) -> Ready { + pub(crate) fn mask(self) -> Ready { match self { Interest::READABLE => Ready::READABLE | Ready::READ_CLOSED, Interest::WRITABLE => Ready::WRITABLE | Ready::WRITE_CLOSED, diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index 899c9cdf148..df5a04521f5 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -205,13 +205,11 @@ pub use self::read_buf::ReadBuf; pub use std::io::{Error, ErrorKind, Result, SeekFrom}; cfg_io_driver_impl! { - pub(crate) mod driver; + pub(crate) mod interest; + pub(crate) mod ready; cfg_net! { - mod interest; pub use interest::Interest; - - mod ready; pub use ready::Ready; } diff --git a/tokio/src/io/poll_evented.rs b/tokio/src/io/poll_evented.rs index 0edc88800b3..b8c0be2c09f 100644 --- a/tokio/src/io/poll_evented.rs +++ b/tokio/src/io/poll_evented.rs @@ -1,5 +1,5 @@ -use crate::io::driver::{Handle, Registration}; -use crate::io::Interest; +use crate::io::interest::Interest; +use crate::runtime::io::{Handle, Registration}; use mio::event::Source; use std::fmt; diff --git a/tokio/src/process/unix/orphan.rs b/tokio/src/process/unix/orphan.rs index 0e52530c37b..b32b5d77367 100644 --- a/tokio/src/process/unix/orphan.rs +++ b/tokio/src/process/unix/orphan.rs @@ -120,7 +120,7 @@ where #[cfg(all(test, not(loom)))] pub(crate) mod test { use super::*; - use crate::io::driver::Driver as IoDriver; + use crate::runtime::io::Driver as IoDriver; use crate::signal::unix::driver::{Driver as SignalDriver, Handle as SignalHandle}; use crate::sync::watch; use std::cell::{Cell, RefCell}; diff --git a/tokio/src/runtime/driver.rs b/tokio/src/runtime/driver.rs index 7e459779bbd..916e1bfbdc6 100644 --- a/tokio/src/runtime/driver.rs +++ b/tokio/src/runtime/driver.rs @@ -8,9 +8,9 @@ use std::time::Duration; // ===== io driver ===== cfg_io_driver! { - type IoDriver = crate::io::driver::Driver; + type IoDriver = crate::runtime::io::Driver; type IoStack = crate::park::either::Either; - pub(crate) type IoHandle = Option; + pub(crate) type IoHandle = Option; fn create_io_stack(enabled: bool) -> io::Result<(IoStack, IoHandle, SignalHandle)> { use crate::park::either::Either; @@ -19,7 +19,7 @@ cfg_io_driver! { assert!(!enabled); let ret = if enabled { - let io_driver = crate::io::driver::Driver::new()?; + let io_driver = crate::runtime::io::Driver::new()?; let io_handle = io_driver.handle(); let (signal_driver, signal_handle) = create_signal_driver(io_driver)?; diff --git a/tokio/src/io/driver/metrics.rs b/tokio/src/runtime/io/metrics.rs similarity index 100% rename from tokio/src/io/driver/metrics.rs rename to tokio/src/runtime/io/metrics.rs diff --git a/tokio/src/io/driver/mod.rs b/tokio/src/runtime/io/mod.rs similarity index 98% rename from tokio/src/io/driver/mod.rs rename to tokio/src/runtime/io/mod.rs index f3e98fec067..d447b87eba4 100644 --- a/tokio/src/io/driver/mod.rs +++ b/tokio/src/runtime/io/mod.rs @@ -8,7 +8,8 @@ use scheduled_io::ScheduledIo; mod metrics; -use crate::io::{Interest, Ready}; +use crate::io::interest::Interest; +use crate::io::ready::Ready; use crate::park::{Park, Unpark}; use crate::util::slab::{self, Slab}; use crate::{loom::sync::RwLock, util::bit}; @@ -261,7 +262,7 @@ cfg_rt! { /// This function panics if there is no current reactor set and `rt` feature /// flag is not enabled. #[track_caller] - pub(super) fn current() -> Self { + pub(crate) fn current() -> Self { crate::runtime::context::io_handle().expect("A Tokio 1.x context was found, but IO is disabled. Call `enable_io` on the runtime builder to enable IO.") } } @@ -276,7 +277,7 @@ cfg_not_rt! { /// This function panics if there is no current reactor set, or if the `rt` /// feature flag is not enabled. #[track_caller] - pub(super) fn current() -> Self { + pub(crate) fn current() -> Self { panic!("{}", crate::util::error::CONTEXT_MISSING_ERROR) } } diff --git a/tokio/src/io/driver/platform.rs b/tokio/src/runtime/io/platform.rs similarity index 100% rename from tokio/src/io/driver/platform.rs rename to tokio/src/runtime/io/platform.rs diff --git a/tokio/src/io/driver/registration.rs b/tokio/src/runtime/io/registration.rs similarity index 98% rename from tokio/src/io/driver/registration.rs rename to tokio/src/runtime/io/registration.rs index 4867ad8dddc..a82eaee46b1 100644 --- a/tokio/src/io/driver/registration.rs +++ b/tokio/src/runtime/io/registration.rs @@ -1,7 +1,7 @@ #![cfg_attr(not(feature = "net"), allow(dead_code))] -use crate::io::driver::{Direction, Handle, ReadyEvent, ScheduledIo}; -use crate::io::Interest; +use crate::io::interest::Interest; +use crate::runtime::io::{Direction, Handle, ReadyEvent, ScheduledIo}; use crate::util::slab; use mio::event::Source; diff --git a/tokio/src/io/driver/scheduled_io.rs b/tokio/src/runtime/io/scheduled_io.rs similarity index 99% rename from tokio/src/io/driver/scheduled_io.rs rename to tokio/src/runtime/io/scheduled_io.rs index 8aafdad1cdc..af42ba8a31e 100644 --- a/tokio/src/io/driver/scheduled_io.rs +++ b/tokio/src/runtime/io/scheduled_io.rs @@ -1,5 +1,6 @@ use super::{ReadyEvent, Tick}; -use crate::io::{Interest, Ready}; +use crate::io::interest::Interest; +use crate::io::ready::Ready; use crate::loom::sync::atomic::AtomicUsize; use crate::loom::sync::Mutex; use crate::util::bit; diff --git a/tokio/src/runtime/mod.rs b/tokio/src/runtime/mod.rs index 7fb907f5a04..d242aa4c1be 100644 --- a/tokio/src/runtime/mod.rs +++ b/tokio/src/runtime/mod.rs @@ -182,6 +182,10 @@ pub(crate) mod enter; pub(crate) mod task; +cfg_io_driver_impl! { + pub(crate) mod io; +} + cfg_metrics! { mod metrics; pub use metrics::RuntimeMetrics; diff --git a/tokio/src/signal/unix/driver.rs b/tokio/src/signal/unix/driver.rs index 1b057936f84..a7c5ef830e9 100644 --- a/tokio/src/signal/unix/driver.rs +++ b/tokio/src/signal/unix/driver.rs @@ -2,9 +2,10 @@ //! Signal driver -use crate::io::driver::Driver as IoDriver; -use crate::io::{Interest, PollEvented}; +use crate::io::PollEvented; +use crate::io::interest::Interest; use crate::park::Park; +use crate::runtime::io::Driver as IoDriver; use crate::signal::registry::globals; use mio::net::UnixStream; From b89900f42b5399e33129216f2e63c2c7a5f7285c Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 25 Aug 2022 10:29:14 -0700 Subject: [PATCH 3/4] fix feature build --- tokio/src/lib.rs | 5 +++++ tokio/src/runtime/mod.rs | 38 ++++++++++++++++----------------- tokio/src/signal/unix/driver.rs | 2 +- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/tokio/src/lib.rs b/tokio/src/lib.rs index 30f5abe06dd..b302f7548b6 100644 --- a/tokio/src/lib.rs +++ b/tokio/src/lib.rs @@ -458,6 +458,11 @@ mod blocking; cfg_rt! { pub mod runtime; } +cfg_not_rt! { + cfg_io_driver_impl! { + pub(crate) mod runtime; + } +} pub(crate) mod coop; diff --git a/tokio/src/runtime/mod.rs b/tokio/src/runtime/mod.rs index d242aa4c1be..24e201362dd 100644 --- a/tokio/src/runtime/mod.rs +++ b/tokio/src/runtime/mod.rs @@ -178,31 +178,15 @@ #[macro_use] mod tests; -pub(crate) mod enter; - -pub(crate) mod task; - cfg_io_driver_impl! { pub(crate) mod io; } -cfg_metrics! { - mod metrics; - pub use metrics::RuntimeMetrics; - - pub(crate) use metrics::{MetricsBatch, SchedulerMetrics, WorkerMetrics}; - - cfg_net! { - pub(crate) use metrics::IoDriverMetrics; - } -} +cfg_rt! { + pub(crate) mod enter; -cfg_not_metrics! { - pub(crate) mod metrics; - pub(crate) use metrics::{SchedulerMetrics, WorkerMetrics, MetricsBatch}; -} + pub(crate) mod task; -cfg_rt! { mod basic_scheduler; use basic_scheduler::BasicScheduler; @@ -239,6 +223,22 @@ cfg_rt! { mod spawner; use self::spawner::Spawner; + + cfg_metrics! { + mod metrics; + pub use metrics::RuntimeMetrics; + + pub(crate) use metrics::{MetricsBatch, SchedulerMetrics, WorkerMetrics}; + + cfg_net! { + pub(crate) use metrics::IoDriverMetrics; + } + } + + cfg_not_metrics! { + pub(crate) mod metrics; + pub(crate) use metrics::{SchedulerMetrics, WorkerMetrics, MetricsBatch}; + } } cfg_rt_multi_thread! { diff --git a/tokio/src/signal/unix/driver.rs b/tokio/src/signal/unix/driver.rs index a7c5ef830e9..54959e04df5 100644 --- a/tokio/src/signal/unix/driver.rs +++ b/tokio/src/signal/unix/driver.rs @@ -2,8 +2,8 @@ //! Signal driver -use crate::io::PollEvented; use crate::io::interest::Interest; +use crate::io::PollEvented; use crate::park::Park; use crate::runtime::io::Driver as IoDriver; use crate::signal::registry::globals; From 9aa66a1aa381b62d51405ed3cf0f7f06be1d34b8 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 25 Aug 2022 10:40:52 -0700 Subject: [PATCH 4/4] fix more CI failures --- tokio/src/io/bsd/poll_aio.rs | 3 ++- tokio/src/io/mod.rs | 2 -- tokio/src/lib.rs | 1 + tokio/src/runtime/tests/mod.rs | 4 ++++ 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tokio/src/io/bsd/poll_aio.rs b/tokio/src/io/bsd/poll_aio.rs index 8b6aa9010f2..ceb83135c16 100644 --- a/tokio/src/io/bsd/poll_aio.rs +++ b/tokio/src/io/bsd/poll_aio.rs @@ -1,6 +1,7 @@ //! Use POSIX AIO futures with Tokio. -use crate::runtime::io::{Handle, Interest, ReadyEvent, Registration}; +use crate::io::interest::Interest; +use crate::runtime::io::{Handle, ReadyEvent, Registration}; use mio::event::Source; use mio::Registry; use mio::Token; diff --git a/tokio/src/io/mod.rs b/tokio/src/io/mod.rs index df5a04521f5..e5c09a3a4ae 100644 --- a/tokio/src/io/mod.rs +++ b/tokio/src/io/mod.rs @@ -1,5 +1,3 @@ -#![cfg_attr(loom, allow(dead_code, unreachable_pub))] - //! Traits, helpers, and type definitions for asynchronous I/O functionality. //! //! This module is the asynchronous version of `std::io`. Primarily, it diff --git a/tokio/src/lib.rs b/tokio/src/lib.rs index b302f7548b6..e71fd341e63 100644 --- a/tokio/src/lib.rs +++ b/tokio/src/lib.rs @@ -16,6 +16,7 @@ ))] #![cfg_attr(docsrs, feature(doc_cfg))] #![cfg_attr(docsrs, allow(unused_attributes))] +#![cfg_attr(loom, allow(dead_code, unreachable_pub))] //! A runtime for writing reliable network applications without compromising speed. //! diff --git a/tokio/src/runtime/tests/mod.rs b/tokio/src/runtime/tests/mod.rs index 08724d43ee4..4c5d69aeeab 100644 --- a/tokio/src/runtime/tests/mod.rs +++ b/tokio/src/runtime/tests/mod.rs @@ -1,3 +1,7 @@ +// Enable dead_code / unreachable_pub here. It has been disabled in lib.rs for +// other code when running loom tests. +#![cfg_attr(loom, warn(dead_code, unreachable_pub))] + use self::unowned_wrapper::unowned; mod unowned_wrapper {