1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 19:32:49 +01:00
openrw/rwlib/source/job/WorkContext.cpp

39 lines
761 B
C++
Raw Normal View History

#include <job/WorkContext.hpp>
2014-06-04 12:53:11 +02:00
2016-09-09 22:13:21 +02:00
void LoadWorker::start() {
while (_running) {
_context->workNext();
std::this_thread::yield();
}
2014-06-04 12:53:11 +02:00
}
2016-09-09 22:13:21 +02:00
void WorkContext::workNext() {
WorkJob* j = nullptr;
2016-09-09 22:13:21 +02:00
{
std::lock_guard<std::mutex> guard(_inMutex);
2016-09-09 22:13:21 +02:00
if (!_workQueue.empty()) {
j = _workQueue.front();
_workQueue.pop();
}
}
2016-09-09 22:13:21 +02:00
if (j == nullptr) return;
2014-06-04 12:53:11 +02:00
2016-09-09 22:13:21 +02:00
j->work();
2014-06-04 12:53:11 +02:00
2016-09-09 22:13:21 +02:00
std::lock_guard<std::mutex> guard(_outMutex);
_completeQueue.push(j);
2014-06-04 12:53:11 +02:00
}
2016-09-09 22:13:21 +02:00
void WorkContext::update() {
std::lock_guard<std::mutex> guard(_outMutex);
while (!_completeQueue.empty()) {
WorkJob* j = _completeQueue.front();
_completeQueue.pop();
j->complete();
delete j;
}
}