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

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