Skip to content

Commit 4dc3dd9

Browse files
committedDec 5, 2016
Allow task priority to be specified
1 parent 9b76374 commit 4dc3dd9

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed
 

‎python/core/qgstaskmanager.sip

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,18 +311,21 @@ class QgsTaskManager : QObject
311311

312312
/** Adds a task to the manager. Ownership of the task is transferred
313313
* to the manager, and the task manager will be responsible for starting
314-
* the task.
314+
* the task. The priority argument can be used to control the run queue's
315+
* order of execution.
315316
* @returns unique task ID
316317
*/
317-
long addTask( QgsTask* task /Transfer/ );
318+
long addTask( QgsTask* task /Transfer/, int priority = 0 );
318319

319320
/**
320321
* Adds a task to the manager, using a full task definition (including dependancy
321322
* handling). Ownership of the task is transferred to the manager, and the task
322-
* manager will be responsible for starting the task.
323+
* manager will be responsible for starting the task. The priority argument can
324+
* be used to control the run queue's order of execution.
323325
* @returns unique task ID
324326
*/
325-
long addTask( const TaskDefinition& task /Transfer/ );
327+
long addTask( const TaskDefinition& task /Transfer/, int priority = 0 );
328+
326329

327330
/** Returns the task with matching ID.
328331
* @param id task ID

‎src/core/qgstaskmanager.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ QgsTask::QgsTask( const QString &name, const Flags& flags )
4040

4141
QgsTask::~QgsTask()
4242
{
43-
Q_ASSERT_X( mStatus == Queued || mStatus == Terminated || mStatus == Complete, "delete", QString( "status was %1" ).arg( mStatus ).toLatin1() );
43+
Q_ASSERT_X( mStatus != Running, "delete", QString( "status was %1" ).arg( mStatus ).toLatin1() );
4444

4545
QThreadPool::globalInstance()->cancel( this );
4646

@@ -333,25 +333,26 @@ QgsTaskManager::~QgsTaskManager()
333333
delete mLayerDependenciesMutex;
334334
}
335335

336-
long QgsTaskManager::addTask( QgsTask* task )
336+
long QgsTaskManager::addTask( QgsTask* task, int priority )
337337
{
338-
return addTaskPrivate( task, QgsTaskList(), false );
338+
return addTaskPrivate( task, QgsTaskList(), false, priority );
339339
}
340340

341-
long QgsTaskManager::addTask( const QgsTaskManager::TaskDefinition& definition )
341+
long QgsTaskManager::addTask( const QgsTaskManager::TaskDefinition& definition, int priority )
342342
{
343343
return addTaskPrivate( definition.task,
344344
definition.dependencies,
345-
false );
345+
false,
346+
priority );
346347
}
347348

348349

349-
long QgsTaskManager::addTaskPrivate( QgsTask* task, QgsTaskList dependencies, bool isSubTask )
350+
long QgsTaskManager::addTaskPrivate( QgsTask* task, QgsTaskList dependencies, bool isSubTask, int priority )
350351
{
351352
long taskId = mNextTaskId++;
352353

353354
mTaskMutex->lockForWrite();
354-
mTasks.insert( taskId, task );
355+
mTasks.insert( taskId, TaskInfo( task, priority ) );
355356
mTaskMutex->unlock();
356357

357358
if ( isSubTask )
@@ -387,7 +388,7 @@ long QgsTaskManager::addTaskPrivate( QgsTask* task, QgsTaskList dependencies, bo
387388
break;
388389
}
389390
//recursively add sub tasks
390-
addTaskPrivate( subTask.task, subTask.dependencies, true );
391+
addTaskPrivate( subTask.task, subTask.dependencies, true, priority );
391392
}
392393

393394
if ( !dependencies.isEmpty() )
@@ -735,7 +736,7 @@ void QgsTaskManager::processQueue()
735736
QgsTask* task = it.value().task;
736737
if ( task && task->mStatus == QgsTask::Queued && dependenciesSatisified( it.key() ) && it.value().added.testAndSetRelaxed( 0, 1 ) )
737738
{
738-
QThreadPool::globalInstance()->start( task );
739+
QThreadPool::globalInstance()->start( task, it.value().priority );
739740
}
740741

741742
if ( task && ( task->mStatus != QgsTask::Complete && task->mStatus != QgsTask::Terminated ) )

‎src/core/qgstaskmanager.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,18 +386,20 @@ class CORE_EXPORT QgsTaskManager : public QObject
386386

387387
/** Adds a task to the manager. Ownership of the task is transferred
388388
* to the manager, and the task manager will be responsible for starting
389-
* the task.
389+
* the task. The priority argument can be used to control the run queue's
390+
* order of execution.
390391
* @returns unique task ID
391392
*/
392-
long addTask( QgsTask* task );
393+
long addTask( QgsTask* task, int priority = 0 );
393394

394395
/**
395396
* Adds a task to the manager, using a full task definition (including dependancy
396397
* handling). Ownership of the task is transferred to the manager, and the task
397-
* manager will be responsible for starting the task.
398+
* manager will be responsible for starting the task. The priority argument can
399+
* be used to control the run queue's order of execution.
398400
* @returns unique task ID
399401
*/
400-
long addTask( const TaskDefinition& task );
402+
long addTask( const TaskDefinition& task, int priority = 0 );
401403

402404
/** Returns the task with matching ID.
403405
* @param id task ID
@@ -498,12 +500,14 @@ class CORE_EXPORT QgsTaskManager : public QObject
498500

499501
struct TaskInfo
500502
{
501-
TaskInfo( QgsTask* task = nullptr )
503+
TaskInfo( QgsTask* task = nullptr, int priority = 0 )
502504
: task( task )
503-
, added( false )
505+
, added( 0 )
506+
, priority( priority )
504507
{}
505508
QgsTask* task;
506509
QAtomicInt added;
510+
int priority;
507511
};
508512

509513
mutable QReadWriteLock* mTaskMutex;
@@ -531,7 +535,8 @@ class CORE_EXPORT QgsTaskManager : public QObject
531535

532536
long addTaskPrivate( QgsTask* task,
533537
QgsTaskList dependencies,
534-
bool isSubTask );
538+
bool isSubTask,
539+
int priority );
535540

536541
bool cleanupAndDeleteTask( QgsTask* task );
537542

0 commit comments

Comments
 (0)
Please sign in to comment.