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 explicit QThreadStorageData(void (*func)(void *));
34 ~QThreadStorageData();
35
36 void** get() const;
37 void** set(void* p);
38
39 int id;
40};
41
42// pointer specialization
43template <typename T>
44inline
45T *&qThreadStorage_localData(QThreadStorageData &d, T **)
46{
47 void **v = d.get();
48 if (!v) v = d.set(nullptr);
49 return *(reinterpret_cast<T**>(v));
50}
51
52template <typename T>
53inline
54T *qThreadStorage_localData_const(const QThreadStorageData &d, T **)
55{
56 void **v = d.get();
57 return v ? *(reinterpret_cast<T**>(v)) : 0;
58}
59
60template <typename T>
61inline
62void qThreadStorage_setLocalData(QThreadStorageData &d, T **t)
63{ (void) d.set(*t); }
64
65template <typename T>
66inline
67void qThreadStorage_deleteData(void *d, T **)
68{ delete static_cast<T *>(d); }
69
70// value-based specialization
71template <typename T>
72inline
73T &qThreadStorage_localData(QThreadStorageData &d, T *)
74{
75 void **v = d.get();
76 if (!v) v = d.set(new T());
77 return *(reinterpret_cast<T*>(*v));
78}
79
80template <typename T>
81inline
82T qThreadStorage_localData_const(const QThreadStorageData &d, T *)
83{
84 void **v = d.get();
85 return v ? *(reinterpret_cast<T*>(*v)) : T();
86}
87
88template <typename T>
89inline
90void qThreadStorage_setLocalData(QThreadStorageData &d, T *t)
91{ (void) d.set(new T(*t)); }
92
93template <typename T>
94inline
95void qThreadStorage_deleteData(void *d, T *)
96{ delete static_cast<T *>(d); }
97
98template <class T>
99class QThreadStorage
100{
101private:
102 using Trait = QThreadStorageTraits<std::is_trivially_default_constructible_v<T> &&
103 std::is_trivially_copyable_v<T> && !std::is_pointer_v<T>>;
104 QThreadStorageData d;
105
106 Q_DISABLE_COPY(QThreadStorage)
107
108 static inline void deleteData(void *x)
109 { qThreadStorage_deleteData(x, reinterpret_cast<T*>(0)); }
110
111public:
112 inline QThreadStorage() : d(deleteData) { Trait::warnAboutTrivial(); }
113 inline ~QThreadStorage() { }
114
115 inline bool hasLocalData() const
116 { return d.get() != nullptr; }
117
118 inline T& localData()
119 { return qThreadStorage_localData(d, reinterpret_cast<T*>(0)); }
120 inline T localData() const
121 { return qThreadStorage_localData_const(d, reinterpret_cast<T*>(0)); }
122
123 inline void setLocalData(T t)
124 { qThreadStorage_setLocalData(d, &t); }
125};
126
127#else // !QT_CONFIG(thread)
128
129template <typename T, typename U>
130inline bool qThreadStorage_hasLocalData(const std::unique_ptr<T, U> &data)
131{
132 return !!data;
133}
134
135template <typename T, typename U>
136inline bool qThreadStorage_hasLocalData(const std::unique_ptr<T*, U> &data)
137{
138 return !!data ? *data != nullptr : false;
139}
140
141template <typename T>
143{
144 delete t;
145}
146
147template <typename T>
149{
150 delete *t;
151 delete t;
152}
153
154template <class T>
156{
157private:
158 struct ScopedPointerThreadStorageDeleter
159 {
160 void operator()(T *t) const noexcept
161 {
162 if (t == nullptr)
163 return;
164 qThreadStorage_deleteLocalData(t);
165 }
166 };
167 std::unique_ptr<T, ScopedPointerThreadStorageDeleter> data;
168
169public:
170 QThreadStorage() = default;
171 ~QThreadStorage() = default;
172 QThreadStorage(const QThreadStorage &rhs) = delete;
173 QThreadStorage &operator=(const QThreadStorage &rhs) = delete;
174
175 inline bool hasLocalData() const
176 {
177 return qThreadStorage_hasLocalData(data);
178 }
179
180 inline T &localData()
181 {
182 if (!data)
183 data.reset(new T());
184 return *data;
185 }
186
187 inline T localData() const
188 {
189 return !!data ? *data : T();
190 }
191
192 inline void setLocalData(T t)
193 {
194 data.reset(new T(t));
195 }
196};
197
198#endif // QT_CONFIG(thread)
199
200QT_END_NAMESPACE
201
202#endif // QTHREADSTORAGE_H
\inmodule QtCore
Definition qcoreevent.h:390
\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:59
Q_CORE_EXPORT ~QEventLoopLocker()
Destroys this event loop locker object.
\inmodule QtCore
Definition qeventloop.h:16
\inmodule QtCore
Definition qcoreevent.h:49
\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:372
#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)
QString qTrId(const char *id, int n)
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:16
#define Q_DECL_EVENT_COMMON(Class)
Definition qcoreevent.h:23
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)