Simplify QgsTask::waitForFinished, hopefully fix bugs

This commit is contained in:
Nyall Dawson 2018-08-07 17:19:05 +10:00
parent 28fa839a08
commit 9b4be7ae64
3 changed files with 13 additions and 11 deletions

View File

@ -178,7 +178,7 @@ be canceled if any of these layers are about to be removed.
.. seealso:: :py:func:`setDependentLayers`
%End
bool waitForFinished( unsigned long timeout = 30000 );
bool waitForFinished( int timeout = 30000 );
%Docstring
Blocks the current thread until the task finishes or a maximum of ``timeout`` milliseconds.
If ``timeout`` is ``0`` the thread will be blocked forever.

View File

@ -144,7 +144,7 @@ QList<QgsMapLayer *> QgsTask::dependentLayers() const
return _qgis_listQPointerToRaw( mDependentLayers );
}
bool QgsTask::waitForFinished( unsigned long timeout )
bool QgsTask::waitForFinished( int timeout )
{
bool rv = true;
if ( mOverallStatus == Complete || mOverallStatus == Terminated )
@ -154,8 +154,16 @@ bool QgsTask::waitForFinished( unsigned long timeout )
else
{
if ( timeout == 0 )
timeout = ULONG_MAX;
rv = mTaskFinished.wait( &mNotFinishedMutex, timeout );
timeout = std::numeric_limits< int >::max();
if ( mNotFinishedMutex.tryLock( timeout ) )
{
mNotFinishedMutex.unlock();
rv = true;
}
else
{
rv = false;
}
}
return rv;
}
@ -253,9 +261,7 @@ void QgsTask::processSubTasksForCompletion()
setProgress( 100.0 );
emit statusChanged( Complete );
emit taskCompleted();
mTaskFinished.wakeAll();
mNotFinishedMutex.unlock();
mTaskFinished.wakeAll();
}
else if ( mStatus == Complete )
{
@ -282,9 +288,7 @@ void QgsTask::processSubTasksForTermination()
emit statusChanged( Terminated );
emit taskTerminated();
mTaskFinished.wakeAll();
mNotFinishedMutex.unlock();
mTaskFinished.wakeAll();
}
else if ( mStatus == Terminated && !subTasksTerminated )
{

View File

@ -204,7 +204,7 @@ class CORE_EXPORT QgsTask : public QObject
*
* The result will be false if the wait timed out and true in any other case.
*/
bool waitForFinished( unsigned long timeout = 30000 );
bool waitForFinished( int timeout = 30000 );
signals:
@ -312,8 +312,6 @@ class CORE_EXPORT QgsTask : public QObject
bool mShouldTerminate = false;
int mStartCount = 0;
QWaitCondition mTaskFinished;
struct SubTask
{
SubTask( QgsTask *task, const QgsTaskList &dependencies, SubTaskDependency dependency )