Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qthreadstorage.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QTHREADSTORAGE_H
6#define QTHREADSTORAGE_H
7
8#include <QtCore/qglobal.h>
9
10#if !QT_CONFIG(thread)
11#include <memory>
12#endif
13
15
16#if QT_CONFIG(thread)
17
18template <bool ShouldWarn> struct QThreadStorageTraits
19{
20 static constexpr void warnAboutTrivial() {}
21};
22template <> struct QThreadStorageTraits<true>
23{
24#ifndef Q_NO_THREAD_STORAGE_TRIVIAL_WARNING
25 Q_DECL_DEPRECATED_X("QThreadStorage used with a trivial non-pointer type; consider using thread_local")
26#endif
27 static constexpr void warnAboutTrivial() noexcept {}
28};
29
30class Q_CORE_EXPORT QThreadStorageData
31{
32public:
33 using DeleterFn = void (*)(void *);
34 explicit QThreadStorageData(DeleterFn func);
35 ~QThreadStorageData();
36
37 void** get() const;
38 void** set(void* p);
39
40 int id;
41};
42
43// pointer specialization
44template <typename T>
45inline
46T *&qThreadStorage_localData(QThreadStorageData &d, T **)
47{
48 void **v = d.get();
49 if (!v) v = d.set(nullptr);
50 return *(reinterpret_cast<T**>(v));
51}
52
53template <typename T>
54inline
55T *qThreadStorage_localData_const(const QThreadStorageData &d, T **)
56{
57 void **v = d.get();
58 return v ? *(reinterpret_cast<T**>(v)) : 0;
59}
60
61template <typename T>
62inline
63void qThreadStorage_setLocalData(QThreadStorageData &d, T **t)
64{ (void) d.set(*t); }
65
66template <typename T>
67inline
68void qThreadStorage_deleteData(void *d, T **)
69{ delete static_cast<T *>(d); }
70
71// value-based specialization
72template <typename T>
73inline
74T &qThreadStorage_localData(QThreadStorageData &d, T *)
75{
76 void **v = d.get();
77 if (!v) v = d.set(new T());
78 return *(reinterpret_cast<T*>(*v));
79}
80
81template <typename T>
82inline
83T qThreadStorage_localData_const(const QThreadStorageData &d, T *)
84{
85 void **v = d.get();
86 return v ? *(reinterpret_cast<T*>(*v)) : T();
87}
88
89template <typename T>
90inline
91void qThreadStorage_setLocalData(QThreadStorageData &d, T *t)
92{ (void) d.set(new T(*t)); }
93
94template <typename T>
95inline
96void qThreadStorage_deleteData(void *d, T *)
97{ delete static_cast<T *>(d); }
98
99template <class T>
100class QThreadStorage
101{
102private:
103 using Trait = QThreadStorageTraits<std::is_trivially_default_constructible_v<T> &&
104 std::is_trivially_copyable_v<T> && !std::is_pointer_v<T>>;
105 QThreadStorageData d;
106
107 Q_DISABLE_COPY(QThreadStorage)
108
109 static inline void deleteData(void *x)
110 { qThreadStorage_deleteData(x, reinterpret_cast<T*>(0)); }
111
112 static inline QThreadStorageData::DeleterFn deleter()
113 {
114 if constexpr (std::is_trivially_destructible_v<T> && !std::is_pointer_v<T>)
115 return nullptr;
116 else
117 return deleteData;
118 }
119
120public:
121 inline QThreadStorage() : d(deleter()) { Trait::warnAboutTrivial(); }
122 inline ~QThreadStorage() { }
123
124 inline bool hasLocalData() const
125 { return d.get() != nullptr; }
126
127 inline T& localData()
128 { return qThreadStorage_localData(d, reinterpret_cast<T*>(0)); }
129 inline T localData() const
130 { return qThreadStorage_localData_const(d, reinterpret_cast<T*>(0)); }
131
132 inline void setLocalData(T t)
133 { qThreadStorage_setLocalData(d, &t); }
134};
135
136#else // !QT_CONFIG(thread)
137
138template <typename T, typename U>
139inline bool qThreadStorage_hasLocalData(const std::unique_ptr<T, U> &data)
140{
141 return !!data;
142}
143
144template <typename T, typename U>
145inline bool qThreadStorage_hasLocalData(const std::unique_ptr<T*, U> &data)
146{
147 return !!data ? *data != nullptr : false;
148}
149
150template <typename T>
152{
153 delete t;
154}
155
156template <typename T>
158{
159 delete *t;
160 delete t;
161}
162
163template <class T>
165{
166private:
167 struct ScopedPointerThreadStorageDeleter
168 {
169 void operator()(T *t) const noexcept
170 {
171 if (t == nullptr)
172 return;
173 qThreadStorage_deleteLocalData(t);
174 }
175 };
176 std::unique_ptr<T, ScopedPointerThreadStorageDeleter> data;
177
178public:
179 QThreadStorage() = default;
180 ~QThreadStorage() = default;
181 QThreadStorage(const QThreadStorage &rhs) = delete;
182 QThreadStorage &operator=(const QThreadStorage &rhs) = delete;
183
184 inline bool hasLocalData() const
185 {
186 return qThreadStorage_hasLocalData(data);
187 }
188
189 inline T &localData()
190 {
191 if (!data)
192 data.reset(new T());
193 return *data;
194 }
195
196 inline T localData() const
197 {
198 return !!data ? *data : T();
199 }
200
201 inline void setLocalData(T t)
202 {
203 data.reset(new T(t));
204 }
205};
206
207#endif // QT_CONFIG(thread)
208
209QT_END_NAMESPACE
210
211#endif // QTHREADSTORAGE_H
\inmodule QtCore
Definition qcoreevent.h:391
\inmodule QtCore
int scopeLevel() const
int loopLevel() const
QDeferredDeleteEvent(int loopLevel, int scopeLevel)
Constructs a deferred delete event with the given loop and scope level.
\inmodule QtCore
Definition qeventloop.h:60
Q_CORE_EXPORT ~QEventLoopLocker()
Destroys this event loop locker object.
\inmodule QtCore
Definition qeventloop.h:17
\inmodule QtCore
Definition qcoreevent.h:50
\inmodule QtCore
void setLocalData(T t)
Sets the local data for the calling thread to data.
~QThreadStorage()=default
Destroys the per-thread data storage object.
QThreadStorage()=default
Constructs a new per-thread data storage object.
T localData() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
T & localData()
Returns a reference to the data that was set by the calling thread.
QThreadStorage(const QThreadStorage &rhs)=delete
bool hasLocalData() const
If T is a pointer type, returns true if the calling thread has non-zero data available.
QThreadStorage & operator=(const QThreadStorage &rhs)=delete
\inmodule QtCore
Definition qcoreevent.h:373
Combined button and popup list for selecting options.
#define __has_include(x)
QString qAppName()
Q_TRACE_POINT(qtcore, QCoreApplication_notify_exit, bool consumed, bool filtered)
void QT_MANGLE_NAMESPACE qt_startup_hook()
static QString qAppFileName()
QList< QtStartUpFunction > QStartUpFuncList
Q_TRACE_METADATA(qtcore, "ENUM { AUTO, RANGE User ... MaxUser } QEvent::Type;")
QString qtTrId(const char *id, int n)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_exit)
static void qt_call_pre_routines()
qsizetype qGlobalPostedEventsCount()
static Q_CONSTINIT bool preRoutinesCalled
QList< QtCleanUpFunction > QVFuncList
static void replacePercentN(QString *result, int n)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_event_compressed, QObject *receiver, QEvent *event)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_entry, QObject *receiver, QEvent *event, QEvent::Type type)
Q_TRACE_PREFIX(qtcore, "#include <qcoreevent.h>")
Q_CORE_EXPORT void qAddPostRoutine(QtCleanUpFunction)
void(* QtCleanUpFunction)()
void(* QtStartUpFunction)()
Q_CORE_EXPORT void qAddPreRoutine(QtStartUpFunction)
Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction)
#define qApp
#define Q_EVENT_DISABLE_COPY(Class)
Definition qcoreevent.h:17
#define Q_DECL_EVENT_COMMON(Class)
Definition qcoreevent.h:24
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
void qThreadStorage_deleteLocalData(T *t)
bool qThreadStorage_hasLocalData(const std::unique_ptr< T *, U > &data)
void qThreadStorage_deleteLocalData(T **t)