Skip to content

Commit

Permalink
Make the task manager cancel operation thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 authored and nyalldawson committed Mar 20, 2020
1 parent 722a41e commit 046b1e8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/core/qgstaskmanager.cpp
Expand Up @@ -91,7 +91,9 @@ void QgsTask::cancel()
if ( mOverallStatus == Complete || mOverallStatus == Terminated )
return;

mShouldTerminateMutex.lock();
mShouldTerminate = true;
mShouldTerminateMutex.unlock();
if ( mStatus == Queued || mStatus == OnHold )
{
// immediately terminate unstarted jobs
Expand All @@ -111,6 +113,12 @@ void QgsTask::cancel()
}
}

bool QgsTask::isCanceled() const
{
QMutexLocker locker( &mShouldTerminateMutex );
return mShouldTerminate;
}

void QgsTask::hold()
{
if ( mStatus == Queued )
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgstaskmanager.h
Expand Up @@ -283,7 +283,7 @@ class CORE_EXPORT QgsTask : public QObject
* flag, then derived classes' run() methods should periodically check this and
* terminate in a safe manner.
*/
bool isCanceled() const { return mShouldTerminate; }
bool isCanceled() const;

protected slots:

Expand Down Expand Up @@ -323,6 +323,7 @@ class CORE_EXPORT QgsTask : public QObject
//! Overall progress of this task and all subtasks
double mTotalProgress = 0.0;
bool mShouldTerminate = false;
mutable QMutex mShouldTerminateMutex;
int mStartCount = 0;

struct SubTask
Expand Down
3 changes: 0 additions & 3 deletions tests/src/core/testqgstaskmanager.cpp
Expand Up @@ -133,9 +133,6 @@ class CancelableTask : public QgsTask
~CancelableTask() override
{
qDebug() << "deleting task " << description();

int i = 1;
i++;
}

protected:
Expand Down

0 comments on commit 046b1e8

Please sign in to comment.