mirror of
https://github.com/rwengine/openrw.git
synced 2024-11-23 02:42:39 +01:00
clang-format files in rwlib/source/job
This commit is contained in:
parent
981d68713b
commit
1219075e1a
@ -1,41 +1,38 @@
|
|||||||
#include <job/WorkContext.hpp>
|
#include <job/WorkContext.hpp>
|
||||||
|
|
||||||
void LoadWorker::start()
|
void LoadWorker::start() {
|
||||||
{
|
while (_running) {
|
||||||
while( _running ) {
|
_context->workNext();
|
||||||
_context->workNext();
|
std::this_thread::yield();
|
||||||
std::this_thread::yield();
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorkContext::workNext()
|
void WorkContext::workNext() {
|
||||||
{
|
WorkJob* j = nullptr;
|
||||||
WorkJob* j = nullptr;
|
|
||||||
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard( _inMutex );
|
std::lock_guard<std::mutex> guard(_inMutex);
|
||||||
|
|
||||||
if( ! _workQueue.empty() ) {
|
if (!_workQueue.empty()) {
|
||||||
j = _workQueue.front();
|
j = _workQueue.front();
|
||||||
_workQueue.pop();
|
_workQueue.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if( j == nullptr ) return;
|
if (j == nullptr) return;
|
||||||
|
|
||||||
j->work();
|
j->work();
|
||||||
|
|
||||||
std::lock_guard<std::mutex> guard( _outMutex );
|
std::lock_guard<std::mutex> guard(_outMutex);
|
||||||
_completeQueue.push(j);
|
_completeQueue.push(j);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorkContext::update()
|
void WorkContext::update() {
|
||||||
{
|
std::lock_guard<std::mutex> guard(_outMutex);
|
||||||
std::lock_guard<std::mutex> guard( _outMutex );
|
while (!_completeQueue.empty()) {
|
||||||
while( ! _completeQueue.empty() ) {
|
WorkJob* j = _completeQueue.front();
|
||||||
WorkJob* j = _completeQueue.front(); _completeQueue.pop();
|
_completeQueue.pop();
|
||||||
j->complete();
|
j->complete();
|
||||||
delete j;
|
delete j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,58 +1,59 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <queue>
|
|
||||||
#include <thread>
|
|
||||||
#include <mutex>
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <memory>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
#include <queue>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
class WorkContext;
|
class WorkContext;
|
||||||
|
|
||||||
class LoadWorker
|
class LoadWorker {
|
||||||
{
|
WorkContext* _context;
|
||||||
WorkContext* _context;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
std::atomic<bool> _running;
|
||||||
|
std::thread _thread;
|
||||||
|
void start();
|
||||||
|
|
||||||
std::atomic<bool> _running;
|
LoadWorker(WorkContext* context)
|
||||||
std::thread _thread;
|
: _context(context)
|
||||||
void start();
|
, _running(true)
|
||||||
|
, _thread(std::bind(&LoadWorker::start, this)) {
|
||||||
|
}
|
||||||
|
|
||||||
LoadWorker( WorkContext* context )
|
~LoadWorker() {
|
||||||
: _context( context ), _running(true),
|
_running = false;
|
||||||
_thread( std::bind(&LoadWorker::start, this) ) { }
|
_thread.join();
|
||||||
|
}
|
||||||
~LoadWorker( )
|
|
||||||
{
|
|
||||||
_running = false;
|
|
||||||
_thread.join();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Interface for background work
|
* @brief Interface for background work
|
||||||
*/
|
*/
|
||||||
class WorkJob
|
class WorkJob {
|
||||||
{
|
WorkContext* _context;
|
||||||
WorkContext* _context;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
WorkJob(WorkContext* context) : _context(context) {
|
||||||
|
}
|
||||||
|
|
||||||
WorkJob(WorkContext* context)
|
virtual ~WorkJob() {
|
||||||
: _context(context) {}
|
}
|
||||||
|
|
||||||
virtual ~WorkJob() {}
|
/**
|
||||||
|
* @brief getContext
|
||||||
|
* @return The loading context for this Loader
|
||||||
|
*/
|
||||||
|
WorkContext* getContext() const {
|
||||||
|
return _context;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
virtual void work() = 0;
|
||||||
* @brief getContext
|
virtual void complete() {
|
||||||
* @return The loading context for this Loader
|
}
|
||||||
*/
|
|
||||||
WorkContext* getContext() const { return _context; }
|
|
||||||
|
|
||||||
virtual void work() = 0;
|
|
||||||
virtual void complete() {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -61,44 +62,40 @@ public:
|
|||||||
* Work is added with queueJob, once it completes the job is added
|
* Work is added with queueJob, once it completes the job is added
|
||||||
* to the _completeQueue to be finalised on the "main" thread.
|
* to the _completeQueue to be finalised on the "main" thread.
|
||||||
*/
|
*/
|
||||||
class WorkContext
|
class WorkContext {
|
||||||
{
|
std::mutex _inMutex;
|
||||||
std::mutex _inMutex;
|
std::mutex _outMutex;
|
||||||
std::mutex _outMutex;
|
|
||||||
|
|
||||||
std::queue<WorkJob*> _workQueue;
|
std::queue<WorkJob*> _workQueue;
|
||||||
std::queue<WorkJob*> _completeQueue;
|
std::queue<WorkJob*> _completeQueue;
|
||||||
|
|
||||||
// Construct the worker last, so that it may use the queues
|
// Construct the worker last, so that it may use the queues
|
||||||
// immediately after initialization.
|
// immediately after initialization.
|
||||||
std::unique_ptr<LoadWorker> _worker;
|
std::unique_ptr<LoadWorker> _worker;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
WorkContext() : _worker(new LoadWorker(this)) {
|
||||||
|
}
|
||||||
|
|
||||||
WorkContext()
|
void queueJob(WorkJob* job) {
|
||||||
: _worker(new LoadWorker(this)) { }
|
std::lock_guard<std::mutex> guard(_inMutex);
|
||||||
|
_workQueue.push(job);
|
||||||
|
}
|
||||||
|
|
||||||
void queueJob( WorkJob* job )
|
void stop() {
|
||||||
{
|
// Stop serving the queue.
|
||||||
std::lock_guard<std::mutex> guard( _inMutex );
|
_worker.reset(nullptr);
|
||||||
_workQueue.push( job );
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void stop()
|
// Called by the worker thread - don't touch
|
||||||
{
|
void workNext();
|
||||||
// Stop serving the queue.
|
|
||||||
_worker.reset(nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Called by the worker thread - don't touch
|
bool isEmpty() {
|
||||||
void workNext();
|
std::lock_guard<std::mutex> guardIn(_inMutex);
|
||||||
|
std::lock_guard<std::mutex> guardOut(_outMutex);
|
||||||
|
|
||||||
bool isEmpty() {
|
return (_workQueue.size() + _completeQueue.size()) == 0;
|
||||||
std::lock_guard<std::mutex> guardIn( _inMutex );
|
}
|
||||||
std::lock_guard<std::mutex> guardOut( _outMutex );
|
|
||||||
|
|
||||||
return (_workQueue.size() + _completeQueue.size()) == 0;
|
void update();
|
||||||
}
|
|
||||||
|
|
||||||
void update();
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user