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>
This commit is contained in:
Thiago Macieira 2024-11-06 07:15:42 -08:00
parent d25ff2c7e2
commit f2b5c779cd
4 changed files with 17 additions and 29 deletions

View File

@ -13,6 +13,8 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
/* /*
QPostEventList QPostEventList
*/ */
@ -113,6 +115,20 @@ QAbstractEventDispatcher *QThreadData::createEventDispatcher()
QAdoptedThread::QAdoptedThread(QThreadData *data) QAdoptedThread::QAdoptedThread(QThreadData *data)
: QThread(*new QThreadPrivate(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 // thread should be running and not finished for the lifetime
// of the application (even if QCoreApplication goes away) // of the application (even if QCoreApplication goes away)
#if QT_CONFIG(thread) #if QT_CONFIG(thread)
@ -1130,15 +1146,6 @@ QThreadData *QThreadData::current(bool createIfNecessary)
if (!data && createIfNecessary) { if (!data && createIfNecessary) {
data = new QThreadData; data = new QThreadData;
data->thread = new QAdoptedThread(data); 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; return data;
} }

View File

@ -363,7 +363,7 @@ class QAdoptedThread : public QThread
Q_DECLARE_PRIVATE(QThread) Q_DECLARE_PRIVATE(QThread)
public: public:
QAdoptedThread(QThreadData *data = nullptr); QAdoptedThread(QThreadData *data);
~QAdoptedThread(); ~QAdoptedThread();
void init(); void init();

View File

@ -245,15 +245,6 @@ QThreadData *QThreadData::current(bool createIfNecessary)
data = nullptr; data = nullptr;
QT_RETHROW; QT_RETHROW;
} }
data->deref();
data->isAdopted = true;
data->threadId.storeRelaxed(QThread::currentThreadId());
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; return data;
} }

View File

@ -107,16 +107,6 @@ QThreadData *QThreadData::current(bool createIfNecessary)
threadData = 0; threadData = 0;
QT_RETHROW; QT_RETHROW;
} }
threadData->deref();
threadData->isAdopted = true;
threadData->threadId.storeRelaxed(reinterpret_cast<Qt::HANDLE>(quintptr(GetCurrentThreadId())));
if (!QCoreApplicationPrivate::theMainThreadId) {
auto *mainThread = threadData->thread.loadRelaxed();
mainThread->setObjectName("Qt mainThread");
QCoreApplicationPrivate::theMainThread.storeRelease(mainThread);
QCoreApplicationPrivate::theMainThreadId.storeRelaxed(threadData->threadId.loadRelaxed());
}
} }
return threadData; return threadData;
} }