Make thumbnail code threadsafe

The thumbnailing in qt-models/divepicturemodel.cpp was performed
concurrently, but the thumbnailCache was not protected from races.
Resolve this by guarding the thumbnalCache accesses with mutexes.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2017-12-10 00:17:57 +01:00 committed by Robert C. Helling
parent c73828d605
commit 92ad7865d0

View file

@ -8,20 +8,26 @@
#include <QtConcurrent>
extern QHash <QString, QImage> thumbnailCache;
static QMutex thumbnailMutex;
void scaleImages(PictureEntry &entry)
{
QMutexLocker l(&thumbnailMutex);
if (thumbnailCache.contains(entry.filename) && !thumbnailCache.value(entry.filename).isNull()) {
entry.image = thumbnailCache.value(entry.filename);
} else {
int dim = defaultIconMetrics().sz_pic;
QImage p = SHashedImage(entry.picture);
if(!p.isNull()) {
p = p.scaled(dim, dim, Qt::KeepAspectRatio);
thumbnailCache.insert(entry.filename, p);
}
entry.image = p;
return;
}
l.unlock();
int dim = defaultIconMetrics().sz_pic;
QImage p = SHashedImage(entry.picture);
if(!p.isNull()) {
p = p.scaled(dim, dim, Qt::KeepAspectRatio);
QMutexLocker l(&thumbnailMutex);
if (!thumbnailCache.contains(entry.filename))
thumbnailCache.insert(entry.filename, p);
}
entry.image = p;
}
DivePictureModel *DivePictureModel::instance()