This commit is contained in:
Reece Wilson 2023-09-05 14:48:14 +01:00
parent d3587dbf08
commit 3adb019cf1
2 changed files with 23 additions and 16 deletions

View File

@ -435,6 +435,24 @@ namespace Aurora::Async
}
}
void ThreadPool::DoThing(ThreadState *pState)
{
auto uState = pState->cvHasWork;
auto uMin = AuMin<AuUInt32>(uState, pState->pendingWorkItems.size());
if (!uMin) uMin = 1;
while (uState &&
AuAtomicCompareExchange(&pState->cvHasWork, uState - uMin, uState) != uState)
{
uState = pState->cvHasWork;
if (uState < uMin)
{
uMin = uState;
}
}
}
bool ThreadPool::PollInternal(AuSPtr<ThreadState> state, bool block, AuUInt32 &uCount)
{
if (!state)
@ -455,6 +473,8 @@ namespace Aurora::Async
{
group->workQueue.Dequeue(state->pendingWorkItems, state->multipopCount, state->id.second);
this->DoThing(state.get());
// Consider blocking for more work
if (!block)
{
@ -497,23 +517,10 @@ namespace Aurora::Async
AuAtomicSub(&state->cvSleepCount, 1u);
return false;
}
}
while (state->pendingWorkItems.empty() && block);
{
auto uState = state->cvHasWork;
while (uState &&
AuAtomicCompareExchange(&state->cvHasWork, uState - 1, uState) != uState)
{
uState = state->cvHasWork;
if (uState == 0)
{
break;
}
}
}
if (!block && // quick hack: is worthy of io reset by virtue of having polled externally (most likely)?
group->workQueue.IsEmpty(this, state->id))
@ -608,7 +615,7 @@ namespace Aurora::Async
if (state->pendingWorkItems.size())
{
AU_LOCK_GUARD(state->cvWorkMutex);
for (const auto &item : state->pendingWorkItems)
{
group->workQueue.AddWorkEntry(state.get(), item);
@ -617,7 +624,6 @@ namespace Aurora::Async
state->pendingWorkItems.clear();
state->cvVariable->Broadcast();
state->eventLs->Set();
state->pendingWorkItems.clear();
}
// Account for

View File

@ -97,6 +97,7 @@ namespace Aurora::Async
bool InternalRunOne(AuSPtr<ThreadState>, bool block, AuUInt32 &uCount);
bool PollInternal(AuSPtr<ThreadState>, bool block, AuUInt32 &uCount);
void DoThing(ThreadState *pState);
size_t GetThreadWorkersCount(ThreadGroup_t group);