2019-08-10 00:07:57 +09:00
|
|
|
#![warn(rust_2018_idioms)]
|
2019-06-27 22:30:56 -07:00
|
|
|
|
|
|
|
|
use tokio_executor::park::{Park, Unpark};
|
2019-10-19 11:09:40 -07:00
|
|
|
use tokio_executor::thread_pool::*;
|
2019-01-17 22:12:25 +01:00
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
use futures_util::future::poll_fn;
|
2018-02-21 07:42:22 -08:00
|
|
|
use std::cell::Cell;
|
2019-06-27 22:30:56 -07:00
|
|
|
use std::future::Future;
|
|
|
|
|
use std::pin::Pin;
|
2018-02-21 07:42:22 -08:00
|
|
|
use std::sync::atomic::Ordering::Relaxed;
|
2019-02-21 11:56:15 -08:00
|
|
|
use std::sync::atomic::*;
|
|
|
|
|
use std::sync::{mpsc, Arc};
|
2019-06-27 22:30:56 -07:00
|
|
|
use std::task::{Context, Poll, Waker};
|
2018-02-21 07:42:22 -08:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
thread_local!(static FOO: Cell<u32> = Cell::new(0));
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-10-19 11:09:40 -07:00
|
|
|
fn shutdown_drops_futures() {
|
2018-02-21 07:42:22 -08:00
|
|
|
for _ in 0..1_000 {
|
|
|
|
|
let num_inc = Arc::new(AtomicUsize::new(0));
|
|
|
|
|
let num_dec = Arc::new(AtomicUsize::new(0));
|
|
|
|
|
let num_drop = Arc::new(AtomicUsize::new(0));
|
|
|
|
|
|
|
|
|
|
struct Never(Arc<AtomicUsize>);
|
|
|
|
|
|
|
|
|
|
impl Future for Never {
|
2019-06-27 22:30:56 -07:00
|
|
|
type Output = ();
|
2018-02-21 07:42:22 -08:00
|
|
|
|
2019-06-27 22:30:56 -07:00
|
|
|
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {
|
|
|
|
|
Poll::Pending
|
2018-02-21 07:42:22 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Never {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.0.fetch_add(1, Relaxed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let a = num_inc.clone();
|
|
|
|
|
let b = num_dec.clone();
|
|
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
let mut pool = Builder::new()
|
|
|
|
|
.around_worker(move |_, work| {
|
2018-02-21 07:42:22 -08:00
|
|
|
a.fetch_add(1, Relaxed);
|
2019-10-19 11:09:40 -07:00
|
|
|
work();
|
2018-02-21 07:42:22 -08:00
|
|
|
b.fetch_add(1, Relaxed);
|
|
|
|
|
})
|
|
|
|
|
.build();
|
|
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
// let tx = pool.sender().clone();
|
|
|
|
|
|
|
|
|
|
pool.spawn(Never(num_drop.clone()));
|
2018-02-21 07:42:22 -08:00
|
|
|
|
|
|
|
|
// Wait for the pool to shutdown
|
2019-10-19 11:09:40 -07:00
|
|
|
pool.shutdown_now();
|
2018-02-21 07:42:22 -08:00
|
|
|
|
|
|
|
|
// Assert that only a single thread was spawned.
|
|
|
|
|
let a = num_inc.load(Relaxed);
|
|
|
|
|
assert!(a >= 1);
|
|
|
|
|
|
|
|
|
|
// Assert that all threads shutdown
|
|
|
|
|
let b = num_dec.load(Relaxed);
|
|
|
|
|
assert_eq!(a, b);
|
|
|
|
|
|
|
|
|
|
// Assert that the future was dropped
|
|
|
|
|
let c = num_drop.load(Relaxed);
|
|
|
|
|
assert_eq!(c, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 21:33:45 -08:00
|
|
|
#[test]
|
|
|
|
|
fn drop_threadpool_drops_futures() {
|
2019-10-19 11:09:40 -07:00
|
|
|
const NUM_THREADS: usize = 10;
|
|
|
|
|
|
2018-03-06 21:33:45 -08:00
|
|
|
for _ in 0..1_000 {
|
|
|
|
|
let num_inc = Arc::new(AtomicUsize::new(0));
|
|
|
|
|
let num_dec = Arc::new(AtomicUsize::new(0));
|
|
|
|
|
let num_drop = Arc::new(AtomicUsize::new(0));
|
|
|
|
|
|
|
|
|
|
struct Never(Arc<AtomicUsize>);
|
|
|
|
|
|
|
|
|
|
impl Future for Never {
|
2019-06-27 22:30:56 -07:00
|
|
|
type Output = ();
|
2018-03-06 21:33:45 -08:00
|
|
|
|
2019-06-27 22:30:56 -07:00
|
|
|
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {
|
|
|
|
|
Poll::Pending
|
2018-03-06 21:33:45 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Never {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.0.fetch_add(1, Relaxed);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let a = num_inc.clone();
|
|
|
|
|
let b = num_dec.clone();
|
|
|
|
|
|
2018-04-10 23:33:05 +03:00
|
|
|
let pool = Builder::new()
|
2019-10-19 11:09:40 -07:00
|
|
|
.num_threads(NUM_THREADS)
|
|
|
|
|
.around_worker(move |_, work| {
|
2018-03-06 21:33:45 -08:00
|
|
|
a.fetch_add(1, Relaxed);
|
2019-10-19 11:09:40 -07:00
|
|
|
work();
|
2018-03-06 21:33:45 -08:00
|
|
|
b.fetch_add(1, Relaxed);
|
|
|
|
|
})
|
|
|
|
|
.build();
|
|
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
pool.spawn(Never(num_drop.clone()));
|
2018-03-06 21:33:45 -08:00
|
|
|
|
|
|
|
|
// Wait for the pool to shutdown
|
|
|
|
|
drop(pool);
|
|
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
// Assert that all the threads spawned
|
2018-03-06 21:33:45 -08:00
|
|
|
let a = num_inc.load(Relaxed);
|
2019-10-19 11:09:40 -07:00
|
|
|
assert_eq!(a, NUM_THREADS);
|
2018-03-06 21:33:45 -08:00
|
|
|
|
|
|
|
|
// Assert that all threads shutdown
|
|
|
|
|
let b = num_dec.load(Relaxed);
|
|
|
|
|
assert_eq!(a, b);
|
|
|
|
|
|
|
|
|
|
// Assert that the future was dropped
|
|
|
|
|
let c = num_drop.load(Relaxed);
|
|
|
|
|
assert_eq!(c, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 07:42:22 -08:00
|
|
|
#[test]
|
|
|
|
|
fn many_oneshot_futures() {
|
2019-10-19 11:09:40 -07:00
|
|
|
// used for notifying the main thread
|
2018-02-21 07:42:22 -08:00
|
|
|
const NUM: usize = 10_000;
|
|
|
|
|
|
|
|
|
|
for _ in 0..50 {
|
2019-10-19 11:09:40 -07:00
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
|
|
let mut pool = new_pool();
|
2018-02-21 07:42:22 -08:00
|
|
|
let cnt = Arc::new(AtomicUsize::new(0));
|
|
|
|
|
|
|
|
|
|
for _ in 0..NUM {
|
|
|
|
|
let cnt = cnt.clone();
|
2019-10-19 11:09:40 -07:00
|
|
|
let tx = tx.clone();
|
|
|
|
|
|
|
|
|
|
pool.spawn(async move {
|
|
|
|
|
let num = cnt.fetch_add(1, Relaxed) + 1;
|
|
|
|
|
|
|
|
|
|
if num == NUM {
|
|
|
|
|
tx.send(()).unwrap();
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-02-21 07:42:22 -08:00
|
|
|
}
|
|
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
rx.recv().unwrap();
|
2018-02-21 07:42:22 -08:00
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
// Wait for the pool to shutdown
|
|
|
|
|
pool.shutdown_now();
|
2018-02-21 07:42:22 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn many_multishot_futures() {
|
2019-06-27 22:30:56 -07:00
|
|
|
use tokio::sync::mpsc;
|
2018-02-21 07:42:22 -08:00
|
|
|
|
|
|
|
|
const CHAIN: usize = 200;
|
|
|
|
|
const CYCLES: usize = 5;
|
|
|
|
|
const TRACKS: usize = 50;
|
|
|
|
|
|
|
|
|
|
for _ in 0..50 {
|
2019-10-19 11:09:40 -07:00
|
|
|
let pool = new_pool();
|
2018-02-21 07:42:22 -08:00
|
|
|
let mut start_txs = Vec::with_capacity(TRACKS);
|
|
|
|
|
let mut final_rxs = Vec::with_capacity(TRACKS);
|
|
|
|
|
|
|
|
|
|
for _ in 0..TRACKS {
|
|
|
|
|
let (start_tx, mut chain_rx) = mpsc::channel(10);
|
|
|
|
|
|
|
|
|
|
for _ in 0..CHAIN {
|
2019-06-27 22:30:56 -07:00
|
|
|
let (mut next_tx, next_rx) = mpsc::channel(10);
|
2018-02-21 07:42:22 -08:00
|
|
|
|
|
|
|
|
// Forward all the messages
|
2019-10-19 11:09:40 -07:00
|
|
|
pool.spawn(async move {
|
|
|
|
|
while let Some(v) = chain_rx.recv().await {
|
|
|
|
|
next_tx.send(v).await.unwrap();
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-02-21 07:42:22 -08:00
|
|
|
|
|
|
|
|
chain_rx = next_rx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This final task cycles if needed
|
2019-06-27 22:30:56 -07:00
|
|
|
let (mut final_tx, final_rx) = mpsc::channel(10);
|
|
|
|
|
let mut cycle_tx = start_tx.clone();
|
2018-02-21 07:42:22 -08:00
|
|
|
let mut rem = CYCLES;
|
|
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
pool.spawn(async move {
|
|
|
|
|
for _ in 0..CYCLES {
|
|
|
|
|
let msg = chain_rx.recv().await.unwrap();
|
2019-06-27 22:30:56 -07:00
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
rem -= 1;
|
2019-06-27 22:30:56 -07:00
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
if rem == 0 {
|
|
|
|
|
final_tx.send(msg).await.unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
cycle_tx.send(msg).await.unwrap();
|
2019-06-27 22:30:56 -07:00
|
|
|
}
|
2019-10-19 11:09:40 -07:00
|
|
|
}
|
|
|
|
|
});
|
2018-02-21 07:42:22 -08:00
|
|
|
|
|
|
|
|
start_txs.push(start_tx);
|
|
|
|
|
final_rxs.push(final_rx);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 22:30:56 -07:00
|
|
|
{
|
|
|
|
|
let mut e = tokio_executor::enter().unwrap();
|
2018-02-21 07:42:22 -08:00
|
|
|
|
2019-06-27 22:30:56 -07:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-02-21 07:42:22 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn global_executor_is_configured() {
|
2019-10-19 11:09:40 -07:00
|
|
|
let pool = new_pool();
|
2018-02-21 07:42:22 -08:00
|
|
|
|
|
|
|
|
let (signal_tx, signal_rx) = mpsc::channel();
|
|
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
pool.spawn(async move {
|
2019-06-27 22:30:56 -07:00
|
|
|
tokio_executor::spawn(async move {
|
2018-02-21 07:42:22 -08:00
|
|
|
signal_tx.send(()).unwrap();
|
2019-06-27 22:30:56 -07:00
|
|
|
});
|
2019-10-19 11:09:40 -07:00
|
|
|
});
|
2018-02-21 07:42:22 -08:00
|
|
|
|
|
|
|
|
signal_rx.recv().unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn new_threadpool_is_idle() {
|
2019-10-19 11:09:40 -07:00
|
|
|
let mut pool = new_pool();
|
|
|
|
|
pool.shutdown_now();
|
2018-02-21 07:42:22 -08:00
|
|
|
}
|
2018-03-13 09:44:14 -07:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn panic_in_task() {
|
2019-10-19 11:09:40 -07:00
|
|
|
let pool = new_pool();
|
|
|
|
|
let (tx, rx) = mpsc::channel();
|
2018-03-13 09:44:14 -07:00
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
struct Boom(mpsc::Sender<()>);
|
2018-03-13 09:44:14 -07:00
|
|
|
|
|
|
|
|
impl Future for Boom {
|
2019-06-27 22:30:56 -07:00
|
|
|
type Output = ();
|
2018-03-13 09:44:14 -07:00
|
|
|
|
2019-06-27 22:30:56 -07:00
|
|
|
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {
|
2018-03-13 09:44:14 -07:00
|
|
|
panic!();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Boom {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
assert!(::std::thread::panicking());
|
2019-10-19 11:09:40 -07:00
|
|
|
self.0.send(()).unwrap();
|
2018-03-13 09:44:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
pool.spawn(Boom(tx));
|
|
|
|
|
rx.recv().unwrap();
|
2019-04-21 19:26:09 -04:00
|
|
|
}
|
|
|
|
|
|
2018-05-24 22:06:32 -07:00
|
|
|
#[test]
|
|
|
|
|
fn multi_threadpool() {
|
2019-06-27 22:30:56 -07:00
|
|
|
use tokio_sync::oneshot;
|
2018-05-24 22:06:32 -07:00
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
let pool1 = new_pool();
|
|
|
|
|
let pool2 = new_pool();
|
2018-05-24 22:06:32 -07:00
|
|
|
|
|
|
|
|
let (tx, rx) = oneshot::channel();
|
|
|
|
|
let (done_tx, done_rx) = mpsc::channel();
|
|
|
|
|
|
2019-06-27 22:30:56 -07:00
|
|
|
pool2.spawn(async move {
|
|
|
|
|
rx.await.unwrap();
|
|
|
|
|
done_tx.send(()).unwrap();
|
2018-05-24 22:06:32 -07:00
|
|
|
});
|
|
|
|
|
|
2019-06-27 22:30:56 -07:00
|
|
|
pool1.spawn(async move {
|
2018-05-24 22:06:32 -07:00
|
|
|
tx.send(()).unwrap();
|
2019-06-27 22:30:56 -07:00
|
|
|
});
|
2018-05-24 22:06:32 -07:00
|
|
|
|
|
|
|
|
done_rx.recv().unwrap();
|
|
|
|
|
}
|
2019-01-17 22:12:25 +01:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn eagerly_drops_futures() {
|
2019-10-19 11:09:40 -07:00
|
|
|
use std::sync::{mpsc, Mutex};
|
2019-01-17 22:12:25 +01:00
|
|
|
|
|
|
|
|
struct MyPark {
|
2019-10-19 11:09:40 -07:00
|
|
|
rx: mpsc::Receiver<()>,
|
|
|
|
|
tx: Mutex<mpsc::Sender<()>>,
|
2019-01-17 22:12:25 +01:00
|
|
|
#[allow(dead_code)]
|
|
|
|
|
park_tx: mpsc::SyncSender<()>,
|
|
|
|
|
unpark_tx: mpsc::SyncSender<()>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Park for MyPark {
|
|
|
|
|
type Unpark = MyUnpark;
|
2019-10-19 11:09:40 -07:00
|
|
|
type Error = ();
|
2019-01-17 22:12:25 +01:00
|
|
|
|
|
|
|
|
fn unpark(&self) -> Self::Unpark {
|
|
|
|
|
MyUnpark {
|
2019-10-19 11:09:40 -07:00
|
|
|
tx: Mutex::new(self.tx.lock().unwrap().clone()),
|
2019-01-17 22:12:25 +01:00
|
|
|
unpark_tx: self.unpark_tx.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn park(&mut self) -> Result<(), Self::Error> {
|
2019-10-19 11:09:40 -07:00
|
|
|
let _ = self.rx.recv();
|
|
|
|
|
Ok(())
|
2019-01-17 22:12:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn park_timeout(&mut self, duration: Duration) -> Result<(), Self::Error> {
|
2019-10-19 11:09:40 -07:00
|
|
|
let _ = self.rx.recv_timeout(duration);
|
|
|
|
|
Ok(())
|
2019-01-17 22:12:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct MyUnpark {
|
2019-10-19 11:09:40 -07:00
|
|
|
tx: Mutex<mpsc::Sender<()>>,
|
2019-01-17 22:12:25 +01:00
|
|
|
#[allow(dead_code)]
|
|
|
|
|
unpark_tx: mpsc::SyncSender<()>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Unpark for MyUnpark {
|
|
|
|
|
fn unpark(&self) {
|
2019-10-19 11:09:40 -07:00
|
|
|
let _ = self.tx.lock().unwrap().send(());
|
2019-01-17 22:12:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (task_tx, task_rx) = mpsc::channel();
|
|
|
|
|
let (drop_tx, drop_rx) = mpsc::channel();
|
|
|
|
|
let (park_tx, park_rx) = mpsc::sync_channel(0);
|
|
|
|
|
let (unpark_tx, unpark_rx) = mpsc::sync_channel(0);
|
|
|
|
|
|
2019-10-19 11:09:40 -07:00
|
|
|
let pool = Builder::new().num_threads(4).build_with_park(move |_| {
|
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
MyPark {
|
|
|
|
|
tx: Mutex::new(tx),
|
|
|
|
|
rx,
|
2019-02-21 11:56:15 -08:00
|
|
|
park_tx: park_tx.clone(),
|
|
|
|
|
unpark_tx: unpark_tx.clone(),
|
2019-10-19 11:09:40 -07:00
|
|
|
}
|
|
|
|
|
});
|
2019-01-17 22:12:25 +01:00
|
|
|
|
2019-06-27 22:30:56 -07:00
|
|
|
struct MyTask {
|
|
|
|
|
task_tx: Option<mpsc::Sender<Waker>>,
|
|
|
|
|
drop_tx: mpsc::Sender<()>,
|
|
|
|
|
}
|
2019-01-17 22:12:25 +01:00
|
|
|
|
2019-06-27 22:30:56 -07:00
|
|
|
impl Future for MyTask {
|
|
|
|
|
type Output = ();
|
2019-01-17 22:12:25 +01:00
|
|
|
|
2019-06-27 22:30:56 -07:00
|
|
|
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();
|
2019-02-21 11:56:15 -08:00
|
|
|
}
|
2019-01-17 22:12:25 +01:00
|
|
|
|
2019-06-27 22:30:56 -07:00
|
|
|
Poll::Pending
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-17 22:12:25 +01:00
|
|
|
|
2019-06-27 22:30:56 -07:00
|
|
|
impl Drop for MyTask {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.drop_tx.send(()).unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool.spawn(MyTask {
|
|
|
|
|
task_tx: Some(task_tx),
|
|
|
|
|
drop_tx,
|
|
|
|
|
});
|
2019-01-17 22:12:25 +01:00
|
|
|
|
|
|
|
|
// Wait until we get the task handle.
|
|
|
|
|
let task = task_rx.recv().unwrap();
|
|
|
|
|
|
|
|
|
|
// Drop the pool, this should result in futures being forcefully dropped.
|
|
|
|
|
drop(pool);
|
|
|
|
|
|
|
|
|
|
// Make sure `MyPark` and `MyUnpark` were dropped during shutdown.
|
|
|
|
|
assert_eq!(park_rx.try_recv(), Err(mpsc::TryRecvError::Disconnected));
|
|
|
|
|
assert_eq!(unpark_rx.try_recv(), Err(mpsc::TryRecvError::Disconnected));
|
|
|
|
|
|
|
|
|
|
// If the future is forcefully dropped, then we will get a signal here.
|
|
|
|
|
drop_rx.recv().unwrap();
|
|
|
|
|
|
|
|
|
|
// Ensure `task` lives until after the test completes.
|
|
|
|
|
drop(task);
|
|
|
|
|
}
|
2019-10-19 11:09:40 -07:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn park_called_at_interval() {
|
|
|
|
|
struct MyPark {
|
|
|
|
|
park_light: Arc<AtomicBool>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct MyUnpark {}
|
|
|
|
|
|
|
|
|
|
impl Park for MyPark {
|
|
|
|
|
type Unpark = MyUnpark;
|
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
|
|
fn unpark(&self) -> Self::Unpark {
|
|
|
|
|
MyUnpark {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn park(&mut self) -> Result<(), Self::Error> {
|
|
|
|
|
use std::thread;
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
|
|
thread::sleep(Duration::from_millis(1));
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn park_timeout(&mut self, duration: Duration) -> Result<(), Self::Error> {
|
|
|
|
|
if duration == Duration::from_millis(0) {
|
|
|
|
|
self.park_light.store(true, Relaxed);
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
self.park()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Unpark for MyUnpark {
|
|
|
|
|
fn unpark(&self) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let park_light_1 = Arc::new(AtomicBool::new(false));
|
|
|
|
|
let park_light_2 = park_light_1.clone();
|
|
|
|
|
|
|
|
|
|
let (done_tx, done_rx) = mpsc::channel();
|
|
|
|
|
|
|
|
|
|
// Use 1 thread to ensure the worker stays busy.
|
|
|
|
|
let pool = Builder::new().num_threads(1).build_with_park(move |idx| {
|
|
|
|
|
assert_eq!(idx, 0);
|
|
|
|
|
MyPark {
|
|
|
|
|
park_light: park_light_2.clone(),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let mut cnt = 0;
|
|
|
|
|
|
|
|
|
|
pool.spawn(poll_fn(move |cx| {
|
|
|
|
|
let did_park_light = park_light_1.load(Relaxed);
|
|
|
|
|
|
|
|
|
|
if did_park_light {
|
|
|
|
|
// There is a bit of a race where the worker can tick a few times
|
|
|
|
|
// before seeing the task
|
|
|
|
|
assert!(cnt > 50);
|
|
|
|
|
done_tx.send(()).unwrap();
|
|
|
|
|
return Poll::Ready(());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cnt += 1;
|
|
|
|
|
|
|
|
|
|
cx.waker().wake_by_ref();
|
|
|
|
|
Poll::Pending
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
done_rx.recv().unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn new_pool() -> ThreadPool {
|
|
|
|
|
Builder::new().num_threads(4).build()
|
|
|
|
|
}
|