threadpool: update to std::future (#1219)

An initial pass at updating `tokio-threadpool` to `std::future`. The
codebase and tests both now run using `std::future` but the wake
mechanism is not ideal. Follow up work will be required to improve on
this.

Refs: #1200
This commit is contained in:
Carl Lerche
2019-06-27 22:30:56 -07:00
committed by GitHub
parent e4415d986a
commit e7488d983e
17 changed files with 436 additions and 485 deletions
+20 -11
View File
@@ -1,9 +1,13 @@
use crate::task::Task;
use crate::worker;
use tokio_sync::task::AtomicWaker;
use crossbeam_deque::Injector;
use futures::task::AtomicTask;
use futures::{Async, Future, Poll};
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
/// Future that resolves when the thread pool is shutdown.
///
@@ -27,7 +31,7 @@ pub struct Shutdown {
#[derive(Debug)]
struct Inner {
/// The task to notify when the threadpool completes the shutdown process.
task: AtomicTask,
task: AtomicWaker,
/// `true` if the threadpool has been shut down.
completed: bool,
}
@@ -38,20 +42,25 @@ impl Shutdown {
inner: trigger.inner.clone(),
}
}
/// Wait for the shutdown to complete
pub fn wait(self) {
let mut enter = tokio_executor::enter().unwrap();
enter.block_on(self);
}
}
impl Future for Shutdown {
type Item = ();
type Error = ();
type Output = ();
fn poll(&mut self) -> Poll<(), ()> {
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
let inner = self.inner.lock().unwrap();
if !inner.completed {
inner.task.register();
Ok(Async::NotReady)
inner.task.register_by_ref(cx.waker());
Poll::Pending
} else {
Ok(().into())
Poll::Ready(())
}
}
}
@@ -74,7 +83,7 @@ impl ShutdownTrigger {
) -> ShutdownTrigger {
ShutdownTrigger {
inner: Arc::new(Mutex::new(Inner {
task: AtomicTask::new(),
task: AtomicWaker::new(),
completed: false,
})),
workers,
@@ -96,6 +105,6 @@ impl Drop for ShutdownTrigger {
// Notify the task interested in shutdown.
let mut inner = self.inner.lock().unwrap();
inner.completed = true;
inner.task.notify();
inner.task.wake();
}
}