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).

Change-Id: Iedcd0827d1e659f3de35fffd60cac0b47cc496bb
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
(cherry picked from commit 9bac90db46913ae70fd29676cb22890b4ac9aaa4)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Thiago Macieira 2024-11-06 20:31:52 -08:00 committed by Qt Cherry-pick Bot
parent 752ee25559
commit 2b3e56e2b4

View File

@ -1168,7 +1168,7 @@ bool QCoreApplication::notifyInternal2(QObject *receiver, QEvent *event)
return doNotify(receiver, event);
#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0)
if (threadData->thread.loadRelaxed() != QCoreApplicationPrivate::mainThread())
if (!QThread::isMainThread())
return false;
#endif
return qApp->notify(receiver, event);
@ -1278,7 +1278,7 @@ static bool doNotify(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)
Q_ASSERT(receiver->d_func()->threadData.loadAcquire()->thread.loadRelaxed() == mainThread());
Q_ASSERT(QThread::isMainThread());
if (extraData) {
// application event filters are only called for objects in the GUI thread
@ -1330,7 +1330,7 @@ bool QCoreApplicationPrivate::notify_helper(QObject *receiver, QEvent * event)
Q_TRACE_EXIT(QCoreApplication_notify_exit, consumed, filtered);
// 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->d_func()->sendThroughApplicationEventFilters(receiver, event)) {
filtered = true;
@ -2195,7 +2195,7 @@ void QCoreApplicationPrivate::quit()
{
Q_Q(QCoreApplication);
if (QThread::currentThread() == mainThread()) {
if (QThread::isMainThread()) {
QEvent quitEvent(QEvent::Quit);
QCoreApplication::sendEvent(q, &quitEvent);
} else {
@ -2955,7 +2955,7 @@ void QCoreApplication::requestPermission(const QPermission &requestedPermission,
QtPrivate::SlotObjUniquePtr slotObj{slotObjRaw}; // adopts
Q_ASSERT(slotObj);
if (QThread::currentThread() != QCoreApplicationPrivate::mainThread()) {
if (!QThread::isMainThread()) {
qCWarning(lcPermissions, "Permissions can only be requested from the GUI (main) thread");
return;
}