[+] Aurora::IO::Async::SpawnMoreThreads

[+] Aurora::IO::Async::GetSpawnedThreads
This commit is contained in:
Reece Wilson 2024-03-10 11:34:18 +00:00
parent 41412876b6
commit fceb937bfd
2 changed files with 78 additions and 2 deletions

View File

@ -10,4 +10,8 @@
namespace Aurora::IO::Async namespace Aurora::IO::Async
{ {
AUKN_SYM void UseSpecifiedWorkerGroup(Aurora::Async::WorkerPId_t worker); AUKN_SYM void UseSpecifiedWorkerGroup(Aurora::Async::WorkerPId_t worker);
AUKN_SYM AuUInt32 SpawnMoreThreads(AuUInt32 uRequest);
AUKN_SYM AuUInt32 GetSpawnedThreads();
} }

View File

@ -14,19 +14,91 @@ namespace Aurora::IO::Async
static AuAsync::WorkerPId_t gDefaultGroup; static AuAsync::WorkerPId_t gDefaultGroup;
static AuSPtr<AuAsync::IThreadPool> gThreadPool; static AuSPtr<AuAsync::IThreadPool> gThreadPool;
static AuList<AuWPtr<AuAsync::IThreadPool>> gWorkersRegistered; static AuList<AuWPtr<AuAsync::IThreadPool>> 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) AUKN_SYM void UseSpecifiedWorkerGroup(AuAsync::WorkerPId_t worker)
{ {
AU_LOCK_GUARD(gMutex); AU_LOCK_GUARD(gMutex);
gDefaultGroup = worker; 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<AuUInt>(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() static AuAsync::WorkerPId_t SpawnDefaultGroup()
{ {
AuAsync::WorkerPId_t ret; AuAsync::WorkerPId_t ret;
// TODO: gJobRunners = runtime config? gJobRunners = GetDefaultThreadCount();
gThreadPool = AuAsync::NewThreadPool(); gThreadPool = AuAsync::NewThreadPool();
if (!gThreadPool) if (!gThreadPool)