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 <fabian.kosmale@qt.io>
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
This commit is contained in:
Thiago Macieira 2024-11-14 13:13:31 -07:00
parent 49182a0d57
commit 32ee5539fd
4 changed files with 14 additions and 21 deletions

View File

@ -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);

View File

@ -113,7 +113,6 @@ public:
static QBasicAtomicPointer<QThread> theMainThread;
static QBasicAtomicPointer<void> theMainThreadId;
static QThread *mainThread();
static bool threadRequiresCoreApplication();
static void sendPostedEvents(QObject *receiver, int event_type, QThreadData *data);

View File

@ -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();
}
}

View File

@ -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;