From dd6d1bf1c1c324873b2fc39c5fa28dd3a4c4a0da Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 1 Apr 2019 08:09:37 -0400 Subject: [PATCH] [partition] Protect PartitionModel against concurrent access - Try to avoid concurrent access while the model is being reset. --- src/modules/partition/core/PartitionModel.cpp | 7 +++++++ src/modules/partition/core/PartitionModel.h | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/src/modules/partition/core/PartitionModel.cpp b/src/modules/partition/core/PartitionModel.cpp index a685a8bf5..e92a9fe32 100644 --- a/src/modules/partition/core/PartitionModel.cpp +++ b/src/modules/partition/core/PartitionModel.cpp @@ -40,11 +40,16 @@ PartitionModel::ResetHelper::ResetHelper( PartitionModel* model ) : m_model( model ) { + m_model->m_lock.lock(); m_model->beginResetModel(); } PartitionModel::ResetHelper::~ResetHelper() { + // We need to unlock the mutex before emitting the reset signal, + // because the reset will cause clients to start looking at the + // (new) data. + m_model->m_lock.unlock(); m_model->endResetModel(); } @@ -58,6 +63,7 @@ PartitionModel::PartitionModel( QObject* parent ) void PartitionModel::init( Device* device , const OsproberEntryList& osproberEntries ) { + QMutexLocker lock(&m_lock); beginResetModel(); m_device = device; m_osproberEntries = osproberEntries; @@ -271,6 +277,7 @@ PartitionModel::headerData( int section, Qt::Orientation orientation, int role ) Partition* PartitionModel::partitionForIndex( const QModelIndex& index ) const { + QMutexLocker lock(&m_lock); if ( !index.isValid() ) return nullptr; return reinterpret_cast< Partition* >( index.internalPointer() ); diff --git a/src/modules/partition/core/PartitionModel.h b/src/modules/partition/core/PartitionModel.h index f36a496ea..f5289254b 100644 --- a/src/modules/partition/core/PartitionModel.h +++ b/src/modules/partition/core/PartitionModel.h @@ -23,6 +23,7 @@ // Qt #include +#include class Device; class Partition; @@ -115,8 +116,11 @@ public: void update(); private: + friend class ResetHelper; + Device* m_device; OsproberEntryList m_osproberEntries; + mutable QMutex m_lock; }; #endif /* PARTITIONMODEL_H */