Get rid of Enter for with_default (#1315)

We want executors to enforce that there are never multiple active at the
same time. This is ensured through `Enter`, which will panic if you
attempt to create more than one. However, by requiring you to pass an
`&mut Enter` to `executor::with_default`, we were *also* disallowing
temporarily overriding the current executor.

This patch removes that requirement.
This commit is contained in:
Jon Gjengset
2019-07-16 14:29:35 -04:00
committed by GitHub
parent 6d186fe40e
commit 003b4d8074
10 changed files with 49 additions and 77 deletions
+14 -21
View File
@@ -41,7 +41,7 @@ use std::task::{Context, Poll, Waker};
use std::thread;
use std::time::{Duration, Instant};
use tokio_executor::park::{Park, ParkThread, Unpark};
use tokio_executor::{Enter, SpawnError};
use tokio_executor::SpawnError;
/// Executes tasks on the current thread
pub struct CurrentThread<P: Park = ParkThread> {
@@ -96,7 +96,6 @@ impl Turn {
/// A `CurrentThread` instance bound to a supplied execution context.
pub struct Entered<'a, P: Park> {
executor: &'a mut CurrentThread<P>,
enter: &'a mut Enter,
}
/// Error returned by the `run` function.
@@ -322,38 +321,35 @@ impl<P: Park> CurrentThread<P> {
where
F: Future,
{
let mut enter = tokio_executor::enter().expect("failed to start `current_thread::Runtime`");
self.enter(&mut enter).block_on(future)
let _enter = tokio_executor::enter().expect("failed to start `current_thread::Runtime`");
self.enter().block_on(future)
}
/// Run the executor to completion, blocking the thread until **all**
/// spawned futures have completed.
pub fn run(&mut self) -> Result<(), RunError> {
let mut enter = tokio_executor::enter().expect("failed to start `current_thread::Runtime`");
self.enter(&mut enter).run()
let _enter = tokio_executor::enter().expect("failed to start `current_thread::Runtime`");
self.enter().run()
}
/// Run the executor to completion, blocking the thread until all
/// spawned futures have completed **or** `duration` time has elapsed.
pub fn run_timeout(&mut self, duration: Duration) -> Result<(), RunTimeoutError> {
let mut enter = tokio_executor::enter().expect("failed to start `current_thread::Runtime`");
self.enter(&mut enter).run_timeout(duration)
let _enter = tokio_executor::enter().expect("failed to start `current_thread::Runtime`");
self.enter().run_timeout(duration)
}
/// Perform a single iteration of the event loop.
///
/// This function blocks the current thread even if the executor is idle.
pub fn turn(&mut self, duration: Option<Duration>) -> Result<Turn, TurnError> {
let mut enter = tokio_executor::enter().expect("failed to start `current_thread::Runtime`");
self.enter(&mut enter).turn(duration)
let _enter = tokio_executor::enter().expect("failed to start `current_thread::Runtime`");
self.enter().turn(duration)
}
/// Bind `CurrentThread` instance with an execution context.
pub fn enter<'a>(&'a mut self, enter: &'a mut Enter) -> Entered<'a, P> {
Entered {
executor: self,
enter,
}
fn enter<'a>(&'a mut self) -> Entered<'a, P> {
Entered { executor: self }
}
/// Returns a reference to the underlying `Park` instance.
@@ -478,7 +474,7 @@ impl<'a, P: Park> Entered<'a, P> {
let res = self
.executor
.borrow()
.enter(self.enter, || future.as_mut().poll(&mut cx));
.enter(|| future.as_mut().poll(&mut cx));
match res {
Poll::Ready(e) => return e,
@@ -595,9 +591,7 @@ impl<'a, P: Park> Entered<'a, P> {
}
// After any pending futures were scheduled, do the actual tick
borrow
.scheduler
.tick(borrow.id, &mut *self.enter, borrow.num_futures)
borrow.scheduler.tick(borrow.id, borrow.num_futures)
}
}
@@ -605,7 +599,6 @@ impl<'a, P: Park> fmt::Debug for Entered<'a, P> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("Entered")
.field("executor", &self.executor)
.field("enter", &self.enter)
.finish()
}
}
@@ -748,7 +741,7 @@ where
// ===== impl Borrow =====
impl<'a, U: Unpark> Borrow<'a, U> {
fn enter<F, R>(&mut self, _: &mut Enter, f: F) -> R
fn enter<F, R>(&mut self, f: F) -> R
where
F: FnOnce() -> R,
{
+3 -7
View File
@@ -12,7 +12,6 @@ use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
use std::thread;
use std::usize;
use tokio_executor::park::Unpark;
use tokio_executor::Enter;
/// A generic task-aware scheduler.
///
@@ -199,7 +198,7 @@ where
///
/// This function should be called whenever the caller is notified via a
/// wakeup.
pub fn tick(&mut self, eid: u64, enter: &mut Enter, num_futures: &AtomicUsize) -> bool {
pub fn tick(&mut self, eid: u64, num_futures: &AtomicUsize) -> bool {
let mut ret = false;
let tick = self.inner.tick_num.fetch_add(1, SeqCst).wrapping_add(1);
@@ -251,14 +250,13 @@ where
//
struct Bomb<'a, U: Unpark> {
borrow: &'a mut Borrow<'a, U>,
enter: &'a mut Enter,
node: Option<Arc<Node<U>>>,
}
impl<'a, U: Unpark> Drop for Bomb<'a, U> {
fn drop(&mut self) {
if let Some(node) = self.node.take() {
self.borrow.enter(self.enter, || release_node(node))
self.borrow.enter(|| release_node(node))
}
}
}
@@ -273,7 +271,6 @@ where
let mut bomb = Bomb {
node: Some(node),
enter: enter,
borrow: &mut borrow,
};
@@ -308,7 +305,6 @@ where
// the internal allocation, appropriately accessing fields and
// deallocating the node if need be.
let borrow = &mut *bomb.borrow;
let enter = &mut *bomb.enter;
let mut scheduled = Scheduled {
task: item,
@@ -316,7 +312,7 @@ where
done: &mut done,
};
if borrow.enter(enter, || scheduled.tick()) {
if borrow.enter(|| scheduled.tick()) {
// we have a borrow of the Runtime, so we know it's not shut down
borrow.num_futures.fetch_sub(2, SeqCst);
}