diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2012-12-21 11:41:30 -0800 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-12-23 20:34:46 +0100 |
commit | 3adbcb58d590c48e5799bce7ded0157c26460f6c (patch) | |
tree | 2ce7eec2db115875fa451a9ece57c9625f00f6df /tests | |
parent | 96d06124a1da7e2bbd8bda2fc8e762cb46af8204 (diff) |
Fix QMutex::tryLock with negative values
The Linux futex implementation had a Q_ASSERT for positive values, but
the documentation says that negative values should be interpreted as
infinite (equal to lock()).
Test that too.
Change-Id: I2f96a502d672732781e88e49797756ca9a809121
Reviewed-by: David Faure (KDE) <faure@kde.org>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/thread/qmutex/tst_qmutex.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp b/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp index 7e3e720739..e51f8c9a23 100644 --- a/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp +++ b/tests/auto/corelib/thread/qmutex/tst_qmutex.cpp @@ -57,6 +57,8 @@ private slots: void stressTest(); void tryLockRace(); void tryLockDeadlock(); + void tryLockNegative_data(); + void tryLockNegative(); void moreStress(); }; @@ -579,6 +581,55 @@ void tst_QMutex::tryLockDeadlock() QCOMPARE(tryLockDeadlockFailureCount, 0); } +void tst_QMutex::tryLockNegative_data() +{ + QTest::addColumn<int>("timeout"); + QTest::newRow("-1") << -1; + QTest::newRow("-2") << -2; + QTest::newRow("INT_MIN/2") << INT_MIN/2; + QTest::newRow("INT_MIN") << INT_MIN; +} + +void tst_QMutex::tryLockNegative() +{ + // the documentation says tryLock() with a negative number is the same as lock() + struct TrylockThread : QThread { + TrylockThread(QMutex &mut, int timeout) + : mut(mut), timeout(timeout), tryLockResult(-1) + {} + QMutex &mut; + int timeout; + int tryLockResult; + void run() { + tryLockResult = mut.tryLock(timeout); + mut.unlock(); + } + }; + + QFETCH(int, timeout); + + QMutex mutex; + TrylockThread thr(mutex, timeout); + QSignalSpy spy(&thr, SIGNAL(started())); + mutex.lock(); + thr.start(); + + // the thread should have stopped in tryLock(), waiting for us to unlock + // the mutex. The following test can be falsely positive due to timing: + // tryLock may still fail but hasn't failed yet. But it certainly cannot be + // a false negative: if wait() returns, tryLock failed. + QVERIFY(!thr.wait(200)); + + // after we unlock the mutex, the thread should succeed in locking, then + // unlock and exit. Do this before more tests to avoid deadlocking due to + // ~QThread waiting forever on a thread that won't exit. + mutex.unlock(); + + QCOMPARE(spy.count(), 1); + QVERIFY(thr.wait()); + QCOMPARE(thr.tryLockResult, 1); +} + class MoreStressTestThread : public QThread { |