From 763e5a27cdbd87fa24680bccb4f8d9a2a7cf6d7f Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 6 Nov 2024 09:21:28 -0800 Subject: [PATCH] Make QCoreApplication::self an atomic for Qt 7 We use it from many places in Qt that may run before QCoreApplication has been created (officially not supported but it happens) and after it has been destroyed. In particular, we have code in QtDBus that runs in another thread after QCoreApplication has been destroyed and uses the event system. This is not a complete change for Qt 7, it's just a reminder for work to be completed then. Change-Id: Ie3a0fc665babafd9888dfffd6c551e42f87a9dbd Reviewed-by: Edward Welbourne Reviewed-by: Ivan Solovev --- src/corelib/kernel/qcoreapplication.cpp | 14 ++++++++++++++ src/corelib/kernel/qcoreapplication.h | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 16776fe8f9a..4afe22e8a56 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -140,10 +140,15 @@ extern QString qAppFileName(); Q_CONSTINIT bool QCoreApplicationPrivate::setuidAllowed = false; +#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) +# warning "Audit remaining direct usages of this variable for memory ordering semantics" +Q_CONSTINIT QBasicAtomicPointer QCoreApplication::self = nullptr; +#else Q_CONSTINIT QCoreApplication *QCoreApplication::self = nullptr; Q_CONSTINIT static QBasicAtomicPointer g_self = nullptr; # undef qApp # define qApp g_self.loadRelaxed() +#endif #if !defined(Q_OS_WIN) #ifdef Q_OS_DARWIN @@ -812,8 +817,12 @@ void Q_TRACE_INSTRUMENT(qtcore) QCoreApplicationPrivate::init() initLocale(); Q_ASSERT_X(!QCoreApplication::self, "QCoreApplication", "there should be only one application object"); +#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) QCoreApplication::self = q; g_self.storeRelaxed(q); +#else + QCoreApplication::self.storeRelaxed(q); +#endif #if QT_CONFIG(thread) #ifdef Q_OS_WASM @@ -914,8 +923,13 @@ QCoreApplication::~QCoreApplication() qt_call_post_routines(); +#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) self = nullptr; g_self.storeRelaxed(nullptr); +#else + self.storeRelaxed(nullptr); +#endif + #ifndef QT_NO_QOBJECT QCoreApplicationPrivate::is_app_closing = true; QCoreApplicationPrivate::is_app_running = false; diff --git a/src/corelib/kernel/qcoreapplication.h b/src/corelib/kernel/qcoreapplication.h index e27086aa052..421578117cd 100644 --- a/src/corelib/kernel/qcoreapplication.h +++ b/src/corelib/kernel/qcoreapplication.h @@ -94,7 +94,11 @@ public: static void setSetuidAllowed(bool allow); static bool isSetuidAllowed(); +#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) + static QCoreApplication *instance() noexcept { return self.loadRelaxed(); } +#else static QCoreApplication *instance() noexcept { return self; } +#endif #ifndef QT_NO_QOBJECT static int exec(); @@ -227,7 +231,11 @@ private: static QStringList libraryPathsLocked(); #endif +#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) + static QBasicAtomicPointer self; +#else static QCoreApplication *self; +#endif Q_DISABLE_COPY(QCoreApplication)