feat: add a minimum number of threads to spawn with env MIN_THREAD override

This commit is contained in:
CJ van den Berg 2025-02-11 18:32:26 +01:00
parent db3ad5f45e
commit 9ca04ddfc7
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -58,12 +58,16 @@ using asio::windows::stream_handle;
namespace thespian::executor {
const char *MIN_THREAD_STR = getenv("MIN_THREAD"); // NOLINT
const auto MIN_THREAD =
static_cast<long>(atoi(MIN_THREAD_STR ? MIN_THREAD_STR : "4")); // NOLINT
const char *MAX_THREAD_STR = getenv("MAX_THREAD"); // NOLINT
const auto MAX_THREAD =
static_cast<long>(atoi(MAX_THREAD_STR ? MAX_THREAD_STR : "64")); // NOLINT
#if !defined(_WIN32)
const auto threads = min(sysconf(_SC_NPROCESSORS_ONLN), MAX_THREAD);
const auto threads = max(min(sysconf(_SC_NPROCESSORS_ONLN), MAX_THREAD), MIN_THREAD);
#else
namespace {
static auto get_num_processors() -> long {
@ -72,7 +76,7 @@ static auto get_num_processors() -> long {
return si.dwNumberOfProcessors;
}
} // namespace
const auto threads = min(get_num_processors(), MAX_THREAD);
const auto threads = max(min(get_num_processors(), MAX_THREAD), MIN_THREAD);
#endif
struct context_impl {