diff --git a/src/backend.cpp b/src/backend.cpp index 870ff815..a3cc8bf9 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -396,7 +396,7 @@ void Backend::describe(const QVariantMap &item, bool inTrash) { }); // Stepping through files faster than they're read queues readings; the // ones overtaken before they start are skipped rather than left to tie - // up the threads the next folder is listed on. + // up the global pool's threads. watcher->setFuture(QtConcurrent::run([path, kind, inTrash, latest = m_describing, generation] { QVariantMap read; if (*latest != generation) diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index a34caf2c..4fbf50ad 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -40,6 +41,19 @@ FolderModel::~FolderModel() { *m_latest = 0; } +// Never deleted, like Places', so a dead mount can't stop Folio quitting: a listing +// gets a second to finish, and one stuck longer may still meet destroyed statics. +QThreadPool &FolderModel::listingPool() { + static QThreadPool *const pool = [] { + qAddPostRoutine([] { listingPool().waitForDone(1000); }); + auto *p = new QThreadPool; + // Eight, not the CPU count, so a few folders stuck on dead mounts leave others listing. + p->setMaxThreadCount(8); + return p; + }(); + return *pool; +} + // Recent listings, shared by every model (the window's folder and each // column), so stepping back and forth or opening a column for a folder just // seen shows it in the same frame instead of an old or empty one. Each is @@ -520,7 +534,7 @@ void FolderModel::reload() { remember(); countFolders(generation); }); - watcher->setFuture(QtConcurrent::run([path, showHidden, sort, query, items, filter, latest, generation, locale] { + watcher->setFuture(QtConcurrent::run(&FolderModel::listingPool(), [path, showHidden, sort, query, items, filter, latest, generation, locale] { QString error; const auto overtaken = [&] { return *latest != generation; }; auto entries = isPluginView(path) ? listItems(items, query) diff --git a/src/foldermodel.h b/src/foldermodel.h index 3fbc149d..b20fb8b6 100644 --- a/src/foldermodel.h +++ b/src/foldermodel.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -110,6 +111,10 @@ class FolderModel : public QAbstractListModel { Q_INVOKABLE bool isSelected(int row) const; Q_INVOKABLE QStringList selectedPaths() const; + // Listings run here, not on the global pool, so folder counts and file + // transfers can't hold up the folder just opened. + static QThreadPool &listingPool(); + // Synchronous building blocks, used by the background passes and tests. // A listing stops, empty, as soon as `stop` returns true. Names sort // by `locale`'s alphabet; a background pass passes the one it was diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 42458bd1..64219594 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -745,11 +745,8 @@ private slots: void folderModelStopsReadingWhatsBeenOvertaken() { - // A big folder left before it's been read, or a search of it, stops - // being read instead of holding on to a thread of the pool that the - // next listing, the folder counts and file transfers all wait for. - // With a pool of one thread, the next folder waits for the overtaken - // read to stop, so it shows in a fraction of the time the read takes. + // With one listing thread, the next folder shows in a fraction of the + // time a big read takes only if the overtaken read stops. QTemporaryDir big, small, other; for (int i = 0; i < 20000; ++i) write(big.filePath(QString("%1.txt").arg(i)), "x"); @@ -762,7 +759,7 @@ private slots: FolderModel::search(big.path(), "none", false, "name"); const qint64 searching = timer.elapsed(); - QThreadPool *pool = QThreadPool::globalInstance(); + QThreadPool *pool = &FolderModel::listingPool(); pool->waitForDone(); const int threads = pool->maxThreadCount(); pool->setMaxThreadCount(1); @@ -3320,6 +3317,35 @@ private slots: QCOMPARE(backend.takeSelection(), QString("report.pdf")); } + void folderModelListsWhileOtherWorkIsStuck() + { + // Every thread of the global pool busy, and seven listings hung on dead mounts. + QTemporaryDir dir; + write(dir.filePath("a.txt"), "x"); + QThreadPool *pool = QThreadPool::globalInstance(); + pool->waitForDone(); + FolderModel::listingPool().waitForDone(); + std::atomic hold = true; + const auto stuck = [&] { + while (hold) + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + }; + for (int i = 0; i < pool->maxThreadCount(); ++i) + pool->start(stuck); + for (int i = 0; i < 7; ++i) + FolderModel::listingPool().start(stuck); + const auto release = qScopeGuard([&] { + hold = false; + pool->waitForDone(); + FolderModel::listingPool().waitForDone(); + }); + FolderModel model; + QSignalSpy loaded(&model, &FolderModel::loaded); + model.setPath(dir.path()); + QVERIFY(loaded.wait(3000)); + QCOMPARE(model.count(), 1); + } + void backendReadsDetailsOffTheUiThread() { // A picture's size and a text's opening lines are read in the