Commit 52ceb245 authored by David Edmundson's avatar David Edmundson
Browse files

Add mutex round static member used in a thread

Summary:
BackgroundFinder runs in a separate thread, it uses a static QStringList cache
It's perfectly plausible that two BackgroundFinders threads could run at once

Test Plan:
Old code didn't crash, it was a pure hypothetical.
Works as before

Reviewers: #plasma, mart

Reviewed By: #plasma, mart

Subscribers: anthonyfieroni, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D11590
parent 21d97beb
......@@ -33,6 +33,8 @@
#include <QImageReader>
#include <QMimeDatabase>
#include <QMimeType>
#include <QMutex>
#include <QMutexLocker>
#include <QDebug>
#include <KIO/PreviewJob>
......@@ -48,7 +50,8 @@
#include "image.h"
QStringList BackgroundFinder::m_suffixes;
QStringList BackgroundFinder::s_suffixes;
QMutex BackgroundFinder::s_suffixMutex;
ImageSizeFinder::ImageSizeFinder(const QString &path, QObject *parent)
: QObject(parent),
......@@ -512,9 +515,10 @@ QString BackgroundFinder::token() const
return m_token;
}
const QStringList &BackgroundFinder::suffixes()
QStringList BackgroundFinder::suffixes()
{
if (m_suffixes.isEmpty()) {
QMutexLocker lock(&s_suffixMutex);
if (s_suffixes.isEmpty()) {
QSet<QString> suffixes;
QMimeDatabase db;
......@@ -525,10 +529,10 @@ const QStringList &BackgroundFinder::suffixes()
}
}
m_suffixes = suffixes.toList();
s_suffixes = suffixes.toList();
}
return m_suffixes;
return s_suffixes;
}
bool BackgroundFinder::isAcceptableSuffix(const QString &suffix)
......
......@@ -26,6 +26,7 @@
#include <QPixmap>
#include <QRunnable>
#include <QThread>
#include <QMutex>
#include <QSet>
#include <KDirWatch>
......@@ -131,7 +132,7 @@ public:
QString token() const;
static const QStringList &suffixes();
static QStringList suffixes();
static bool isAcceptableSuffix(const QString &suffix);
Q_SIGNALS:
......@@ -144,7 +145,8 @@ private:
QStringList m_paths;
QString m_token;
static QStringList m_suffixes;
static QMutex s_suffixMutex;
static QStringList s_suffixes;
};
#endif // BACKGROUNDLISTMODEL_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment