remove extra shared_ptr use from util::ThreadPool
This commit is contained in:
parent
7f0e12bb4e
commit
054b3926cd
@ -272,15 +272,15 @@ public:
|
||||
LoaderWorker(AssetsLoader* loader) : loader(loader) {
|
||||
}
|
||||
|
||||
assetload::postfunc operator()(const std::shared_ptr<aloader_entry>& entry
|
||||
assetload::postfunc operator()(const aloader_entry& entry
|
||||
) override {
|
||||
aloader_func loadfunc = loader->getLoader(entry->tag);
|
||||
aloader_func loadfunc = loader->getLoader(entry.tag);
|
||||
return loadfunc(
|
||||
loader,
|
||||
loader->getPaths(),
|
||||
entry->filename,
|
||||
entry->alias,
|
||||
entry->config
|
||||
entry.filename,
|
||||
entry.alias,
|
||||
entry.config
|
||||
);
|
||||
}
|
||||
};
|
||||
@ -290,14 +290,13 @@ std::shared_ptr<Task> AssetsLoader::startTask(runnable onDone) {
|
||||
std::make_shared<util::ThreadPool<aloader_entry, assetload::postfunc>>(
|
||||
"assets-loader-pool",
|
||||
[=]() { return std::make_shared<LoaderWorker>(this); },
|
||||
[=](assetload::postfunc& func) { func(assets); }
|
||||
[=](const assetload::postfunc& func) { func(assets); }
|
||||
);
|
||||
pool->setOnComplete(std::move(onDone));
|
||||
while (!entries.empty()) {
|
||||
const aloader_entry& entry = entries.front();
|
||||
auto ptr = std::make_shared<aloader_entry>(entry);
|
||||
pool->enqueueJob(ptr);
|
||||
aloader_entry entry = std::move(entries.front());
|
||||
entries.pop();
|
||||
pool->enqueueJob(std::move(entry));
|
||||
}
|
||||
return pool;
|
||||
}
|
||||
|
||||
@ -43,8 +43,8 @@ using aloader_func = std::function<
|
||||
|
||||
struct aloader_entry {
|
||||
AssetType tag;
|
||||
const std::string filename;
|
||||
const std::string alias;
|
||||
std::string filename;
|
||||
std::string alias;
|
||||
std::shared_ptr<AssetCfg> config;
|
||||
};
|
||||
|
||||
|
||||
@ -27,8 +27,8 @@ public:
|
||||
: converter(std::move(converter)) {
|
||||
}
|
||||
|
||||
int operator()(const std::shared_ptr<ConvertTask>& task) override {
|
||||
converter->convert(*task);
|
||||
int operator()(const ConvertTask& task) override {
|
||||
converter->convert(task);
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
@ -169,10 +169,10 @@ std::shared_ptr<Task> WorldConverter::startTask(
|
||||
);
|
||||
auto& converterTasks = converter->tasks;
|
||||
while (!converterTasks.empty()) {
|
||||
const ConvertTask& task = converterTasks.front();
|
||||
auto ptr = std::make_shared<ConvertTask>(task);
|
||||
pool->enqueueJob(ptr);
|
||||
ConvertTask task = std::move(converterTasks.front());
|
||||
converterTasks.pop();
|
||||
pool->enqueueJob(std::move(task));
|
||||
|
||||
}
|
||||
pool->setOnComplete([=]() {
|
||||
converter->write();
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
|
||||
static debug::Logger logger("chunks-render");
|
||||
|
||||
class RendererWorker : public util::Worker<Chunk, RendererResult> {
|
||||
class RendererWorker : public util::Worker<std::shared_ptr<Chunk>, RendererResult> {
|
||||
Level* level;
|
||||
BlocksRenderer renderer;
|
||||
public:
|
||||
|
||||
@ -30,7 +30,7 @@ class ChunksRenderer {
|
||||
std::unordered_map<glm::ivec2, std::shared_ptr<Mesh>> meshes;
|
||||
std::unordered_map<glm::ivec2, bool> inwork;
|
||||
|
||||
util::ThreadPool<Chunk, RendererResult> threadPool;
|
||||
util::ThreadPool<std::shared_ptr<Chunk>, RendererResult> threadPool;
|
||||
public:
|
||||
ChunksRenderer(
|
||||
Level* level,
|
||||
|
||||
@ -17,7 +17,7 @@ namespace util {
|
||||
|
||||
template <class J, class T>
|
||||
struct ThreadPoolResult {
|
||||
std::shared_ptr<J> job;
|
||||
J job;
|
||||
std::condition_variable& variable;
|
||||
int workerIndex;
|
||||
bool& locked;
|
||||
@ -29,13 +29,13 @@ namespace util {
|
||||
public:
|
||||
Worker() = default;
|
||||
virtual ~Worker() = default;
|
||||
virtual R operator()(const std::shared_ptr<T>&) = 0;
|
||||
virtual R operator()(const T&) = 0;
|
||||
};
|
||||
|
||||
template <class T, class R>
|
||||
class ThreadPool : public Task {
|
||||
debug::Logger logger;
|
||||
std::queue<std::shared_ptr<T>> jobs;
|
||||
std::queue<T> jobs;
|
||||
std::queue<ThreadPoolResult<T, R>> results;
|
||||
std::mutex resultsMutex;
|
||||
std::vector<std::thread> threads;
|
||||
@ -43,7 +43,7 @@ namespace util {
|
||||
std::mutex jobsMutex;
|
||||
std::vector<std::unique_lock<std::mutex>> workersBlocked;
|
||||
consumer<R&> resultConsumer;
|
||||
consumer<std::shared_ptr<T>&> onJobFailed = nullptr;
|
||||
consumer<T&> onJobFailed = nullptr;
|
||||
runnable onComplete = nullptr;
|
||||
std::atomic<int> busyWorkers = 0;
|
||||
std::atomic<uint> jobsDone = 0;
|
||||
@ -57,7 +57,7 @@ namespace util {
|
||||
std::mutex mutex;
|
||||
bool locked = false;
|
||||
while (working) {
|
||||
std::shared_ptr<T> job;
|
||||
T job;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(jobsMutex);
|
||||
jobsMutexCondition.wait(lock, [this] {
|
||||
@ -66,7 +66,7 @@ namespace util {
|
||||
if (!working || failed) {
|
||||
break;
|
||||
}
|
||||
job = jobs.front();
|
||||
job = std::move(jobs.front());
|
||||
jobs.pop();
|
||||
|
||||
busyWorkers++;
|
||||
@ -229,10 +229,10 @@ namespace util {
|
||||
}
|
||||
}
|
||||
|
||||
void enqueueJob(const std::shared_ptr<T>& job) {
|
||||
void enqueueJob(T job) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(jobsMutex);
|
||||
jobs.push(job);
|
||||
jobs.push(std::move(job));
|
||||
}
|
||||
jobsMutexCondition.notify_one();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user