8#include "private/qcoreapplication_p.h"
18#ifdef THREADSTORAGE_DEBUG
19# define DEBUG_MSG qtsDebug
23void qtsDebug(
const char *fmt, ...)
28 fprintf(stderr,
"QThreadStorage: ");
29 vfprintf(stderr, fmt, va);
30 fprintf(stderr,
"\n");
35# define DEBUG_MSG if (false)qDebug
40Q_GLOBAL_STATIC(DestructorMap, destructors)
42QThreadStorageData::QThreadStorageData(
void (*func)(
void *))
44 QMutexLocker locker(&destructorsMutex);
45 DestructorMap *destr = destructors();
48
49
50
51
52
53
54
55
56 QThreadData *data = QThreadData::current();
57 id = data->tls.size();
58 DEBUG_MSG(
"QThreadStorageData: Allocated id %d, destructor %p cannot be stored", id, func);
61 for (id = 0; id < destr->size(); id++) {
62 if (destr->at(id) ==
nullptr)
65 if (id == destr->size()) {
70 DEBUG_MSG(
"QThreadStorageData: Allocated id %d, destructor %p", id, func);
73QThreadStorageData::~QThreadStorageData()
75 DEBUG_MSG(
"QThreadStorageData: Released id %d", id);
76 QMutexLocker locker(&destructorsMutex);
78 (*destructors())[id] =
nullptr;
81void **QThreadStorageData::get()
const
83 QThreadData *data = QThreadData::current();
84 QList<
void *> &tls = data->tls;
89 DEBUG_MSG(
"QThreadStorageData: Returning storage %d, data %p, for thread %p",
92 data->thread.loadRelaxed());
94 return *v ? v :
nullptr;
97void **QThreadStorageData::set(
void *p)
99 QThreadData *data = QThreadData::current();
100 QList<
void *> &tls = data->tls;
101 if (tls.size() <= id)
104 void *&value = tls[id];
106 if (value !=
nullptr) {
107 DEBUG_MSG(
"QThreadStorageData: Deleting previous storage %d, data %p, for thread %p",
110 data->thread.loadRelaxed());
112 QMutexLocker locker(&destructorsMutex);
113 DestructorMap *destr = destructors();
114 void (*destructor)(
void *) = destr ? destr->value(id) :
nullptr;
126 DEBUG_MSG(
"QThreadStorageData: Set storage %d for thread %p to %p", id, data->thread.loadRelaxed(), p);
130void QThreadStoragePrivate::init()
137void QThreadStoragePrivate::finish(QList<
void *> *tls,
bool suppressWarnings)
139 if (tls->isEmpty() || !destructors())
141 if (!QCoreApplication::instanceExists())
142 suppressWarnings =
true;
144 DEBUG_MSG(
"QThreadStorageData: Destroying storage for thread %p", QThread::currentThread());
145 while (!tls->isEmpty()) {
146 void *&value = tls->last();
149 int i = tls->size() - 1;
157 QMutexLocker locker(&destructorsMutex);
158 void (*destructor)(
void *) = destructors()->value(i);
162 if (!suppressWarnings)
163 qWarning(
"QThreadStorage: entry %d destroyed before end of thread %p",
164 i, QThread::currentThread());
169 if (tls->size() > i) {
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
229
230
231
232
235
236
237
238
239
240
241
242
243
244
247
248
249
250
251
252
253
254
255
256
259
260
261
262
263
264
265
266
267
268
271
272
273
274
275
276
277
280
281
282
283
284
285
286
287
288
289
290
static Q_CONSTINIT QBasicMutex destructorsMutex
QList< void(*)(void *)> DestructorMap