From c8fc7abade6e28507f2a1590007a098e647026a9 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Fri, 14 Feb 2020 01:28:50 -0500 Subject: thread-safe OriginConnection ThreadPool now keeps threads running keep ModThreads around so avoid reallocating buffers --- src/directoryrefresher.cpp | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) (limited to 'src/directoryrefresher.cpp') diff --git a/src/directoryrefresher.cpp b/src/directoryrefresher.cpp index 20d6a52b..a59ddf9d 100644 --- a/src/directoryrefresher.cpp +++ b/src/directoryrefresher.cpp @@ -203,8 +203,8 @@ struct ModThread std::wstring modName; std::wstring path; int prio = -1; - env::Directory* dir = nullptr; DirectoryStats* stats = nullptr; + env::DirectoryWalker walker; std::condition_variable cv; std::mutex mutex; @@ -212,7 +212,11 @@ struct ModThread void wakeup() { - ready = true; + { + std::scoped_lock lock(mutex); + ready = true; + } + cv.notify_one(); } @@ -222,7 +226,7 @@ struct ModThread cv.wait(lock, [&]{ return ready; }); SetThisThreadName(QString::fromStdWString(modName + L" refresher")); - ds->addFromOrigin(modName, path, prio, *stats); + ds->addFromOrigin(walker, modName, path, prio, *stats); /*if (Settings::instance().archiveParsing()) { addModBSAToStructure( @@ -237,6 +241,9 @@ struct ModThread } }; +env::ThreadPool g_threads; + + void dumpStats(std::vector& stats) { static int run = 0; @@ -268,35 +275,33 @@ void DirectoryRefresher::addMultipleModsFilesToStructure( MOShared::DirectoryEntry *directoryStructure, const std::vector& entries, bool emitProgress) { - std::vector dirs(entries.size()); std::vector stats(entries.size()); + g_threads.setMax(m_threadCount); + { TimeThis tt("walk dirs"); - env::ThreadPool threads(m_threadCount); - for (std::size_t i=0; i(i + 1); + stats[i].mod = entries[i].modName.toStdString(); + try { if (e.stealFiles.length() > 0) { stealModFilesIntoStructure( directoryStructure, e.modName, prio, e.absolutePath, e.stealFiles); } else { - auto& mt = threads.request(); + auto& mt = g_threads.request(); mt.ds = directoryStructure; mt.modName = entries[i].modName.toStdWString(); mt.path = QDir::toNativeSeparators(e.absolutePath).toStdWString(); mt.prio = prio; - mt.dir = &dirs[i]; mt.stats = &stats[i]; - stats[i].mod = entries[i].modName.toStdString(); - mt.wakeup(); } } catch (const std::exception& ex) { @@ -308,7 +313,7 @@ void DirectoryRefresher::addMultipleModsFilesToStructure( } } - threads.join(); + g_threads.waitForAll(); } dumpStats(stats); @@ -347,12 +352,12 @@ void DirectoryRefresher::refresh() m_DirectoryStructure->getFileRegister()->sortOrigins(); - emit progress(100); - cleanStructure(m_DirectoryStructure); + } - emit refreshed(); + emit progress(100); - //logcounts("after refresh"); - } + emit refreshed(); + + //logcounts("after refresh"); } -- cgit v1.3.1