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:
parent
d47af17a0d
commit
54ef8bf929
@ -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();
|
||||
|
@ -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])
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -2840,7 +2840,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);
|
||||
|
@ -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) {
|
||||
|
@ -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());
|
||||
|
@ -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);
|
||||
|
@ -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,
|
||||
|
@ -955,7 +955,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
|
||||
|
Loading…
x
Reference in New Issue
Block a user