Files
FEX-Emu--FEX/FEXCore/Source/Utils/WorkQueueThread.cpp
T
Ryan Houdek 516ce6b15c Threads: Support setting thread names through creation
Gives the DiskCache a thread name so it's easier to find.
2026-09-08 19:11:52 -07:00

55 lines
1.2 KiB
C++

// SPDX-License-Identifier: MIT
#include <FEXCore/Utils/WorkQueueThread.h>
#include <FEXCore/Utils/LogManager.h>
namespace FEXCore {
WorkQueueThread::WorkQueueThread(FEXCore::Threads::Flags ThreadFlags, const char* ThreadName) {
Thread = FEXCore::Threads::Thread::Create(ThreadEntry, this, ThreadFlags, ThreadName);
}
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