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 <edward.welbourne@qt.io>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Thiago Macieira 2024-11-06 09:21:28 -08:00
parent 43a27888b4
commit 763e5a27cd
2 changed files with 22 additions and 0 deletions

View File

@ -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> QCoreApplication::self = nullptr;
#else
Q_CONSTINIT QCoreApplication *QCoreApplication::self = nullptr;
Q_CONSTINIT static QBasicAtomicPointer<QCoreApplication> 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;

View File

@ -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<QCoreApplication> self;
#else
static QCoreApplication *self;
#endif
Q_DISABLE_COPY(QCoreApplication)