1
0
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:
Daniel Evans 2016-09-09 21:13:21 +01:00
parent 981d68713b
commit 1219075e1a
2 changed files with 87 additions and 93 deletions

View File

@ -1,15 +1,13 @@
#include <job/WorkContext.hpp>
void LoadWorker::start()
{
void LoadWorker::start() {
while (_running) {
_context->workNext();
std::this_thread::yield();
}
}
void WorkContext::workNext()
{
void WorkContext::workNext() {
WorkJob* j = nullptr;
{
@ -29,13 +27,12 @@ void WorkContext::workNext()
_completeQueue.push(j);
}
void WorkContext::update()
{
void WorkContext::update() {
std::lock_guard<std::mutex> guard(_outMutex);
while (!_completeQueue.empty()) {
WorkJob* j = _completeQueue.front(); _completeQueue.pop();
WorkJob* j = _completeQueue.front();
_completeQueue.pop();
j->complete();
delete j;
}
}

View File

@ -1,31 +1,30 @@
#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
{
class LoadWorker {
WorkContext* _context;
public:
std::atomic<bool> _running;
std::thread _thread;
void start();
LoadWorker(WorkContext* context)
: _context( context ), _running(true),
_thread( std::bind(&LoadWorker::start, this) ) { }
: _context(context)
, _running(true)
, _thread(std::bind(&LoadWorker::start, this)) {
}
~LoadWorker( )
{
~LoadWorker() {
_running = false;
_thread.join();
}
@ -34,25 +33,27 @@ public:
/**
* @brief Interface for background work
*/
class WorkJob
{
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; }
WorkContext* getContext() const {
return _context;
}
virtual void work() = 0;
virtual void complete() {}
virtual void complete() {
}
};
/**
@ -61,8 +62,7 @@ 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
{
class WorkContext {
std::mutex _inMutex;
std::mutex _outMutex;
@ -74,18 +74,15 @@ class WorkContext
std::unique_ptr<LoadWorker> _worker;
public:
WorkContext() : _worker(new LoadWorker(this)) {
}
WorkContext()
: _worker(new LoadWorker(this)) { }
void queueJob( WorkJob* job )
{
void queueJob(WorkJob* job) {
std::lock_guard<std::mutex> guard(_inMutex);
_workQueue.push(job);
}
void stop()
{
void stop() {
// Stop serving the queue.
_worker.reset(nullptr);
}