mirror of
https://github.com/rwengine/openrw.git
synced 2024-09-18 16:32:32 +02:00
Remove all traces of WorkContext
This commit is contained in:
parent
b65a513bbb
commit
8d10c80196
@ -60,8 +60,6 @@ set(RWENGINE_SOURCES
|
||||
src/engine/ScreenText.hpp
|
||||
src/items/Weapon.cpp
|
||||
src/items/Weapon.hpp
|
||||
src/loaders/DataLoader.cpp
|
||||
src/loaders/DataLoader.hpp
|
||||
src/loaders/GenericDATLoader.cpp
|
||||
src/loaders/GenericDATLoader.hpp
|
||||
src/loaders/LoaderCOL.cpp
|
||||
|
@ -1,41 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef _DATALOADER_HPP_
|
||||
#define _DATALOADER_HPP_
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class WorkContext;
|
||||
|
||||
class DataLoader {
|
||||
WorkContext* _context;
|
||||
|
||||
public:
|
||||
DataLoader(WorkContext* context) : _context(context) {
|
||||
}
|
||||
|
||||
virtual ~DataLoader() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief getContext
|
||||
* @return The loading context for this Loader
|
||||
*/
|
||||
WorkContext* getContext() const {
|
||||
return _context;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief load the data contained in a set of bytes
|
||||
* @param data The bytes from which to load data.
|
||||
* @param size The number of bytes.
|
||||
* @return true if the data was valid and loaded, false otherwise.
|
||||
*/
|
||||
virtual bool load(const char* data, size_t size) = 0;
|
||||
|
||||
/**
|
||||
* @brief create perform any after-load activitiy that is required.
|
||||
*/
|
||||
virtual void create() = 0;
|
||||
};
|
||||
|
||||
#endif
|
@ -35,9 +35,6 @@ SET(RWLIB_SOURCES
|
||||
"source/loaders/LoaderSDT.cpp"
|
||||
"source/loaders/LoaderTXD.hpp"
|
||||
"source/loaders/LoaderTXD.cpp"
|
||||
|
||||
"source/job/WorkContext.hpp"
|
||||
"source/job/WorkContext.cpp"
|
||||
)
|
||||
|
||||
add_library(rwlib
|
||||
|
@ -1,38 +0,0 @@
|
||||
#include <job/WorkContext.hpp>
|
||||
|
||||
void LoadWorker::start() {
|
||||
while (_running) {
|
||||
_context->workNext();
|
||||
std::this_thread::yield();
|
||||
}
|
||||
}
|
||||
|
||||
void WorkContext::workNext() {
|
||||
WorkJob* j = nullptr;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(_inMutex);
|
||||
|
||||
if (!_workQueue.empty()) {
|
||||
j = _workQueue.front();
|
||||
_workQueue.pop();
|
||||
}
|
||||
}
|
||||
|
||||
if (j == nullptr) return;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,101 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
|
||||
class WorkContext;
|
||||
|
||||
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)) {
|
||||
}
|
||||
|
||||
~LoadWorker() {
|
||||
_running = false;
|
||||
_thread.join();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Interface for background work
|
||||
*/
|
||||
class WorkJob {
|
||||
WorkContext* _context;
|
||||
|
||||
public:
|
||||
WorkJob(WorkContext* context) : _context(context) {
|
||||
}
|
||||
|
||||
virtual ~WorkJob() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief getContext
|
||||
* @return The loading context for this Loader
|
||||
*/
|
||||
WorkContext* getContext() const {
|
||||
return _context;
|
||||
}
|
||||
|
||||
virtual void work() = 0;
|
||||
virtual void complete() {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A worker queue that runs work in the background.
|
||||
*
|
||||
* 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;
|
||||
|
||||
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;
|
||||
|
||||
public:
|
||||
WorkContext() : _worker(new LoadWorker(this)) {
|
||||
}
|
||||
|
||||
void queueJob(WorkJob* job) {
|
||||
std::lock_guard<std::mutex> guard(_inMutex);
|
||||
_workQueue.push(job);
|
||||
}
|
||||
|
||||
void stop() {
|
||||
// Stop serving the queue.
|
||||
_worker.reset(nullptr);
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
return (_workQueue.size() + _completeQueue.size()) == 0;
|
||||
}
|
||||
|
||||
void update();
|
||||
};
|
@ -44,7 +44,6 @@ set(TEST_SOURCES
|
||||
"test_vehicle.cpp"
|
||||
"test_VisualFX.cpp"
|
||||
"test_weapon.cpp"
|
||||
"test_worker.cpp"
|
||||
"test_world.cpp"
|
||||
|
||||
# Hack in rwgame sources until there's a per-target test suite
|
||||
|
@ -1,49 +0,0 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <job/WorkContext.hpp>
|
||||
|
||||
class TestJob : public WorkJob {
|
||||
public:
|
||||
bool *_worked, *_completed;
|
||||
|
||||
TestJob(WorkContext *context, bool *w, bool *c)
|
||||
: WorkJob(context), _worked(w), _completed(c) {
|
||||
}
|
||||
|
||||
void work() {
|
||||
*_worked = true;
|
||||
}
|
||||
|
||||
void complete() {
|
||||
*_completed = true;
|
||||
}
|
||||
};
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(WorkTests)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_interface) {
|
||||
{
|
||||
WorkContext context;
|
||||
|
||||
bool worked = false, completed = false;
|
||||
|
||||
TestJob *job = new TestJob(&context, &worked, &completed);
|
||||
|
||||
BOOST_CHECK(!worked);
|
||||
BOOST_CHECK(!completed);
|
||||
|
||||
context.queueJob(job);
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
|
||||
BOOST_CHECK(worked);
|
||||
BOOST_CHECK(!completed);
|
||||
|
||||
context.update();
|
||||
;
|
||||
|
||||
BOOST_CHECK(worked);
|
||||
BOOST_CHECK(completed);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
Loading…
Reference in New Issue
Block a user