2023-11-11 10:11:09 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: AuGroupWorkQueue.cpp
|
|
|
|
Date: 2023-11-04
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
|
|
|
#include "AuGroupWorkQueue.hpp"
|
2023-11-11 11:27:01 +00:00
|
|
|
#include "Async.hpp"
|
2023-11-11 10:11:09 +00:00
|
|
|
#include "ThreadPool.hpp"
|
2023-12-07 09:20:23 +00:00
|
|
|
#include "IAsyncRunnable.hpp"
|
2023-11-11 10:11:09 +00:00
|
|
|
|
|
|
|
namespace Aurora::Async
|
|
|
|
{
|
2024-01-23 22:35:18 +00:00
|
|
|
void GroupWorkQueue::AddWorkEntry(WorkEntry_t entry)
|
2023-11-11 10:11:09 +00:00
|
|
|
{
|
|
|
|
AU_DEBUG_MEMCRUNCH;
|
|
|
|
|
|
|
|
auto prio = (int)entry.second->GetPrio();
|
2023-12-07 09:20:23 +00:00
|
|
|
SysAssert(prio < AuAsync::kEWorkPriorityCount, "Invalid PRIO");
|
2023-11-11 10:11:09 +00:00
|
|
|
|
|
|
|
AU_LOCK_GUARD(this->mutex);
|
|
|
|
this->sortedWork[prio].push_back(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroupWorkQueue::IsEmpty()
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(this->mutex);
|
2023-12-07 09:20:23 +00:00
|
|
|
for (AU_ITERATE_N(i, AuAsync::kEWorkPriorityCount))
|
2023-11-11 10:11:09 +00:00
|
|
|
{
|
|
|
|
if (this->sortedWork[i].size())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GroupWorkQueue::IsEmpty(ThreadPool *pPool, AuWorkerId_t id)
|
|
|
|
{
|
|
|
|
#if 1
|
|
|
|
auto pHandle = pPool->GetThreadHandle(id);
|
|
|
|
if (!pHandle)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !AuAtomicLoad(&pHandle->sync.cvHasWork);
|
|
|
|
#else
|
|
|
|
AU_LOCK_GUARD(this->mutex);
|
|
|
|
|
2023-12-07 09:20:23 +00:00
|
|
|
for (AU_ITERATE_N(i, AuAsync::kEWorkPriorityCount))
|
2023-11-11 10:11:09 +00:00
|
|
|
{
|
|
|
|
for (const auto &[srcId, pA] : this->sortedWork[i])
|
|
|
|
{
|
|
|
|
if (id == srcId)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-09-05 16:38:54 +00:00
|
|
|
bool GroupWorkQueue::Dequeue(AuListOfHeap<WorkEntry_t> &queue,
|
2024-04-10 06:39:11 +00:00
|
|
|
AuUInt uMaxPopCount,
|
|
|
|
AuAsync::ThreadId_t id)
|
2023-11-11 10:11:09 +00:00
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(this->mutex);
|
2024-04-10 06:39:11 +00:00
|
|
|
AuUInt uAnyCount {};
|
2023-11-11 10:11:09 +00:00
|
|
|
|
2023-12-07 09:20:23 +00:00
|
|
|
for (AU_ITERATE_N(i, AuAsync::kEWorkPriorityCount))
|
2023-11-11 10:11:09 +00:00
|
|
|
{
|
2023-12-07 09:20:23 +00:00
|
|
|
auto &group = this->sortedWork[(int)AuAsync::kEWorkPriorityMaxLegal - i];
|
2023-11-11 10:11:09 +00:00
|
|
|
|
2024-04-10 06:39:11 +00:00
|
|
|
for (auto itr = group.begin(); ((itr != group.end()) && (queue.size() < uMaxPopCount)); )
|
2023-11-11 10:11:09 +00:00
|
|
|
{
|
2024-04-10 06:39:11 +00:00
|
|
|
if ((!uAnyCount) &&
|
|
|
|
(itr->first == Async::kThreadIdAny))
|
2023-11-11 10:11:09 +00:00
|
|
|
{
|
2024-09-05 16:38:54 +00:00
|
|
|
if (!AuTryInsert(queue, *itr))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-11 10:11:09 +00:00
|
|
|
itr = group.erase(itr);
|
2024-04-10 06:39:11 +00:00
|
|
|
uAnyCount++;
|
2023-11-11 10:11:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((itr->first != Async::kThreadIdAny) &&
|
|
|
|
(itr->first == id))
|
|
|
|
{
|
2024-09-05 16:38:54 +00:00
|
|
|
if (!AuTryInsert(queue, *itr))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-11 10:11:09 +00:00
|
|
|
itr = group.erase(itr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
itr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (queue.size())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 16:38:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2023-11-11 10:11:09 +00:00
|
|
|
}
|