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 void clear();
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(std::move(*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
112public:
113 inline QThreadStorage() : d(deleteData) { Trait::warnAboutTrivial(); }
114 inline ~QThreadStorage() { }
115
116 inline bool hasLocalData() const
117 { return d.get() != nullptr; }
118
119 inline T& localData()
120 { return qThreadStorage_localData(d, reinterpret_cast<T*>(0)); }
121 inline T localData() const
122 { return qThreadStorage_localData_const(d, reinterpret_cast<T*>(0)); }
123
124 inline void setLocalData(T t)
125 { qThreadStorage_setLocalData(d, &t); }
126
127 void clearLocalData() { d.clear(); }
128};
129
130#else // !QT_CONFIG(thread)
131
132template <typename T, typename U>
133inline bool qThreadStorage_hasLocalData(const std::unique_ptr<T, U> &data)
134{
135 return !!data;
136}
137
138template <typename T, typename U>
139inline bool qThreadStorage_hasLocalData(const std::unique_ptr<T*, U> &data)
140{
141 return !!data ? *data != nullptr : false;
142}
143
144template <typename T>
146{
147 delete t;
148}
149
150template <typename T>
152{
153 delete *t;
154 delete t;
155}
156
157template <class T>
159{
160private:
161 struct ScopedPointerThreadStorageDeleter
162 {
163 void operator()(T *t) const noexcept
164 {
165 if (t == nullptr)
166 return;
167 qThreadStorage_deleteLocalData(t);
168 }
169 };
170 std::unique_ptr<T, ScopedPointerThreadStorageDeleter> data;
171
172public:
173 QThreadStorage() = default;
174 ~QThreadStorage() = default;
175 QThreadStorage(const QThreadStorage &rhs) = delete;
176 QThreadStorage &operator=(const QThreadStorage &rhs) = delete;
177
178 inline bool hasLocalData() const
179 {
180 return qThreadStorage_hasLocalData(data);
181 }
182
183 inline T &localData()
184 {
185 if (!data)
186 data.reset(new T());
187 return *data;
188 }
189
190 inline T localData() const
191 {
192 return !!data ? *data : T();
193 }
194
195 inline void setLocalData(T t)
196 {
197 data.reset(new T(t));
198 }
199
200 void clearLocalData() { data.reset(); }
201};
202
203#endif // QT_CONFIG(thread)
204
205QT_END_NAMESPACE
206
207#endif // QTHREADSTORAGE_H
\inmodule QtCore
Definition qcoreevent.h:389
\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.
QDisconnectNotifyEvent(int signalIndex)
\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:371
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)