From 92ad7865d0f5269bfe12471b062fbda3bb373b7f Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 10 Dec 2017 00:17:57 +0100 Subject: 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 --- qt-models/divepicturemodel.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'qt-models') diff --git a/qt-models/divepicturemodel.cpp b/qt-models/divepicturemodel.cpp index d135e0faa..41035aba2 100644 --- a/qt-models/divepicturemodel.cpp +++ b/qt-models/divepicturemodel.cpp @@ -8,20 +8,26 @@ #include extern QHash 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); + 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; } + entry.image = p; } DivePictureModel *DivePictureModel::instance() -- cgit v1.2.3-70-g09d2