summaryrefslogtreecommitdiffstats
path: root/src/core/jobs
diff options
context:
space:
mode:
authorPaul Lemire <paul.lemire@kdab.com>2020-05-04 08:43:22 +0200
committerPaul Lemire <paul.lemire@kdab.com>2020-05-07 12:18:12 +0200
commit1097607f112e79ddb4fc08d012db95faa193c4ea (patch)
tree6e663574e6d2f466b4384785f0714ab7c2ef9450 /src/core/jobs
parent764924da8ab3255c1b8d1ad5b39f85675f4b855e (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>
Diffstat (limited to 'src/core/jobs')
-rw-r--r--src/core/jobs/qthreadpooler.cpp26
-rw-r--r--src/core/jobs/qthreadpooler_p.h2
2 files changed, 17 insertions, 11 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
diff --git a/src/core/jobs/qthreadpooler_p.h b/src/core/jobs/qthreadpooler_p.h
index 43a67033e..ea69b5b78 100644
--- a/src/core/jobs/qthreadpooler_p.h
+++ b/src/core/jobs/qthreadpooler_p.h
@@ -77,7 +77,7 @@ public:
void taskFinished(RunnableInterface *task);
QFuture<void> future();
- int maxThreadCount() const;
+ static int maxThreadCount();
private:
void enqueueTasks(const QVector<RunnableInterface *> &tasks);