Skip to content

Commit

Permalink
BlockingSchedule::new: Avoid calling Handle::current().
Browse files Browse the repository at this point in the history
It does not always work; and is it happens, the caller has a handle already and
can just pass it in.
  • Loading branch information
jorendorff committed Nov 16, 2022
1 parent 4eccfcc commit cbb4c38
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion tokio/src/runtime/blocking/pool.rs
Expand Up @@ -379,7 +379,7 @@ impl Spawner {
#[cfg(not(all(tokio_unstable, feature = "tracing")))]
let _ = name;

let (task, handle) = task::unowned(fut, BlockingSchedule::new(), id);
let (task, handle) = task::unowned(fut, BlockingSchedule::new(rt), id);

let spawned = self.spawn_task(Task::new(task, is_mandatory), rt);
(handle, spawned)
Expand Down
30 changes: 16 additions & 14 deletions tokio/src/runtime/blocking/schedule.rs
@@ -1,6 +1,7 @@
use crate::runtime::task::{self, Task};
#[cfg(feature = "test-util")]
use crate::runtime::{scheduler, Handle};
use crate::runtime::scheduler;
use crate::runtime::task::{self, Task};
use crate::runtime::Handle;

/// `task::Schedule` implementation that does nothing (except some bookkeeping
/// in test-util builds). This is unique to the blocking scheduler as tasks
Expand All @@ -14,20 +15,21 @@ pub(crate) struct BlockingSchedule {
}

impl BlockingSchedule {
pub(crate) fn new() -> Self {
#[cfg_attr(not(feature = "test-util"), allow(unused_variables))]
pub(crate) fn new(handle: &Handle) -> Self {
#[cfg(feature = "test-util")]
{
match &handle.inner {
scheduler::Handle::CurrentThread(handle) => {
handle.driver.clock.inhibit_auto_advance();
}
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
scheduler::Handle::MultiThread(_) => {}
}
}
BlockingSchedule {
#[cfg(feature = "test-util")]
handle: {
let handle = Handle::current();
match &handle.inner {
scheduler::Handle::CurrentThread(handle) => {
handle.driver.clock.inhibit_auto_advance();
}
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
scheduler::Handle::MultiThread(_) => {}
}
handle
},
handle: handle.clone(),
}
}
}
Expand Down

0 comments on commit cbb4c38

Please sign in to comment.