diff options
Diffstat (limited to 'src/corelib/kernel/qapplicationstatic.h')
-rw-r--r-- | src/corelib/kernel/qapplicationstatic.h | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/src/corelib/kernel/qapplicationstatic.h b/src/corelib/kernel/qapplicationstatic.h index 2f2cab9174..345a880748 100644 --- a/src/corelib/kernel/qapplicationstatic.h +++ b/src/corelib/kernel/qapplicationstatic.h @@ -11,6 +11,10 @@ #include <new> +#if 0 +#pragma qt_class(QApplicationStatic) +#endif + QT_BEGIN_NAMESPACE namespace QtGlobalStatic { @@ -30,7 +34,8 @@ template <typename QAS> struct ApplicationHolder Q_DISABLE_COPY_MOVE(ApplicationHolder) ~ApplicationHolder() { - if (guard.loadRelaxed() == QtGlobalStatic::Initialized) { + if (guard.loadAcquire() == QtGlobalStatic::Initialized) { + // No mutex! Up to external code to ensure no race happens. guard.storeRelease(QtGlobalStatic::Destroyed); realPointer()->~PlainType(); } @@ -44,24 +49,26 @@ template <typename QAS> struct ApplicationHolder // called from QGlobalStatic::instance() PlainType *pointer() noexcept(MutexLockIsNoexcept && ConstructionIsNoexcept) { - if (guard.loadRelaxed() == QtGlobalStatic::Initialized) + if (guard.loadAcquire() == QtGlobalStatic::Initialized) return realPointer(); QMutexLocker locker(&mutex); if (guard.loadRelaxed() == QtGlobalStatic::Uninitialized) { QAS::innerFunction(&storage); - QObject::connect(QCoreApplication::instance(), &QObject::destroyed, reset); - guard.storeRelaxed(QtGlobalStatic::Initialized); + const auto *app = QCoreApplication::instance(); + Q_ASSERT_X(app, Q_FUNC_INFO, + "The application static was used without a QCoreApplication instance"); + QObject::connect(app, &QObject::destroyed, app, reset, Qt::DirectConnection); + guard.storeRelease(QtGlobalStatic::Initialized); } return realPointer(); } static void reset() { - if (guard.loadRelaxed() == QtGlobalStatic::Initialized) { - QMutexLocker locker(&mutex); - realPointer()->~PlainType(); - guard.storeRelaxed(QtGlobalStatic::Uninitialized); - } + // we only synchronize using the mutex here, not the guard + QMutexLocker locker(&mutex); + realPointer()->~PlainType(); + guard.storeRelaxed(QtGlobalStatic::Uninitialized); } }; } // namespace QtGlobalStatic |