Skip to content
Permalink
Browse files

Force task status update to be main in the main thread

  • Loading branch information
Julien Cabieces authored and nyalldawson committed Jan 14, 2020
1 parent 121cc00 commit e91cac05c724f3fab8eb6c0b6f2bcf13bf905b8e
Showing with 21 additions and 18 deletions.
  1. +5 −6 src/core/qgstaskmanager.cpp
  2. +6 −3 src/core/qgstaskmanager.h
  3. +10 −9 tests/src/core/testqgstaskmanager.cpp
@@ -172,6 +172,7 @@ bool QgsTask::waitForFinished( int timeout )
if ( mNotFinishedMutex.tryLock( timeout ) )
{
mNotFinishedMutex.unlock();
QCoreApplication::sendPostedEvents( this );
rv = true;
}
else
@@ -254,7 +255,8 @@ void QgsTask::setProgress( double progress )
void QgsTask::completed()
{
mStatus = Complete;
processSubTasksForCompletion();
QMetaObject::invokeMethod( this, &QgsTask::processSubTasksForCompletion, Qt::AutoConnection );
mNotFinishedMutex.unlock();
}

void QgsTask::processSubTasksForCompletion()
@@ -277,8 +279,6 @@ void QgsTask::processSubTasksForCompletion()
setProgress( 100.0 );
emit statusChanged( Complete );
emit taskCompleted();
mNotFinishedMutex.tryLock(); // we're not guaranteed to already have the lock in place here
mNotFinishedMutex.unlock();
}
else if ( mStatus == Complete )
{
@@ -306,8 +306,6 @@ void QgsTask::processSubTasksForTermination()

emit statusChanged( Terminated );
emit taskTerminated();
mNotFinishedMutex.tryLock(); // we're not guaranteed to already have the lock in place here
mNotFinishedMutex.unlock();
}
else if ( mStatus == Terminated && !subTasksTerminated )
{
@@ -344,7 +342,8 @@ void QgsTask::processSubTasksForHold()
void QgsTask::terminated()
{
mStatus = Terminated;
processSubTasksForTermination();
QMetaObject::invokeMethod( this, &QgsTask::processSubTasksForTermination, Qt::AutoConnection );
mNotFinishedMutex.unlock();
}


@@ -358,16 +358,19 @@ class CORE_EXPORT QgsTask : public QObject
*/
void terminated();

void processSubTasksForCompletion();

void processSubTasksForTermination();

void processSubTasksForHold();

friend class QgsTaskManager;
friend class QgsTaskRunnableWrapper;
friend class TestQgsTaskManager;

private slots:

void processSubTasksForCompletion();

void processSubTasksForTermination();

};


@@ -522,7 +522,10 @@ void TestQgsTaskManager::taskFinished()
manager.addTask( task );

while ( task->status() == QgsTask::Running
|| task->status() == QgsTask::Queued ) { }
|| task->status() == QgsTask::Queued )
{
QCoreApplication::processEvents();
}
while ( manager.countActiveTasks() > 0 )
{
QCoreApplication::processEvents();
@@ -533,9 +536,6 @@ void TestQgsTaskManager::taskFinished()

void TestQgsTaskManager::subTask()
{
if ( QgsTest::isTravis() )
QSKIP( "This test is disabled on Travis CI environment" );

QgsTaskManager manager;

// parent with one subtask
@@ -848,20 +848,19 @@ void TestQgsTaskManager::waitForFinishedBeforeStart()
QgsTaskManager manager;

// add a wait task so the test task is not started when we call waitforfinished
QPointer<QgsTask> waitTask = new WaitTask();
QPointer<QgsTask> waitTask = new WaitTask( "wait_task" );
manager.addTask( waitTask );

QPointer<QgsTask> testTask = new TestTask();
QPointer<QgsTask> testTask = new TestTask( "test_task" );
manager.addTask( testTask );

testTask->waitForFinished();

QgsTask::TaskStatus status = testTask->status();

Q_ASSERT( status == QgsTask::Complete );
QCOMPARE( status, QgsTask::Complete );

waitTask->waitForFinished();
Q_ASSERT( waitTask->status() == QgsTask::Complete );
QCOMPARE( waitTask->status(), QgsTask::Complete );

// wait for task to be removed from active task
while ( manager.count() > 0 )
@@ -871,6 +870,8 @@ void TestQgsTaskManager::waitForFinishedBeforeStart()

// restore max thread count (for other tests)
QThreadPool::globalInstance()->setMaxThreadCount( maxThreadCount );

flushEvents();
}

void TestQgsTaskManager::progressChanged()

0 comments on commit e91cac0

Please sign in to comment.
You can’t perform that action at this time.