summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qthread.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2024-11-06 07:15:42 -0800
committerThiago Macieira <thiago.macieira@intel.com>2024-11-14 16:55:44 -0800
commitf2b5c779cdee1afabca300b04187955c0009fe1c (patch)
treef36215ceec1131eab596a8cab0fc1a6007da8f97 /src/corelib/thread/qthread.cpp
parentd25ff2c7e28cc5a0a150849782958d2633efc110 (diff)
QThread: merge some Unix/Windows/no-thread code for QAdoptedThread
It was duplicated in all three files. The remaining code is very similar, but I don't think we want to merge it any further. For one thing, we must set the thread-local variable before QAdoptedThread calls the QObject constructor. Change-Id: Iac9f7f7528085a1137d7fffdecf080a2b6e1aefe Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/thread/qthread.cpp')
-rw-r--r--src/corelib/thread/qthread.cpp25
1 files changed, 16 insertions, 9 deletions
diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp
index 5527cfcd84d..ae280e3671b 100644
--- a/src/corelib/thread/qthread.cpp
+++ b/src/corelib/thread/qthread.cpp
@@ -13,6 +13,8 @@
QT_BEGIN_NAMESPACE
+using namespace Qt::StringLiterals;
+
/*
QPostEventList
*/
@@ -113,6 +115,20 @@ QAbstractEventDispatcher *QThreadData::createEventDispatcher()
QAdoptedThread::QAdoptedThread(QThreadData *data)
: QThread(*new QThreadPrivate(data))
{
+ // avoid a cyclic reference count: QThreadData owns this QAdoptedThread
+ // object but QObject's constructor increased the count
+ data->deref();
+
+ data->isAdopted = true;
+ Qt::HANDLE id = QThread::currentThreadId();
+ data->threadId.storeRelaxed(id);
+ if (!QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
+ // we are the main thread
+ QCoreApplicationPrivate::theMainThread.storeRelease(this);
+ QCoreApplicationPrivate::theMainThreadId.storeRelaxed(id);
+ setObjectName(u"Qt mainThread"_s);
+ }
+
// thread should be running and not finished for the lifetime
// of the application (even if QCoreApplication goes away)
#if QT_CONFIG(thread)
@@ -1130,15 +1146,6 @@ QThreadData *QThreadData::current(bool createIfNecessary)
if (!data && createIfNecessary) {
data = new QThreadData;
data->thread = new QAdoptedThread(data);
- data->threadId.storeRelaxed(Qt::HANDLE(data->thread.loadAcquire()));
- data->deref();
- data->isAdopted = true;
- if (!QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
- auto *mainThread = data->thread.loadRelaxed();
- mainThread->setObjectName("Qt mainThread");
- QCoreApplicationPrivate::theMainThread.storeRelease(mainThread);
- QCoreApplicationPrivate::theMainThreadId.storeRelaxed(data->threadId.loadRelaxed());
- }
}
return data;
}