From 65109de4884785754ff0f18a60b4809de1ebc2c9 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Wed, 5 Jul 2023 17:32:11 +0300 Subject: [PATCH] UI: WorkerManager:: Simplify complex conditionals --- src/ui/util/worker/workermanager.cpp | 24 ++++++++++++++++++++---- src/ui/util/worker/workermanager.h | 4 ++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/ui/util/worker/workermanager.cpp b/src/ui/util/worker/workermanager.cpp index 131592a5..95fa4d16 100644 --- a/src/ui/util/worker/workermanager.cpp +++ b/src/ui/util/worker/workermanager.cpp @@ -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); diff --git a/src/ui/util/worker/workermanager.h b/src/ui/util/worker/workermanager.h index 30aa8314..7629ff20 100644 --- a/src/ui/util/worker/workermanager.h +++ b/src/ui/util/worker/workermanager.h @@ -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;