QCoreApplication: use QThread::isMainThread() more

QCoreApplicationPrivate::mainThread() asserts that there is a main
thread, which means we can't use it to detect after the main thread has
been unset (late destructor-on-exit).

Pick-to: 6.8
Change-Id: Iedcd0827d1e659f3de35fffd60cac0b47cc496bb
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Thiago Macieira 2024-11-06 20:31:52 -08:00
parent a90349aa21
commit 9bac90db46

View File

@ -1124,7 +1124,7 @@ bool QCoreApplication::notifyInternal2(QObject *receiver, QEvent *event)
return doNotify(receiver, event); return doNotify(receiver, event);
#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) #if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0)
if (threadData->thread.loadRelaxed() != QCoreApplicationPrivate::mainThread()) if (!QThread::isMainThread())
return false; return false;
#endif #endif
return qApp->notify(receiver, event); return qApp->notify(receiver, event);
@ -1234,7 +1234,7 @@ static bool doNotify(QObject *receiver, QEvent *event)
bool QCoreApplicationPrivate::sendThroughApplicationEventFilters(QObject *receiver, QEvent *event) bool QCoreApplicationPrivate::sendThroughApplicationEventFilters(QObject *receiver, QEvent *event)
{ {
// We can't access the application event filters outside of the main thread (race conditions) // We can't access the application event filters outside of the main thread (race conditions)
Q_ASSERT(receiver->d_func()->threadData.loadAcquire()->thread.loadRelaxed() == mainThread()); Q_ASSERT(QThread::isMainThread());
if (extraData) { if (extraData) {
// application event filters are only called for objects in the GUI thread // application event filters are only called for objects in the GUI thread
@ -1286,7 +1286,7 @@ bool QCoreApplicationPrivate::notify_helper(QObject *receiver, QEvent * event)
Q_TRACE_EXIT(QCoreApplication_notify_exit, consumed, filtered); Q_TRACE_EXIT(QCoreApplication_notify_exit, consumed, filtered);
// send to all application event filters (only does anything in the main thread) // send to all application event filters (only does anything in the main thread)
if (receiver->d_func()->threadData.loadRelaxed()->thread.loadAcquire() == mainThread() if (QThread::isMainThread()
&& QCoreApplication::self && QCoreApplication::self
&& QCoreApplication::self->d_func()->sendThroughApplicationEventFilters(receiver, event)) { && QCoreApplication::self->d_func()->sendThroughApplicationEventFilters(receiver, event)) {
filtered = true; filtered = true;
@ -2150,7 +2150,7 @@ void QCoreApplicationPrivate::quit()
{ {
Q_Q(QCoreApplication); Q_Q(QCoreApplication);
if (QThread::currentThread() == mainThread()) { if (QThread::isMainThread()) {
QEvent quitEvent(QEvent::Quit); QEvent quitEvent(QEvent::Quit);
QCoreApplication::sendEvent(q, &quitEvent); QCoreApplication::sendEvent(q, &quitEvent);
} else { } else {
@ -2913,7 +2913,7 @@ void QCoreApplication::requestPermission(const QPermission &requestedPermission,
QtPrivate::SlotObjUniquePtr slotObj{slotObjRaw}; // adopts QtPrivate::SlotObjUniquePtr slotObj{slotObjRaw}; // adopts
Q_ASSERT(slotObj); Q_ASSERT(slotObj);
if (QThread::currentThread() != QCoreApplicationPrivate::mainThread()) { if (!QThread::isMainThread()) {
qCWarning(lcPermissions, "Permissions can only be requested from the GUI (main) thread"); qCWarning(lcPermissions, "Permissions can only be requested from the GUI (main) thread");
return; return;
} }