From 6677d52c2df87eab7f06400f9fc1099d725c96f8 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Tue, 12 Nov 2019 00:35:29 +0100 Subject: [PATCH 1/3] Improve thread creating algorithm in spawn_blocking --- src/task/spawn_blocking.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/task/spawn_blocking.rs b/src/task/spawn_blocking.rs index 6076d1bcd..a93a68bf4 100644 --- a/src/task/spawn_blocking.rs +++ b/src/task/spawn_blocking.rs @@ -68,16 +68,13 @@ static POOL: Lazy = Lazy::new(|| { fn start_thread() { SLEEPING.fetch_add(1, Ordering::SeqCst); - - // Generate a random duration of time between 1 second and 10 seconds. If the thread doesn't - // receive the next task in this duration of time, it will stop running. - let timeout = Duration::from_millis(1000 + u64::from(random(9_000))); + let timeout = Duration::from_secs(10); thread::Builder::new() .name("async-std/blocking".to_string()) .spawn(move || { loop { - let task = match POOL.receiver.recv_timeout(timeout) { + let mut task = match POOL.receiver.recv_timeout(timeout) { Ok(task) => task, Err(_) => { // Check whether this is the last sleeping thread. @@ -100,8 +97,22 @@ fn start_thread() { start_thread(); } - // Run the task. - abort_on_panic(|| task.run()); + loop { + // Run the task. + abort_on_panic(|| task.run()); + + // Try taking another task if there are any available. + task = match POOL.receiver.try_recv() { + Ok(task) => task, + Err(_) => break, + }; + } + + // If there is at least one sleeping thread, stop this thread instead of putting it + // to sleep. + if SLEEPING.load(Ordering::SeqCst) > 0 { + return; + } SLEEPING.fetch_add(1, Ordering::SeqCst); } From 21c5c48cb6cbf2cd6ab687f4e7938bcb5e35cea3 Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Tue, 12 Nov 2019 00:37:54 +0100 Subject: [PATCH 2/3] Lower the timeout to 1 second --- src/task/spawn_blocking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/task/spawn_blocking.rs b/src/task/spawn_blocking.rs index a93a68bf4..6cce082df 100644 --- a/src/task/spawn_blocking.rs +++ b/src/task/spawn_blocking.rs @@ -68,7 +68,7 @@ static POOL: Lazy = Lazy::new(|| { fn start_thread() { SLEEPING.fetch_add(1, Ordering::SeqCst); - let timeout = Duration::from_secs(10); + let timeout = Duration::from_secs(1); thread::Builder::new() .name("async-std/blocking".to_string()) From 1a50ffd144684199325cbd8a38fc0953373dc5ee Mon Sep 17 00:00:00 2001 From: Stjepan Glavina Date: Tue, 12 Nov 2019 00:38:22 +0100 Subject: [PATCH 3/3] Delete unused import --- src/task/spawn_blocking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/task/spawn_blocking.rs b/src/task/spawn_blocking.rs index 6cce082df..578afa4e3 100644 --- a/src/task/spawn_blocking.rs +++ b/src/task/spawn_blocking.rs @@ -6,7 +6,7 @@ use crossbeam_channel::{unbounded, Receiver, Sender}; use once_cell::sync::Lazy; use crate::task::{JoinHandle, Task}; -use crate::utils::{abort_on_panic, random}; +use crate::utils::abort_on_panic; /// Spawns a blocking task. ///