Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion src/foldermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QCache>
#include <QCollator>
#include <QCoreApplication>
#include <QDir>
#include <QDirIterator>
#include <QXmlStreamReader>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/foldermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QFileSystemWatcher>
#include <QLocale>
#include <QStringList>
#include <QThreadPool>
#include <QTimer>
#include <QVariantList>
#include <QSet>
Expand Down Expand Up @@ -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
Expand Down
38 changes: 32 additions & 6 deletions tests/folio_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down Expand Up @@ -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<bool> 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
Expand Down
Loading