|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
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> |
|
46 #include <QMutexLocker> |
|
47 #include <QSemaphore> |
|
48 #include <QThread> |
|
49 |
|
50 //TESTED_CLASS= |
|
51 //TESTED_FILES= |
|
52 |
|
53 class tst_QMutexLockerThread : public QThread |
|
54 { |
|
55 public: |
|
56 QMutex mutex; |
|
57 QSemaphore semaphore, testSemaphore; |
|
58 |
|
59 void waitForTest() |
|
60 { |
|
61 semaphore.release(); |
|
62 testSemaphore.acquire(); |
|
63 } |
|
64 |
|
65 tst_QMutexLockerThread() |
|
66 : mutex(QMutex::Recursive) |
|
67 { |
|
68 } |
|
69 }; |
|
70 |
|
71 class tst_QMutexLocker : public QObject |
|
72 { |
|
73 Q_OBJECT |
|
74 |
|
75 public: |
|
76 tst_QMutexLocker(); |
|
77 ~tst_QMutexLocker(); |
|
78 |
|
79 tst_QMutexLockerThread *thread; |
|
80 |
|
81 void waitForThread() |
|
82 { |
|
83 thread->semaphore.acquire(); |
|
84 } |
|
85 void releaseThread() |
|
86 { |
|
87 thread->testSemaphore.release(); |
|
88 } |
|
89 |
|
90 private slots: |
|
91 void scopeTest(); |
|
92 void unlockAndRelockTest(); |
|
93 void lockerStateTest(); |
|
94 }; |
|
95 |
|
96 tst_QMutexLocker::tst_QMutexLocker() |
|
97 { |
|
98 } |
|
99 |
|
100 tst_QMutexLocker::~tst_QMutexLocker() |
|
101 { |
|
102 } |
|
103 |
|
104 void tst_QMutexLocker::scopeTest() |
|
105 { |
|
106 class ScopeTestThread : public tst_QMutexLockerThread |
|
107 { |
|
108 public: |
|
109 void run() |
|
110 { |
|
111 waitForTest(); |
|
112 |
|
113 { |
|
114 QMutexLocker locker(&mutex); |
|
115 waitForTest(); |
|
116 } |
|
117 |
|
118 waitForTest(); |
|
119 } |
|
120 }; |
|
121 |
|
122 thread = new ScopeTestThread; |
|
123 thread->start(); |
|
124 |
|
125 waitForThread(); |
|
126 // mutex should be unlocked before entering the scope that creates the QMutexLocker |
|
127 QVERIFY(thread->mutex.tryLock()); |
|
128 thread->mutex.unlock(); |
|
129 releaseThread(); |
|
130 |
|
131 waitForThread(); |
|
132 // mutex should be locked by the QMutexLocker |
|
133 QVERIFY(!thread->mutex.tryLock()); |
|
134 releaseThread(); |
|
135 |
|
136 waitForThread(); |
|
137 // mutex should be unlocked when the QMutexLocker goes out of scope |
|
138 QVERIFY(thread->mutex.tryLock()); |
|
139 thread->mutex.unlock(); |
|
140 releaseThread(); |
|
141 |
|
142 QVERIFY(thread->wait()); |
|
143 |
|
144 delete thread; |
|
145 thread = 0; |
|
146 } |
|
147 |
|
148 |
|
149 void tst_QMutexLocker::unlockAndRelockTest() |
|
150 { |
|
151 class UnlockAndRelockThread : public tst_QMutexLockerThread |
|
152 { |
|
153 public: |
|
154 void run() |
|
155 { |
|
156 QMutexLocker locker(&mutex); |
|
157 |
|
158 waitForTest(); |
|
159 |
|
160 locker.unlock(); |
|
161 |
|
162 waitForTest(); |
|
163 |
|
164 locker.relock(); |
|
165 |
|
166 waitForTest(); |
|
167 } |
|
168 }; |
|
169 |
|
170 thread = new UnlockAndRelockThread; |
|
171 thread->start(); |
|
172 |
|
173 waitForThread(); |
|
174 // mutex should be locked by the QMutexLocker |
|
175 QVERIFY(!thread->mutex.tryLock()); |
|
176 releaseThread(); |
|
177 |
|
178 waitForThread(); |
|
179 // mutex has been explicitly unlocked via QMutexLocker |
|
180 QVERIFY(thread->mutex.tryLock()); |
|
181 thread->mutex.unlock(); |
|
182 releaseThread(); |
|
183 |
|
184 waitForThread(); |
|
185 // mutex has been explicity relocked via QMutexLocker |
|
186 QVERIFY(!thread->mutex.tryLock()); |
|
187 releaseThread(); |
|
188 |
|
189 QVERIFY(thread->wait()); |
|
190 |
|
191 delete thread; |
|
192 thread = 0; |
|
193 } |
|
194 |
|
195 void tst_QMutexLocker::lockerStateTest() |
|
196 { |
|
197 class LockerStateThread : public tst_QMutexLockerThread |
|
198 { |
|
199 public: |
|
200 void run() |
|
201 { |
|
202 { |
|
203 QMutexLocker locker(&mutex); |
|
204 locker.relock(); |
|
205 locker.unlock(); |
|
206 |
|
207 waitForTest(); |
|
208 } |
|
209 |
|
210 waitForTest(); |
|
211 } |
|
212 }; |
|
213 |
|
214 thread = new LockerStateThread; |
|
215 thread->start(); |
|
216 |
|
217 waitForThread(); |
|
218 // even though we relock() after creating the QMutexLocker, it shouldn't lock the mutex more than once |
|
219 QVERIFY(thread->mutex.tryLock()); |
|
220 thread->mutex.unlock(); |
|
221 releaseThread(); |
|
222 |
|
223 waitForThread(); |
|
224 // if we call QMutexLocker::unlock(), its destructor should do nothing |
|
225 QVERIFY(thread->mutex.tryLock()); |
|
226 thread->mutex.unlock(); |
|
227 releaseThread(); |
|
228 |
|
229 QVERIFY(thread->wait()); |
|
230 |
|
231 delete thread; |
|
232 thread = 0; |
|
233 } |
|
234 |
|
235 QTEST_MAIN(tst_QMutexLocker) |
|
236 #include "tst_qmutexlocker.moc" |