From fd5a03f5d31bcd8f976c400e10765e0e55f5211a Mon Sep 17 00:00:00 2001
From: Emmanuel Lepage Vallee <elv1313@gmail.com>
Date: Fri, 12 May 2017 10:08:04 -0400
Subject: [PATCH] certificateModel: Add a new mutex for the cache insertion

It isn't the same operation as the already protected sections.
The new mutex is specific to m_hCertificates insertions.

Change-Id: Ib178fa216a7dd082aba18b5131b87271b1d0a4e8
Reviewed-by: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
---
 src/certificatemodel.cpp         | 12 ++++++++++++
 src/private/certificatemodel_p.h |  1 +
 2 files changed, 13 insertions(+)

diff --git a/src/certificatemodel.cpp b/src/certificatemodel.cpp
index 4a4e1a73..543e1d86 100644
--- a/src/certificatemodel.cpp
+++ b/src/certificatemodel.cpp
@@ -629,7 +629,11 @@ Certificate* CertificateModel::getCertificateFromPath(const QString& path, Accou
    if (!cert) {
       cert = new Certificate(path, Certificate::Type::NONE);
       cert->setCollection(m_pFallbackDaemonCollection);
+
+      { // mutex
+      QMutexLocker(&d_ptr->m_CertInsertion);
       d_ptr->m_hCertificates[path.toLatin1()] = cert;
+      } // mutex
 
       //Add it to the model
       d_ptr->addToTree(cert,a);
@@ -656,7 +660,11 @@ Certificate* CertificateModel::getCertificateFromPath(const QString& path, Certi
    if (!cert) {
       cert = new Certificate(path, type);
       cert->setCollection(m_pFallbackDaemonCollection);
+
+      { // mutex
+      QMutexLocker(&d_ptr->m_CertInsertion);
       d_ptr->m_hCertificates[path.toLatin1()] = cert;
+      } // mutex
 
       //Add it to the model
       d_ptr->addToTree(cert);
@@ -672,7 +680,11 @@ Certificate* CertificateModel::getCertificateFromId(const QString& id, Account*
    //The certificate is not loaded yet
    if (!cert) {
       cert = new Certificate(id);
+
+      { // mutex
+      QMutexLocker(&d_ptr->m_CertInsertion);
       d_ptr->m_hCertificates[id.toLatin1()] = cert;
+      } // mutex
 
       if ((!a) && (!category.isEmpty())) {
          CertificateNode* cat = d_ptr->m_hStrToCat.value(category);
diff --git a/src/private/certificatemodel_p.h b/src/private/certificatemodel_p.h
index 163e84e7..c0ff21ac 100644
--- a/src/private/certificatemodel_p.h
+++ b/src/private/certificatemodel_p.h
@@ -57,6 +57,7 @@ public:
    QHash<QString,Certificate*>      m_hCertificates     ;
    CertificateNode*                 m_pDefaultCategory  ;
    QMutex                           m_CertLoader        ;
+   QMutex                           m_CertInsertion     ;
    int                              m_GroupCounter      ;
    QHash<const Account*,CertificateNode*> m_hAccToCat   ;
    QHash<const QString&,CertificateNode*> m_hStrToCat   ;
-- 
GitLab