summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <[email protected]>2025-02-09 11:08:19 -0800
committerThiago Macieira <[email protected]>2025-02-26 17:54:59 -0800
commit6fb39ca2cbb111469ce09c8342997eb5375e3239 (patch)
tree512f026519549c6508f4e9b17224b254e546383d
parent7c7731a8229a44050c02d33c7111e4d522909c1d (diff)
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.9 6.8 Change-Id: I3b4e1c75bb3966e2cd2dfffd79bfc8a40f6cf40b Reviewed-by: Ivan Solovev <[email protected]> Reviewed-by: MÃ¥rten Nordheim <[email protected]>
-rw-r--r--src/assets/downloader/tasking/qprocesstask.cpp2
-rw-r--r--src/dbus/qdbusconnectionmanager.cpp2
-rw-r--r--src/dbus/qdbusintegrator.cpp2
-rw-r--r--src/gui/image/qpixmap.cpp2
-rw-r--r--src/gui/image/qpixmapcache.cpp2
-rw-r--r--src/gui/kernel/qoffscreensurface.cpp2
-rw-r--r--src/gui/kernel/qwindow.cpp2
-rw-r--r--src/gui/kernel/qwindowsysteminterface.cpp2
-rw-r--r--src/gui/text/qtextdocument.cpp2
-rw-r--r--src/gui/vulkan/qvulkanwindow.cpp2
-rw-r--r--src/widgets/dialogs/qerrormessage.cpp2
-rw-r--r--src/widgets/kernel/qwidget.cpp2
12 files changed, 12 insertions, 12 deletions
diff --git a/src/assets/downloader/tasking/qprocesstask.cpp b/src/assets/downloader/tasking/qprocesstask.cpp
index ef10876c00b..b708db14c06 100644
--- a/src/assets/downloader/tasking/qprocesstask.cpp
+++ b/src/assets/downloader/tasking/qprocesstask.cpp
@@ -217,7 +217,7 @@ ProcessReaper::ProcessReaper()
ProcessReaper::~ProcessReaper()
{
- if (QThread::currentThread() != qApp->thread())
+ if (!QThread::isMainThread())
qWarning() << "Destructing process reaper from non-main thread.";
instance()->m_private->waitForFinished();
diff --git a/src/dbus/qdbusconnectionmanager.cpp b/src/dbus/qdbusconnectionmanager.cpp
index ce52c9fa633..73f11d82280 100644
--- a/src/dbus/qdbusconnectionmanager.cpp
+++ b/src/dbus/qdbusconnectionmanager.cpp
@@ -33,7 +33,7 @@ QDBusConnectionPrivate *QDBusConnectionManager::busConnection(QDBusConnection::B
// we'll start in suspended delivery mode if we're in the main thread
// (the event loop will resume delivery)
- bool suspendedDelivery = qApp && qApp->thread() == QThread::currentThread();
+ bool suspendedDelivery = QThread::isMainThread();
const auto locker = qt_scoped_lock(defaultBusMutex);
if (defaultBuses[type])
diff --git a/src/dbus/qdbusintegrator.cpp b/src/dbus/qdbusintegrator.cpp
index bdd49464b50..ed17dddbcf5 100644
--- a/src/dbus/qdbusintegrator.cpp
+++ b/src/dbus/qdbusintegrator.cpp
@@ -2028,7 +2028,7 @@ public:
// 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
// interactivity.
- if (qApp && qApp->thread() == QThread::currentThread())
+ if (QThread::isMainThread())
m_maxCallTimeoutMs = mainThreadWarningAmount;
else
m_maxCallTimeoutMs = otherThreadWarningAmount;
diff --git a/src/gui/image/qpixmap.cpp b/src/gui/image/qpixmap.cpp
index a0ba3ccef51..54d55b05fd1 100644
--- a/src/gui/image/qpixmap.cpp
+++ b/src/gui/image/qpixmap.cpp
@@ -53,7 +53,7 @@ static bool qt_pixmap_thread_test()
return false;
}
if (QGuiApplicationPrivate::instance()
- && qApp->thread() != QThread::currentThread()
+ && !QThread::isMainThread()
&& !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedPixmaps)) {
qWarning("QPixmap: It is not safe to use pixmaps outside the GUI thread on this platform");
return false;
diff --git a/src/gui/image/qpixmapcache.cpp b/src/gui/image/qpixmapcache.cpp
index ecb4a7421d6..75224df3f04 100644
--- a/src/gui/image/qpixmapcache.cpp
+++ b/src/gui/image/qpixmapcache.cpp
@@ -71,7 +71,7 @@ static inline qsizetype cost(const QPixmap &pixmap)
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 false;
diff --git a/src/gui/kernel/qoffscreensurface.cpp b/src/gui/kernel/qoffscreensurface.cpp
index 02e606658a8..25678039c8a 100644
--- a/src/gui/kernel/qoffscreensurface.cpp
+++ b/src/gui/kernel/qoffscreensurface.cpp
@@ -121,7 +121,7 @@ void QOffscreenSurface::create()
d->platformOffscreenSurface = QGuiApplicationPrivate::platformIntegration()->createPlatformOffscreenSurface(this);
// No platform offscreen surface, fallback to an invisible window
if (!d->platformOffscreenSurface) {
- if (QThread::currentThread() != qGuiApp->thread())
+ if (!QThread::isMainThread())
qWarning("Attempting to create QWindow-based QOffscreenSurface outside the gui thread. Expect failures.");
d->offscreenWindow = new QWindow(d->screen);
// Make the window frameless to prevent Windows from enlarging it, should it
diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp
index 072389e8598..f6a05a758f0 100644
--- a/src/gui/kernel/qwindow.cpp
+++ b/src/gui/kernel/qwindow.cpp
@@ -2841,7 +2841,7 @@ void QWindowPrivate::maybeSynthesizeContextMenuEvent(QMouseEvent *event)
*/
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");
Q_D(QWindow);
diff --git a/src/gui/kernel/qwindowsysteminterface.cpp b/src/gui/kernel/qwindowsysteminterface.cpp
index a3fe2a612d4..97201770d8b 100644
--- a/src/gui/kernel/qwindowsysteminterface.cpp
+++ b/src/gui/kernel/qwindowsysteminterface.cpp
@@ -95,7 +95,7 @@ template<>
template<typename EventType, typename ...Args>
bool QWindowSystemHelper<QWindowSystemInterface::SynchronousDelivery>::handleEvent(Args ...args)
{
- if (QThread::currentThread() == QGuiApplication::instance()->thread()) {
+ if (QThread::isMainThread()) {
EventType event(args...);
// Process the event immediately on the Gui thread and return the accepted state
if (QWindowSystemInterfacePrivate::eventHandler) {
diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp
index e5f8ac3a733..d96e457816e 100644
--- a/src/gui/text/qtextdocument.cpp
+++ b/src/gui/text/qtextdocument.cpp
@@ -2331,7 +2331,7 @@ QVariant QTextDocument::loadResource(int type, const QUrl &name)
if (!r.isNull()) {
if (type == ImageResource && r.userType() == QMetaType::QByteArray) {
- if (qApp->thread() != QThread::currentThread()) {
+ if (!QThread::isMainThread()) {
// must use images in non-GUI threads
QImage image;
image.loadFromData(r.toByteArray());
diff --git a/src/gui/vulkan/qvulkanwindow.cpp b/src/gui/vulkan/qvulkanwindow.cpp
index 9bfc7d229bd..91095b23ea8 100644
--- a/src/gui/vulkan/qvulkanwindow.cpp
+++ b/src/gui/vulkan/qvulkanwindow.cpp
@@ -2152,7 +2152,7 @@ void QVulkanWindowPrivate::endFrame()
*/
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");
Q_D(QVulkanWindow);
diff --git a/src/widgets/dialogs/qerrormessage.cpp b/src/widgets/dialogs/qerrormessage.cpp
index 80e4fd73897..f1adb6aa819 100644
--- a/src/widgets/dialogs/qerrormessage.cpp
+++ b/src/widgets/dialogs/qerrormessage.cpp
@@ -210,7 +210,7 @@ static void jump(QtMsgType t, const QMessageLogContext &context, const QString &
rich.chop(4);
if (!metFatal) {
- if (QThread::currentThread() == qApp->thread()) {
+ if (QThread::isMainThread()) {
qtMessageHandler->showMessage(rich);
} else {
QMetaObject::invokeMethod(qtMessageHandler,
diff --git a/src/widgets/kernel/qwidget.cpp b/src/widgets/kernel/qwidget.cpp
index 970eef87486..6dcd11ef74a 100644
--- a/src/widgets/kernel/qwidget.cpp
+++ b/src/widgets/kernel/qwidget.cpp
@@ -945,7 +945,7 @@ void QWidgetPrivate::init(QWidget *parentWidget, Qt::WindowFlags f)
#if QT_CONFIG(thread)
if (!parent) {
- Q_ASSERT_X(q->thread() == qApp->thread(), "QWidget",
+ Q_ASSERT_X(QThread::isMainThread(), "QWidget",
"Widgets must be created in the GUI thread.");
}
#endif