diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-01-13 11:28:27 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-01-13 19:47:23 +0100 |
commit | 552c4a9655ea22c354454132a9bda7a1c7c836f7 (patch) | |
tree | 192269669f3e7f34a6e5423cc1437603b5f44ab7 /src/corelib/thread | |
parent | 3f32dcd1ddcbe04c77ccd83e2eaa566d7212e732 (diff) |
Read QThreadPool::objectName thread-safe
QThreadPool allows method calls from any thread, but QObject does not
so copy objectName so we may use it locally under our own lock.
Pick-to: 6.3 6.2
Task-number: QTBUG-99775
Change-Id: Ib28910649f5d0f9ce698c7da495069635d608d03
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib/thread')
-rw-r--r-- | src/corelib/thread/qthreadpool.cpp | 15 | ||||
-rw-r--r-- | src/corelib/thread/qthreadpool_p.h | 1 |
2 files changed, 10 insertions, 6 deletions
diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp index 713c436371..94b0aff451 100644 --- a/src/corelib/thread/qthreadpool.cpp +++ b/src/corelib/thread/qthreadpool.cpp @@ -273,13 +273,9 @@ bool QThreadPoolPrivate::tooManyThreadsActive() const */ void QThreadPoolPrivate::startThread(QRunnable *runnable) { - Q_Q(QThreadPool); Q_ASSERT(runnable != nullptr); QScopedPointer<QThreadPoolThread> thread(new QThreadPoolThread(this)); - QString objectName; - if (QString myName = q->objectName(); !myName.isEmpty()) - objectName = myName; - else + if (objectName.isEmpty()) objectName = QLatin1String("Thread (pooled)"); thread->setObjectName(objectName); Q_ASSERT(!allThreads.contains(thread.data())); // if this assert hits, we have an ABA problem (deleted threads don't get removed here) @@ -476,7 +472,14 @@ void QThreadPoolPrivate::stealAndRunRunnable(QRunnable *runnable) */ QThreadPool::QThreadPool(QObject *parent) : QObject(*new QThreadPoolPrivate, parent) -{ } +{ + Q_D(QThreadPool); + connect(this, &QObject::objectNameChanged, this, [d](const QString &newName) { + // We keep a copy of the name under our own lock, so we can access it thread-safely. + QMutexLocker locker(&d->mutex); + d->objectName = newName; + }); +} /*! Destroys the QThreadPool. diff --git a/src/corelib/thread/qthreadpool_p.h b/src/corelib/thread/qthreadpool_p.h index 1b6a65f69d..e82e4f16e7 100644 --- a/src/corelib/thread/qthreadpool_p.h +++ b/src/corelib/thread/qthreadpool_p.h @@ -176,6 +176,7 @@ public: QQueue<QThreadPoolThread *> expiredThreads; QList<QueuePage *> queue; QWaitCondition noActiveThreads; + QString objectName; int expiryTimeout = 30000; int requestedMaxThreadCount = QThread::idealThreadCount(); // don't use this directly |