1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
42
#include <QtTest/QtTest>
45
#include <qcoreapplication.h>
46
#include <qdatetime.h>
49
#include <qwaitcondition.h>
51
class tst_QMutex : public QObject
56
void lock_unlock_locked_tryLock();
59
void tryLockDeadlock();
60
void tryLockNegative_data();
61
void tryLockNegative();
65
static const int iterations = 100;
67
QAtomicInt lockCount(0);
68
QMutex normalMutex, recursiveMutex(QMutex::Recursive);
70
QSemaphore threadsTurn;
72
enum { waitTime = 100 };
74
void tst_QMutex::tryLock()
76
// test non-recursive mutex
78
class Thread : public QThread
85
// TEST 1: thread can't acquire lock
86
threadsTurn.acquire();
87
QVERIFY(!normalMutex.tryLock());
90
// TEST 2: thread can acquire lock
91
threadsTurn.acquire();
92
QVERIFY(normalMutex.tryLock());
93
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
94
QVERIFY(!normalMutex.tryLock());
95
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
99
// TEST 3: thread can't acquire lock, timeout = waitTime
100
threadsTurn.acquire();
103
QVERIFY(!normalMutex.tryLock(waitTime));
104
QVERIFY(timer.elapsed() >= waitTime);
107
// TEST 4: thread can acquire lock, timeout = waitTime
108
threadsTurn.acquire();
110
QVERIFY(normalMutex.tryLock(waitTime));
111
QVERIFY(timer.elapsed() <= waitTime);
112
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
114
// it's non-recursive, so the following lock needs to fail
115
QVERIFY(!normalMutex.tryLock(waitTime));
116
QVERIFY(timer.elapsed() >= waitTime);
117
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
118
normalMutex.unlock();
121
// TEST 5: thread can't acquire lock, timeout = 0
122
threadsTurn.acquire();
123
QVERIFY(!normalMutex.tryLock(0));
126
// TEST 6: thread can acquire lock, timeout = 0
127
threadsTurn.acquire();
129
QVERIFY(normalMutex.tryLock(0));
130
QVERIFY(timer.elapsed() < waitTime);
131
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
132
QVERIFY(!normalMutex.tryLock(0));
133
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
134
normalMutex.unlock();
137
// TEST 7 overflow: thread can acquire lock, timeout = 3000 (QTBUG-24795)
138
threadsTurn.acquire();
140
QVERIFY(normalMutex.tryLock(3000));
141
QVERIFY(timer.elapsed() < 3000);
142
normalMutex.unlock();
145
threadsTurn.acquire();
152
// TEST 1: thread can't acquire lock
155
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
156
threadsTurn.release();
158
// TEST 2: thread can acquire lock
160
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
161
normalMutex.unlock();
162
threadsTurn.release();
164
// TEST 3: thread can't acquire lock, timeout = waitTime
167
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
168
threadsTurn.release();
170
// TEST 4: thread can acquire lock, timeout = waitTime
172
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
173
normalMutex.unlock();
174
threadsTurn.release();
176
// TEST 5: thread can't acquire lock, timeout = 0
179
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
180
threadsTurn.release();
182
// TEST 6: thread can acquire lock, timeout = 0
184
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
185
normalMutex.unlock();
186
threadsTurn.release();
188
// TEST 7: thread can acquire lock, timeout = 3000 (QTBUG-24795)
191
threadsTurn.release();
192
QThread::msleep(100);
193
normalMutex.unlock();
195
// wait for thread to finish
197
threadsTurn.release();
201
// test recursive mutex
203
class Thread : public QThread
210
threadsTurn.acquire();
211
QVERIFY(!recursiveMutex.tryLock());
214
threadsTurn.acquire();
215
QVERIFY(recursiveMutex.tryLock());
216
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
217
QVERIFY(recursiveMutex.tryLock());
218
QVERIFY(lockCount.testAndSetRelaxed(1, 2));
219
QVERIFY(lockCount.testAndSetRelaxed(2, 1));
220
recursiveMutex.unlock();
221
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
222
recursiveMutex.unlock();
225
threadsTurn.acquire();
228
QVERIFY(!recursiveMutex.tryLock(waitTime));
229
QVERIFY(timer.elapsed() >= waitTime);
230
QVERIFY(!recursiveMutex.tryLock(0));
233
threadsTurn.acquire();
235
QVERIFY(recursiveMutex.tryLock(waitTime));
236
QVERIFY(timer.elapsed() <= waitTime);
237
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
238
QVERIFY(recursiveMutex.tryLock(waitTime));
239
QVERIFY(lockCount.testAndSetRelaxed(1, 2));
240
QVERIFY(lockCount.testAndSetRelaxed(2, 1));
241
recursiveMutex.unlock();
242
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
243
recursiveMutex.unlock();
246
threadsTurn.acquire();
247
QVERIFY(!recursiveMutex.tryLock(0));
248
QVERIFY(!recursiveMutex.tryLock(0));
251
threadsTurn.acquire();
253
QVERIFY(recursiveMutex.tryLock(0));
254
QVERIFY(timer.elapsed() < waitTime);
255
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
256
QVERIFY(recursiveMutex.tryLock(0));
257
QVERIFY(lockCount.testAndSetRelaxed(1, 2));
258
QVERIFY(lockCount.testAndSetRelaxed(2, 1));
259
recursiveMutex.unlock();
260
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
261
recursiveMutex.unlock();
264
threadsTurn.acquire();
271
// thread can't acquire lock
273
recursiveMutex.lock();
274
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
275
recursiveMutex.lock();
276
QVERIFY(lockCount.testAndSetRelaxed(1, 2));
277
threadsTurn.release();
279
// thread can acquire lock
281
QVERIFY(lockCount.testAndSetRelaxed(2, 1));
282
recursiveMutex.unlock();
283
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
284
recursiveMutex.unlock();
285
threadsTurn.release();
287
// thread can't acquire lock, timeout = waitTime
289
recursiveMutex.lock();
290
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
291
recursiveMutex.lock();
292
QVERIFY(lockCount.testAndSetRelaxed(1, 2));
293
threadsTurn.release();
295
// thread can acquire lock, timeout = waitTime
297
QVERIFY(lockCount.testAndSetRelaxed(2, 1));
298
recursiveMutex.unlock();
299
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
300
recursiveMutex.unlock();
301
threadsTurn.release();
303
// thread can't acquire lock, timeout = 0
305
recursiveMutex.lock();
306
QVERIFY(lockCount.testAndSetRelaxed(0, 1));
307
recursiveMutex.lock();
308
QVERIFY(lockCount.testAndSetRelaxed(1, 2));
309
threadsTurn.release();
311
// thread can acquire lock, timeout = 0
313
QVERIFY(lockCount.testAndSetRelaxed(2, 1));
314
recursiveMutex.unlock();
315
QVERIFY(lockCount.testAndSetRelaxed(1, 0));
316
recursiveMutex.unlock();
317
threadsTurn.release();
321
threadsTurn.release();
326
class mutex_Thread : public QThread
334
inline mutex_Thread(QMutex &m) : test_mutex(m) { }
341
for (int i = 0; i < iterations; ++i) {
351
class rmutex_Thread : public QThread
359
inline rmutex_Thread(QMutex &m) : test_mutex(m) { }
369
for (int i = 0; i < iterations; ++i) {
382
void tst_QMutex::lock_unlock_locked_tryLock()
386
mutex_Thread thread(mutex);
388
QMutex rmutex(QMutex::Recursive);
389
rmutex_Thread rthread(rmutex);
391
for (int i = 0; i < iterations; ++i) {
393
QVERIFY(mutex.tryLock());
399
for (int j = 0; j < iterations; ++j) {
400
QVERIFY(thread.cond.wait(&thread.mutex, 10000));
401
QVERIFY(!mutex.tryLock());
403
thread.cond.wakeOne();
406
thread.mutex.unlock();
408
QVERIFY(thread.wait(10000));
409
QVERIFY(mutex.tryLock());
414
QVERIFY(rmutex.tryLock());
415
QVERIFY(rmutex.tryLock());
416
QVERIFY(rmutex.tryLock());
417
QVERIFY(rmutex.tryLock());
424
rthread.mutex.lock();
427
for (int k = 0; k < iterations; ++k) {
428
QVERIFY(rthread.cond.wait(&rthread.mutex, 10000));
429
QVERIFY(!rmutex.tryLock());
431
rthread.cond.wakeOne();
434
rthread.mutex.unlock();
436
QVERIFY(rthread.wait(10000));
437
QVERIFY(rmutex.tryLock());
438
QVERIFY(rmutex.tryLock());
439
QVERIFY(rmutex.tryLock());
440
QVERIFY(rmutex.tryLock());
449
enum { one_minute = 6 * 1000, //not really one minute, but else it is too long.
452
class StressTestThread : public QThread
456
static QBasicAtomicInt lockCount;
457
static QBasicAtomicInt sentinel;
459
static int errorCount;
467
while (t.elapsed() < one_minute) {
469
if (sentinel.ref()) ++errorCount;
470
if (!sentinel.deref()) ++errorCount;
473
if (mutex.tryLock()) {
474
if (sentinel.ref()) ++errorCount;
475
if (!sentinel.deref()) ++errorCount;
482
QMutex StressTestThread::mutex;
483
QBasicAtomicInt StressTestThread::lockCount = Q_BASIC_ATOMIC_INITIALIZER(0);
484
QBasicAtomicInt StressTestThread::sentinel = Q_BASIC_ATOMIC_INITIALIZER(-1);
485
int StressTestThread::errorCount = 0;
487
void tst_QMutex::stressTest()
489
StressTestThread threads[threadCount];
490
for (int i = 0; i < threadCount; ++i)
492
QVERIFY(threads[0].wait(one_minute + 10000));
493
for (int i = 1; i < threadCount; ++i)
494
QVERIFY(threads[i].wait(10000));
495
QCOMPARE(StressTestThread::errorCount, 0);
496
qDebug("locked %d times", int(StressTestThread::lockCount.load()));
499
class TryLockRaceThread : public QThread
511
} while (t.elapsed() < one_minute/2);
514
QMutex TryLockRaceThread::mutex;
516
void tst_QMutex::tryLockRace()
518
// mutex not in use, should be able to lock it
519
QVERIFY(TryLockRaceThread::mutex.tryLock());
520
TryLockRaceThread::mutex.unlock();
522
// try to break tryLock
523
TryLockRaceThread thread[threadCount];
524
for (int i = 0; i < threadCount; ++i)
526
for (int i = 0; i < threadCount; ++i)
527
QVERIFY(thread[i].wait());
529
// mutex not in use, should be able to lock it
530
QVERIFY(TryLockRaceThread::mutex.tryLock());
531
TryLockRaceThread::mutex.unlock();
534
// The following is a regression test for QTBUG-16115, where QMutex could
535
// deadlock after calling tryLock repeatedly.
537
// Variable that will be protected by the mutex. Volatile so that the
538
// the optimiser doesn't mess with it based on the increment-then-decrement
540
static volatile int tryLockDeadlockCounter;
541
// Counter for how many times the protected variable has an incorrect value.
542
static int tryLockDeadlockFailureCount = 0;
544
void tst_QMutex::tryLockDeadlock()
546
//Used to deadlock on unix
547
struct TrylockThread : QThread {
548
TrylockThread(QMutex &mut) : mut(mut) {}
551
for (int i = 0; i < 100000; ++i) {
552
if (mut.tryLock(0)) {
553
if ((++tryLockDeadlockCounter) != 1)
554
++tryLockDeadlockFailureCount;
555
if ((--tryLockDeadlockCounter) != 0)
556
++tryLockDeadlockFailureCount;
563
TrylockThread t1(mut);
564
TrylockThread t2(mut);
565
TrylockThread t3(mut);
570
for (int i = 0; i < 100000; ++i) {
572
if ((++tryLockDeadlockCounter) != 1)
573
++tryLockDeadlockFailureCount;
574
if ((--tryLockDeadlockCounter) != 0)
575
++tryLockDeadlockFailureCount;
581
QCOMPARE(tryLockDeadlockFailureCount, 0);
584
void tst_QMutex::tryLockNegative_data()
586
QTest::addColumn<int>("timeout");
587
QTest::newRow("-1") << -1;
588
QTest::newRow("-2") << -2;
589
QTest::newRow("INT_MIN/2") << INT_MIN/2;
590
QTest::newRow("INT_MIN") << INT_MIN;
593
void tst_QMutex::tryLockNegative()
595
// the documentation says tryLock() with a negative number is the same as lock()
596
struct TrylockThread : QThread {
597
TrylockThread(QMutex &mut, int timeout)
598
: mut(mut), timeout(timeout), tryLockResult(-1)
604
tryLockResult = mut.tryLock(timeout);
609
QFETCH(int, timeout);
612
TrylockThread thr(mutex, timeout);
613
QSignalSpy spy(&thr, SIGNAL(started()));
617
// the thread should have stopped in tryLock(), waiting for us to unlock
618
// the mutex. The following test can be falsely positive due to timing:
619
// tryLock may still fail but hasn't failed yet. But it certainly cannot be
620
// a false negative: if wait() returns, tryLock failed.
621
QVERIFY(!thr.wait(200));
623
// after we unlock the mutex, the thread should succeed in locking, then
624
// unlock and exit. Do this before more tests to avoid deadlocking due to
625
// ~QThread waiting forever on a thread that won't exit.
628
QCOMPARE(spy.count(), 1);
630
QCOMPARE(thr.tryLockResult, 1);
634
class MoreStressTestThread : public QThread
638
static QAtomicInt lockCount;
639
static QAtomicInt sentinel[threadCount];
640
static QMutex mutex[threadCount];
641
static QAtomicInt errorCount;
650
while (t.elapsed() < one_minute) {
652
uint nb = (i * 9 + lockCount.load() * 13) % threadCount;
653
QMutexLocker locker(&mutex[nb]);
654
if (sentinel[nb].load()) errorCount.ref();
655
if (sentinel[nb].fetchAndAddRelaxed(5)) errorCount.ref();
656
if (!sentinel[nb].testAndSetRelaxed(5, 0)) errorCount.ref();
657
if (sentinel[nb].load()) errorCount.ref();
659
nb = (nb * 17 + i * 5 + lockCount.load() * 3) % threadCount;
660
if (mutex[nb].tryLock()) {
661
if (sentinel[nb].load()) errorCount.ref();
662
if (sentinel[nb].fetchAndAddRelaxed(16)) errorCount.ref();
663
if (!sentinel[nb].testAndSetRelaxed(16, 0)) errorCount.ref();
664
if (sentinel[nb].load()) errorCount.ref();
668
nb = (nb * 15 + i * 47 + lockCount.load() * 31) % threadCount;
669
if (mutex[nb].tryLock(2)) {
670
if (sentinel[nb].load()) errorCount.ref();
671
if (sentinel[nb].fetchAndAddRelaxed(53)) errorCount.ref();
672
if (!sentinel[nb].testAndSetRelaxed(53, 0)) errorCount.ref();
673
if (sentinel[nb].load()) errorCount.ref();
680
QMutex MoreStressTestThread::mutex[threadCount];
681
QAtomicInt MoreStressTestThread::lockCount;
682
QAtomicInt MoreStressTestThread::sentinel[threadCount];
683
QAtomicInt MoreStressTestThread::errorCount = 0;
685
void tst_QMutex::moreStress()
687
MoreStressTestThread threads[threadCount];
688
for (int i = 0; i < threadCount; ++i)
690
QVERIFY(threads[0].wait(one_minute + 10000));
691
for (int i = 1; i < threadCount; ++i)
692
QVERIFY(threads[i].wait(10000));
693
qDebug("locked %d times", MoreStressTestThread::lockCount.load());
694
QCOMPARE(MoreStressTestThread::errorCount.load(), 0);
698
QTEST_MAIN(tst_QMutex)
699
#include "tst_qmutex.moc"