mirror of
https://github.com/tnodir/fort
synced 2024-11-15 07:57:24 +00:00
UI: WorkerManager:: Simplify complex conditionals
This commit is contained in:
parent
19e1f417e3
commit
65109de488
@ -19,9 +19,7 @@ WorkerManager::~WorkerManager()
|
||||
|
||||
void WorkerManager::setupWorker()
|
||||
{
|
||||
const int workersCount = m_workers.size();
|
||||
|
||||
if (workersCount != 0 && (workersCount >= maxWorkersCount() || m_jobQueue.isEmpty()))
|
||||
if (!checkNewWorkerNeeded())
|
||||
return;
|
||||
|
||||
WorkerObject *worker = createWorker(); // autoDelete = true
|
||||
@ -30,6 +28,16 @@ void WorkerManager::setupWorker()
|
||||
QThreadPool::globalInstance()->start(worker);
|
||||
}
|
||||
|
||||
bool WorkerManager::checkNewWorkerNeeded() const
|
||||
{
|
||||
const int workersCount = m_workers.size();
|
||||
|
||||
if (workersCount == 0)
|
||||
return true;
|
||||
|
||||
return workersCount < maxWorkersCount() && !m_jobQueue.isEmpty();
|
||||
}
|
||||
|
||||
void WorkerManager::workerFinished(WorkerObject *worker)
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
@ -46,6 +54,14 @@ WorkerObject *WorkerManager::createWorker()
|
||||
return new WorkerObject(this);
|
||||
}
|
||||
|
||||
bool WorkerManager::mergeJob(WorkerJobPtr job)
|
||||
{
|
||||
if (!canMergeJobs() || m_jobQueue.isEmpty())
|
||||
return false;
|
||||
|
||||
return m_jobQueue.last()->mergeJob(*job);
|
||||
}
|
||||
|
||||
void WorkerManager::clear()
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
@ -75,7 +91,7 @@ void WorkerManager::enqueueJob(WorkerJobPtr job)
|
||||
|
||||
setupWorker();
|
||||
|
||||
if (canMergeJobs() && !m_jobQueue.isEmpty() && m_jobQueue.last()->mergeJob(*job))
|
||||
if (mergeJob(job))
|
||||
return;
|
||||
|
||||
m_jobQueue.enqueue(job);
|
||||
|
@ -39,9 +39,13 @@ protected:
|
||||
virtual WorkerObject *createWorker();
|
||||
virtual bool canMergeJobs() const { return false; }
|
||||
|
||||
bool mergeJob(WorkerJobPtr job);
|
||||
|
||||
private:
|
||||
void setupWorker();
|
||||
|
||||
bool checkNewWorkerNeeded() const;
|
||||
|
||||
private:
|
||||
volatile bool m_aborted = false;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user