2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-01-19 20:13:51 +00:00

Spawn several threads when we fail to enqueue work in the blocki… (#181)

* Rebase onto master

* Switch to unbounded channels
This commit is contained in:
Tyler Neely 2019-11-01 21:53:13 +01:00 committed by Stjepan Glavina
parent 1a51ca424a
commit 5adc608791

View file

@ -2,7 +2,7 @@ use std::sync::atomic::{AtomicU64, Ordering};
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use crossbeam_channel::{bounded, Receiver, Sender}; use crossbeam_channel::{unbounded, Receiver, Sender};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use crate::task::{JoinHandle, Task}; use crate::task::{JoinHandle, Task};
@ -79,7 +79,7 @@ static POOL: Lazy<Pool> = Lazy::new(|| {
// before being acted on by a core. This helps keep // before being acted on by a core. This helps keep
// latency snappy in the overall async system by // latency snappy in the overall async system by
// reducing bufferbloat. // reducing bufferbloat.
let (sender, receiver) = bounded(0); let (sender, receiver) = unbounded();
Pool { sender, receiver } Pool { sender, receiver }
}); });
@ -95,6 +95,9 @@ fn maybe_create_another_blocking_thread() {
return; return;
} }
let n_to_spawn = std::cmp::min(2 + (workers / 10), 10);
for _ in 0..n_to_spawn {
// We want to avoid having all threads terminate at // We want to avoid having all threads terminate at
// exactly the same time, causing thundering herd // exactly the same time, causing thundering herd
// effects. We want to stagger their destruction over // effects. We want to stagger their destruction over
@ -117,6 +120,7 @@ fn maybe_create_another_blocking_thread() {
}) })
.expect("cannot start a dynamic thread driving blocking tasks"); .expect("cannot start a dynamic thread driving blocking tasks");
} }
}
// Enqueues work, attempting to send to the threadpool in a // Enqueues work, attempting to send to the threadpool in a
// nonblocking way and spinning up another worker thread if // nonblocking way and spinning up another worker thread if