From fceb937bfd39fc5f64fdcbfd51159ef53082e7d8 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Sun, 10 Mar 2024 11:34:18 +0000 Subject: [PATCH] [+] Aurora::IO::Async::SpawnMoreThreads [+] Aurora::IO::Async::GetSpawnedThreads --- Include/Aurora/IO/Async/IOAsync.hpp | 4 ++ Source/IO/Async/AuIOThreadPool.cpp | 76 ++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/Include/Aurora/IO/Async/IOAsync.hpp b/Include/Aurora/IO/Async/IOAsync.hpp index 73d41f10..4fa17b78 100644 --- a/Include/Aurora/IO/Async/IOAsync.hpp +++ b/Include/Aurora/IO/Async/IOAsync.hpp @@ -10,4 +10,8 @@ namespace Aurora::IO::Async { AUKN_SYM void UseSpecifiedWorkerGroup(Aurora::Async::WorkerPId_t worker); + + AUKN_SYM AuUInt32 SpawnMoreThreads(AuUInt32 uRequest); + + AUKN_SYM AuUInt32 GetSpawnedThreads(); } \ No newline at end of file diff --git a/Source/IO/Async/AuIOThreadPool.cpp b/Source/IO/Async/AuIOThreadPool.cpp index dc45ac9d..278f9a07 100644 --- a/Source/IO/Async/AuIOThreadPool.cpp +++ b/Source/IO/Async/AuIOThreadPool.cpp @@ -14,19 +14,91 @@ namespace Aurora::IO::Async static AuAsync::WorkerPId_t gDefaultGroup; static AuSPtr gThreadPool; static AuList> gWorkersRegistered; - static AuUInt gJobRunners = 4; + static AuUInt gJobRunners = 0; + static bool bOwnsThreadPool { true }; + + static AuUInt32 GetDefaultThreadCount() + { + AuUInt32 uRet {}; + + // TODO: gJobRunners = runtime config? + + if (!uRet) + { + uRet = AuClamp(AuHWInfo::GetCPUInfo().uThreads / 3, 2, 8); + } + + return uRet; + } AUKN_SYM void UseSpecifiedWorkerGroup(AuAsync::WorkerPId_t worker) { AU_LOCK_GUARD(gMutex); gDefaultGroup = worker; + bOwnsThreadPool = false; + } + + AUKN_SYM AuUInt32 GetSpawnedThreads() + { + return bOwnsThreadPool ? gJobRunners : 0; + } + + AUKN_SYM AuUInt32 SpawnMoreThreads(AuUInt32 uRequest) + { + AU_LOCK_GUARD(gMutex); + + AuUInt32 ret {}; + AuUInt32 uStartOffset {}; + + if (uRequest > 64) + { + return 0; + } + + if (!bOwnsThreadPool) + { + return 0; + } + + if (!gThreadPool) + { + gThreadPool = AuAsync::NewThreadPool(); + if (!gThreadPool) + { + return 0; + } + + uRequest += (uStartOffset = GetDefaultThreadCount()); + } + + { + AuAsync::WorkerPId_t workerId { gThreadPool, 0, AuAsync::kThreadIdAny }; + + auto uMax = AuClamp(gJobRunners + uRequest, 0, 255); + + for (AU_ITERATE_N_TO_X(i, gJobRunners, uMax)) + { + AuAsync::WorkerPId_t copy = workerId; + copy.second = i; + + if (!gThreadPool->Spawn(copy)) + { + break; + } + + ret++; + } + } + + gJobRunners += ret; + return ret - uStartOffset; } static AuAsync::WorkerPId_t SpawnDefaultGroup() { AuAsync::WorkerPId_t ret; - // TODO: gJobRunners = runtime config? + gJobRunners = GetDefaultThreadCount(); gThreadPool = AuAsync::NewThreadPool(); if (!gThreadPool)