Skip to content

Commit

Permalink
Simplify QgsTask::waitForFinished, hopefully fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 7, 2018
1 parent 28fa839 commit 9b4be7a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion python/core/auto_generated/qgstaskmanager.sip.in
Expand Up @@ -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.
Expand Down
18 changes: 11 additions & 7 deletions src/core/qgstaskmanager.cpp
Expand Up @@ -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 )
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 )
{
Expand All @@ -282,9 +288,7 @@ void QgsTask::processSubTasksForTermination()

emit statusChanged( Terminated );
emit taskTerminated();
mTaskFinished.wakeAll();
mNotFinishedMutex.unlock();
mTaskFinished.wakeAll();
}
else if ( mStatus == Terminated && !subTasksTerminated )
{
Expand Down
4 changes: 1 addition & 3 deletions src/core/qgstaskmanager.h
Expand Up @@ -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:

Expand Down Expand Up @@ -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 )
Expand Down

0 comments on commit 9b4be7a

Please sign in to comment.