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 // TSAN does not support atomic_thread_fence and GCC complains:
44 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97868
45 // https://github.com/google/sanitizers/issues/1352
46 // QTBUG-134415
47QT_WARNING_PUSH
48#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU >= 1100
49QT_WARNING_DISABLE_GCC("-Wtsan")
50#endif
51 // import changes to *pointer() by other threads before running ~PlainType():
52 std::atomic_thread_fence(m: std::memory_order_acquire);
53QT_WARNING_POP
54 pointer()->~PlainType();
55 guard.storeRelease(newValue: QtGlobalStatic::Destroyed);
56 }
57
58 PlainType *pointer() noexcept
59 {
60 return &storage;
61 }
62
63 Q_DISABLE_COPY_MOVE(Holder)
64};
65}
66
67template <typename Holder> struct QGlobalStatic
68{
69 using Type = typename Holder::Type;
70
71 bool isDestroyed() const noexcept { return guardValue() <= QtGlobalStatic::Destroyed; }
72 bool exists() const noexcept { return guardValue() == QtGlobalStatic::Initialized; }
73 operator Type *()
74 {
75 if (isDestroyed())
76 return nullptr;
77 return instance();
78 }
79 Type *operator()()
80 {
81 if (isDestroyed())
82 return nullptr;
83 return instance();
84 }
85 Type *operator->()
86 {
87 Q_ASSERT_X(!isDestroyed(), Q_FUNC_INFO,
88 "The global static was used after being destroyed");
89 return instance();
90 }
91 Type &operator*()
92 {
93 Q_ASSERT_X(!isDestroyed(), Q_FUNC_INFO,
94 "The global static was used after being destroyed");
95 return *instance();
96 }
97
98protected:
99 static Type *instance() noexcept(Holder::ConstructionIsNoexcept)
100 {
101 static Holder holder;
102 return holder.pointer();
103 }
104 static QtGlobalStatic::GuardValues guardValue() noexcept
105 {
106 return QtGlobalStatic::GuardValues(Holder::guard.loadAcquire());
107 }
108};
109
110#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS) \
111 QT_WARNING_PUSH \
112 QT_WARNING_DISABLE_CLANG("-Wunevaluated-expression") \
113 namespace { struct Q_QGS_ ## NAME { \
114 typedef TYPE QGS_Type; \
115 static void innerFunction(void *pointer) \
116 noexcept(noexcept(std::remove_cv_t<QGS_Type> ARGS)) \
117 { \
118 new (pointer) QGS_Type ARGS; \
119 } \
120 }; } \
121 Q_CONSTINIT static QGlobalStatic<QtGlobalStatic::Holder<Q_QGS_ ## NAME>> NAME; \
122 QT_WARNING_POP
123 /**/
124
125#define Q_GLOBAL_STATIC(TYPE, NAME, ...) \
126 Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, (__VA_ARGS__))
127
128QT_END_NAMESPACE
129#endif // QGLOBALSTATIC_H
130

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