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