1// Copyright (C) 2021 Intel Corporation.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QGLOBALSTATIC_H
5#define QGLOBALSTATIC_H
6
7#include <QtCore/qassert.h>
8#include <QtCore/qatomic.h>
9#include <QtCore/qtclasshelpermacros.h>
10
11#include <atomic> // for bootstrapped (no thread) builds
12#include <type_traits>
13
14QT_BEGIN_NAMESPACE
15
16namespace QtGlobalStatic {
17enum GuardValues {
18 Destroyed = -2,
19 Initialized = -1,
20 Uninitialized = 0,
21 Initializing = 1
22};
23
24template <typename QGS> union Holder
25{
26 using Type = typename QGS::QGS_Type;
27 using PlainType = std::remove_cv_t<Type>;
28
29 static constexpr bool ConstructionIsNoexcept = noexcept(QGS::innerFunction(nullptr));
30 Q_CONSTINIT static inline QBasicAtomicInteger<qint8> guard = { QtGlobalStatic::Uninitialized };
31
32 // union's sole member
33 PlainType storage;
34
35 Holder() noexcept(ConstructionIsNoexcept)
36 {
37 QGS::innerFunction(pointer());
38 guard.storeRelaxed(newValue: QtGlobalStatic::Initialized);
39 }
40
41 ~Holder()
42 {
43 pointer()->~PlainType();
44 std::atomic_thread_fence(m: std::memory_order_acquire); // avoid mixing stores to guard and *pointer()
45 guard.storeRelaxed(newValue: QtGlobalStatic::Destroyed);
46 }
47
48 PlainType *pointer() noexcept
49 {
50 return &storage;
51 }
52
53 Q_DISABLE_COPY_MOVE(Holder)
54};
55}
56
57template <typename Holder> struct QGlobalStatic
58{
59 using Type = typename Holder::Type;
60
61 bool isDestroyed() const noexcept { return guardValue() <= QtGlobalStatic::Destroyed; }
62 bool exists() const noexcept { return guardValue() == QtGlobalStatic::Initialized; }
63 operator Type *()
64 {
65 if (isDestroyed())
66 return nullptr;
67 return instance();
68 }
69 Type *operator()()
70 {
71 if (isDestroyed())
72 return nullptr;
73 return instance();
74 }
75 Type *operator->()
76 {
77 Q_ASSERT_X(!isDestroyed(), Q_FUNC_INFO,
78 "The global static was used after being destroyed");
79 return instance();
80 }
81 Type &operator*()
82 {
83 Q_ASSERT_X(!isDestroyed(), Q_FUNC_INFO,
84 "The global static was used after being destroyed");
85 return *instance();
86 }
87
88protected:
89 static Type *instance() noexcept(Holder::ConstructionIsNoexcept)
90 {
91 static Holder holder;
92 return holder.pointer();
93 }
94 static QtGlobalStatic::GuardValues guardValue() noexcept
95 {
96 return QtGlobalStatic::GuardValues(Holder::guard.loadAcquire());
97 }
98};
99
100#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS) \
101 QT_WARNING_PUSH \
102 QT_WARNING_DISABLE_CLANG("-Wunevaluated-expression") \
103 namespace { struct Q_QGS_ ## NAME { \
104 typedef TYPE QGS_Type; \
105 static void innerFunction(void *pointer) \
106 noexcept(noexcept(std::remove_cv_t<QGS_Type> ARGS)) \
107 { \
108 new (pointer) QGS_Type ARGS; \
109 } \
110 }; } \
111 Q_CONSTINIT static QGlobalStatic<QtGlobalStatic::Holder<Q_QGS_ ## NAME>> NAME; \
112 QT_WARNING_POP
113 /**/
114
115#define Q_GLOBAL_STATIC(TYPE, NAME, ...) \
116 Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, (__VA_ARGS__))
117
118QT_END_NAMESPACE
119#endif // QGLOBALSTATIC_H
120

source code of qtbase/src/corelib/global/qglobalstatic.h