author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 22 Apr 2010 16:15:11 +0300 | |
branch | RCL_3 |
changeset 14 | 8c4229025c0b |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
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 test suite 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 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
||
45 |
#include <qcoreapplication.h> |
|
46 |
#include <qmutex.h> |
|
47 |
#include <qthread.h> |
|
48 |
#include <qwaitcondition.h> |
|
49 |
#include <qthreadstorage.h> |
|
50 |
||
51 |
#ifdef Q_OS_UNIX |
|
52 |
#include <pthread.h> |
|
53 |
#endif |
|
54 |
#ifdef Q_OS_WIN |
|
55 |
#ifndef Q_OS_WINCE |
|
56 |
#include <process.h> |
|
57 |
#endif |
|
58 |
#include <windows.h> |
|
59 |
#endif |
|
60 |
||
61 |
//TESTED_CLASS= |
|
62 |
//TESTED_FILES= |
|
63 |
||
64 |
class tst_QThreadStorage : public QObject |
|
65 |
{ |
|
66 |
Q_OBJECT |
|
67 |
||
68 |
public: |
|
69 |
tst_QThreadStorage(); |
|
70 |
||
71 |
private slots: |
|
72 |
void hasLocalData(); |
|
73 |
void localData(); |
|
74 |
void localData_const(); |
|
75 |
void setLocalData(); |
|
76 |
void autoDelete(); |
|
77 |
void adoptedThreads(); |
|
78 |
void ensureCleanupOrder(); |
|
79 |
}; |
|
80 |
||
81 |
class Pointer |
|
82 |
{ |
|
83 |
public: |
|
84 |
static int count; |
|
85 |
inline Pointer() { ++count; } |
|
86 |
inline ~Pointer() { --count; } |
|
87 |
}; |
|
88 |
int Pointer::count = 0; |
|
89 |
||
90 |
tst_QThreadStorage::tst_QThreadStorage() |
|
91 |
||
92 |
{ } |
|
93 |
||
94 |
void tst_QThreadStorage::hasLocalData() |
|
95 |
{ |
|
96 |
QThreadStorage<Pointer *> pointers; |
|
97 |
QVERIFY(!pointers.hasLocalData()); |
|
98 |
pointers.setLocalData(new Pointer); |
|
99 |
QVERIFY(pointers.hasLocalData()); |
|
100 |
pointers.setLocalData(0); |
|
101 |
QVERIFY(!pointers.hasLocalData()); |
|
102 |
} |
|
103 |
||
104 |
void tst_QThreadStorage::localData() |
|
105 |
{ |
|
106 |
QThreadStorage<Pointer*> pointers; |
|
107 |
Pointer *p = new Pointer; |
|
108 |
QVERIFY(!pointers.hasLocalData()); |
|
109 |
pointers.setLocalData(p); |
|
110 |
QVERIFY(pointers.hasLocalData()); |
|
111 |
QCOMPARE(pointers.localData(), p); |
|
112 |
pointers.setLocalData(0); |
|
113 |
QCOMPARE(pointers.localData(), (Pointer *)0); |
|
114 |
QVERIFY(!pointers.hasLocalData()); |
|
115 |
} |
|
116 |
||
117 |
void tst_QThreadStorage::localData_const() |
|
118 |
{ |
|
119 |
QThreadStorage<Pointer *> pointers; |
|
120 |
const QThreadStorage<Pointer *> &const_pointers = pointers; |
|
121 |
Pointer *p = new Pointer; |
|
122 |
QVERIFY(!pointers.hasLocalData()); |
|
123 |
pointers.setLocalData(p); |
|
124 |
QVERIFY(pointers.hasLocalData()); |
|
125 |
QCOMPARE(const_pointers.localData(), p); |
|
126 |
pointers.setLocalData(0); |
|
127 |
QCOMPARE(const_pointers.localData(), (Pointer *)0); |
|
128 |
QVERIFY(!pointers.hasLocalData()); |
|
129 |
} |
|
130 |
||
131 |
void tst_QThreadStorage::setLocalData() |
|
132 |
{ |
|
133 |
QThreadStorage<Pointer *> pointers; |
|
134 |
QVERIFY(!pointers.hasLocalData()); |
|
135 |
pointers.setLocalData(new Pointer); |
|
136 |
QVERIFY(pointers.hasLocalData()); |
|
137 |
pointers.setLocalData(0); |
|
138 |
QVERIFY(!pointers.hasLocalData()); |
|
139 |
} |
|
140 |
||
141 |
class Thread : public QThread |
|
142 |
{ |
|
143 |
public: |
|
144 |
QThreadStorage<Pointer *> &pointers; |
|
145 |
||
146 |
QMutex mutex; |
|
147 |
QWaitCondition cond; |
|
148 |
||
149 |
Thread(QThreadStorage<Pointer *> &p) |
|
150 |
: pointers(p) |
|
151 |
{ } |
|
152 |
||
153 |
void run() |
|
154 |
{ |
|
155 |
pointers.setLocalData(new Pointer); |
|
156 |
||
157 |
QMutexLocker locker(&mutex); |
|
158 |
cond.wakeOne(); |
|
159 |
cond.wait(&mutex); |
|
160 |
} |
|
161 |
}; |
|
162 |
||
163 |
void tst_QThreadStorage::autoDelete() |
|
164 |
{ |
|
165 |
QThreadStorage<Pointer *> pointers; |
|
166 |
QVERIFY(!pointers.hasLocalData()); |
|
167 |
||
168 |
Thread thread(pointers); |
|
169 |
int c = Pointer::count; |
|
170 |
{ |
|
171 |
QMutexLocker locker(&thread.mutex); |
|
172 |
thread.start(); |
|
173 |
thread.cond.wait(&thread.mutex); |
|
174 |
// QCOMPARE(Pointer::count, c + 1); |
|
175 |
thread.cond.wakeOne(); |
|
176 |
} |
|
177 |
thread.wait(); |
|
178 |
QCOMPARE(Pointer::count, c); |
|
179 |
} |
|
180 |
||
181 |
bool threadStorageOk; |
|
182 |
void testAdoptedThreadStorageWin(void *p) |
|
183 |
{ |
|
184 |
QThreadStorage<Pointer *> *pointers = reinterpret_cast<QThreadStorage<Pointer *> *>(p); |
|
185 |
if (pointers->hasLocalData()) { |
|
186 |
threadStorageOk = false; |
|
187 |
return; |
|
188 |
} |
|
189 |
||
190 |
Pointer *pointer = new Pointer(); |
|
191 |
pointers->setLocalData(pointer); |
|
192 |
||
193 |
if (pointers->hasLocalData() == false) { |
|
194 |
threadStorageOk = false; |
|
195 |
return; |
|
196 |
} |
|
197 |
||
198 |
if (pointers->localData() != pointer) { |
|
199 |
threadStorageOk = false; |
|
200 |
return; |
|
201 |
} |
|
202 |
QObject::connect(QThread::currentThread(), SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); |
|
203 |
} |
|
204 |
void *testAdoptedThreadStorageUnix(void *pointers) |
|
205 |
{ |
|
206 |
testAdoptedThreadStorageWin(pointers); |
|
207 |
return 0; |
|
208 |
} |
|
209 |
void tst_QThreadStorage::adoptedThreads() |
|
210 |
{ |
|
211 |
QTestEventLoop::instance(); // Make sure the instance is created in this thread. |
|
212 |
QThreadStorage<Pointer *> pointers; |
|
213 |
int c = Pointer::count; |
|
214 |
threadStorageOk = true; |
|
215 |
{ |
|
216 |
#ifdef Q_OS_UNIX |
|
217 |
pthread_t thread; |
|
218 |
const int state = pthread_create(&thread, 0, testAdoptedThreadStorageUnix, &pointers); |
|
219 |
QCOMPARE(state, 0); |
|
220 |
pthread_join(thread, 0); |
|
221 |
#elif defined Q_OS_WIN |
|
222 |
HANDLE thread; |
|
223 |
#if defined(Q_OS_WINCE) |
|
224 |
thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)testAdoptedThreadStorageWin, &pointers, 0, NULL); |
|
225 |
#else |
|
226 |
thread = (HANDLE)_beginthread(testAdoptedThreadStorageWin, 0, &pointers); |
|
227 |
#endif |
|
228 |
QVERIFY(thread); |
|
229 |
WaitForSingleObject(thread, INFINITE); |
|
230 |
#endif |
|
231 |
} |
|
232 |
QVERIFY(threadStorageOk); |
|
233 |
||
234 |
QTestEventLoop::instance().enterLoop(2); |
|
235 |
QVERIFY(!QTestEventLoop::instance().timeout()); |
|
236 |
||
237 |
QCOMPARE(Pointer::count, c); |
|
238 |
} |
|
239 |
||
240 |
QBasicAtomicInt cleanupOrder = Q_BASIC_ATOMIC_INITIALIZER(0); |
|
241 |
||
242 |
class First |
|
243 |
{ |
|
244 |
public: |
|
245 |
~First() |
|
246 |
{ |
|
247 |
order = cleanupOrder.fetchAndAddRelaxed(1); |
|
248 |
} |
|
249 |
static int order; |
|
250 |
}; |
|
251 |
int First::order = -1; |
|
252 |
||
253 |
class Second |
|
254 |
{ |
|
255 |
public: |
|
256 |
~Second() |
|
257 |
{ |
|
258 |
order = cleanupOrder.fetchAndAddRelaxed(1); |
|
259 |
} |
|
260 |
static int order; |
|
261 |
}; |
|
262 |
int Second::order = -1; |
|
263 |
||
264 |
void tst_QThreadStorage::ensureCleanupOrder() |
|
265 |
{ |
|
266 |
class Thread : public QThread |
|
267 |
{ |
|
268 |
public: |
|
269 |
QThreadStorage<First *> &first; |
|
270 |
QThreadStorage<Second *> &second; |
|
271 |
||
272 |
Thread(QThreadStorage<First *> &first, |
|
273 |
QThreadStorage<Second *> &second) |
|
274 |
: first(first), second(second) |
|
275 |
{ } |
|
276 |
||
277 |
void run() |
|
278 |
{ |
|
279 |
// set in reverse order, but shouldn't matter, the data |
|
280 |
// will be deleted in the order the thread storage objects |
|
281 |
// were created |
|
282 |
second.setLocalData(new Second); |
|
283 |
first.setLocalData(new First); |
|
284 |
} |
|
285 |
}; |
|
286 |
||
287 |
QThreadStorage<Second *> second; |
|
288 |
QThreadStorage<First *> first; |
|
289 |
Thread thread(first, second); |
|
290 |
thread.start(); |
|
291 |
thread.wait(); |
|
292 |
||
293 |
QVERIFY(First::order < Second::order); |
|
294 |
} |
|
295 |
||
296 |
QTEST_MAIN(tst_QThreadStorage) |
|
297 |
#include "tst_qthreadstorage.moc" |