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* *ptr = &tls[id];
109 if (*ptr !=
nullptr) {
110 DEBUG_MSG(
"QThreadStorageData: Deleting previous storage %d, data %p, for thread %p",
113 data->thread.loadRelaxed());
115 QMutexLocker locker(&destructorsMutex);
116 DestructorMap *destr = destructors();
117 void (*destructor)(
void *) = destr ? destr->value(id) :
nullptr;
121 void *q = std::exchange(*ptr,
nullptr);
130 DEBUG_MSG(
"QThreadStorageData: Set storage %d for thread %p to %p", id, data->thread.loadRelaxed(), p);
134void QThreadStoragePrivate::init()
141void QThreadStoragePrivate::finish(QList<
void *> *tls,
bool suppressWarnings)
143 if (tls->isEmpty() || !destructors())
145 if (!QCoreApplication::instanceExists())
146 suppressWarnings =
true;
148 DEBUG_MSG(
"QThreadStorageData: Destroying storage for thread %p", QThread::currentThread());
149 while (!tls->isEmpty()) {
150 void *&value = tls->last();
153 int i = tls->size() - 1;
161 QMutexLocker locker(&destructorsMutex);
162 void (*destructor)(
void *) = destructors()->value(i);
166 if (!suppressWarnings)
167 qWarning(
"QThreadStorage: entry %d destroyed before end of thread %p",
168 i, QThread::currentThread());
173 if (tls->size() > i) {
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
227
228
229
230
231
232
233
234
237
238
239
240
243
244
245
246
247
248
249
250
251
252
255
256
257
258
259
260
261
262
263
264
267
268
269
270
271
272
273
274
275
276
279
280
281
282
283
284
285
288
289
290
291
292
293
294
295
296
297
298
static Q_CONSTINIT QBasicMutex destructorsMutex
QList< void(*)(void *)> DestructorMap