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)