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: keep driver cfgs in driver.rs #5141

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tokio/src/runtime/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ impl Handle {
.as_ref()
.expect("A Tokio 1.x context was found, but timers are disabled. Call `enable_time` on the runtime builder to enable timers.")
}

cfg_test_util! {
pub(crate) fn clock(&self) -> &Clock {
&self.clock
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions tokio/src/runtime/io/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Registration {
interest: Interest,
handle: scheduler::Handle,
) -> io::Result<Registration> {
let shared = handle.io().add_source(io, interest)?;
let shared = handle.driver().io().add_source(io, interest)?;

Ok(Registration { handle, shared })
}
Expand Down Expand Up @@ -199,7 +199,7 @@ impl Registration {
}

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

Expand Down
20 changes: 0 additions & 20 deletions tokio/src/runtime/scheduler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,6 @@ impl Handle {
Handle::Disabled => unreachable!(),
}
}

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 {
self.driver().time()
}

cfg_test_util! {
pub(crate) fn clock(&self) -> &driver::Clock {
&self.driver().clock
}
}
}
}

cfg_rt! {
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/runtime/time/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ impl TimerEntry {
#[track_caller]
pub(crate) fn new(handle: &scheduler::Handle, deadline: Instant) -> Self {
// Panic if the time driver is not enabled
let _ = handle.time();
let _ = handle.driver().time();

let driver = handle.clone();

Expand Down Expand Up @@ -577,7 +577,7 @@ impl TimerEntry {
}

pub(crate) fn driver(&self) -> &super::Handle {
self.driver.time()
self.driver.driver().time()
}
}

Expand Down
24 changes: 12 additions & 12 deletions tokio/src/runtime/time/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn single_timer() {
let jh = thread::spawn(move || {
let entry = TimerEntry::new(
&handle_.inner,
handle_.inner.clock().now() + Duration::from_secs(1),
handle_.inner.driver().clock().now() + Duration::from_secs(1),
);
pin!(entry);

Expand All @@ -62,7 +62,7 @@ fn single_timer() {

thread::yield_now();

let handle = handle.inner.time();
let handle = handle.inner.driver().time();

// This may or may not return Some (depending on how it races with the
// thread). If it does return None, however, the timer should complete
Expand All @@ -83,7 +83,7 @@ fn drop_timer() {
let jh = thread::spawn(move || {
let entry = TimerEntry::new(
&handle_.inner,
handle_.inner.clock().now() + Duration::from_secs(1),
handle_.inner.driver().clock().now() + Duration::from_secs(1),
);
pin!(entry);

Expand All @@ -97,7 +97,7 @@ fn drop_timer() {

thread::yield_now();

let handle = handle.inner.time();
let handle = handle.inner.driver().time();

// advance 2s in the future.
handle.process_at_time(handle.time_source().now() + 2_000_000_000);
Expand All @@ -116,7 +116,7 @@ fn change_waker() {
let jh = thread::spawn(move || {
let entry = TimerEntry::new(
&handle_.inner,
handle_.inner.clock().now() + Duration::from_secs(1),
handle_.inner.driver().clock().now() + Duration::from_secs(1),
);
pin!(entry);

Expand All @@ -132,7 +132,7 @@ fn change_waker() {

thread::yield_now();

let handle = handle.inner.time();
let handle = handle.inner.driver().time();

// advance 2s
handle.process_at_time(handle.time_source().now() + 2_000_000_000);
Expand All @@ -151,7 +151,7 @@ fn reset_future() {

let handle_ = handle.clone();
let finished_early_ = finished_early.clone();
let start = handle.inner.clock().now();
let start = handle.inner.driver().clock().now();

let jh = thread::spawn(move || {
let entry = TimerEntry::new(&handle_.inner, start + Duration::from_secs(1));
Expand All @@ -174,7 +174,7 @@ fn reset_future() {

thread::yield_now();

let handle = handle.inner.time();
let handle = handle.inner.driver().time();

// This may or may not return a wakeup time.
handle.process_at_time(
Expand Down Expand Up @@ -217,7 +217,7 @@ fn poll_process_levels() {
for i in 0..normal_or_miri(1024, 64) {
let mut entry = Box::pin(TimerEntry::new(
&handle.inner,
handle.inner.clock().now() + Duration::from_millis(i),
handle.inner.driver().clock().now() + Duration::from_millis(i),
));

let _ = entry
Expand All @@ -228,7 +228,7 @@ fn poll_process_levels() {
}

for t in 1..normal_or_miri(1024, 64) {
handle.inner.time().process_at_time(t as u64);
handle.inner.driver().time().process_at_time(t as u64);

for (deadline, future) in entries.iter_mut().enumerate() {
let mut context = Context::from_waker(noop_waker_ref());
Expand All @@ -251,11 +251,11 @@ fn poll_process_levels_targeted() {

let e1 = TimerEntry::new(
&handle.inner,
handle.inner.clock().now() + Duration::from_millis(193),
handle.inner.driver().clock().now() + Duration::from_millis(193),
);
pin!(e1);

let handle = handle.inner.time();
let handle = handle.inner.driver().time();

handle.process_at_time(62);
assert!(e1.as_mut().poll_elapsed(&mut context).is_pending());
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/time/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ cfg_test_util! {
use crate::runtime::Handle;

match Handle::try_current() {
Ok(handle) => Some(handle.inner.clock().clone()),
Ok(handle) => Some(handle.inner.driver().clock().clone()),
Err(ref e) if e.is_missing_context() => None,
Err(_) => panic!("{}", crate::util::error::THREAD_LOCAL_DESTROYED_ERROR),
}
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/time/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl Sleep {

#[cfg(all(tokio_unstable, feature = "tracing"))]
let inner = {
let handle = &handle.time();
let handle = &handle.driver().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());
Expand Down