From 159ea47e2fc8ec7225db4feed3cb8926fbb93fc2 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Thu, 21 Mar 2024 09:30:18 +0100 Subject: QThread: Reuse isMainThread() Internally, reuse threadId and theMainThreadId. Change-Id: Iea6e7d8fcbcaf7e2f4dbf8ab33890d0f7954edc0 Reviewed-by: Thiago Macieira --- src/corelib/thread/qthread.cpp | 8 ++++---- src/corelib/thread/qthread_unix.cpp | 2 +- src/corelib/thread/qthread_win.cpp | 2 +- .../auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp | 8 ++++---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/corelib/thread/qthread.cpp b/src/corelib/thread/qthread.cpp index 0ad402b77c..8d8f353aaa 100644 --- a/src/corelib/thread/qthread.cpp +++ b/src/corelib/thread/qthread.cpp @@ -65,7 +65,7 @@ QThreadData::~QThreadData() // crashing during QCoreApplicationData's global static cleanup we need to // safeguard the main thread here.. This fix is a bit crude, but it solves // the problem... - if (this->thread.loadAcquire() == QCoreApplicationPrivate::theMainThread.loadAcquire()) { + if (threadId.loadAcquire() == QCoreApplicationPrivate::theMainThreadId.loadAcquire()) { QCoreApplicationPrivate::theMainThread.storeRelease(nullptr); QCoreApplicationPrivate::theMainThreadId.storeRelaxed(nullptr); QThreadData::clearCurrentThreadData(); @@ -1084,7 +1084,7 @@ QThreadData *QThreadData::current(bool createIfNecessary) data->threadId.storeRelaxed(Qt::HANDLE(data->thread.loadAcquire())); data->deref(); data->isAdopted = true; - if (!QCoreApplicationPrivate::theMainThread.loadAcquire()) { + if (!QCoreApplicationPrivate::theMainThreadId.loadAcquire()) { QCoreApplicationPrivate::theMainThread.storeRelease(data->thread.loadRelaxed()); QCoreApplicationPrivate::theMainThreadId.storeRelaxed(data->threadId.loadRelaxed()); } @@ -1207,11 +1207,11 @@ bool QThread::event(QEvent *event) 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"); return; } - Q_D(QThread); QMutexLocker locker(&d->mutex); if (!d->running || d->finished || d->isInFinish) return; diff --git a/src/corelib/thread/qthread_unix.cpp b/src/corelib/thread/qthread_unix.cpp index 8916da09b2..23a79074d6 100644 --- a/src/corelib/thread/qthread_unix.cpp +++ b/src/corelib/thread/qthread_unix.cpp @@ -191,7 +191,7 @@ QThreadData *QThreadData::current(bool createIfNecessary) data->deref(); data->isAdopted = true; data->threadId.storeRelaxed(to_HANDLE(pthread_self())); - if (!QCoreApplicationPrivate::theMainThread.loadAcquire()) { + if (!QCoreApplicationPrivate::theMainThreadId.loadAcquire()) { QCoreApplicationPrivate::theMainThread.storeRelease(data->thread.loadRelaxed()); QCoreApplicationPrivate::theMainThreadId.storeRelaxed(data->threadId.loadRelaxed()); } diff --git a/src/corelib/thread/qthread_win.cpp b/src/corelib/thread/qthread_win.cpp index 475a61bf65..a193595532 100644 --- a/src/corelib/thread/qthread_win.cpp +++ b/src/corelib/thread/qthread_win.cpp @@ -87,7 +87,7 @@ QThreadData *QThreadData::current(bool createIfNecessary) threadData->isAdopted = true; threadData->threadId.storeRelaxed(reinterpret_cast(quintptr(GetCurrentThreadId()))); - if (!QCoreApplicationPrivate::theMainThread) { + if (!QCoreApplicationPrivate::theMainThreadId) { QCoreApplicationPrivate::theMainThread = threadData->thread.loadRelaxed(); QCoreApplicationPrivate::theMainThreadId.storeRelaxed(threadData->threadId.loadRelaxed()); } else { diff --git a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp index 0745ea6ceb..8f8ab33e64 100644 --- a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp +++ b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp @@ -1041,7 +1041,7 @@ void tst_QCoreApplication::addRemoveLibPaths() static bool theMainThreadIsSet() { // 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 @@ -1053,8 +1053,8 @@ void tst_QCoreApplication::theMainThread() int argc = 1; char *argv[] = { const_cast(QTest::currentAppName()) }; TestApplication app(argc, argv); - QVERIFY(QCoreApplicationPrivate::theMainThread.loadRelaxed()); - QCOMPARE(QCoreApplicationPrivate::theMainThread.loadRelaxed(), thread()); + QVERIFY(QCoreApplicationPrivate::theMainThreadId.loadRelaxed()); + QVERIFY(QThread::isMainThread()); QCOMPARE(app.thread(), thread()); QCOMPARE(app.thread(), QThread::currentThread()); } @@ -1067,7 +1067,7 @@ static void createQObjectOnDestruction() #if !defined(QT_QGUIAPPLICATIONTEST) && !defined(Q_OS_WIN) // QCoreApplicationData's global static destructor has run and cleaned up - // the QAdoptedThrad. + // the QAdoptedThread. if (theMainThreadIsSet()) qFatal("theMainThreadIsSet() returned true; some QObject must have leaked"); #endif -- cgit v1.2.3