summaryrefslogtreecommitdiffstats
path: root/src/concurrent/qtconcurrentiteratekernel.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/concurrent/qtconcurrentiteratekernel.h')
-rw-r--r--src/concurrent/qtconcurrentiteratekernel.h195
1 files changed, 100 insertions, 95 deletions
diff --git a/src/concurrent/qtconcurrentiteratekernel.h b/src/concurrent/qtconcurrentiteratekernel.h
index 89fd3d2592..232f4c8bfe 100644
--- a/src/concurrent/qtconcurrentiteratekernel.h
+++ b/src/concurrent/qtconcurrentiteratekernel.h
@@ -1,48 +1,12 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtConcurrent module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QTCONCURRENT_ITERATEKERNEL_H
#define QTCONCURRENT_ITERATEKERNEL_H
#include <QtConcurrent/qtconcurrent_global.h>
-#if !defined(QT_NO_CONCURRENT) || defined(Q_CLANG_QDOC)
+#if !defined(QT_NO_CONCURRENT) || defined(Q_QDOC)
#include <QtCore/qatomic.h>
#include <QtConcurrent/qtconcurrentmedian.h>
@@ -61,37 +25,13 @@ namespace QtConcurrent {
reserve and process at a time. This is done by measuring the time spent
in the user code versus the control part code, and then increasing
the block size if the ratio between them is to small. The block size
- management is done on the basis of the median of several timing measuremens,
- and it is done induvidualy for each thread.
+ management is done on the basis of the median of several timing measurements,
+ and it is done individually for each thread.
*/
class Q_CONCURRENT_EXPORT BlockSizeManager
{
public:
- BlockSizeManager(int iterationCount);
- void timeBeforeUser();
- void timeAfterUser();
- int blockSize();
-private:
- inline bool blockSizeMaxed()
- {
- return (m_blockSize >= maxBlockSize);
- }
-
- const int maxBlockSize;
- qint64 beforeUser;
- qint64 afterUser;
- Median<double> controlPartElapsed;
- Median<double> userPartElapsed;
- int m_blockSize;
-
- Q_DISABLE_COPY(BlockSizeManager)
-};
-
-// ### Qt6: Replace BlockSizeManager with V2 implementation
-class Q_CONCURRENT_EXPORT BlockSizeManagerV2
-{
-public:
- explicit BlockSizeManagerV2(int iterationCount);
+ explicit BlockSizeManager(QThreadPool *pool, int iterationCount);
void timeBeforeUser();
void timeAfterUser();
@@ -106,34 +46,33 @@ private:
const int maxBlockSize;
qint64 beforeUser;
qint64 afterUser;
- MedianDouble controlPartElapsed;
- MedianDouble userPartElapsed;
+ Median controlPartElapsed;
+ Median userPartElapsed;
int m_blockSize;
- Q_DISABLE_COPY(BlockSizeManagerV2)
+ Q_DISABLE_COPY(BlockSizeManager)
};
template <typename T>
class ResultReporter
{
public:
- ResultReporter(ThreadEngine<T> *_threadEngine)
- :threadEngine(_threadEngine)
+ ResultReporter(ThreadEngine<T> *_threadEngine, T &_defaultValue)
+ : threadEngine(_threadEngine), defaultValue(_defaultValue)
{
-
}
void reserveSpace(int resultCount)
{
currentResultCount = resultCount;
- vector.resize(qMax(resultCount, vector.count()));
+ resizeList(qMax(resultCount, vector.size()));
}
void reportResults(int begin)
{
const int useVectorThreshold = 4; // Tunable parameter.
if (currentResultCount > useVectorThreshold) {
- vector.resize(currentResultCount);
+ resizeList(currentResultCount);
threadEngine->reportResults(vector, begin);
} else {
for (int i = 0; i < currentResultCount; ++i)
@@ -146,9 +85,20 @@ public:
return vector.data();
}
- int currentResultCount;
+ int currentResultCount = 0;
ThreadEngine<T> *threadEngine;
- QVector<T> vector;
+ QList<T> vector;
+
+private:
+ void resizeList(qsizetype size)
+ {
+ if constexpr (std::is_default_constructible_v<T>)
+ vector.resize(size);
+ else
+ vector.resize(size, defaultValue);
+ }
+
+ T &defaultValue;
};
template <>
@@ -161,6 +111,22 @@ public:
inline void * getPointer() { return nullptr; }
};
+template<typename T>
+struct DefaultValueContainer
+{
+ template<typename U = T>
+ DefaultValueContainer(U &&_value) : value(std::forward<U>(_value))
+ {
+ }
+
+ T value;
+};
+
+template<>
+struct DefaultValueContainer<void>
+{
+};
+
inline bool selectIteration(std::bidirectional_iterator_tag)
{
return false; // while
@@ -179,22 +145,53 @@ inline bool selectIteration(std::random_access_iterator_tag)
template <typename Iterator, typename T>
class IterateKernel : public ThreadEngine<T>
{
+ using IteratorCategory = typename std::iterator_traits<Iterator>::iterator_category;
+
public:
typedef T ResultType;
- IterateKernel(Iterator _begin, Iterator _end)
- : begin(_begin), end(_end), current(_begin), currentIndex(0),
- forIteration(selectIteration(typename std::iterator_traits<Iterator>::iterator_category())), progressReportingEnabled(true)
+ template<typename U = T, std::enable_if_t<std::is_same_v<U, void>, bool> = true>
+ IterateKernel(QThreadPool *pool, Iterator _begin, Iterator _end)
+ : ThreadEngine<U>(pool),
+ begin(_begin),
+ end(_end),
+ current(_begin),
+ iterationCount(selectIteration(IteratorCategory()) ? static_cast<int>(std::distance(_begin, _end)) : 0),
+ forIteration(selectIteration(IteratorCategory())),
+ progressReportingEnabled(true)
+ {
+ }
+
+ template<typename U = T, std::enable_if_t<!std::is_same_v<U, void>, bool> = true>
+ IterateKernel(QThreadPool *pool, Iterator _begin, Iterator _end)
+ : ThreadEngine<U>(pool),
+ begin(_begin),
+ end(_end),
+ current(_begin),
+ iterationCount(selectIteration(IteratorCategory()) ? static_cast<int>(std::distance(_begin, _end)) : 0),
+ forIteration(selectIteration(IteratorCategory())),
+ progressReportingEnabled(true),
+ defaultValue(U())
+ {
+ }
+
+ template<typename U = T, std::enable_if_t<!std::is_same_v<U, void>, bool> = true>
+ IterateKernel(QThreadPool *pool, Iterator _begin, Iterator _end, U &&_defaultValue)
+ : ThreadEngine<U>(pool),
+ begin(_begin),
+ end(_end),
+ current(_begin),
+ iterationCount(selectIteration(IteratorCategory()) ? static_cast<int>(std::distance(_begin, _end)) : 0),
+ forIteration(selectIteration(IteratorCategory())),
+ progressReportingEnabled(true),
+ defaultValue(std::forward<U>(_defaultValue))
{
- iterationCount = forIteration ? std::distance(_begin, _end) : 0;
}
virtual ~IterateKernel() { }
- virtual bool runIteration(Iterator it, int index , T *result)
- { Q_UNUSED(it); Q_UNUSED(index); Q_UNUSED(result); return false; }
- virtual bool runIterations(Iterator _begin, int beginIndex, int endIndex, T *results)
- { Q_UNUSED(_begin); Q_UNUSED(beginIndex); Q_UNUSED(endIndex); Q_UNUSED(results); return false; }
+ virtual bool runIteration(Iterator, int , T *) { return false; }
+ virtual bool runIterations(Iterator, int, int, T *) { return false; }
void start() override
{
@@ -206,9 +203,9 @@ public:
bool shouldStartThread() override
{
if (forIteration)
- return (currentIndex.load() < iterationCount) && !this->shouldThrottleThread();
+ return (currentIndex.loadRelaxed() < iterationCount) && !this->shouldThrottleThread();
else // whileIteration
- return (iteratorThreads.load() == 0);
+ return (iteratorThreads.loadRelaxed() == 0);
}
ThreadFunctionResult threadFunction() override
@@ -221,8 +218,8 @@ public:
ThreadFunctionResult forThreadFunction()
{
- BlockSizeManagerV2 blockSizeManager(iterationCount);
- ResultReporter<T> resultReporter(this);
+ BlockSizeManager blockSizeManager(ThreadEngineBase::threadPool, iterationCount);
+ ResultReporter<T> resultReporter = createResultsReporter();
for(;;) {
if (this->isCanceled())
@@ -230,7 +227,7 @@ public:
const int currentBlockSize = blockSizeManager.blockSize();
- if (currentIndex.load() >= iterationCount)
+ if (currentIndex.loadRelaxed() >= iterationCount)
break;
// Atomically reserve a block of iterationCount for this thread.
@@ -261,7 +258,7 @@ public:
// Report progress if progress reporting enabled.
if (progressReportingEnabled) {
completed.fetchAndAddAcquire(finalBlockSize);
- this->setProgressValue(this->completed.load());
+ this->setProgressValue(this->completed.loadRelaxed());
}
if (this->shouldThrottleThread())
@@ -275,7 +272,7 @@ public:
if (iteratorThreads.testAndSetAcquire(0, 1) == false)
return ThreadFinished;
- ResultReporter<T> resultReporter(this);
+ ResultReporter<T> resultReporter = createResultsReporter();
resultReporter.reserveSpace(1);
while (current != end) {
@@ -306,18 +303,26 @@ public:
return ThreadFinished;
}
+private:
+ ResultReporter<T> createResultsReporter()
+ {
+ if constexpr (!std::is_same_v<T, void>)
+ return ResultReporter<T>(this, defaultValue.value);
+ else
+ return ResultReporter<T>(this);
+ }
public:
const Iterator begin;
const Iterator end;
Iterator current;
QAtomicInt currentIndex;
- bool forIteration;
QAtomicInt iteratorThreads;
- int iterationCount;
-
- bool progressReportingEnabled;
QAtomicInt completed;
+ const int iterationCount;
+ const bool forIteration;
+ bool progressReportingEnabled;
+ DefaultValueContainer<ResultType> defaultValue;
};
} // namespace QtConcurrent