[partition] Protect PartitionModel against concurrent access

- Try to avoid concurrent access while the model is being reset.
This commit is contained in:
Adriaan de Groot 2019-04-01 08:09:37 -04:00
parent df921606b9
commit dd6d1bf1c1
2 changed files with 11 additions and 0 deletions

View File

@ -40,11 +40,16 @@
PartitionModel::ResetHelper::ResetHelper( PartitionModel* model ) PartitionModel::ResetHelper::ResetHelper( PartitionModel* model )
: m_model( model ) : m_model( model )
{ {
m_model->m_lock.lock();
m_model->beginResetModel(); m_model->beginResetModel();
} }
PartitionModel::ResetHelper::~ResetHelper() 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(); m_model->endResetModel();
} }
@ -58,6 +63,7 @@ PartitionModel::PartitionModel( QObject* parent )
void void
PartitionModel::init( Device* device , const OsproberEntryList& osproberEntries ) PartitionModel::init( Device* device , const OsproberEntryList& osproberEntries )
{ {
QMutexLocker lock(&m_lock);
beginResetModel(); beginResetModel();
m_device = device; m_device = device;
m_osproberEntries = osproberEntries; m_osproberEntries = osproberEntries;
@ -271,6 +277,7 @@ PartitionModel::headerData( int section, Qt::Orientation orientation, int role )
Partition* Partition*
PartitionModel::partitionForIndex( const QModelIndex& index ) const PartitionModel::partitionForIndex( const QModelIndex& index ) const
{ {
QMutexLocker lock(&m_lock);
if ( !index.isValid() ) if ( !index.isValid() )
return nullptr; return nullptr;
return reinterpret_cast< Partition* >( index.internalPointer() ); return reinterpret_cast< Partition* >( index.internalPointer() );

View File

@ -23,6 +23,7 @@
// Qt // Qt
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QMutex>
class Device; class Device;
class Partition; class Partition;
@ -115,8 +116,11 @@ public:
void update(); void update();
private: private:
friend class ResetHelper;
Device* m_device; Device* m_device;
OsproberEntryList m_osproberEntries; OsproberEntryList m_osproberEntries;
mutable QMutex m_lock;
}; };
#endif /* PARTITIONMODEL_H */ #endif /* PARTITIONMODEL_H */