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
4#ifndef QTHREADSTORAGE_H
5#define QTHREADSTORAGE_H
6
7#include <QtCore/qglobal.h>
8
9#if !QT_CONFIG(thread)
10#include <memory>
11#endif
12
14
15#if QT_CONFIG(thread)
16
17template <bool ShouldWarn> struct QThreadStorageTraits
18{
19 static constexpr void warnAboutTrivial() {}
20};
21template <> struct QThreadStorageTraits<true>
22{
23#ifndef Q_NO_THREAD_STORAGE_TRIVIAL_WARNING
24 Q_DECL_DEPRECATED_X("QThreadStorage used with a trivial non-pointer type; consider using thread_local")
25#endif
26 static constexpr void warnAboutTrivial() noexcept {}
27};
28
29class Q_CORE_EXPORT QThreadStorageData
30{
31public:
32 explicit QThreadStorageData(void (*func)(void *));
33 ~QThreadStorageData();
34
35 void** get() const;
36 void** set(void* p);
37
38 static void finish(void**);
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_trivial_v<T> && !std::is_pointer_v<T>>;
103 QThreadStorageData d;
104
105 Q_DISABLE_COPY(QThreadStorage)
106
107 static inline void deleteData(void *x)
108 { qThreadStorage_deleteData(x, reinterpret_cast<T*>(0)); }
109
110public:
111 inline QThreadStorage() : d(deleteData) { Trait::warnAboutTrivial(); }
112 inline ~QThreadStorage() { }
113
114 inline bool hasLocalData() const
115 { return d.get() != nullptr; }
116
117 inline T& localData()
118 { return qThreadStorage_localData(d, reinterpret_cast<T*>(0)); }
119 inline T localData() const
120 { return qThreadStorage_localData_const(d, reinterpret_cast<T*>(0)); }
121
122 inline void setLocalData(T t)
123 { qThreadStorage_setLocalData(d, &t); }
124};
125
126#else // !QT_CONFIG(thread)
127
128template <typename T, typename U>
129inline bool qThreadStorage_hasLocalData(const std::unique_ptr<T, U> &data)
130{
131 return !!data;
132}
133
134template <typename T, typename U>
135inline bool qThreadStorage_hasLocalData(const std::unique_ptr<T*, U> &data)
136{
137 return !!data ? *data != nullptr : false;
138}
139
140template <typename T>
142{
143 delete t;
144}
145
146template <typename T>
148{
149 delete *t;
150 delete t;
151}
152
153template <class T>
155{
156private:
157 struct ScopedPointerThreadStorageDeleter
158 {
159 void operator()(T *t) const noexcept
160 {
161 if (t == nullptr)
162 return;
163 qThreadStorage_deleteLocalData(t);
164 }
165 };
166 std::unique_ptr<T, ScopedPointerThreadStorageDeleter> data;
167
168public:
169 QThreadStorage() = default;
170 ~QThreadStorage() = default;
171 QThreadStorage(const QThreadStorage &rhs) = delete;
172 QThreadStorage &operator=(const QThreadStorage &rhs) = delete;
173
174 inline bool hasLocalData() const
175 {
176 return qThreadStorage_hasLocalData(data);
177 }
178
179 inline T &localData()
180 {
181 if (!data)
182 data.reset(new T());
183 return *data;
184 }
185
186 inline T localData() const
187 {
188 return !!data ? *data : T();
189 }
190
191 inline void setLocalData(T t)
192 {
193 data.reset(new T(t));
194 }
195};
196
197#endif // QT_CONFIG(thread)
198
199QT_END_NAMESPACE
200
201#endif // QTHREADSTORAGE_H
\inmodule QtCore
Definition qcoreevent.h:387
\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:46
\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:369
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)
static QString qAppFileName()
QList< QtStartUpFunction > QStartUpFuncList
void qt_startup_hook()
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:14
#define Q_DECL_EVENT_COMMON(Class)
Definition qcoreevent.h:21
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)