author | eckhart.koppen@nokia.com |
Wed, 31 Mar 2010 11:06:36 +0300 | |
changeset 7 | f7bc934e204c |
parent 3 | 41300fa6a67c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qthreadstorage.h" |
|
43 |
||
44 |
#ifndef QT_NO_THREAD |
|
45 |
#include "qthread.h" |
|
46 |
#include "qthread_p.h" |
|
47 |
#include "qmutex.h" |
|
48 |
||
49 |
#include <string.h> |
|
50 |
||
51 |
QT_BEGIN_NAMESPACE |
|
52 |
||
53 |
// #define THREADSTORAGE_DEBUG |
|
54 |
#ifdef THREADSTORAGE_DEBUG |
|
55 |
# define DEBUG_MSG qtsDebug |
|
56 |
||
57 |
# include <stdio.h> |
|
58 |
# include <stdarg.h> |
|
59 |
void qtsDebug(const char *fmt, ...) |
|
60 |
{ |
|
61 |
va_list va; |
|
62 |
va_start(va, fmt); |
|
63 |
||
64 |
fprintf(stderr, "QThreadStorage: "); |
|
65 |
vfprintf(stderr, fmt, va); |
|
66 |
fprintf(stderr, "\n"); |
|
67 |
||
68 |
va_end(va); |
|
69 |
} |
|
70 |
#else |
|
71 |
# define DEBUG_MSG if(false)qDebug |
|
72 |
#endif |
|
73 |
||
74 |
Q_GLOBAL_STATIC(QMutex, mutex) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
75 |
typedef QVector<void (*)(void *)> DestructorMap; |
0 | 76 |
Q_GLOBAL_STATIC(DestructorMap, destructors) |
77 |
||
78 |
QThreadStorageData::QThreadStorageData(void (*func)(void *)) |
|
79 |
{ |
|
80 |
QMutexLocker locker(mutex()); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
81 |
DestructorMap *destr = destructors(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
82 |
for (id = 0; id < destr->count(); id++) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
if (destr->at(id) == 0) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
break; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
85 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
86 |
if (id == destr->count()) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
87 |
destr->append(func); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
88 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
89 |
(*destr)[id] = func; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
90 |
} |
0 | 91 |
DEBUG_MSG("QThreadStorageData: Allocated id %d, destructor %p", id, func); |
92 |
} |
|
93 |
||
94 |
QThreadStorageData::~QThreadStorageData() |
|
95 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
96 |
DEBUG_MSG("QThreadStorageData: Released id %d", id); |
0 | 97 |
QMutexLocker locker(mutex()); |
98 |
if (destructors()) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
99 |
(*destructors())[id] = 0; |
0 | 100 |
} |
101 |
||
102 |
void **QThreadStorageData::get() const |
|
103 |
{ |
|
104 |
QThreadData *data = QThreadData::current(); |
|
105 |
if (!data) { |
|
106 |
qWarning("QThreadStorage::get: QThreadStorage can only be used with threads started with QThread"); |
|
107 |
return 0; |
|
108 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
109 |
QVector<void *> &tls = data->tls; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
110 |
if (tls.size() <= id) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
111 |
tls.resize(id + 1); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
112 |
void **v = &tls[id]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
113 |
|
0 | 114 |
DEBUG_MSG("QThreadStorageData: Returning storage %d, data %p, for thread %p", |
115 |
id, |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
116 |
*v, |
0 | 117 |
data->thread); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
118 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
119 |
return *v ? v : 0; |
0 | 120 |
} |
121 |
||
122 |
void **QThreadStorageData::set(void *p) |
|
123 |
{ |
|
124 |
QThreadData *data = QThreadData::current(); |
|
125 |
if (!data) { |
|
126 |
qWarning("QThreadStorage::set: QThreadStorage can only be used with threads started with QThread"); |
|
127 |
return 0; |
|
128 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
129 |
QVector<void *> &tls = data->tls; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
130 |
if (tls.size() <= id) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
131 |
tls.resize(id + 1); |
0 | 132 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
133 |
void *&value = tls[id]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
134 |
// delete any previous data |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
135 |
if (value != 0) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
136 |
DEBUG_MSG("QThreadStorageData: Deleting previous storage %d, data %p, for thread %p", |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
137 |
id, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
138 |
value, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
139 |
data->thread); |
0 | 140 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
141 |
QMutexLocker locker(mutex()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
142 |
void (*destructor)(void *) = destructors()->value(id); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
143 |
locker.unlock(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
144 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
145 |
void *q = value; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
146 |
value = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
147 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
148 |
destructor(q); |
0 | 149 |
} |
150 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
151 |
// store new data |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
152 |
value = p; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
153 |
DEBUG_MSG("QThreadStorageData: Set storage %d for thread %p to %p", id, data->thread, p); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
154 |
return &value; |
0 | 155 |
} |
156 |
||
157 |
void QThreadStorageData::finish(void **p) |
|
158 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
QVector<void *> *tls = reinterpret_cast<QVector<void *> *>(p); |
0 | 160 |
if (!tls || tls->isEmpty() || !mutex()) |
161 |
return; // nothing to do |
|
162 |
||
163 |
DEBUG_MSG("QThreadStorageData: Destroying storage for thread %p", QThread::currentThread()); |
|
164 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
165 |
for(int i = tls->size() - 1; i >= 0; i--) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
void *&value = (*tls)[i]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
void *q = value; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
value = 0; |
0 | 169 |
|
170 |
if (!q) { |
|
171 |
// data already deleted |
|
172 |
continue; |
|
173 |
} |
|
174 |
||
175 |
QMutexLocker locker(mutex()); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
176 |
void (*destructor)(void *) = destructors()->value(i); |
0 | 177 |
locker.unlock(); |
178 |
||
179 |
if (!destructor) { |
|
180 |
if (QThread::currentThread()) |
|
181 |
qWarning("QThreadStorage: Thread %p exited after QThreadStorage %d destroyed", |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
182 |
QThread::currentThread(), i); |
0 | 183 |
continue; |
184 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
185 |
destructor(q); //crash here might mean the thread exited after qthreadstorage was destroyed |
0 | 186 |
} |
187 |
tls->clear(); |
|
188 |
} |
|
189 |
||
190 |
/*! |
|
191 |
\class QThreadStorage |
|
192 |
\brief The QThreadStorage class provides per-thread data storage. |
|
193 |
||
194 |
\threadsafe |
|
195 |
||
196 |
\ingroup thread |
|
197 |
||
198 |
QThreadStorage is a template class that provides per-thread data |
|
199 |
storage. |
|
200 |
||
201 |
\e{Note that due to compiler limitations, QThreadStorage can only |
|
202 |
store pointers.} |
|
203 |
||
204 |
The setLocalData() function stores a single thread-specific value |
|
205 |
for the calling thread. The data can be accessed later using |
|
206 |
localData(). QThreadStorage takes ownership of the data (which |
|
207 |
must be created on the heap with \c new) and deletes it when the |
|
208 |
thread exits, either normally or via termination. |
|
209 |
||
210 |
The hasLocalData() function allows the programmer to determine if |
|
211 |
data has previously been set using the setLocalData() function. |
|
212 |
This is also useful for lazy initializiation. |
|
213 |
||
214 |
For example, the following code uses QThreadStorage to store a |
|
215 |
single cache for each thread that calls the cacheObject() and |
|
216 |
removeFromCache() functions. The cache is automatically |
|
217 |
deleted when the calling thread exits. |
|
218 |
||
219 |
\snippet doc/src/snippets/threads/threads.cpp 7 |
|
220 |
\snippet doc/src/snippets/threads/threads.cpp 8 |
|
221 |
\snippet doc/src/snippets/threads/threads.cpp 9 |
|
222 |
||
223 |
\section1 Caveats |
|
224 |
||
225 |
\list |
|
226 |
||
227 |
\o As noted above, QThreadStorage can only store pointers due to |
|
228 |
compiler limitations. |
|
229 |
||
230 |
\o The QThreadStorage destructor does not delete per-thread data. |
|
231 |
QThreadStorage only deletes per-thread data when the thread exits |
|
232 |
or when setLocalData() is called multiple times. |
|
233 |
||
234 |
\o QThreadStorage can be used to store data for the \c main() |
|
235 |
thread. QThreadStorage deletes all data set for the \c main() |
|
236 |
thread when QApplication is destroyed, regardless of whether or |
|
237 |
not the \c main() thread has actually finished. |
|
238 |
||
239 |
\endlist |
|
240 |
||
241 |
\sa QThread |
|
242 |
*/ |
|
243 |
||
244 |
/*! |
|
245 |
\fn QThreadStorage::QThreadStorage() |
|
246 |
||
247 |
Constructs a new per-thread data storage object. |
|
248 |
*/ |
|
249 |
||
250 |
/*! |
|
251 |
\fn QThreadStorage::~QThreadStorage() |
|
252 |
||
253 |
Destroys the per-thread data storage object. |
|
254 |
||
255 |
Note: The per-thread data stored is not deleted. Any data left |
|
256 |
in QThreadStorage is leaked. Make sure that all threads using |
|
257 |
QThreadStorage have exited before deleting the QThreadStorage. |
|
258 |
||
259 |
\sa hasLocalData() |
|
260 |
*/ |
|
261 |
||
262 |
/*! |
|
263 |
\fn bool QThreadStorage::hasLocalData() const |
|
264 |
||
265 |
Returns true if the calling thread has non-zero data available; |
|
266 |
otherwise returns false. |
|
267 |
||
268 |
\sa localData() |
|
269 |
*/ |
|
270 |
||
271 |
/*! |
|
272 |
\fn T &QThreadStorage::localData() |
|
273 |
||
274 |
Returns a reference to the data that was set by the calling |
|
275 |
thread. |
|
276 |
||
277 |
Note: QThreadStorage can only store pointers. This function |
|
278 |
returns a reference to the pointer that was set by the calling |
|
279 |
thread. The value of this reference is 0 if no data was set by |
|
280 |
the calling thread, |
|
281 |
||
282 |
\sa hasLocalData() |
|
283 |
*/ |
|
284 |
||
285 |
/*! |
|
286 |
\fn const T QThreadStorage::localData() const |
|
287 |
\overload |
|
288 |
||
289 |
Returns a copy of the data that was set by the calling thread. |
|
290 |
||
291 |
Note: QThreadStorage can only store pointers. This function |
|
292 |
returns a pointer to the data that was set by the calling thread. |
|
293 |
If no data was set by the calling thread, this function returns 0. |
|
294 |
||
295 |
\sa hasLocalData() |
|
296 |
*/ |
|
297 |
||
298 |
/*! |
|
299 |
\fn void QThreadStorage::setLocalData(T data) |
|
300 |
||
301 |
Sets the local data for the calling thread to \a data. It can be |
|
302 |
accessed later using the localData() functions. |
|
303 |
||
304 |
If \a data is 0, this function deletes the previous data (if |
|
305 |
any) and returns immediately. |
|
306 |
||
307 |
If \a data is non-zero, QThreadStorage takes ownership of the \a |
|
308 |
data and deletes it automatically either when the thread exits |
|
309 |
(either normally or via termination) or when setLocalData() is |
|
310 |
called again. |
|
311 |
||
312 |
Note: QThreadStorage can only store pointers. The \a data |
|
313 |
argument must be either a pointer to an object created on the heap |
|
314 |
(i.e. using \c new) or 0. You should not delete \a data |
|
315 |
yourself; QThreadStorage takes ownership and will delete the \a |
|
316 |
data itself. |
|
317 |
||
318 |
\sa localData(), hasLocalData() |
|
319 |
*/ |
|
320 |
||
321 |
#endif // QT_NO_THREAD |
|
322 |
||
323 |
QT_END_NAMESPACE |