[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 )
: 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() );

View File

@ -23,6 +23,7 @@
// Qt
#include <QAbstractItemModel>
#include <QMutex>
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 */