mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-27 00:00:12 +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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user