Skip to content

Commit

Permalink
Force task status update to be main in the main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Cabieces authored and nyalldawson committed Mar 20, 2020
1 parent 121cc00 commit e91cac0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
11 changes: 5 additions & 6 deletions src/core/qgstaskmanager.cpp
Expand Up @@ -172,6 +172,7 @@ bool QgsTask::waitForFinished( int timeout )
if ( mNotFinishedMutex.tryLock( timeout ) )
{
mNotFinishedMutex.unlock();
QCoreApplication::sendPostedEvents( this );
rv = true;
}
else
Expand Down Expand Up @@ -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()
Expand All @@ -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 )
{
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -344,7 +342,8 @@ void QgsTask::processSubTasksForHold()
void QgsTask::terminated()
{
mStatus = Terminated;
processSubTasksForTermination();
QMetaObject::invokeMethod( this, &QgsTask::processSubTasksForTermination, Qt::AutoConnection );
mNotFinishedMutex.unlock();
}


Expand Down
9 changes: 6 additions & 3 deletions src/core/qgstaskmanager.h
Expand Up @@ -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();

};


Expand Down
19 changes: 10 additions & 9 deletions tests/src/core/testqgstaskmanager.cpp
Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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 )
Expand All @@ -871,6 +870,8 @@ void TestQgsTaskManager::waitForFinishedBeforeStart()

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

flushEvents();
}

void TestQgsTaskManager::progressChanged()
Expand Down

0 comments on commit e91cac0

Please sign in to comment.