Files
FEX-Emu--FEX/FEXCore/Source/Utils/WorkQueueThread.cpp
T
Pierre-Loup A. Griffais 008d990f20 Utils: add WorkQueueThread
Straightforward queue for arbitrary work.
2026-08-21 11:16:07 -07:00

55 lines
1.1 KiB
C++

// SPDX-License-Identifier: MIT
#include <FEXCore/Utils/WorkQueueThread.h>
#include <FEXCore/Utils/LogManager.h>
namespace FEXCore {
WorkQueueThread::WorkQueueThread() {
Thread = FEXCore::Threads::Thread::Create(ThreadEntry, this);
}
WorkQueueThread::~WorkQueueThread() {
{
std::unique_lock lk {Mutex};
Stop = true;
}
CV.notify_one();
if (Thread && Thread->joinable()) {
Thread->join(nullptr);
}
}
void WorkQueueThread::QueueWork(fextl::unique_ptr<WorkItem> Work) {
{
std::unique_lock lk {Mutex};
Queue.push_back(std::move(Work));
}
CV.notify_one();
}
void WorkQueueThread::ThreadProc() {
while (true) {
fextl::unique_ptr<WorkItem> Work;
{
std::unique_lock lk {Mutex};
while (!(Stop || !Queue.empty())) {
CV.wait(lk);
}
if (Queue.empty()) {
// nothing to do? must be stopping
LOGMAN_THROW_A_FMT(Stop, "WorkQueueThread wakes up empty but no Stop?");
return;
}
Work = std::move(Queue.front());
Queue.pop_front();
}
Work->Run();
// Work is destroyed here
}
}
} // namespace FEXCore