UI: WorkerManager:: Simplify complex conditionals

This commit is contained in:
Nodir Temirkhodjaev 2023-07-05 17:32:11 +03:00
parent 19e1f417e3
commit 65109de488
2 changed files with 24 additions and 4 deletions

View File

@ -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);

View File

@ -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;