QThread: Reuse isMainThread()

Internally, reuse threadId and theMainThreadId.

Change-Id: Iea6e7d8fcbcaf7e2f4dbf8ab33890d0f7954edc0
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Jarek Kobus 2024-03-21 09:30:18 +01:00
parent de0f145022
commit 159ea47e2f
4 changed files with 10 additions and 10 deletions

View File

@ -65,7 +65,7 @@ QThreadData::~QThreadData()
// crashing during QCoreApplicationData's global static cleanup we need to // crashing during QCoreApplicationData's global static cleanup we need to
// safeguard the main thread here.. This fix is a bit crude, but it solves // safeguard the main thread here.. This fix is a bit crude, but it solves
// the problem... // the problem...
if (this->thread.loadAcquire() == QCoreApplicationPrivate::theMainThread.loadAcquire()) { if (threadId.loadAcquire() == QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
QCoreApplicationPrivate::theMainThread.storeRelease(nullptr); QCoreApplicationPrivate::theMainThread.storeRelease(nullptr);
QCoreApplicationPrivate::theMainThreadId.storeRelaxed(nullptr); QCoreApplicationPrivate::theMainThreadId.storeRelaxed(nullptr);
QThreadData::clearCurrentThreadData(); QThreadData::clearCurrentThreadData();
@ -1084,7 +1084,7 @@ QThreadData *QThreadData::current(bool createIfNecessary)
data->threadId.storeRelaxed(Qt::HANDLE(data->thread.loadAcquire())); data->threadId.storeRelaxed(Qt::HANDLE(data->thread.loadAcquire()));
data->deref(); data->deref();
data->isAdopted = true; data->isAdopted = true;
if (!QCoreApplicationPrivate::theMainThread.loadAcquire()) { if (!QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
QCoreApplicationPrivate::theMainThread.storeRelease(data->thread.loadRelaxed()); QCoreApplicationPrivate::theMainThread.storeRelease(data->thread.loadRelaxed());
QCoreApplicationPrivate::theMainThreadId.storeRelaxed(data->threadId.loadRelaxed()); QCoreApplicationPrivate::theMainThreadId.storeRelaxed(data->threadId.loadRelaxed());
} }
@ -1207,11 +1207,11 @@ bool QThread::event(QEvent *event)
void QThread::requestInterruption() void QThread::requestInterruption()
{ {
if (this == QCoreApplicationPrivate::theMainThread.loadAcquire()) { Q_D(QThread);
if (d->threadId() == QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
qWarning("QThread::requestInterruption has no effect on the main thread"); qWarning("QThread::requestInterruption has no effect on the main thread");
return; return;
} }
Q_D(QThread);
QMutexLocker locker(&d->mutex); QMutexLocker locker(&d->mutex);
if (!d->running || d->finished || d->isInFinish) if (!d->running || d->finished || d->isInFinish)
return; return;

View File

@ -191,7 +191,7 @@ QThreadData *QThreadData::current(bool createIfNecessary)
data->deref(); data->deref();
data->isAdopted = true; data->isAdopted = true;
data->threadId.storeRelaxed(to_HANDLE(pthread_self())); data->threadId.storeRelaxed(to_HANDLE(pthread_self()));
if (!QCoreApplicationPrivate::theMainThread.loadAcquire()) { if (!QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
QCoreApplicationPrivate::theMainThread.storeRelease(data->thread.loadRelaxed()); QCoreApplicationPrivate::theMainThread.storeRelease(data->thread.loadRelaxed());
QCoreApplicationPrivate::theMainThreadId.storeRelaxed(data->threadId.loadRelaxed()); QCoreApplicationPrivate::theMainThreadId.storeRelaxed(data->threadId.loadRelaxed());
} }

View File

@ -87,7 +87,7 @@ QThreadData *QThreadData::current(bool createIfNecessary)
threadData->isAdopted = true; threadData->isAdopted = true;
threadData->threadId.storeRelaxed(reinterpret_cast<Qt::HANDLE>(quintptr(GetCurrentThreadId()))); threadData->threadId.storeRelaxed(reinterpret_cast<Qt::HANDLE>(quintptr(GetCurrentThreadId())));
if (!QCoreApplicationPrivate::theMainThread) { if (!QCoreApplicationPrivate::theMainThreadId) {
QCoreApplicationPrivate::theMainThread = threadData->thread.loadRelaxed(); QCoreApplicationPrivate::theMainThread = threadData->thread.loadRelaxed();
QCoreApplicationPrivate::theMainThreadId.storeRelaxed(threadData->threadId.loadRelaxed()); QCoreApplicationPrivate::theMainThreadId.storeRelaxed(threadData->threadId.loadRelaxed());
} else { } else {

View File

@ -1041,7 +1041,7 @@ void tst_QCoreApplication::addRemoveLibPaths()
static bool theMainThreadIsSet() static bool theMainThreadIsSet()
{ {
// QCoreApplicationPrivate::mainThread() has a Q_ASSERT we'd trigger // QCoreApplicationPrivate::mainThread() has a Q_ASSERT we'd trigger
return QCoreApplicationPrivate::theMainThread.loadRelaxed() != nullptr; return QCoreApplicationPrivate::theMainThreadId.loadRelaxed() != nullptr;
} }
static bool theMainThreadWasUnset = !theMainThreadIsSet(); // global static static bool theMainThreadWasUnset = !theMainThreadIsSet(); // global static
@ -1053,8 +1053,8 @@ void tst_QCoreApplication::theMainThread()
int argc = 1; int argc = 1;
char *argv[] = { const_cast<char*>(QTest::currentAppName()) }; char *argv[] = { const_cast<char*>(QTest::currentAppName()) };
TestApplication app(argc, argv); TestApplication app(argc, argv);
QVERIFY(QCoreApplicationPrivate::theMainThread.loadRelaxed()); QVERIFY(QCoreApplicationPrivate::theMainThreadId.loadRelaxed());
QCOMPARE(QCoreApplicationPrivate::theMainThread.loadRelaxed(), thread()); QVERIFY(QThread::isMainThread());
QCOMPARE(app.thread(), thread()); QCOMPARE(app.thread(), thread());
QCOMPARE(app.thread(), QThread::currentThread()); QCOMPARE(app.thread(), QThread::currentThread());
} }
@ -1067,7 +1067,7 @@ static void createQObjectOnDestruction()
#if !defined(QT_QGUIAPPLICATIONTEST) && !defined(Q_OS_WIN) #if !defined(QT_QGUIAPPLICATIONTEST) && !defined(Q_OS_WIN)
// QCoreApplicationData's global static destructor has run and cleaned up // QCoreApplicationData's global static destructor has run and cleaned up
// the QAdoptedThrad. // the QAdoptedThread.
if (theMainThreadIsSet()) if (theMainThreadIsSet())
qFatal("theMainThreadIsSet() returned true; some QObject must have leaked"); qFatal("theMainThreadIsSet() returned true; some QObject must have leaked");
#endif #endif