-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathengineworkerscheduler.cpp
61 lines (53 loc) · 1.64 KB
/
engineworkerscheduler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "engine/engineworkerscheduler.h"
#include <QtDebug>
#include "engine/engineworker.h"
#include "moc_engineworkerscheduler.cpp"
#include "util/compatibility/qmutex.h"
#include "util/event.h"
EngineWorkerScheduler::EngineWorkerScheduler(QObject* pParent)
: m_bWakeScheduler(false),
m_bQuit(false) {
Q_UNUSED(pParent);
}
EngineWorkerScheduler::~EngineWorkerScheduler() {
m_bQuit = true;
m_waitCondition.wakeAll();
wait();
}
void EngineWorkerScheduler::workerReady() {
m_bWakeScheduler = true;
}
void EngineWorkerScheduler::addWorker(EngineWorker* pWorker) {
DEBUG_ASSERT(pWorker);
const auto locker = lockMutex(&m_mutex);
m_workers.push_back(pWorker);
}
void EngineWorkerScheduler::runWorkers() {
// Wake the scheduler if we have written a worker-ready message to the
// scheduler. There is no race condition in accessing this boolean because
// both workerReady and runWorkers are called from the callback thread.
if (m_bWakeScheduler) {
m_bWakeScheduler = false;
m_waitCondition.wakeAll();
}
}
void EngineWorkerScheduler::run() {
static const QString tag("EngineWorkerScheduler");
while (!m_bQuit) {
Event::start(tag);
{
const auto locker = lockMutex(&m_mutex);
for(const auto& pWorker: m_workers) {
pWorker->wakeIfReady();
}
}
Event::end(tag);
{
const auto lock = lockMutex(&m_mutex);
if (!m_bQuit) {
// Wait for next runWorkers() call
m_waitCondition.wait(&m_mutex); // unlock mutex and wait
}
}
}
}