From 6baf5247cb360012713d3c4d46b7b2cbe8cb9d32 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Fri, 17 Mar 2023 16:01:52 +0100 Subject: [PATCH] Fixed `parallel_run` logic on single CPU systems --- src/uv_util.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/uv_util.h b/src/uv_util.h index b3b9477..7764916 100644 --- a/src/uv_util.h +++ b/src/uv_util.h @@ -179,16 +179,17 @@ bool CallOnLoop(uv_loop_t* loop, T&& callback) template void parallel_run(uv_loop_t* loop, T&& callback, bool wait = false) { - uint32_t THREAD_COUNT = std::thread::hardware_concurrency(); + const uint32_t THREAD_COUNT = std::thread::hardware_concurrency(); - if (THREAD_COUNT > 0) { - --THREAD_COUNT; + // Don't start other threads on single CPU systems + if (THREAD_COUNT <= 1) { + callback(); + return; } + // "THREAD_COUNT - 1" because current thread is already running // No more than 8 threads because our UV worker thread pool has 8 threads - if (THREAD_COUNT > 8) { - THREAD_COUNT = 8; - } + const uint32_t THREADS_TO_START = std::min(THREAD_COUNT - 1, 8); struct Callback { @@ -206,7 +207,7 @@ void parallel_run(uv_loop_t* loop, T&& callback, bool wait = false) std::shared_ptr cb; }; - for (size_t i = 0; i < THREAD_COUNT; ++i) { + for (size_t i = 0; i < THREADS_TO_START; ++i) { Work* w = new Work{ {}, cb }; w->req.data = w;