From 552c4a9655ea22c354454132a9bda7a1c7c836f7 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Thu, 13 Jan 2022 11:28:27 +0100 Subject: [PATCH] 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 --- src/corelib/thread/qthreadpool.cpp | 15 +++++++++------ 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 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 expiredThreads; QList queue; QWaitCondition noActiveThreads; + QString objectName; int expiryTimeout = 30000; int requestedMaxThreadCount = QThread::idealThreadCount(); // don't use this directly