Lock the whole replace operation in a mutex.

This commit is contained in:
Teo Mrnjavac 2015-12-24 17:02:50 +01:00
parent 9847b8efa9
commit 14f4335420
2 changed files with 22 additions and 12 deletions

View File

@ -46,6 +46,8 @@
#include <QDir> #include <QDir>
#include <QLabel> #include <QLabel>
#include <QListView> #include <QListView>
#include <QFutureWatcher>
#include <QtConcurrent/QtConcurrent>
@ -433,28 +435,34 @@ ChoicePage::applyActionChoice( ChoicePage::Choice choice )
connect( m_beforePartitionBarsView->selectionModel(), &QItemSelectionModel::currentRowChanged, connect( m_beforePartitionBarsView->selectionModel(), &QItemSelectionModel::currentRowChanged,
this, [ this ]( const QModelIndex& current, const QModelIndex& previous ) this, [ this ]( const QModelIndex& current, const QModelIndex& previous )
{ {
auto doReplace = [=] QFutureWatcher< void >* watcher = new QFutureWatcher< void >();
connect( watcher, &QFutureWatcher< void >::finished,
this, [ watcher ]
{ {
// We can't use the PartitionPtrRole because we need to make changes to the watcher->deleteLater();
// main DeviceModel, not the immutable copy. } );
QString partPath = current.data( PartitionModel::PartitionPathRole ).toString();
Partition* partition = KPMHelpers::findPartitionByPath( { selectedDevice() }, auto doReplace = [ this, current, dev = selectedDevice() ]
partPath ); {
if ( partition ) QMutexLocker( &( this->m_coreMutex ) );
PartitionActions::doReplacePartition( m_core,
selectedDevice(),
partition );
PartitionModel* m = qobject_cast< PartitionModel* >( m_afterPartitionBarsView->model() );
if ( m )
m->update();
};
if ( m_core->isDirty() ) if ( m_core->isDirty() )
{ {
m_core->asyncRevertDevice( selectedDevice(), doReplace ); m_core->revertDevice( dev );
} }
else // We can't use the PartitionPtrRole because we need to make changes to the
doReplace(); // main DeviceModel, not the immutable copy.
QString partPath = current.data( PartitionModel::PartitionPathRole ).toString();
Partition* partition = KPMHelpers::findPartitionByPath( { dev },
partPath );
if ( partition )
PartitionActions::doReplacePartition( m_core,
dev,
partition );
};
QFuture< void > future = QtConcurrent::run( doReplace );
watcher->setFuture( future );
} ); } );
break; break;
case NoChoice: case NoChoice:

View File

@ -105,6 +105,8 @@ private:
QPointer< PartitionLabelsView > m_afterPartitionLabelsView; QPointer< PartitionLabelsView > m_afterPartitionLabelsView;
int m_lastSelectedDeviceIndex; int m_lastSelectedDeviceIndex;
QMutex m_coreMutex;
}; };
#endif // CHOICEPAGE_H #endif // CHOICEPAGE_H