Skip to content

Commit

Permalink
rt: switch io::handle refs with scheduler:Handle (#5128)
Browse files Browse the repository at this point in the history
The `schedule::Handle` reference is the internal runtime handle. This
patch replaces owned refs to `runtime::io::Handle` with
`scheduler::Handle`.
  • Loading branch information
carllerche committed Oct 27, 2022
1 parent 58c4571 commit cb67f28
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 139 deletions.
8 changes: 5 additions & 3 deletions tokio/src/io/async_fd.rs
@@ -1,5 +1,6 @@
use crate::io::Interest;
use crate::runtime::io::{Handle, ReadyEvent, Registration};
use crate::runtime::io::{ReadyEvent, Registration};
use crate::runtime::scheduler;

use mio::unix::SourceFd;
use std::io;
Expand Down Expand Up @@ -200,12 +201,13 @@ impl<T: AsRawFd> AsyncFd<T> {
where
T: AsRawFd,
{
Self::new_with_handle_and_interest(inner, Handle::current(), interest)
Self::new_with_handle_and_interest(inner, scheduler::Handle::current(), interest)
}

#[track_caller]
pub(crate) fn new_with_handle_and_interest(
inner: T,
handle: Handle,
handle: scheduler::Handle,
interest: Interest,
) -> io::Result<Self> {
let fd = inner.as_raw_fd();
Expand Down
5 changes: 3 additions & 2 deletions tokio/src/io/bsd/poll_aio.rs
@@ -1,7 +1,8 @@
//! Use POSIX AIO futures with Tokio.

use crate::io::interest::Interest;
use crate::runtime::io::{Handle, ReadyEvent, Registration};
use crate::runtime::io::{ReadyEvent, Registration};
use crate::runtime::scheduler;
use mio::event::Source;
use mio::Registry;
use mio::Token;
Expand Down Expand Up @@ -118,7 +119,7 @@ impl<E: AioSource> Aio<E> {

fn new_with_interest(io: E, interest: Interest) -> io::Result<Self> {
let mut io = MioSource(io);
let handle = Handle::current();
let handle = scheduler::Handle::current();
let registration = Registration::new_with_interest_and_handle(&mut io, interest, handle)?;
Ok(Self { io, registration })
}
Expand Down
8 changes: 5 additions & 3 deletions tokio/src/io/poll_evented.rs
@@ -1,5 +1,6 @@
use crate::io::interest::Interest;
use crate::runtime::io::{Handle, Registration};
use crate::runtime::io::Registration;
use crate::runtime::scheduler;

use mio::event::Source;
use std::fmt;
Expand Down Expand Up @@ -103,13 +104,14 @@ impl<E: Source> PollEvented<E> {
#[track_caller]
#[cfg_attr(feature = "signal", allow(unused))]
pub(crate) fn new_with_interest(io: E, interest: Interest) -> io::Result<Self> {
Self::new_with_interest_and_handle(io, interest, Handle::current())
Self::new_with_interest_and_handle(io, interest, scheduler::Handle::current())
}

#[track_caller]
pub(crate) fn new_with_interest_and_handle(
mut io: E,
interest: Interest,
handle: Handle,
handle: scheduler::Handle,
) -> io::Result<Self> {
let registration = Registration::new_with_interest_and_handle(&mut io, interest, handle)?;
Ok(Self {
Expand Down
18 changes: 0 additions & 18 deletions tokio/src/runtime/context.rs
Expand Up @@ -24,24 +24,6 @@ pub(crate) fn current() -> Handle {
}
}

cfg_io_driver! {
#[track_caller]
pub(crate) fn io_handle() -> crate::runtime::driver::IoHandle {
match CONTEXT.try_with(|ctx| {
let ctx = ctx.borrow();
ctx.as_ref()
.expect(crate::util::error::CONTEXT_MISSING_ERROR)
.inner
.driver()
.io
.clone()
}) {
Ok(io_handle) => io_handle,
Err(_) => panic!("{}", crate::util::error::THREAD_LOCAL_DESTROYED_ERROR),
}
}
}

cfg_signal_internal! {
#[cfg(unix)]
pub(crate) fn signal_handle() -> crate::runtime::driver::SignalHandle {
Expand Down
11 changes: 2 additions & 9 deletions tokio/src/runtime/driver.rs
Expand Up @@ -82,10 +82,11 @@ impl Handle {
}

cfg_io_driver! {
#[track_caller]
pub(crate) fn io(&self) -> &crate::runtime::io::Handle {
self.io
.as_ref()
.expect("A Tokio 1.x context was found, but I/O is disabled. Call `enable_io` on the runtime builder to enable I/O.")
.expect("A Tokio 1.x context was found, but IO is disabled. Call `enable_io` on the runtime builder to enable IO.")
}
}

Expand Down Expand Up @@ -170,14 +171,6 @@ cfg_io_driver! {
}
}

#[track_caller]
pub(crate) fn expect(self, msg: &'static str) -> crate::runtime::io::Handle {
match self {
IoHandle::Enabled(v) => v,
IoHandle::Disabled(..) => panic!("{}", msg),
}
}

pub(crate) fn as_ref(&self) -> Option<&crate::runtime::io::Handle> {
match self {
IoHandle::Enabled(v) => Some(v),
Expand Down
4 changes: 3 additions & 1 deletion tokio/src/runtime/handle.rs
Expand Up @@ -96,7 +96,9 @@ impl Handle {
/// ```
#[track_caller]
pub fn current() -> Self {
context::current()
Handle {
inner: scheduler::Handle::current(),
}
}

/// Returns a Handle view over the currently running Runtime
Expand Down
32 changes: 0 additions & 32 deletions tokio/src/runtime/io/mod.rs
Expand Up @@ -236,38 +236,6 @@ impl fmt::Debug for Driver {
}
}

// ===== impl Handle =====

cfg_rt! {
impl Handle {
/// Returns a handle to the current reactor.
///
/// # Panics
///
/// This function panics if there is no current reactor set and `rt` feature
/// flag is not enabled.
#[track_caller]
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.")
}
}
}

cfg_not_rt! {
impl Handle {
/// Returns a handle to the current reactor.
///
/// # Panics
///
/// This function panics if there is no current reactor set, or if the `rt`
/// feature flag is not enabled.
#[track_caller]
pub(crate) fn current() -> Self {
panic!("{}", crate::util::error::CONTEXT_MISSING_ERROR)
}
}
}

cfg_net! {
cfg_metrics! {
impl Handle {
Expand Down
20 changes: 13 additions & 7 deletions tokio/src/runtime/io/registration.rs
Expand Up @@ -2,6 +2,7 @@

use crate::io::interest::Interest;
use crate::runtime::io::{Direction, Handle, ReadyEvent, ScheduledIo};
use crate::runtime::scheduler;
use crate::util::slab;

use mio::event::Source;
Expand Down Expand Up @@ -43,8 +44,8 @@ cfg_io_driver! {
/// [`poll_write_ready`]: method@Self::poll_write_ready`
#[derive(Debug)]
pub(crate) struct Registration {
/// Handle to the associated driver.
handle: Handle,
/// Handle to the associated runtime.
handle: scheduler::Handle,

/// Reference to state stored by the driver.
shared: slab::Ref<ScheduledIo>,
Expand All @@ -66,12 +67,13 @@ impl Registration {
///
/// - `Ok` if the registration happened successfully
/// - `Err` if an error was encountered during registration
#[track_caller]
pub(crate) fn new_with_interest_and_handle(
io: &mut impl Source,
interest: Interest,
handle: Handle,
handle: scheduler::Handle,
) -> io::Result<Registration> {
let shared = handle.inner.add_source(io, interest)?;
let shared = handle.io().inner.add_source(io, interest)?;

Ok(Registration { handle, shared })
}
Expand All @@ -93,7 +95,7 @@ impl Registration {
///
/// `Err` is returned if an error is encountered.
pub(crate) fn deregister(&mut self, io: &mut impl Source) -> io::Result<()> {
self.handle.inner.deregister_source(io)
self.handle().inner.deregister_source(io)
}

pub(crate) fn clear_readiness(&self, event: ReadyEvent) {
Expand Down Expand Up @@ -146,7 +148,7 @@ impl Registration {
let coop = ready!(crate::coop::poll_proceed(cx));
let ev = ready!(self.shared.poll_readiness(cx, direction));

if self.handle.inner.is_shutdown() {
if self.handle().inner.is_shutdown() {
return Poll::Ready(Err(gone()));
}

Expand Down Expand Up @@ -195,6 +197,10 @@ impl Registration {
res => res,
}
}

fn handle(&self) -> &Handle {
self.handle.io()
}
}

impl Drop for Registration {
Expand Down Expand Up @@ -224,7 +230,7 @@ cfg_io_readiness! {
pin!(fut);

crate::future::poll_fn(|cx| {
if self.handle.inner.is_shutdown() {
if self.handle().inner.is_shutdown() {
return Poll::Ready(Err(io::Error::new(
io::ErrorKind::Other,
crate::util::error::RUNTIME_SHUTTING_DOWN_ERROR
Expand Down
21 changes: 21 additions & 0 deletions tokio/src/runtime/scheduler/mod.rs
Expand Up @@ -40,6 +40,13 @@ impl Handle {
}
}

cfg_io_driver! {
#[track_caller]
pub(crate) fn io(&self) -> &crate::runtime::io::Handle {
self.driver().io()
}
}

cfg_time! {
#[track_caller]
pub(crate) fn time(&self) -> &crate::runtime::time::Handle {
Expand All @@ -62,6 +69,11 @@ cfg_rt! {
use crate::util::RngSeedGenerator;

impl Handle {
#[track_caller]
pub(crate) fn current() -> Handle {
crate::runtime::context::current().inner
}

pub(crate) fn blocking_spawner(&self) -> &blocking::Spawner {
match self {
Handle::CurrentThread(h) => &h.blocking_spawner,
Expand Down Expand Up @@ -156,3 +168,12 @@ cfg_rt! {
}
}
}

cfg_not_rt! {
impl Handle {
#[track_caller]
pub(crate) fn current() -> Handle {
panic!("{}", crate::util::error::CONTEXT_MISSING_ERROR)
}
}
}

0 comments on commit cb67f28

Please sign in to comment.