Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rt: switch io::handle refs with scheduler:Handle #5128

Merged
merged 2 commits into from Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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.")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an existing test that expected IO to be spelled this way. Instead of updating the test, I changed the message back to what it was before.

}
}

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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving the current() function to scheduler::Handle makes it easier to get the conditional compilation tests to pass.

Eventually, runtime::context will be moved into here, and this will be the primary type to track the "current runtime." I am punting this work to a later PR.

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)
}
}
}
113 changes: 49 additions & 64 deletions tokio/src/time/sleep.rs
Expand Up @@ -248,76 +248,61 @@ cfg_not_trace! {
}

impl Sleep {
cfg_rt! {
#[cfg_attr(not(all(tokio_unstable, feature = "tracing")), allow(unused_variables))]
#[track_caller]
pub(crate) fn new_timeout(
deadline: Instant,
location: Option<&'static Location<'static>>,
) -> Sleep {
use crate::runtime::Handle;

let handle = Handle::current().inner;
let entry = TimerEntry::new(&handle, deadline);

#[cfg(all(tokio_unstable, feature = "tracing"))]
let inner = {
let handle = &handle.time();
let time_source = handle.time_source();
let deadline_tick = time_source.deadline_to_tick(deadline);
let duration = deadline_tick.saturating_sub(time_source.now());

let location = location.expect("should have location if tracing");
let resource_span = tracing::trace_span!(
"runtime.resource",
concrete_type = "Sleep",
kind = "timer",
loc.file = location.file(),
loc.line = location.line(),
loc.col = location.column(),
#[cfg_attr(not(all(tokio_unstable, feature = "tracing")), allow(unused_variables))]
#[track_caller]
pub(crate) fn new_timeout(
deadline: Instant,
location: Option<&'static Location<'static>>,
) -> Sleep {
use crate::runtime::scheduler;

let handle = scheduler::Handle::current();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this change, the conditional compilation test complains that scheduler::Handle::current() is not used when only time is enabled. I opted to do the work to update this code here instead of silencing the warning temporarily.

let entry = TimerEntry::new(&handle, deadline);

#[cfg(all(tokio_unstable, feature = "tracing"))]
let inner = {
let handle = &handle.time();
let time_source = handle.time_source();
let deadline_tick = time_source.deadline_to_tick(deadline);
let duration = deadline_tick.saturating_sub(time_source.now());

let location = location.expect("should have location if tracing");
let resource_span = tracing::trace_span!(
"runtime.resource",
concrete_type = "Sleep",
kind = "timer",
loc.file = location.file(),
loc.line = location.line(),
loc.col = location.column(),
);

let async_op_span = resource_span.in_scope(|| {
tracing::trace!(
target: "runtime::resource::state_update",
duration = duration,
duration.unit = "ms",
duration.op = "override",
);

let async_op_span = resource_span.in_scope(|| {
tracing::trace!(
target: "runtime::resource::state_update",
duration = duration,
duration.unit = "ms",
duration.op = "override",
);

tracing::trace_span!("runtime.resource.async_op", source = "Sleep::new_timeout")
});

let async_op_poll_span =
async_op_span.in_scope(|| tracing::trace_span!("runtime.resource.async_op.poll"));

let ctx = trace::AsyncOpTracingCtx {
async_op_span,
async_op_poll_span,
resource_span,
};

Inner {
deadline,
ctx,
}
tracing::trace_span!("runtime.resource.async_op", source = "Sleep::new_timeout")
});

let async_op_poll_span =
async_op_span.in_scope(|| tracing::trace_span!("runtime.resource.async_op.poll"));

let ctx = trace::AsyncOpTracingCtx {
async_op_span,
async_op_poll_span,
resource_span,
};

#[cfg(not(all(tokio_unstable, feature = "tracing")))]
let inner = Inner { deadline };
Inner { deadline, ctx }
};

Sleep { inner, entry }
}
}
#[cfg(not(all(tokio_unstable, feature = "tracing")))]
let inner = Inner { deadline };

cfg_not_rt! {
#[track_caller]
pub(crate) fn new_timeout(
_deadline: Instant,
_location: Option<&'static Location<'static>>,
) -> Sleep {
panic!("{}", crate::util::error::CONTEXT_MISSING_ERROR)
}
Sleep { inner, entry }
}

pub(crate) fn far_future(location: Option<&'static Location<'static>>) -> Sleep {
Expand Down