diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2017-02-11 09:37:32 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2017-02-13 08:42:41 +0000 |
commit | a85b4d79db5c11f5a309fbbc52e80a81096c0f01 (patch) | |
tree | 62946a9a01467a3d971f5d7dba008ef6809b54df /src/corelib/thread | |
parent | 7efd024b929c7815f48eb10fb9bc70c77b1a72ad (diff) |
QThreadPool: replace a QSet with a QList
QThreadPool maintains three containers of QThreadPoolThread*:
- allThreads, a QSet
- waitingThreads, a QQueue
- expiredThreads, also a QQueue
None of the operations on allThreads make use of QSets fast lookup.
The only functions called on it are isEmpty(), count(), insert(),
and swap().
Since therefore QSet adds nothing but overhead, causes indeterminism
(e.g. when deleting threads in Private::reset()) and code bloat, use
the same container for allThreads that underlies QQueue: QList.
Port insert() to append(). Add an assert to verify that we're not
running into an ABA problem here (but this should never fire, since
we're never deleting threads except in Private::reset(), where we
do remove them from allThreads), just in case.
Saves ~0.5KiB in text size on optimized Linux AMD64 GCC 7.0 builds.
Change-Id: I53a4d5ef2c204420f7c8852f1e72ab3d6ea43d08
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/thread')
-rw-r--r-- | src/corelib/thread/qthreadpool.cpp | 5 | ||||
-rw-r--r-- | src/corelib/thread/qthreadpool_p.h | 2 |
2 files changed, 4 insertions, 3 deletions
diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp index 7ce757064f..2ecf8f729a 100644 --- a/src/corelib/thread/qthreadpool.cpp +++ b/src/corelib/thread/qthreadpool.cpp @@ -245,7 +245,8 @@ void QThreadPoolPrivate::startThread(QRunnable *runnable) { QScopedPointer <QThreadPoolThread> thread(new QThreadPoolThread(this)); thread->setObjectName(QLatin1String("Thread (pooled)")); - allThreads.insert(thread.data()); + Q_ASSERT(!allThreads.contains(thread.data())); // if this assert hits, we have an ABA problem (deleted threads don't get removed here) + allThreads.append(thread.data()); ++activeThreads; if (runnable->autoDelete()) @@ -265,7 +266,7 @@ void QThreadPoolPrivate::reset() while (!allThreads.empty()) { // move the contents of the set out so that we can iterate without the lock - QSet<QThreadPoolThread *> allThreadsCopy; + QList<QThreadPoolThread *> allThreadsCopy; allThreadsCopy.swap(allThreads); locker.unlock(); diff --git a/src/corelib/thread/qthreadpool_p.h b/src/corelib/thread/qthreadpool_p.h index 5694bc8b10..ea8127efef 100644 --- a/src/corelib/thread/qthreadpool_p.h +++ b/src/corelib/thread/qthreadpool_p.h @@ -86,7 +86,7 @@ public: void stealAndRunRunnable(QRunnable *runnable); mutable QMutex mutex; - QSet<QThreadPoolThread *> allThreads; + QList<QThreadPoolThread *> allThreads; QQueue<QThreadPoolThread *> waitingThreads; QQueue<QThreadPoolThread *> expiredThreads; QVector<QPair<QRunnable *, int> > queue; |