rt: move I/O driver into runtime module (#4942)

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 the first step to cleaning up some Tokio internals. There will be
follow-up patches that integrate the I/O driver and other runtime concerns
more closely.

Because `Interest` and `Ready` are public APIs, they were moved to the
top-level `io` module instead of moving the types to `runtime`.

This is an internal refactor and should not impact any public APIs.
This commit is contained in:
Carl Lerche
2022-08-25 12:58:57 -07:00
committed by GitHub
parent a66884a2fb
commit 218f2629ff
17 changed files with 59 additions and 44 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
use crate::io::driver::{Handle, Interest, ReadyEvent, Registration}; use crate::io::Interest;
use crate::runtime::io::{Handle, ReadyEvent, Registration};
use mio::unix::SourceFd; use mio::unix::SourceFd;
use std::io; use std::io;
+2 -1
View File
@@ -1,6 +1,7 @@
//! Use POSIX AIO futures with Tokio. //! Use POSIX AIO futures with Tokio.
use crate::io::driver::{Handle, Interest, ReadyEvent, Registration}; use crate::io::interest::Interest;
use crate::runtime::io::{Handle, ReadyEvent, Registration};
use mio::event::Source; use mio::event::Source;
use mio::Registry; use mio::Registry;
use mio::Token; use mio::Token;
@@ -1,6 +1,6 @@
#![cfg_attr(not(feature = "net"), allow(dead_code, unreachable_pub))] #![cfg_attr(not(feature = "net"), allow(dead_code, unreachable_pub))]
use crate::io::driver::Ready; use crate::io::ready::Ready;
use std::fmt; use std::fmt;
use std::ops; use std::ops;
@@ -100,7 +100,7 @@ impl Interest {
self.0 self.0
} }
pub(super) fn mask(self) -> Ready { pub(crate) fn mask(self) -> Ready {
match self { match self {
Interest::READABLE => Ready::READABLE | Ready::READ_CLOSED, Interest::READABLE => Ready::READABLE | Ready::READ_CLOSED,
Interest::WRITABLE => Ready::WRITABLE | Ready::WRITE_CLOSED, Interest::WRITABLE => Ready::WRITABLE | Ready::WRITE_CLOSED,
+4 -4
View File
@@ -1,5 +1,3 @@
#![cfg_attr(loom, allow(dead_code, unreachable_pub))]
//! Traits, helpers, and type definitions for asynchronous I/O functionality. //! Traits, helpers, and type definitions for asynchronous I/O functionality.
//! //!
//! This module is the asynchronous version of `std::io`. Primarily, it //! This module is the asynchronous version of `std::io`. Primarily, it
@@ -205,10 +203,12 @@ pub use self::read_buf::ReadBuf;
pub use std::io::{Error, ErrorKind, Result, SeekFrom}; pub use std::io::{Error, ErrorKind, Result, SeekFrom};
cfg_io_driver_impl! { cfg_io_driver_impl! {
pub(crate) mod driver; pub(crate) mod interest;
pub(crate) mod ready;
cfg_net! { cfg_net! {
pub use driver::{Interest, Ready}; pub use interest::Interest;
pub use ready::Ready;
} }
#[cfg_attr(tokio_wasi, allow(unused_imports))] #[cfg_attr(tokio_wasi, allow(unused_imports))]
+2 -1
View File
@@ -1,4 +1,5 @@
use crate::io::driver::{Handle, Interest, Registration}; use crate::io::interest::Interest;
use crate::runtime::io::{Handle, Registration};
use mio::event::Source; use mio::event::Source;
use std::fmt; use std::fmt;
+6
View File
@@ -16,6 +16,7 @@
))] ))]
#![cfg_attr(docsrs, feature(doc_cfg))] #![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, allow(unused_attributes))] #![cfg_attr(docsrs, allow(unused_attributes))]
#![cfg_attr(loom, allow(dead_code, unreachable_pub))]
//! A runtime for writing reliable network applications without compromising speed. //! A runtime for writing reliable network applications without compromising speed.
//! //!
@@ -458,6 +459,11 @@ mod blocking;
cfg_rt! { cfg_rt! {
pub mod runtime; pub mod runtime;
} }
cfg_not_rt! {
cfg_io_driver_impl! {
pub(crate) mod runtime;
}
}
pub(crate) mod coop; pub(crate) mod coop;
+1 -1
View File
@@ -120,7 +120,7 @@ where
#[cfg(all(test, not(loom)))] #[cfg(all(test, not(loom)))]
pub(crate) mod test { pub(crate) mod test {
use super::*; 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::signal::unix::driver::{Driver as SignalDriver, Handle as SignalHandle};
use crate::sync::watch; use crate::sync::watch;
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
+3 -3
View File
@@ -8,9 +8,9 @@ use std::time::Duration;
// ===== io driver ===== // ===== io driver =====
cfg_io_driver! { cfg_io_driver! {
type IoDriver = crate::io::driver::Driver; type IoDriver = crate::runtime::io::Driver;
type IoStack = crate::park::either::Either<ProcessDriver, ParkThread>; type IoStack = crate::park::either::Either<ProcessDriver, ParkThread>;
pub(crate) type IoHandle = Option<crate::io::driver::Handle>; pub(crate) type IoHandle = Option<crate::runtime::io::Handle>;
fn create_io_stack(enabled: bool) -> io::Result<(IoStack, IoHandle, SignalHandle)> { fn create_io_stack(enabled: bool) -> io::Result<(IoStack, IoHandle, SignalHandle)> {
use crate::park::either::Either; use crate::park::either::Either;
@@ -19,7 +19,7 @@ cfg_io_driver! {
assert!(!enabled); assert!(!enabled);
let ret = if 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 io_handle = io_driver.handle();
let (signal_driver, signal_handle) = create_signal_driver(io_driver)?; let (signal_driver, signal_handle) = create_signal_driver(io_driver)?;
@@ -1,13 +1,5 @@
#![cfg_attr(not(feature = "rt"), allow(dead_code))] #![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; mod registration;
pub(crate) use registration::Registration; pub(crate) use registration::Registration;
@@ -16,6 +8,8 @@ use scheduled_io::ScheduledIo;
mod metrics; mod metrics;
use crate::io::interest::Interest;
use crate::io::ready::Ready;
use crate::park::{Park, Unpark}; use crate::park::{Park, Unpark};
use crate::util::slab::{self, Slab}; use crate::util::slab::{self, Slab};
use crate::{loom::sync::RwLock, util::bit}; use crate::{loom::sync::RwLock, util::bit};
@@ -268,7 +262,7 @@ cfg_rt! {
/// This function panics if there is no current reactor set and `rt` feature /// This function panics if there is no current reactor set and `rt` feature
/// flag is not enabled. /// flag is not enabled.
#[track_caller] #[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.") 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.")
} }
} }
@@ -283,7 +277,7 @@ cfg_not_rt! {
/// This function panics if there is no current reactor set, or if the `rt` /// This function panics if there is no current reactor set, or if the `rt`
/// feature flag is not enabled. /// feature flag is not enabled.
#[track_caller] #[track_caller]
pub(super) fn current() -> Self { pub(crate) fn current() -> Self {
panic!("{}", crate::util::error::CONTEXT_MISSING_ERROR) panic!("{}", crate::util::error::CONTEXT_MISSING_ERROR)
} }
} }
@@ -1,6 +1,7 @@
#![cfg_attr(not(feature = "net"), allow(dead_code))] #![cfg_attr(not(feature = "net"), allow(dead_code))]
use crate::io::driver::{Direction, Handle, Interest, ReadyEvent, ScheduledIo}; use crate::io::interest::Interest;
use crate::runtime::io::{Direction, Handle, ReadyEvent, ScheduledIo};
use crate::util::slab; use crate::util::slab;
use mio::event::Source; use mio::event::Source;
@@ -1,4 +1,6 @@
use super::{Interest, Ready, ReadyEvent, Tick}; use super::{ReadyEvent, Tick};
use crate::io::interest::Interest;
use crate::io::ready::Ready;
use crate::loom::sync::atomic::AtomicUsize; use crate::loom::sync::atomic::AtomicUsize;
use crate::loom::sync::Mutex; use crate::loom::sync::Mutex;
use crate::util::bit; use crate::util::bit;
+22 -18
View File
@@ -178,27 +178,15 @@
#[macro_use] #[macro_use]
mod tests; mod tests;
pub(crate) mod enter; cfg_io_driver_impl! {
pub(crate) mod io;
pub(crate) mod task;
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! { cfg_rt! {
pub(crate) mod enter;
pub(crate) mod task;
mod basic_scheduler; mod basic_scheduler;
use basic_scheduler::BasicScheduler; use basic_scheduler::BasicScheduler;
@@ -235,6 +223,22 @@ cfg_rt! {
mod spawner; mod spawner;
use self::spawner::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! { cfg_rt_multi_thread! {
+4
View File
@@ -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; use self::unowned_wrapper::unowned;
mod unowned_wrapper { mod unowned_wrapper {
+2 -1
View File
@@ -2,9 +2,10 @@
//! Signal driver //! Signal driver
use crate::io::driver::{Driver as IoDriver, Interest}; use crate::io::interest::Interest;
use crate::io::PollEvented; use crate::io::PollEvented;
use crate::park::Park; use crate::park::Park;
use crate::runtime::io::Driver as IoDriver;
use crate::signal::registry::globals; use crate::signal::registry::globals;
use mio::net::UnixStream; use mio::net::UnixStream;