Skip to content
Permalink
Browse files
Allow task priority to be specified
  • Loading branch information
nyalldawson committed Dec 5, 2016
1 parent 9b76374 commit 4dc3dd90776286ef53795c466954d01b51f122b3
Showing with 29 additions and 20 deletions.
  1. +7 −4 python/core/qgstaskmanager.sip
  2. +10 −9 src/core/qgstaskmanager.cpp
  3. +12 −7 src/core/qgstaskmanager.h
@@ -311,18 +311,21 @@ class QgsTaskManager : QObject

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

/**
* Adds a task to the manager, using a full task definition (including dependancy
* handling). Ownership of the task is transferred to the manager, and the task
* manager will be responsible for starting the task.
* manager will be responsible for starting the task. The priority argument can
* be used to control the run queue's order of execution.
* @returns unique task ID
*/
long addTask( const TaskDefinition& task /Transfer/ );
long addTask( const TaskDefinition& task /Transfer/, int priority = 0 );


/** Returns the task with matching ID.
* @param id task ID
@@ -40,7 +40,7 @@ QgsTask::QgsTask( const QString &name, const Flags& flags )

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

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

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

long QgsTaskManager::addTask( QgsTask* task )
long QgsTaskManager::addTask( QgsTask* task, int priority )
{
return addTaskPrivate( task, QgsTaskList(), false );
return addTaskPrivate( task, QgsTaskList(), false, priority );
}

long QgsTaskManager::addTask( const QgsTaskManager::TaskDefinition& definition )
long QgsTaskManager::addTask( const QgsTaskManager::TaskDefinition& definition, int priority )
{
return addTaskPrivate( definition.task,
definition.dependencies,
false );
false,
priority );
}


long QgsTaskManager::addTaskPrivate( QgsTask* task, QgsTaskList dependencies, bool isSubTask )
long QgsTaskManager::addTaskPrivate( QgsTask* task, QgsTaskList dependencies, bool isSubTask, int priority )
{
long taskId = mNextTaskId++;

mTaskMutex->lockForWrite();
mTasks.insert( taskId, task );
mTasks.insert( taskId, TaskInfo( task, priority ) );
mTaskMutex->unlock();

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

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

if ( task && ( task->mStatus != QgsTask::Complete && task->mStatus != QgsTask::Terminated ) )
@@ -386,18 +386,20 @@ class CORE_EXPORT QgsTaskManager : public QObject

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

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

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

struct TaskInfo
{
TaskInfo( QgsTask* task = nullptr )
TaskInfo( QgsTask* task = nullptr, int priority = 0 )
: task( task )
, added( false )
, added( 0 )
, priority( priority )
{}
QgsTask* task;
QAtomicInt added;
int priority;
};

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

long addTaskPrivate( QgsTask* task,
QgsTaskList dependencies,
bool isSubTask );
bool isSubTask,
int priority );

bool cleanupAndDeleteTask( QgsTask* task );

0 comments on commit 4dc3dd9

Please sign in to comment.