Skip to content

Commit

Permalink
Allow task priority to be specified
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 5, 2016
1 parent 9b76374 commit 4dc3dd9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
11 changes: 7 additions & 4 deletions python/core/qgstaskmanager.sip
Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions src/core/qgstaskmanager.cpp
Expand Up @@ -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 );

Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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() )
Expand Down Expand Up @@ -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 ) )
Expand Down
19 changes: 12 additions & 7 deletions src/core/qgstaskmanager.h
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 );

Expand Down

0 comments on commit 4dc3dd9

Please sign in to comment.