From 32ee5539fd0a24d65490017b2f448bf1e9ba96e3 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 14 Nov 2024 13:13:31 -0700 Subject: [PATCH] QCoreApplication: replace threadRequiresCoreApplication() With a direct access to the threadData's variable. Amends commit 10c529b08de7cd55b4c3e3654464119246498273 ("Add a way for auxiliary threads to handle events without CoreApp", Qt 5.6), which introduced QDaemonThread, for QtDBus use. We don't need to get the QThreadData from TLS, because we are processing events for an object associated with that particular thread. This removes the only use of QThreadData::current(false) in all of Qt. Refactoring in the next commit(s). Change-Id: Ica2bab556bd431519a1bfffd859911ea7daf062f Reviewed-by: Fabian Kosmale Reviewed-by: Ahmad Samir --- src/corelib/kernel/qcoreapplication.cpp | 22 +++++++--------------- src/corelib/kernel/qcoreapplication_p.h | 1 - src/corelib/kernel/qeventloop.cpp | 7 ++++--- src/widgets/kernel/qapplication.cpp | 5 +++-- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 4afe22e8a56..0f1de6b97cd 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -536,14 +536,6 @@ QThread *QCoreApplicationPrivate::mainThread() return theMainThread.loadRelaxed(); } -bool QCoreApplicationPrivate::threadRequiresCoreApplication() -{ - QThreadData *data = QThreadData::current(false); - if (!data) - return true; // default setting - return data->requiresCoreApplication; -} - void QCoreApplicationPrivate::checkReceiverThread(QObject *receiver) { QThread *currentThread = QThread::currentThread(); @@ -1107,7 +1099,13 @@ void QCoreApplication::setQuitLockEnabled(bool enabled) */ bool QCoreApplication::notifyInternal2(QObject *receiver, QEvent *event) { - bool selfRequired = QCoreApplicationPrivate::threadRequiresCoreApplication(); + // Qt enforces the rule that events can only be sent to objects in + // the current thread, so receiver->d_func()->threadData is + // equivalent to QThreadData::current(), just without the function + // call overhead. + QObjectPrivate *d = receiver->d_func(); + QThreadData *threadData = d->threadData.loadAcquire(); + bool selfRequired = threadData->requiresCoreApplication; if (selfRequired && !qApp) return false; @@ -1119,12 +1117,6 @@ bool QCoreApplication::notifyInternal2(QObject *receiver, QEvent *event) return result; } - // Qt enforces the rule that events can only be sent to objects in - // the current thread, so receiver->d_func()->threadData is - // equivalent to QThreadData::current(), just without the function - // call overhead. - QObjectPrivate *d = receiver->d_func(); - QThreadData *threadData = d->threadData.loadAcquire(); QScopedScopeLevelCounter scopeLevelCounter(threadData); if (!selfRequired) return doNotify(receiver, event); diff --git a/src/corelib/kernel/qcoreapplication_p.h b/src/corelib/kernel/qcoreapplication_p.h index 0ad6eb6640e..48783626bdc 100644 --- a/src/corelib/kernel/qcoreapplication_p.h +++ b/src/corelib/kernel/qcoreapplication_p.h @@ -113,7 +113,6 @@ public: static QBasicAtomicPointer theMainThread; static QBasicAtomicPointer theMainThreadId; static QThread *mainThread(); - static bool threadRequiresCoreApplication(); static void sendPostedEvents(QObject *receiver, int event_type, QThreadData *data); diff --git a/src/corelib/kernel/qeventloop.cpp b/src/corelib/kernel/qeventloop.cpp index 2d401e7a425..500b67ea6ae 100644 --- a/src/corelib/kernel/qeventloop.cpp +++ b/src/corelib/kernel/qeventloop.cpp @@ -67,10 +67,11 @@ QEventLoop::QEventLoop(QObject *parent) : QObject(*new QEventLoopPrivate, parent) { Q_D(QEventLoop); - if (!QCoreApplication::instance() && QCoreApplicationPrivate::threadRequiresCoreApplication()) { - qWarning("QEventLoop: Cannot be used without QApplication"); + QThreadData *threadData = d->threadData.loadRelaxed(); + if (!QCoreApplication::instance() && threadData->requiresCoreApplication) { + qWarning("QEventLoop: Cannot be used without QCoreApplication"); } else { - d->threadData.loadRelaxed()->ensureEventDispatcher(); + threadData->ensureEventDispatcher(); } } diff --git a/src/widgets/kernel/qapplication.cpp b/src/widgets/kernel/qapplication.cpp index bc6710d1981..297c3f45ac9 100644 --- a/src/widgets/kernel/qapplication.cpp +++ b/src/widgets/kernel/qapplication.cpp @@ -3262,8 +3262,9 @@ bool QApplicationPrivate::notify_helper(QObject *receiver, QEvent * e) Q_TRACE_EXIT(QApplication_notify_exit, consumed, filtered); // send to all application event filters - if (threadRequiresCoreApplication() - && receiver->d_func()->threadData.loadRelaxed()->thread.loadAcquire() == mainThread() + QThreadData *threadData = receiver->d_func()->threadData.loadRelaxed(); + if (threadData->requiresCoreApplication + && threadData->thread.loadAcquire() == mainThread() && sendThroughApplicationEventFilters(receiver, e)) { filtered = true; return filtered;