summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qglobalstatic.h
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2021-12-01 16:20:29 -0800
committerThiago Macieira <thiago.macieira@intel.com>2021-12-12 14:25:42 -0800
commit81a31beeb25eaf14d5c5f42fe26aa49d6ef29bf8 (patch)
treec972a21eea5306b42503dba9e107cc5e6cda0d55 /src/corelib/global/qglobalstatic.h
parentfbbcd109f518adaedc5b3a009b204ac78a284a9a (diff)
Rewrite Q_{GLOBAL,APPLICATION}_STATIC with C++17 goodies
Especially static inline variables. This greatly reduces the amount of code that existed in macros, moving them to templates. Additionally, this removes one level of indirection from Q_APPLICATION_STATIC by removing the std::unique_ptr. We now directly manage the object's storage. Change-Id: I2cffe62afda945079b63fffd16bcc825cc04334e Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib/global/qglobalstatic.h')
-rw-r--r--src/corelib/global/qglobalstatic.h119
1 files changed, 60 insertions, 59 deletions
diff --git a/src/corelib/global/qglobalstatic.h b/src/corelib/global/qglobalstatic.h
index 6816721b32..01e06e5234 100644
--- a/src/corelib/global/qglobalstatic.h
+++ b/src/corelib/global/qglobalstatic.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 Intel Corporation.
+** Copyright (C) 2021 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -44,6 +44,7 @@
#include <QtCore/qatomic.h>
+#include <atomic> // for bootstrapped (no thread) builds
#include <type_traits>
QT_BEGIN_NAMESPACE
@@ -55,92 +56,92 @@ enum GuardValues {
Uninitialized = 0,
Initializing = 1
};
-}
-#define Q_GLOBAL_STATIC_INTERNAL_HOLDER(ARGS) \
- struct HolderBase \
- { \
- HolderBase() = default; \
- ~HolderBase() noexcept \
- { \
- if (guard.loadRelaxed() == QtGlobalStatic::Initialized) \
- guard.storeRelaxed(QtGlobalStatic::Destroyed); \
- } \
- Q_DISABLE_COPY_MOVE(HolderBase) \
- }; \
- struct Holder : public HolderBase \
- { \
- Type value; \
- Holder() noexcept(noexcept(typename std::remove_cv<Type>::type ARGS)) \
- : value ARGS \
- { \
- guard.storeRelaxed(QtGlobalStatic::Initialized); \
- } \
- };
-
-#if defined(Q_OS_UNIX) && defined(Q_CC_INTEL)
-// Work around Intel issue ID 6000058488:
-// local statics inside an inline function inside an anonymous namespace are global
-// symbols (this affects the IA-64 C++ ABI, so OS X and Linux only)
-# define Q_GLOBAL_STATIC_INTERNAL_DECORATION Q_DECL_HIDDEN
-#else
-# define Q_GLOBAL_STATIC_INTERNAL_DECORATION Q_DECL_HIDDEN inline
-#endif
-
-#define Q_GLOBAL_STATIC_INTERNAL(ARGS) \
- Q_GLOBAL_STATIC_INTERNAL_DECORATION Type *innerFunction() \
- { \
- Q_GLOBAL_STATIC_INTERNAL_HOLDER(ARGS) \
- static Holder holder; \
- return &holder.value; \
+template <typename QGS> struct Holder
+{
+ using Type = typename QGS::QGS_Type;
+ using PlainType = std::remove_cv_t<Type>;
+
+ static constexpr bool ConstructionIsNoexcept = noexcept(QGS::innerFunction(nullptr));
+ std::aligned_union_t<1, PlainType> storage;
+ static inline QBasicAtomicInteger<qint8> guard = { QtGlobalStatic::Uninitialized };
+
+ Holder() noexcept(ConstructionIsNoexcept)
+ {
+ QGS::innerFunction(pointer());
+ guard.storeRelaxed(QtGlobalStatic::Initialized);
+ }
+
+ ~Holder()
+ {
+ guard.storeRelaxed(QtGlobalStatic::Destroyed);
+ std::atomic_thread_fence(std::memory_order_acquire); // avoid mixing stores to guard and *pointer()
+ pointer()->~PlainType();
}
-// this class must be POD, unless the compiler supports thread-safe statics
-template <typename T, T *(&innerFunction)(), QBasicAtomicInt &guard>
-struct QGlobalStatic
+ PlainType *pointer() noexcept
+ {
+ return reinterpret_cast<PlainType *>(&storage);
+ }
+
+ Q_DISABLE_COPY_MOVE(Holder)
+};
+}
+
+template <typename Holder> struct QGlobalStatic
{
- typedef T Type;
+ using Type = typename Holder::Type;
- bool isDestroyed() const { return guard.loadRelaxed() <= QtGlobalStatic::Destroyed; }
- bool exists() const { return guard.loadRelaxed() == QtGlobalStatic::Initialized; }
+ bool isDestroyed() const { return guardValue() <= QtGlobalStatic::Destroyed; }
+ bool exists() const { return guardValue() == QtGlobalStatic::Initialized; }
operator Type *()
{
if (isDestroyed())
return nullptr;
- return innerFunction();
+ return instance();
}
Type *operator()()
{
if (isDestroyed())
return nullptr;
- return innerFunction();
+ return instance();
}
Type *operator->()
{
Q_ASSERT_X(!isDestroyed(), "Q_GLOBAL_STATIC",
"The global static was used after being destroyed");
- return innerFunction();
+ return instance();
}
Type &operator*()
{
Q_ASSERT_X(!isDestroyed(), "Q_GLOBAL_STATIC",
"The global static was used after being destroyed");
- return *innerFunction();
+ return *instance();
+ }
+
+protected:
+ static Type *instance() noexcept(Holder::ConstructionIsNoexcept)
+ {
+ static Holder holder;
+ return holder.pointer();
+ }
+ static QtGlobalStatic::GuardValues guardValue()
+ {
+ return QtGlobalStatic::GuardValues(Holder::guard.loadAcquire());
}
};
#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS) \
- QT_WARNING_PUSH \
- QT_WARNING_DISABLE_CLANG("-Wunevaluated-expression") \
- namespace { namespace Q_QGS_ ## NAME { \
- typedef TYPE Type; \
- QBasicAtomicInt guard = Q_BASIC_ATOMIC_INITIALIZER(QtGlobalStatic::Uninitialized); \
- Q_GLOBAL_STATIC_INTERNAL(ARGS) \
- } } \
- static QGlobalStatic<TYPE, \
- Q_QGS_ ## NAME::innerFunction, \
- Q_QGS_ ## NAME::guard> NAME; \
- QT_WARNING_POP
+ namespace { struct Q_QGS_ ## NAME { \
+ typedef TYPE QGS_Type; \
+ static void innerFunction(void *pointer) \
+ noexcept(noexcept(std::remove_cv_t<QGS_Type> ARGS)) \
+ { \
+ new (pointer) QGS_Type ARGS; \
+ } \
+ }; } \
+ static QGlobalStatic<QtGlobalStatic::Holder<Q_QGS_ ## NAME>> NAME; \
+ /**/
#define Q_GLOBAL_STATIC(TYPE, NAME) \
Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ())