Use QThread::isMainThread() in a few places

It's thread-safe, whereas trying to load qApp isn't in Qt 6.x (will be
in 7.0) and dereferencing it to call QObject::thread() will probably
never be. It's also faster, being a single function call instead of two
or three.

This is not an exhaustive search, it's just a few places I found while
searching for QThread::instance().

Pick-to: 6.8
Change-Id: I3b4e1c75bb3966e2cd2dfffd79bfc8a40f6cf40b
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit 6fb39ca2cbb111469ce09c8342997eb5375e3239)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Thiago Macieira 2025-02-09 11:08:19 -08:00 committed by Qt Cherry-pick Bot
parent d47af17a0d
commit 54ef8bf929
12 changed files with 12 additions and 12 deletions

View File

@ -217,7 +217,7 @@ ProcessReaper::ProcessReaper()
ProcessReaper::~ProcessReaper() ProcessReaper::~ProcessReaper()
{ {
if (QThread::currentThread() != qApp->thread()) if (!QThread::isMainThread())
qWarning() << "Destructing process reaper from non-main thread."; qWarning() << "Destructing process reaper from non-main thread.";
instance()->m_private->waitForFinished(); instance()->m_private->waitForFinished();

View File

@ -33,7 +33,7 @@ QDBusConnectionPrivate *QDBusConnectionManager::busConnection(QDBusConnection::B
// we'll start in suspended delivery mode if we're in the main thread // we'll start in suspended delivery mode if we're in the main thread
// (the event loop will resume delivery) // (the event loop will resume delivery)
bool suspendedDelivery = qApp && qApp->thread() == QThread::currentThread(); bool suspendedDelivery = QThread::isMainThread();
const auto locker = qt_scoped_lock(defaultBusMutex); const auto locker = qt_scoped_lock(defaultBusMutex);
if (defaultBuses[type]) if (defaultBuses[type])

View File

@ -2028,7 +2028,7 @@ public:
// if this call is running on the main thread, we have a much lower // if this call is running on the main thread, we have a much lower
// tolerance for delay because any long-term delay will wreck user // tolerance for delay because any long-term delay will wreck user
// interactivity. // interactivity.
if (qApp && qApp->thread() == QThread::currentThread()) if (QThread::isMainThread())
m_maxCallTimeoutMs = mainThreadWarningAmount; m_maxCallTimeoutMs = mainThreadWarningAmount;
else else
m_maxCallTimeoutMs = otherThreadWarningAmount; m_maxCallTimeoutMs = otherThreadWarningAmount;

View File

@ -53,7 +53,7 @@ static bool qt_pixmap_thread_test()
return false; return false;
} }
if (QGuiApplicationPrivate::instance() if (QGuiApplicationPrivate::instance()
&& qApp->thread() != QThread::currentThread() && !QThread::isMainThread()
&& !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedPixmaps)) { && !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedPixmaps)) {
qWarning("QPixmap: It is not safe to use pixmaps outside the GUI thread on this platform"); qWarning("QPixmap: It is not safe to use pixmaps outside the GUI thread on this platform");
return false; return false;

View File

@ -71,7 +71,7 @@ static inline qsizetype cost(const QPixmap &pixmap)
static inline bool qt_pixmapcache_thread_test() static inline bool qt_pixmapcache_thread_test()
{ {
if (Q_LIKELY(QCoreApplication::instance() && QThread::currentThread() == QCoreApplication::instance()->thread())) if (Q_LIKELY(QThread::isMainThread()))
return true; return true;
return false; return false;

View File

@ -121,7 +121,7 @@ void QOffscreenSurface::create()
d->platformOffscreenSurface = QGuiApplicationPrivate::platformIntegration()->createPlatformOffscreenSurface(this); d->platformOffscreenSurface = QGuiApplicationPrivate::platformIntegration()->createPlatformOffscreenSurface(this);
// No platform offscreen surface, fallback to an invisible window // No platform offscreen surface, fallback to an invisible window
if (!d->platformOffscreenSurface) { if (!d->platformOffscreenSurface) {
if (QThread::currentThread() != qGuiApp->thread()) if (!QThread::isMainThread())
qWarning("Attempting to create QWindow-based QOffscreenSurface outside the gui thread. Expect failures."); qWarning("Attempting to create QWindow-based QOffscreenSurface outside the gui thread. Expect failures.");
d->offscreenWindow = new QWindow(d->screen); d->offscreenWindow = new QWindow(d->screen);
// Make the window frameless to prevent Windows from enlarging it, should it // Make the window frameless to prevent Windows from enlarging it, should it

View File

@ -2840,7 +2840,7 @@ void QWindowPrivate::maybeSynthesizeContextMenuEvent(QMouseEvent *event)
*/ */
void QWindow::requestUpdate() void QWindow::requestUpdate()
{ {
Q_ASSERT_X(QThread::currentThread() == QCoreApplication::instance()->thread(), Q_ASSERT_X(QThread::isMainThread(),
"QWindow", "Updates can only be scheduled from the GUI (main) thread"); "QWindow", "Updates can only be scheduled from the GUI (main) thread");
Q_D(QWindow); Q_D(QWindow);

View File

@ -95,7 +95,7 @@ template<>
template<typename EventType, typename ...Args> template<typename EventType, typename ...Args>
bool QWindowSystemHelper<QWindowSystemInterface::SynchronousDelivery>::handleEvent(Args ...args) bool QWindowSystemHelper<QWindowSystemInterface::SynchronousDelivery>::handleEvent(Args ...args)
{ {
if (QThread::currentThread() == QGuiApplication::instance()->thread()) { if (QThread::isMainThread()) {
EventType event(args...); EventType event(args...);
// Process the event immediately on the Gui thread and return the accepted state // Process the event immediately on the Gui thread and return the accepted state
if (QWindowSystemInterfacePrivate::eventHandler) { if (QWindowSystemInterfacePrivate::eventHandler) {

View File

@ -2331,7 +2331,7 @@ QVariant QTextDocument::loadResource(int type, const QUrl &name)
if (!r.isNull()) { if (!r.isNull()) {
if (type == ImageResource && r.userType() == QMetaType::QByteArray) { if (type == ImageResource && r.userType() == QMetaType::QByteArray) {
if (qApp->thread() != QThread::currentThread()) { if (!QThread::isMainThread()) {
// must use images in non-GUI threads // must use images in non-GUI threads
QImage image; QImage image;
image.loadFromData(r.toByteArray()); image.loadFromData(r.toByteArray());

View File

@ -2152,7 +2152,7 @@ void QVulkanWindowPrivate::endFrame()
*/ */
void QVulkanWindow::frameReady() void QVulkanWindow::frameReady()
{ {
Q_ASSERT_X(QThread::currentThread() == QCoreApplication::instance()->thread(), Q_ASSERT_X(QThread::isMainThread(),
"QVulkanWindow", "frameReady() can only be called from the GUI (main) thread"); "QVulkanWindow", "frameReady() can only be called from the GUI (main) thread");
Q_D(QVulkanWindow); Q_D(QVulkanWindow);

View File

@ -210,7 +210,7 @@ static void jump(QtMsgType t, const QMessageLogContext &context, const QString &
rich.chop(4); rich.chop(4);
if (!metFatal) { if (!metFatal) {
if (QThread::currentThread() == qApp->thread()) { if (QThread::isMainThread()) {
qtMessageHandler->showMessage(rich); qtMessageHandler->showMessage(rich);
} else { } else {
QMetaObject::invokeMethod(qtMessageHandler, QMetaObject::invokeMethod(qtMessageHandler,

View File

@ -955,7 +955,7 @@ void QWidgetPrivate::init(QWidget *parentWidget, Qt::WindowFlags f)
#if QT_CONFIG(thread) #if QT_CONFIG(thread)
if (!parent) { if (!parent) {
Q_ASSERT_X(q->thread() == qApp->thread(), "QWidget", Q_ASSERT_X(QThread::isMainThread(), "QWidget",
"Widgets must be created in the GUI thread."); "Widgets must be created in the GUI thread.");
} }
#endif #endif