mirror of
https://github.com/FEX-Emu/FEX.git
synced 2026-10-08 06:00:16 +02:00
Utils: add WorkQueueThread
Straightforward queue for arbitrary work.
This commit is contained in:
1 parent
c038bb5794
commit
008d990f20
3 files changed
+96
No files matched your search
@@ -0,0 +1,54 @@
|
||||
// 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
|
||||
Reference in new issue
Block a user