28#include "moc_qgstaskmanager.cpp"
30using namespace Qt::StringLiterals;
38 , mDescription( name )
39 , mNotStartedMutex( 1 )
41 mNotStartedMutex.acquire();
46 Q_ASSERT_X( mStatus !=
Running,
"delete", u
"status was %1"_s.arg( mStatus ).toLatin1() );
48 mNotFinishedMutex.lock();
49 const auto constMSubTasks = mSubTasks;
50 for (
const SubTask &subTask : constMSubTasks )
54 mNotFinishedMutex.unlock();
55 mNotStartedMutex.release();
65 return mElapsedTime.elapsed();
70 QMutexLocker locker( &mNotFinishedMutex );
71 mNotStartedMutex.release();
73 Q_ASSERT( mStartCount == 1 );
103 mShouldTerminateMutex.lock();
104 mShouldTerminate =
true;
105 mShouldTerminateMutex.unlock();
110 mNotStartedMutex.release();
115 processSubTasksForTermination();
118 const auto constMSubTasks = mSubTasks;
119 for (
const SubTask &subTask : constMSubTasks )
121 subTask.task->cancel();
127 QMutexLocker locker( &mShouldTerminateMutex );
128 return mShouldTerminate;
136 processSubTasksForHold();
139 const auto constMSubTasks = mSubTasks;
140 for (
const SubTask &subTask : constMSubTasks )
142 subTask.task->hold();
155 const auto constMSubTasks = mSubTasks;
156 for (
const SubTask &subTask : constMSubTasks )
158 subTask.task->unhold();
164 mSubTasks << SubTask( subTask, dependencies, subTaskDependency );
171 return _qgis_listQPointerToRaw( mDependentLayers );
177 mNotStartedMutex.acquire();
178 mNotStartedMutex.release();
188 timeout = std::numeric_limits< int >::max();
189 if ( mNotFinishedMutex.tryLock( timeout ) )
191 mNotFinishedMutex.unlock();
192 QCoreApplication::sendPostedEvents(
this );
208void QgsTask::subTaskStatusChanged(
int status )
210 QgsTask *subTask = qobject_cast< QgsTask * >( sender() );
221 processSubTasksForCompletion();
226 processSubTasksForTermination();
230 processSubTasksForHold();
243 if ( !mSubTasks.isEmpty() )
247 double totalProgress = 0.0;
248 const auto constMSubTasks = mSubTasks;
249 for (
const SubTask &subTask : constMSubTasks )
253 totalProgress += 100.0;
257 totalProgress += subTask.task->
progress();
264 double prevProgress = mTotalProgress;
268 if (
static_cast< int >( prevProgress * 10 ) !=
static_cast< int >( mTotalProgress * 10 ) )
272void QgsTask::completed()
275 QMetaObject::invokeMethod(
this, &QgsTask::processSubTasksForCompletion );
278void QgsTask::processSubTasksForCompletion()
280 bool subTasksCompleted =
true;
281 const auto constMSubTasks = mSubTasks;
282 for (
const SubTask &subTask : constMSubTasks )
286 subTasksCompleted =
false;
291 if ( mStatus ==
Complete && subTasksCompleted )
306void QgsTask::processSubTasksForTermination()
308 bool subTasksTerminated =
true;
309 const auto constMSubTasks = mSubTasks;
310 for (
const SubTask &subTask : constMSubTasks )
314 subTasksTerminated =
false;
326 else if ( mStatus ==
Terminated && !subTasksTerminated )
333void QgsTask::processSubTasksForHold()
335 bool subTasksRunning =
false;
336 const auto constMSubTasks = mSubTasks;
337 for (
const SubTask &subTask : constMSubTasks )
341 subTasksRunning =
true;
346 if ( mStatus ==
OnHold && !subTasksRunning && mOverallStatus !=
OnHold )
351 else if ( mStatus ==
OnHold && subTasksRunning )
358void QgsTask::terminated()
361 QMetaObject::invokeMethod(
this, &QgsTask::processSubTasksForTermination );
367class QgsTaskRunnableWrapper :
public QRunnable
370 explicit QgsTaskRunnableWrapper( QgsTask *task )
373 setAutoDelete(
true );
383 QgsTask *mTask =
nullptr;
395 , mThreadPool( new QThreadPool( this ) )
396 , mTaskMutex( new QRecursiveMutex() )
406 QMap< long, TaskInfo >
tasks = mTasks;
408 mTaskMutex->unlock();
409 QMap< long, TaskInfo >::const_iterator it =
tasks.constBegin();
410 for ( ; it !=
tasks.constEnd(); ++it )
412 cleanupAndDeleteTask( it.value().task );
416 mThreadPool->waitForDone();
435long QgsTaskManager::addTaskPrivate(
QgsTask *task,
QgsTaskList dependencies,
bool isSubTask,
int priority )
443 if (
task->thread() != this->thread() )
446 if ( !
task->moveToThread( this->thread() ) )
461 &QgsTaskManager::layersWillBeRemoved
465 long taskId = mNextTaskId++;
468 mTasks.insert(
taskId, TaskInfo(
task, priority ) );
476 mParentTasks <<
task;
478 if ( !
task->dependentLayers().isEmpty() )
479 mLayerDependencies.insert(
taskId, _qgis_listRawToQPointer(
task->dependentLayers() ) );
480 mTaskMutex->unlock();
489 for (
const QgsTask::SubTask &subTask : std::as_const(
task->mSubTasks ) )
491 switch ( subTask.dependency )
502 addTaskPrivate( subTask.task, subTask.dependencies,
true, priority );
510 if ( hasCircularDependencies(
taskId ) )
528 QMutexLocker ml( mTaskMutex );
530 if ( mTasks.contains(
id ) )
531 t = mTasks.value(
id ).task;
537 QMutexLocker ml( mTaskMutex );
538 return QList<QgsTask *>( mParentTasks.begin(), mParentTasks.end() );
543 QMutexLocker ml( mTaskMutex );
544 return mParentTasks.count();
552 QMutexLocker ml( mTaskMutex );
553 const auto iter = mMapTaskPtrToId.constFind(
task );
554 if ( iter != mMapTaskPtrToId.constEnd() )
562 QSet< QgsTask * > parents = mParentTasks;
564 mTaskMutex->unlock();
566 const auto constParents = parents;
576 QMap< long, QgsTaskList >
dependencies = mTaskDependencies;
578 mTaskMutex->unlock();
596 if ( resolveDependencies(
taskId, results ) )
602bool QgsTaskManager::resolveDependencies(
long thisTaskId, QSet<long> &results )
const
605 QMap< long, QgsTaskList >
dependencies = mTaskDependencies;
607 mTaskMutex->unlock();
609 QSet<long> alreadyExploredTaskIds;
610 QStack<long> stackTaskIds;
611 stackTaskIds.push( thisTaskId );
612 while ( !stackTaskIds.isEmpty() )
614 const long currentTaskId = stackTaskIds.pop();
615 alreadyExploredTaskIds.insert( currentTaskId );
621 const auto &constValue = *iter;
625 if ( dependentTaskId >= 0 )
627 if ( thisTaskId == dependentTaskId )
634 results.insert( dependentTaskId );
637 if ( !alreadyExploredTaskIds.contains( dependentTaskId ) )
639 stackTaskIds.push( dependentTaskId );
648bool QgsTaskManager::hasCircularDependencies(
long taskId )
const
651 return !resolveDependencies(
taskId, d );
656 QMutexLocker ml( mTaskMutex );
662 QMutexLocker ml( mTaskMutex );
663 QList< QgsTask * >
tasks;
664 QMap< long, QgsWeakMapLayerPointerList >::const_iterator layerIt = mLayerDependencies.constBegin();
665 for ( ; layerIt != mLayerDependencies.constEnd(); ++layerIt )
667 if ( _qgis_listQPointerToRaw( layerIt.value() ).contains( layer ) )
679 QMutexLocker ml( mTaskMutex );
687 QMutexLocker ml( mTaskMutex );
688 QSet< QgsTask * >
tasks = mActiveTasks;
690 if ( !includeHidden )
692 QSet< QgsTask * > filteredTasks;
693 filteredTasks.reserve(
tasks.size() );
697 filteredTasks.insert(
task );
699 tasks = filteredTasks;
702 return tasks.intersect( mParentTasks ).count();
711void QgsTaskManager::taskProgressChanged(
double progress )
713 QgsTask *
task = qobject_cast< QgsTask * >( sender() );
730void QgsTaskManager::taskStatusChanged(
int status )
732 QgsTask *
task = qobject_cast< QgsTask * >( sender() );
741 QgsTaskRunnableWrapper *runnable = mTasks.value(
id ).runnable;
742 mTaskMutex->unlock();
743 if ( runnable && mThreadPool->tryTake( runnable ) )
746 mTasks[id].runnable =
nullptr;
752 task->finished( result );
758 cancelDependentTasks(
id );
762 bool isParent = mParentTasks.contains(
task );
763 mTaskMutex->unlock();
764 if ( isParent && !isHidden )
774 cleanupAndDeleteTask(
task );
778void QgsTaskManager::layersWillBeRemoved(
const QList< QgsMapLayer * > &layers )
782 QMap< long, QgsWeakMapLayerPointerList > layerDependencies = mLayerDependencies;
783 layerDependencies.detach();
784 mTaskMutex->unlock();
786 const auto constLayers = layers;
787 for ( QgsMapLayer *layer : constLayers )
790 for ( QMap< long, QgsWeakMapLayerPointerList >::const_iterator it = layerDependencies.constBegin(); it != layerDependencies.constEnd(); ++it )
792 if ( !( _qgis_listQPointerToRaw( it.value() ).contains( layer ) ) )
798 QgsTask *dependentTask =
task( it.key() );
809bool QgsTaskManager::cleanupAndDeleteTask(
QgsTask *task )
818 QgsTaskRunnableWrapper *runnable = mTasks.value(
id ).runnable;
820 task->disconnect(
this );
823 if ( mTaskDependencies.contains(
id ) )
824 mTaskDependencies.remove(
id );
825 mTaskMutex->unlock();
830 bool isParent = mParentTasks.contains(
task );
831 mParentTasks.remove(
task );
832 mSubTasks.remove(
task );
834 mMapTaskPtrToId.remove(
task );
835 mLayerDependencies.remove(
id );
849 if ( runnable && mThreadPool->tryTake( runnable ) )
852 mTasks[id].runnable =
nullptr;
863 for ( QMap< long, QgsTaskList >::iterator it = mTaskDependencies.begin(); it != mTaskDependencies.end(); ++it )
865 if ( it.value().contains(
task ) )
867 it.value().removeAll(
task );
870 mTaskMutex->unlock();
875void QgsTaskManager::processQueue()
879 mActiveTasks.clear();
880 for ( QMap< long, TaskInfo >::iterator it = mTasks.begin(); it != mTasks.end(); ++it )
882 QgsTask *
task = it.value().task;
885 it.value().createRunnable();
886 mThreadPool->start( it.value().runnable, it.value().priority );
891 mActiveTasks <<
task;
895 bool allFinished = mActiveTasks.isEmpty();
896 mTaskMutex->unlock();
904 if ( prevActiveCount != newActiveCount )
910void QgsTaskManager::cancelDependentTasks(
long taskId )
916 QMap< long, QgsTaskList > taskDependencies = mTaskDependencies;
917 taskDependencies.detach();
918 mTaskMutex->unlock();
920 QMap< long, QgsTaskList >::const_iterator it = taskDependencies.constBegin();
921 for ( ; it != taskDependencies.constEnd(); ++it )
923 if ( it.value().contains( canceledTask ) )
929 QgsTask *dependentTask =
task( it.key() );
936QgsTaskManager::TaskInfo::TaskInfo(
QgsTask *task,
int priority )
939 , priority( priority )
942void QgsTaskManager::TaskInfo::createRunnable()
944 Q_ASSERT( !runnable );
945 runnable =
new QgsTaskRunnableWrapper( task );
967 if ( mShouldTerminate )
970 mProgress = 100.0 * ( double( i ) + subTaskProgress / 100.0 ) /
double(
mSubTasksSerial.size() );
973 if ( !subTask->
run() )
975 subTask->completed();
976 mProgress = 100.0 * double( i + 1 ) / double(
mSubTasksSerial.size() );
Base class for all map layer types.
static QgsProject * instance()
Returns the QgsProject singleton instance.
void layersWillBeRemoved(const QStringList &layerIds)
Emitted when one or more layers are about to be removed from the registry.
QList< QgsTask * > tasks() const
Returns all tasks tracked by the manager.
void finalTaskProgressChanged(double progress)
Will be emitted when only a single task remains to complete and that task has reported a progress cha...
void statusChanged(long taskId, int status)
Will be emitted when a task reports a status change.
void taskAdded(long taskId)
Emitted when a new task has been added to the manager.
QList< QgsTask * > activeTasks() const
Returns a list of the active (queued or running) tasks.
QgsTaskManager(QObject *parent=nullptr)
Constructor for QgsTaskManager.
void taskAboutToBeDeleted(long taskId)
Emitted when a task is about to be deleted.
long taskId(QgsTask *task) const
Returns the unique task ID corresponding to a task managed by the class.
int count() const
Returns the number of tasks tracked by the manager.
QList< QgsTask * > tasksDependentOnLayer(QgsMapLayer *layer) const
Returns a list of tasks which depend on a layer.
void allTasksFinished()
Emitted when all tasks are complete.
~QgsTaskManager() override
bool dependenciesSatisfied(long taskId) const
Returns true if all dependencies for the specified task are satisfied.
QThreadPool * threadPool()
Returns the threadpool utilized by the task manager.
void cancelAll()
Instructs all tasks tracked by the manager to terminate.
QSet< long > dependencies(long taskId) const
Returns the set of task IDs on which a task is dependent.
QgsTask * task(long id) const
Returns the task with matching ID.
void progressChanged(long taskId, double progress)
Will be emitted when a task reports a progress change.
int countActiveTasks(bool includeHidden=true) const
Returns the number of active (queued or running) tasks.
void triggerTask(QgsTask *task)
Triggers a task, e.g.
void countActiveTasksChanged(int count)
Emitted when the number of active tasks changes.
QList< QgsMapLayer * > dependentLayers(long taskId) const
Returns a list of layers on which as task is dependent.
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
void taskTriggered(QgsTask *task)
Emitted when a task is triggered.
QList< QgsTask * > mSubTasksSerial
void addSubTask(QgsTask *subTask)
Add a subtask and transfer its ownership.
void cancel() override
Notifies the task that it should terminate.
~QgsTaskWithSerialSubTasks() override
bool run() override
Performs the task's operation.
Abstract base class for long running background tasks.
TaskStatus status() const
Returns the current task status.
Flags flags() const
Returns the flags associated with the task.
void taskCompleted()
Will be emitted by task to indicate its successful completion.
double progress() const
Returns the task's progress (between 0.0 and 100.0).
virtual bool run()=0
Performs the task's operation.
void progressChanged(double progress)
Will be emitted by task when its progress changes.
QList< QgsMapLayer * > dependentLayers() const
Returns the list of layers on which the task depends.
void begun()
Will be emitted by task to indicate its commencement.
virtual void cancel()
Notifies the task that it should terminate.
QgsTask(const QString &description=QString(), QgsTask::Flags flags=AllFlags)
Constructor for QgsTask.
@ Hidden
Hide task from GUI.
void taskTerminated()
Will be emitted by task if it has terminated for any reason other then completion (e....
void statusChanged(int status)
Will be emitted by task when its status changes.
qint64 elapsedTime() const
Returns the elapsed time since the task commenced, in milliseconds.
void unhold()
Releases the task from being held.
void setDependentLayers(const QList< QgsMapLayer * > &dependentLayers)
Sets a list of layers on which the task depends.
@ Terminated
Task was terminated or errored.
@ Queued
Task is queued and has not begun.
@ OnHold
Task is queued but on hold and will not be started.
@ Running
Task is currently running.
@ Complete
Task successfully completed.
void hold()
Places the task on hold.
QString description() const
Returns the task's description.
void addSubTask(QgsTask *subTask, const QgsTaskList &dependencies=QgsTaskList(), SubTaskDependency subTaskDependency=SubTaskIndependent)
Adds a subtask to this task.
void setDescription(const QString &description)
Sets the task's description.
SubTaskDependency
Controls how subtasks relate to their parent task.
@ SubTaskIndependent
Subtask is independent of the parent, and can run before, after or at the same time as the parent.
@ ParentDependsOnSubTask
Subtask must complete before parent can begin.
bool isCanceled() const
Will return true if task should terminate ASAP.
void setProgress(double progress)
Sets the task's current progress.
bool waitForFinished(int timeout=30000)
Blocks the current thread until the task finishes or a maximum of timeout milliseconds.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.
QList< QgsTask * > QgsTaskList
List of QgsTask objects.
Definition of a task for inclusion in the manager.
QgsTaskList dependentTasks
List of dependent tasks which must be completed before task can run.