From dfcec4d9dac441d32e53ffa1beb85186e9bd6c4e Mon Sep 17 00:00:00 2001 From: ZehMatt Date: Tue, 24 Apr 2018 10:44:33 +0200 Subject: [PATCH] Refactor file indexing to use the job pool instead of threads. --- src/openrct2/core/FileIndex.hpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/openrct2/core/FileIndex.hpp b/src/openrct2/core/FileIndex.hpp index e2440dda8a..982fd42f9d 100644 --- a/src/openrct2/core/FileIndex.hpp +++ b/src/openrct2/core/FileIndex.hpp @@ -28,6 +28,7 @@ #include "FileScanner.h" #include "FileStream.hpp" #include "Path.hpp" +#include "JobPool.hpp" template class FileIndex @@ -215,36 +216,35 @@ private: const size_t totalCount = scanResult.Files.size(); if (totalCount > 0) { - const size_t numThreads = std::thread::hardware_concurrency(); - - size_t stepSize = totalCount / numThreads; - - std::vector threads; - std::vector> containers; + JobPool jobPool; std::mutex printLock; // For verbose prints. - containers.resize(numThreads + (totalCount % stepSize == 0 ? 0 : 1)); - for (size_t rangeStart = 0; rangeStart < totalCount; rangeStart += stepSize) + std::vector> containers; + + size_t stepSize = std::thread::hardware_concurrency(); + size_t numTasks = totalCount / stepSize; + size_t taskGroup = 0; + numTasks += (totalCount % stepSize == 0 ? 0 : 1); + + containers.resize(numTasks); + + for (size_t rangeStart = 0; rangeStart < totalCount; rangeStart += stepSize, taskGroup++) { if (rangeStart + stepSize > totalCount) stepSize = totalCount - rangeStart; - auto& items = containers[threads.size()]; + auto& items = containers[taskGroup]; - threads.emplace_back(&FileIndex::BuildRange, + jobPool.addTask(std::bind(&FileIndex::BuildRange, this, std::cref(scanResult), rangeStart, rangeStart + stepSize, std::ref(items), - std::ref(printLock)); + std::ref(printLock))); } - for (auto&& itr : threads) - { - if (itr.joinable()) - itr.join(); - } + jobPool.join(); for (auto&& itr : containers) {