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
11QT_BEGIN_NAMESPACE
12
13
14class Q_CORE_EXPORT QThreadStorageData
15{
16public:
17 explicit QThreadStorageData(void (*func)(void *));
18 ~QThreadStorageData();
19
20 void** get() const;
21 void** set(void* p);
22
23 static void finish(void**);
24 int id;
25};
26
27#if !defined(QT_MOC_CPP)
28// MOC_SKIP_BEGIN
29
30// pointer specialization
31template <typename T>
32inline
33T *&qThreadStorage_localData(QThreadStorageData &d, T **)
34{
35 void **v = d.get();
36 if (!v) v = d.set(nullptr);
37 return *(reinterpret_cast<T**>(v));
38}
39
40template <typename T>
41inline
42T *qThreadStorage_localData_const(const QThreadStorageData &d, T **)
43{
44 void **v = d.get();
45 return v ? *(reinterpret_cast<T**>(v)) : 0;
46}
47
48template <typename T>
49inline
50void qThreadStorage_setLocalData(QThreadStorageData &d, T **t)
51{ (void) d.set(*t); }
52
53template <typename T>
54inline
55void qThreadStorage_deleteData(void *d, T **)
56{ delete static_cast<T *>(d); }
57
58// value-based specialization
59template <typename T>
60inline
61T &qThreadStorage_localData(QThreadStorageData &d, T *)
62{
63 void **v = d.get();
64 if (!v) v = d.set(new T());
65 return *(reinterpret_cast<T*>(*v));
66}
67
68template <typename T>
69inline
70T qThreadStorage_localData_const(const QThreadStorageData &d, T *)
71{
72 void **v = d.get();
73 return v ? *(reinterpret_cast<T*>(*v)) : T();
74}
75
76template <typename T>
77inline
78void qThreadStorage_setLocalData(QThreadStorageData &d, T *t)
79{ (void) d.set(new T(*t)); }
80
81template <typename T>
82inline
83void qThreadStorage_deleteData(void *d, T *)
84{ delete static_cast<T *>(d); }
85
86
87// MOC_SKIP_END
88#endif
89
90template <class T>
91class QThreadStorage
92{
93private:
94 QThreadStorageData d;
95
96 Q_DISABLE_COPY(QThreadStorage)
97
98 static inline void deleteData(void *x)
99 { qThreadStorage_deleteData(x, reinterpret_cast<T*>(0)); }
100
101public:
102 inline QThreadStorage() : d(deleteData) { }
103 inline ~QThreadStorage() { }
104
105 inline bool hasLocalData() const
106 { return d.get() != nullptr; }
107
108 inline T& localData()
109 { return qThreadStorage_localData(d, reinterpret_cast<T*>(0)); }
110 inline T localData() const
111 { return qThreadStorage_localData_const(d, reinterpret_cast<T*>(0)); }
112
113 inline void setLocalData(T t)
114 { qThreadStorage_setLocalData(d, &t); }
115};
116
117QT_END_NAMESPACE
118
119#else // !QT_CONFIG(thread)
120
121#include <QtCore/qscopedpointer.h>
122
123#include <type_traits>
124
125QT_BEGIN_NAMESPACE
126
127template <typename T, typename U>
128inline bool qThreadStorage_hasLocalData(const QScopedPointer<T, U> &data)
129{
130 return !!data;
131}
132
133template <typename T, typename U>
134inline bool qThreadStorage_hasLocalData(const QScopedPointer<T*, U> &data)
135{
136 return !!data ? *data != nullptr : false;
137}
138
139template <typename T>
140inline void qThreadStorage_deleteLocalData(T *t)
141{
142 delete t;
143}
144
145template <typename T>
146inline void qThreadStorage_deleteLocalData(T **t)
147{
148 delete *t;
149 delete t;
150}
151
152template <class T>
153class QThreadStorage
154{
155private:
156 struct ScopedPointerThreadStorageDeleter
157 {
158 static inline void cleanup(T *t)
159 {
160 if (t == nullptr)
161 return;
162 qThreadStorage_deleteLocalData(t);
163 }
164 };
165 QScopedPointer<T, ScopedPointerThreadStorageDeleter> data;
166
167public:
168 QThreadStorage() = default;
169 ~QThreadStorage() = default;
170 QThreadStorage(const QThreadStorage &rhs) = delete;
171 QThreadStorage &operator=(const QThreadStorage &rhs) = delete;
172
173 inline bool hasLocalData() const
174 {
175 return qThreadStorage_hasLocalData(data);
176 }
177
178 inline T &localData()
179 {
180 if (!data)
181 data.reset(new T());
182 return *data;
183 }
184
185 inline T localData() const
186 {
187 return !!data ? *data : T();
188 }
189
190 inline void setLocalData(T t)
191 {
192 data.reset(new T(t));
193 }
194};
195
196QT_END_NAMESPACE
197
198#endif // QT_CONFIG(thread)
199
200#endif // QTHREADSTORAGE_H
201

source code of qtbase/src/corelib/thread/qthreadstorage.h