mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-28 00:00:11 +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,14 +1,26 @@
|
||||
#![deny(warnings, rust_2018_idioms)]
|
||||
#![deny(/* warnings, */ rust_2018_idioms)]
|
||||
#![feature(async_await)]
|
||||
|
||||
use futures::future::{lazy, poll_fn};
|
||||
use futures::*;
|
||||
use tokio_test::*;
|
||||
use tokio_threadpool::*;
|
||||
|
||||
use async_util::future::poll_fn;
|
||||
use rand::*;
|
||||
use std::sync::atomic::Ordering::*;
|
||||
use std::sync::atomic::*;
|
||||
use std::sync::*;
|
||||
use std::task::{Poll, Waker};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use tokio_threadpool::*;
|
||||
|
||||
macro_rules! ready {
|
||||
($e:expr) => {
|
||||
match $e {
|
||||
::std::task::Poll::Ready(t) => t,
|
||||
::std::task::Poll::Pending => return ::std::task::Poll::Pending,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
@@ -19,21 +31,18 @@ fn basic() {
|
||||
let (tx1, rx1) = mpsc::channel();
|
||||
let (tx2, rx2) = mpsc::channel();
|
||||
|
||||
pool.spawn(lazy(move || {
|
||||
pool.spawn(async move {
|
||||
let res = blocking(|| {
|
||||
let v = rx1.recv().unwrap();
|
||||
tx2.send(v).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
});
|
||||
|
||||
assert!(res.is_ready());
|
||||
Ok(().into())
|
||||
}));
|
||||
assert_ready!(res).unwrap();
|
||||
});
|
||||
|
||||
pool.spawn(lazy(move || {
|
||||
pool.spawn(async move {
|
||||
tx1.send(()).unwrap();
|
||||
Ok(().into())
|
||||
}));
|
||||
});
|
||||
|
||||
rx2.recv().unwrap();
|
||||
}
|
||||
@@ -51,11 +60,11 @@ fn notify_task_on_capacity() {
|
||||
let rem = rem.clone();
|
||||
let tx = tx.clone();
|
||||
|
||||
pool.spawn(lazy(move || {
|
||||
poll_fn(move || {
|
||||
pool.spawn(async move {
|
||||
poll_fn(move |_| {
|
||||
blocking(|| {
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
let prev = rem.fetch_sub(1, Relaxed);
|
||||
let prev = rem.fetch_sub(1, SeqCst);
|
||||
|
||||
if prev == 1 {
|
||||
tx.send(()).unwrap();
|
||||
@@ -63,20 +72,19 @@ fn notify_task_on_capacity() {
|
||||
})
|
||||
.map_err(|e| panic!("blocking err {:?}", e))
|
||||
})
|
||||
}));
|
||||
.await
|
||||
.unwrap()
|
||||
});
|
||||
}
|
||||
|
||||
rx.recv().unwrap();
|
||||
|
||||
assert_eq!(0, rem.load(Relaxed));
|
||||
assert_eq!(0, rem.load(SeqCst));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn capacity_is_use_it_or_lose_it() {
|
||||
use futures::sync::oneshot;
|
||||
use futures::task::Task;
|
||||
use futures::Async::*;
|
||||
use futures::*;
|
||||
use tokio_sync::oneshot;
|
||||
|
||||
// TODO: Run w/ bigger pool size
|
||||
|
||||
@@ -88,74 +96,77 @@ fn capacity_is_use_it_or_lose_it() {
|
||||
let (tx4, rx4) = mpsc::channel();
|
||||
|
||||
// First, fill the blocking capacity
|
||||
pool.spawn(lazy(move || {
|
||||
poll_fn(move || {
|
||||
pool.spawn(async move {
|
||||
poll_fn(move |_| {
|
||||
blocking(|| {
|
||||
rx1.recv().unwrap();
|
||||
})
|
||||
.map_err(|_| panic!())
|
||||
})
|
||||
}));
|
||||
.await
|
||||
.unwrap()
|
||||
});
|
||||
|
||||
pool.spawn(lazy(move || {
|
||||
rx2.map_err(|_| panic!()).and_then(|task: Task| {
|
||||
poll_fn(move || {
|
||||
blocking(|| {
|
||||
// Notify the other task
|
||||
task.notify();
|
||||
pool.spawn(async move {
|
||||
let task: Waker = rx2.await.unwrap();
|
||||
|
||||
// Block until woken
|
||||
rx3.recv().unwrap();
|
||||
})
|
||||
.map_err(|_| panic!())
|
||||
poll_fn(move |_| {
|
||||
blocking(|| {
|
||||
// Notify the other task
|
||||
task.wake_by_ref();
|
||||
|
||||
// Block until woken
|
||||
rx3.recv().unwrap();
|
||||
})
|
||||
})
|
||||
}));
|
||||
.await
|
||||
.unwrap();
|
||||
});
|
||||
|
||||
// Spawn a future that will try to block, get notified, then not actually
|
||||
// use the blocking
|
||||
let mut i = 0;
|
||||
let mut tx2 = Some(tx2);
|
||||
|
||||
pool.spawn(lazy(move || {
|
||||
poll_fn(move || {
|
||||
pool.spawn(async move {
|
||||
poll_fn(move |cx| {
|
||||
match i {
|
||||
0 => {
|
||||
i = 1;
|
||||
|
||||
let res = blocking(|| unreachable!()).map_err(|_| panic!());
|
||||
|
||||
assert!(res.unwrap().is_not_ready());
|
||||
assert_pending!(res);
|
||||
|
||||
// Unblock the first blocker
|
||||
tx1.send(()).unwrap();
|
||||
|
||||
return Ok(NotReady);
|
||||
return Poll::Pending;
|
||||
}
|
||||
1 => {
|
||||
i = 2;
|
||||
|
||||
// Skip blocking, and notify the second task that it should
|
||||
// start blocking
|
||||
let me = task::current();
|
||||
let me = cx.waker().clone();
|
||||
tx2.take().unwrap().send(me).unwrap();
|
||||
|
||||
return Ok(NotReady);
|
||||
return Poll::Pending;
|
||||
}
|
||||
2 => {
|
||||
let res = blocking(|| unreachable!()).map_err(|_| panic!());
|
||||
|
||||
assert!(res.unwrap().is_not_ready());
|
||||
assert_pending!(res);
|
||||
|
||||
// Unblock the first blocker
|
||||
tx3.send(()).unwrap();
|
||||
tx4.send(()).unwrap();
|
||||
Ok(().into())
|
||||
Poll::Ready(())
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
})
|
||||
}));
|
||||
.await
|
||||
});
|
||||
|
||||
rx4.recv().unwrap();
|
||||
}
|
||||
@@ -173,33 +184,35 @@ fn blocking_thread_does_not_take_over_shutdown_worker_thread() {
|
||||
{
|
||||
let exited = exited.clone();
|
||||
|
||||
pool.spawn(lazy(move || {
|
||||
poll_fn(move || {
|
||||
pool.spawn(async move {
|
||||
poll_fn(move |_| {
|
||||
blocking(|| {
|
||||
enter_tx.send(()).unwrap();
|
||||
exit_rx.recv().unwrap();
|
||||
exited.store(true, Relaxed);
|
||||
exited.store(true, SeqCst);
|
||||
})
|
||||
.map_err(|_| panic!())
|
||||
})
|
||||
}));
|
||||
.await
|
||||
.unwrap()
|
||||
});
|
||||
}
|
||||
|
||||
// Wait for the task to block
|
||||
let _ = enter_rx.recv().unwrap();
|
||||
|
||||
// Spawn another task that attempts to block
|
||||
pool.spawn(lazy(move || {
|
||||
poll_fn(move || {
|
||||
let res = blocking(|| {}).unwrap();
|
||||
pool.spawn(async move {
|
||||
poll_fn(move |_| {
|
||||
let res = blocking(|| {});
|
||||
|
||||
assert_eq!(res.is_ready(), exited.load(Relaxed));
|
||||
assert_eq!(res.is_ready(), exited.load(SeqCst));
|
||||
|
||||
try_tx.send(res.is_ready()).unwrap();
|
||||
|
||||
Ok(res)
|
||||
res.map(|_| ())
|
||||
})
|
||||
}));
|
||||
.await
|
||||
});
|
||||
|
||||
// Wait for the second task to try to block (and not be ready).
|
||||
let res = try_rx.recv().unwrap();
|
||||
@@ -230,35 +243,35 @@ fn blocking_one_time_gets_capacity_for_multiple_blocks() {
|
||||
let rem = rem.clone();
|
||||
let tx = tx.clone();
|
||||
|
||||
pool.spawn(lazy(move || {
|
||||
poll_fn(move || {
|
||||
pool.spawn(async move {
|
||||
poll_fn(move |_| {
|
||||
// First block
|
||||
let res = blocking(|| {
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
})
|
||||
.map_err(|e| panic!("blocking err {:?}", e));
|
||||
});
|
||||
|
||||
try_ready!(res);
|
||||
ready!(res).unwrap();
|
||||
|
||||
let res = blocking(|| {
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
let prev = rem.fetch_sub(1, Relaxed);
|
||||
let prev = rem.fetch_sub(1, SeqCst);
|
||||
|
||||
if prev == 1 {
|
||||
tx.send(()).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
assert!(res.unwrap().is_ready());
|
||||
assert!(res.is_ready());
|
||||
|
||||
Ok(().into())
|
||||
Poll::Ready(())
|
||||
})
|
||||
}));
|
||||
.await
|
||||
});
|
||||
}
|
||||
|
||||
rx.recv().unwrap();
|
||||
|
||||
assert_eq!(0, rem.load(Relaxed));
|
||||
assert_eq!(0, rem.load(SeqCst));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -280,10 +293,10 @@ fn shutdown() {
|
||||
.pool_size(1)
|
||||
.max_blocking(BLOCKING)
|
||||
.after_start(move || {
|
||||
num_inc.fetch_add(1, Relaxed);
|
||||
num_inc.fetch_add(1, SeqCst);
|
||||
})
|
||||
.before_stop(move || {
|
||||
num_dec.fetch_add(1, Relaxed);
|
||||
num_dec.fetch_add(1, SeqCst);
|
||||
})
|
||||
.build()
|
||||
};
|
||||
@@ -294,18 +307,16 @@ fn shutdown() {
|
||||
let barrier = barrier.clone();
|
||||
let tx = tx.clone();
|
||||
|
||||
pool.spawn(lazy(move || {
|
||||
pool.spawn(async move {
|
||||
let res = blocking(|| {
|
||||
barrier.wait();
|
||||
Ok::<_, ()>(())
|
||||
})
|
||||
.unwrap();
|
||||
});
|
||||
|
||||
tx.send(()).unwrap();
|
||||
|
||||
assert!(res.is_ready());
|
||||
Ok(().into())
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
for _ in 0..BLOCKING {
|
||||
@@ -315,8 +326,8 @@ fn shutdown() {
|
||||
// Shutdown
|
||||
drop(pool);
|
||||
|
||||
assert_eq!(11, num_inc.load(Relaxed));
|
||||
assert_eq!(11, num_dec.load(Relaxed));
|
||||
assert_eq!(11, num_inc.load(SeqCst));
|
||||
assert_eq!(11, num_dec.load(SeqCst));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -356,10 +367,10 @@ fn hammer() {
|
||||
let cnt_task = cnt_task.clone();
|
||||
let cnt_block = cnt_block.clone();
|
||||
|
||||
pool.spawn(lazy(move || {
|
||||
cnt_task.fetch_add(1, Relaxed);
|
||||
pool.spawn(async move {
|
||||
cnt_task.fetch_add(1, SeqCst);
|
||||
|
||||
poll_fn(move || {
|
||||
poll_fn(move |_| {
|
||||
blocking(|| {
|
||||
match sleep {
|
||||
Skip => {}
|
||||
@@ -375,18 +386,20 @@ fn hammer() {
|
||||
}
|
||||
}
|
||||
|
||||
cnt_block.fetch_add(1, Relaxed);
|
||||
cnt_block.fetch_add(1, SeqCst);
|
||||
})
|
||||
.map_err(|_| panic!())
|
||||
})
|
||||
}));
|
||||
.await
|
||||
.unwrap()
|
||||
});
|
||||
}
|
||||
|
||||
// Wait for the work to complete
|
||||
pool.shutdown_on_idle().wait().unwrap();
|
||||
pool.shutdown_on_idle().wait();
|
||||
|
||||
assert_eq!(n, cnt_task.load(Relaxed));
|
||||
assert_eq!(n, cnt_block.load(Relaxed));
|
||||
assert_eq!(n, cnt_task.load(SeqCst));
|
||||
assert_eq!(n, cnt_block.load(SeqCst));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,21 @@
|
||||
#![deny(warnings, rust_2018_idioms)]
|
||||
#![feature(async_await)]
|
||||
|
||||
use futures::future::lazy;
|
||||
use futures::{Async, Future, Poll, Sink, Stream};
|
||||
use std::cell::Cell;
|
||||
use std::sync::atomic::Ordering::Relaxed;
|
||||
use std::sync::atomic::*;
|
||||
use std::sync::{mpsc, Arc};
|
||||
use std::time::Duration;
|
||||
use tokio_executor::park::{Park, Unpark};
|
||||
use tokio_test::assert_pending;
|
||||
use tokio_threadpool::park::{DefaultPark, DefaultUnpark};
|
||||
use tokio_threadpool::*;
|
||||
|
||||
thread_local!(static FOO: Cell<u32> = Cell::new(0));
|
||||
use std::cell::Cell;
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::sync::atomic::Ordering::Relaxed;
|
||||
use std::sync::atomic::*;
|
||||
use std::sync::{mpsc, Arc};
|
||||
use std::task::{Context, Poll, Waker};
|
||||
use std::time::Duration;
|
||||
|
||||
fn ignore_results<F: Future + Send + 'static>(
|
||||
f: F,
|
||||
) -> Box<dyn Future<Item = (), Error = ()> + Send> {
|
||||
Box::new(f.map(|_| ()).map_err(|_| ()))
|
||||
}
|
||||
thread_local!(static FOO: Cell<u32> = Cell::new(0));
|
||||
|
||||
#[test]
|
||||
fn natural_shutdown_simple_futures() {
|
||||
@@ -47,26 +45,24 @@ fn natural_shutdown_simple_futures() {
|
||||
|
||||
let a = {
|
||||
let (t, rx) = mpsc::channel();
|
||||
tx.spawn(lazy(move || {
|
||||
tx.spawn(async move {
|
||||
// Makes sure this runs on a worker thread
|
||||
FOO.with(|f| assert_eq!(f.get(), 0));
|
||||
|
||||
t.send("one").unwrap();
|
||||
Ok(())
|
||||
}))
|
||||
})
|
||||
.unwrap();
|
||||
rx
|
||||
};
|
||||
|
||||
let b = {
|
||||
let (t, rx) = mpsc::channel();
|
||||
tx.spawn(lazy(move || {
|
||||
tx.spawn(async move {
|
||||
// Makes sure this runs on a worker thread
|
||||
FOO.with(|f| assert_eq!(f.get(), 0));
|
||||
|
||||
t.send("two").unwrap();
|
||||
Ok(())
|
||||
}))
|
||||
})
|
||||
.unwrap();
|
||||
rx
|
||||
};
|
||||
@@ -77,7 +73,7 @@ fn natural_shutdown_simple_futures() {
|
||||
assert_eq!("two", b.recv().unwrap());
|
||||
|
||||
// Wait for the pool to shutdown
|
||||
pool.shutdown().wait().unwrap();
|
||||
pool.shutdown().wait();
|
||||
|
||||
// Assert that at least one thread started
|
||||
let num_inc = num_inc.load(Relaxed);
|
||||
@@ -102,11 +98,10 @@ fn force_shutdown_drops_futures() {
|
||||
struct Never(Arc<AtomicUsize>);
|
||||
|
||||
impl Future for Never {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
type Output = ();
|
||||
|
||||
fn poll(&mut self) -> Poll<(), ()> {
|
||||
Ok(Async::NotReady)
|
||||
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +126,7 @@ fn force_shutdown_drops_futures() {
|
||||
tx.spawn(Never(num_drop.clone())).unwrap();
|
||||
|
||||
// Wait for the pool to shutdown
|
||||
pool.shutdown_now().wait().unwrap();
|
||||
pool.shutdown_now().wait();
|
||||
|
||||
// Assert that only a single thread was spawned.
|
||||
let a = num_inc.load(Relaxed);
|
||||
@@ -159,11 +154,10 @@ fn drop_threadpool_drops_futures() {
|
||||
struct Never(Arc<AtomicUsize>);
|
||||
|
||||
impl Future for Never {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
type Output = ();
|
||||
|
||||
fn poll(&mut self) -> Poll<(), ()> {
|
||||
Ok(Async::NotReady)
|
||||
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,15 +213,14 @@ fn many_oneshot_futures() {
|
||||
|
||||
for _ in 0..NUM {
|
||||
let cnt = cnt.clone();
|
||||
tx.spawn(lazy(move || {
|
||||
tx.spawn(async move {
|
||||
cnt.fetch_add(1, Relaxed);
|
||||
Ok(())
|
||||
}))
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// Wait for the pool to shutdown
|
||||
pool.shutdown().wait().unwrap();
|
||||
pool.shutdown().wait();
|
||||
|
||||
let num = cnt.load(Relaxed);
|
||||
assert_eq!(num, NUM);
|
||||
@@ -236,7 +229,7 @@ fn many_oneshot_futures() {
|
||||
|
||||
#[test]
|
||||
fn many_multishot_futures() {
|
||||
use futures::sync::mpsc;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
const CHAIN: usize = 200;
|
||||
const CYCLES: usize = 5;
|
||||
@@ -255,57 +248,61 @@ fn many_multishot_futures() {
|
||||
let (start_tx, mut chain_rx) = mpsc::channel(10);
|
||||
|
||||
for _ in 0..CHAIN {
|
||||
let (next_tx, next_rx) = mpsc::channel(10);
|
||||
|
||||
let rx = chain_rx.map_err(|e| panic!("{:?}", e));
|
||||
let (mut next_tx, next_rx) = mpsc::channel(10);
|
||||
|
||||
// Forward all the messages
|
||||
pool_tx
|
||||
.spawn(
|
||||
next_tx
|
||||
.send_all(rx)
|
||||
.map(|_| ())
|
||||
.map_err(|e| panic!("{:?}", e)),
|
||||
)
|
||||
.spawn(async move {
|
||||
while let Some(v) = chain_rx.recv().await {
|
||||
next_tx.send(v).await.unwrap();
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
chain_rx = next_rx;
|
||||
}
|
||||
|
||||
// This final task cycles if needed
|
||||
let (final_tx, final_rx) = mpsc::channel(10);
|
||||
let cycle_tx = start_tx.clone();
|
||||
let (mut final_tx, final_rx) = mpsc::channel(10);
|
||||
let mut cycle_tx = start_tx.clone();
|
||||
let mut rem = CYCLES;
|
||||
|
||||
let task = chain_rx.take(CYCLES as u64).for_each(move |msg| {
|
||||
rem -= 1;
|
||||
let send = if rem == 0 {
|
||||
final_tx.clone().send(msg)
|
||||
} else {
|
||||
cycle_tx.clone().send(msg)
|
||||
};
|
||||
pool_tx
|
||||
.spawn(async move {
|
||||
for _ in 0..CYCLES {
|
||||
let msg = chain_rx.recv().await.unwrap();
|
||||
|
||||
send.then(|res| {
|
||||
res.unwrap();
|
||||
Ok(())
|
||||
rem -= 1;
|
||||
|
||||
if rem == 0 {
|
||||
final_tx.send(msg).await.unwrap();
|
||||
} else {
|
||||
cycle_tx.send(msg).await.unwrap();
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
pool_tx.spawn(ignore_results(task)).unwrap();
|
||||
.unwrap();
|
||||
|
||||
start_txs.push(start_tx);
|
||||
final_rxs.push(final_rx);
|
||||
}
|
||||
|
||||
for start_tx in start_txs {
|
||||
start_tx.send("ping").wait().unwrap();
|
||||
}
|
||||
{
|
||||
let mut e = tokio_executor::enter().unwrap();
|
||||
|
||||
for final_rx in final_rxs {
|
||||
final_rx.wait().next().unwrap().unwrap();
|
||||
e.block_on(async move {
|
||||
for mut start_tx in start_txs {
|
||||
start_tx.send("ping").await.unwrap();
|
||||
}
|
||||
|
||||
for mut final_rx in final_rxs {
|
||||
final_rx.recv().await.unwrap();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Shutdown the pool
|
||||
pool.shutdown().wait().unwrap();
|
||||
pool.shutdown().wait();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -316,30 +313,27 @@ fn global_executor_is_configured() {
|
||||
|
||||
let (signal_tx, signal_rx) = mpsc::channel();
|
||||
|
||||
tx.spawn(lazy(move || {
|
||||
tokio_executor::spawn(lazy(move || {
|
||||
tx.spawn(async move {
|
||||
tokio_executor::spawn(async move {
|
||||
signal_tx.send(()).unwrap();
|
||||
Ok(())
|
||||
}));
|
||||
|
||||
Ok(())
|
||||
}))
|
||||
});
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
signal_rx.recv().unwrap();
|
||||
|
||||
pool.shutdown().wait().unwrap();
|
||||
pool.shutdown().wait();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_threadpool_is_idle() {
|
||||
let pool = ThreadPool::new();
|
||||
pool.shutdown_on_idle().wait().unwrap();
|
||||
pool.shutdown_on_idle().wait();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn busy_threadpool_is_not_idle() {
|
||||
use futures::sync::oneshot;
|
||||
use tokio_sync::oneshot;
|
||||
|
||||
// let pool = ThreadPool::new();
|
||||
let pool = Builder::new().pool_size(4).max_blocking(2).build();
|
||||
@@ -347,26 +341,31 @@ fn busy_threadpool_is_not_idle() {
|
||||
|
||||
let (term_tx, term_rx) = oneshot::channel();
|
||||
|
||||
tx.spawn(term_rx.then(|_| Ok(()))).unwrap();
|
||||
tx.spawn(async move {
|
||||
term_rx.await.unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let mut idle = pool.shutdown_on_idle();
|
||||
|
||||
struct IdleFut<'a>(&'a mut Shutdown);
|
||||
|
||||
impl<'a> Future for IdleFut<'a> {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
fn poll(&mut self) -> Poll<(), ()> {
|
||||
assert!(self.0.poll().unwrap().is_not_ready());
|
||||
Ok(Async::Ready(()))
|
||||
type Output = ();
|
||||
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
|
||||
assert_pending!(Pin::new(&mut self.as_mut().0).poll(cx));
|
||||
Poll::Ready(())
|
||||
}
|
||||
}
|
||||
|
||||
IdleFut(&mut idle).wait().unwrap();
|
||||
let idle_fut = IdleFut(&mut idle);
|
||||
tokio_executor::enter().unwrap().block_on(idle_fut);
|
||||
|
||||
term_tx.send(()).unwrap();
|
||||
|
||||
idle.wait().unwrap();
|
||||
let idle_fut = IdleFut(&mut idle);
|
||||
tokio_executor::enter().unwrap().block_on(idle_fut);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -377,10 +376,9 @@ fn panic_in_task() {
|
||||
struct Boom;
|
||||
|
||||
impl Future for Boom {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
type Output = ();
|
||||
|
||||
fn poll(&mut self) -> Poll<(), ()> {
|
||||
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {
|
||||
panic!();
|
||||
}
|
||||
}
|
||||
@@ -393,7 +391,7 @@ fn panic_in_task() {
|
||||
|
||||
tx.spawn(Boom).unwrap();
|
||||
|
||||
pool.shutdown_on_idle().wait().unwrap();
|
||||
pool.shutdown_on_idle().wait();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -407,15 +405,15 @@ fn count_panics() {
|
||||
})
|
||||
.build();
|
||||
// Spawn a future that will panic.
|
||||
pool.spawn(lazy(|| -> Result<(), ()> { panic!() }));
|
||||
pool.shutdown_on_idle().wait().unwrap();
|
||||
pool.spawn(async { panic!() });
|
||||
pool.shutdown_on_idle().wait();
|
||||
let counter = counter.load(Relaxed);
|
||||
assert_eq!(counter, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi_threadpool() {
|
||||
use futures::sync::oneshot;
|
||||
use tokio_sync::oneshot;
|
||||
|
||||
let pool1 = ThreadPool::new();
|
||||
let pool2 = ThreadPool::new();
|
||||
@@ -423,36 +421,22 @@ fn multi_threadpool() {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let (done_tx, done_rx) = mpsc::channel();
|
||||
|
||||
pool2.spawn({
|
||||
rx.and_then(move |_| {
|
||||
done_tx.send(()).unwrap();
|
||||
Ok(())
|
||||
})
|
||||
.map_err(|e| panic!("err={:?}", e))
|
||||
pool2.spawn(async move {
|
||||
rx.await.unwrap();
|
||||
done_tx.send(()).unwrap();
|
||||
});
|
||||
|
||||
pool1.spawn(lazy(move || {
|
||||
pool1.spawn(async move {
|
||||
tx.send(()).unwrap();
|
||||
Ok(())
|
||||
}));
|
||||
});
|
||||
|
||||
done_rx.recv().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eagerly_drops_futures() {
|
||||
use futures::future::{empty, lazy, Future};
|
||||
use futures::task;
|
||||
use std::sync::mpsc;
|
||||
|
||||
struct NotifyOnDrop(mpsc::Sender<()>);
|
||||
|
||||
impl Drop for NotifyOnDrop {
|
||||
fn drop(&mut self) {
|
||||
self.0.send(()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
struct MyPark {
|
||||
inner: DefaultPark,
|
||||
#[allow(dead_code)]
|
||||
@@ -497,9 +481,6 @@ fn eagerly_drops_futures() {
|
||||
let (park_tx, park_rx) = mpsc::sync_channel(0);
|
||||
let (unpark_tx, unpark_rx) = mpsc::sync_channel(0);
|
||||
|
||||
// Get the signal that the handler dropped.
|
||||
let notify_on_drop = NotifyOnDrop(drop_tx);
|
||||
|
||||
let pool = tokio_threadpool::Builder::new()
|
||||
.custom_park(move |_| MyPark {
|
||||
inner: DefaultPark::new(),
|
||||
@@ -508,29 +489,33 @@ fn eagerly_drops_futures() {
|
||||
})
|
||||
.build();
|
||||
|
||||
pool.spawn(lazy(move || {
|
||||
// Get a handle to the current task.
|
||||
let task = task::current();
|
||||
struct MyTask {
|
||||
task_tx: Option<mpsc::Sender<Waker>>,
|
||||
drop_tx: mpsc::Sender<()>,
|
||||
}
|
||||
|
||||
// Send it to the main thread to hold on to.
|
||||
task_tx.send(task).unwrap();
|
||||
impl Future for MyTask {
|
||||
type Output = ();
|
||||
|
||||
// This future will never resolve, it is only used to hold on to thee
|
||||
// `notify_on_drop` handle.
|
||||
empty::<(), ()>().then(move |_| {
|
||||
// This code path should never be reached.
|
||||
if true {
|
||||
panic!()
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
|
||||
if let Some(tx) = self.get_mut().task_tx.take() {
|
||||
tx.send(cx.waker().clone()).unwrap();
|
||||
}
|
||||
|
||||
// Explicitly drop `notify_on_drop` here, this is mostly to ensure
|
||||
// that the `notify_on_drop` handle gets moved into the task. It
|
||||
// will actually get dropped when the runtime is dropped.
|
||||
drop(notify_on_drop);
|
||||
Poll::Pending
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}));
|
||||
impl Drop for MyTask {
|
||||
fn drop(&mut self) {
|
||||
self.drop_tx.send(()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
pool.spawn(MyTask {
|
||||
task_tx: Some(task_tx),
|
||||
drop_tx,
|
||||
});
|
||||
|
||||
// Wait until we get the task handle.
|
||||
let task = task_rx.recv().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user