mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-08 00:00:13 +02:00
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:
@@ -1,16 +1,18 @@
|
||||
#![deny(warnings, rust_2018_idioms)]
|
||||
#![feature(async_await)]
|
||||
|
||||
use futures::{Future, Poll, Sink, Stream};
|
||||
use tokio_sync::{mpsc, oneshot};
|
||||
use tokio_threadpool::*;
|
||||
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::sync::atomic::Ordering::*;
|
||||
use std::sync::Arc;
|
||||
use tokio_threadpool::*;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
#[test]
|
||||
fn hammer() {
|
||||
use futures::future;
|
||||
use futures::sync::{mpsc, oneshot};
|
||||
|
||||
const N: usize = 1000;
|
||||
const ITER: usize = 20;
|
||||
|
||||
@@ -20,11 +22,13 @@ fn hammer() {
|
||||
}
|
||||
|
||||
impl<T: Future> Future for Counted<T> {
|
||||
type Item = T::Item;
|
||||
type Error = T::Error;
|
||||
type Output = T::Output;
|
||||
|
||||
fn poll(&mut self) -> Poll<T::Item, T::Error> {
|
||||
self.inner.poll()
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T::Output> {
|
||||
unsafe {
|
||||
let inner = &mut self.get_unchecked_mut().inner;
|
||||
Pin::new_unchecked(inner).poll(cx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,13 +45,30 @@ fn hammer() {
|
||||
|
||||
let cnt = Arc::new(AtomicUsize::new(0));
|
||||
|
||||
let (listen_tx, listen_rx) = mpsc::unbounded::<oneshot::Sender<oneshot::Sender<()>>>();
|
||||
let mut listen_tx = listen_tx.wait();
|
||||
let (mut listen_tx, mut listen_rx) =
|
||||
mpsc::unbounded_channel::<oneshot::Sender<oneshot::Sender<()>>>();
|
||||
|
||||
pool.spawn({
|
||||
let c1 = cnt.clone();
|
||||
let c2 = cnt.clone();
|
||||
let pool = pool.sender().clone();
|
||||
let task = async move {
|
||||
while let Some(tx) = listen_rx.recv().await {
|
||||
let task = async {
|
||||
let (tx2, rx2) = oneshot::channel();
|
||||
tx.send(tx2).unwrap();
|
||||
rx2.await.unwrap()
|
||||
};
|
||||
|
||||
pool.spawn(Counted {
|
||||
inner: task,
|
||||
cnt: c1.clone(),
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
let task = listen_rx
|
||||
.map_err(|e| panic!("accept error = {:?}", e))
|
||||
.for_each(move |tx| {
|
||||
@@ -68,6 +89,7 @@ fn hammer() {
|
||||
|
||||
Ok(())
|
||||
});
|
||||
*/
|
||||
|
||||
Counted {
|
||||
inner: task,
|
||||
@@ -78,21 +100,28 @@ fn hammer() {
|
||||
for _ in 0..N {
|
||||
let cnt = cnt.clone();
|
||||
let (tx, rx) = oneshot::channel();
|
||||
listen_tx.send(tx).unwrap();
|
||||
listen_tx.try_send(tx).unwrap();
|
||||
|
||||
pool.spawn({
|
||||
pool.spawn(async {
|
||||
let task = async {
|
||||
let tx = rx.await.unwrap();
|
||||
tx.send(()).unwrap();
|
||||
};
|
||||
|
||||
/*
|
||||
let task = rx.map_err(|e| panic!("rx err={:?}", e)).and_then(|tx| {
|
||||
tx.send(()).unwrap();
|
||||
Ok(())
|
||||
});
|
||||
*/
|
||||
|
||||
Counted { inner: task, cnt }
|
||||
Counted { inner: task, cnt }.await
|
||||
});
|
||||
}
|
||||
|
||||
drop(listen_tx);
|
||||
|
||||
pool.shutdown_on_idle().wait().unwrap();
|
||||
pool.shutdown_on_idle().wait();
|
||||
assert_eq!(N * 2 + 1, cnt.load(Relaxed));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user