diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2020-05-04 08:43:22 +0200 |
---|---|---|
committer | Paul Lemire <paul.lemire@kdab.com> | 2020-05-27 15:31:36 +0200 |
commit | d52cba5b86cdfc5077a0ee1cc7cf868cb4d6cd87 (patch) | |
tree | c88ddc0e22dc16f40eaa290af686288a31cee268 /src/core/jobs/qthreadpooler.cpp | |
parent | bfefac652ba826ab95a4969ef31acc02b6ef067f (diff) |
Centralize the maxThreadCount information
Add a static function on QThreadPooler which returns the ideal thread count
(capped by QT3D_MAX_THREAD_COUNT) if set.
Call that function from all places that need that information. Avoids code
duplication and potentially reading the env value every frame.
Change-Id: I699691af33ed0a65aa557ed48aabb9de8929dcc2
Reviewed-by: Mike Krus <mike.krus@kdab.com>
(cherry picked from commit 1097607f112e79ddb4fc08d012db95faa193c4ea)
Diffstat (limited to 'src/core/jobs/qthreadpooler.cpp')
-rw-r--r-- | src/core/jobs/qthreadpooler.cpp | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/src/core/jobs/qthreadpooler.cpp b/src/core/jobs/qthreadpooler.cpp index f1c2883dc..59c905f71 100644 --- a/src/core/jobs/qthreadpooler.cpp +++ b/src/core/jobs/qthreadpooler.cpp @@ -52,14 +52,7 @@ QThreadPooler::QThreadPooler(QObject *parent) , m_threadPool(QThreadPool::globalInstance()) , m_totalRunJobs(0) { - const QByteArray maxThreadCount = qgetenv("QT3D_MAX_THREAD_COUNT"); - if (!maxThreadCount.isEmpty()) { - bool conversionOK = false; - const int maxThreadCountValue = maxThreadCount.toInt(&conversionOK); - if (conversionOK) - m_threadPool->setMaxThreadCount(maxThreadCountValue); - } - + m_threadPool->setMaxThreadCount(QThreadPooler::maxThreadCount()); // Ensures that threads will never be recycled m_threadPool->setExpiryTimeout(-1); } @@ -206,9 +199,22 @@ int QThreadPooler::currentCount() const return m_taskCount.loadRelaxed(); } -int QThreadPooler::maxThreadCount() const +int QThreadPooler::maxThreadCount() { - return m_threadPool->maxThreadCount(); + static int threadCount = 0; + + if (threadCount == 0) { + threadCount = QThread::idealThreadCount(); + const QByteArray maxThreadCount = qgetenv("QT3D_MAX_THREAD_COUNT"); + if (!maxThreadCount.isEmpty()) { + bool conversionOK = false; + const int maxThreadCountValue = maxThreadCount.toInt(&conversionOK); + if (conversionOK) + threadCount = std::min(threadCount, maxThreadCountValue); + } + } + + return threadCount; } } // namespace Qt3DCore |