summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/corelib/thread/qwaitcondition/tst_qwaitcondition.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/benchmarks/corelib/thread/qwaitcondition/tst_qwaitcondition.cpp')
-rw-r--r--tests/benchmarks/corelib/thread/qwaitcondition/tst_qwaitcondition.cpp210
1 files changed, 210 insertions, 0 deletions
diff --git a/tests/benchmarks/corelib/thread/qwaitcondition/tst_qwaitcondition.cpp b/tests/benchmarks/corelib/thread/qwaitcondition/tst_qwaitcondition.cpp
new file mode 100644
index 0000000000..1bfc637402
--- /dev/null
+++ b/tests/benchmarks/corelib/thread/qwaitcondition/tst_qwaitcondition.cpp
@@ -0,0 +1,210 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtCore/QtCore>
+#include <QtTest/QtTest>
+
+#include <math.h>
+
+
+class tst_QWaitCondition : public QObject
+{
+ Q_OBJECT
+
+public:
+ tst_QWaitCondition()
+ {
+ }
+
+private slots:
+ void oscillate_data();
+ void oscillate();
+
+ void thrash_data();
+ void thrash();
+
+public:
+ static QWaitCondition local, remote;
+ enum Turn {LocalTurn, RemoteTurn};
+ static Turn turn;
+};
+
+QWaitCondition tst_QWaitCondition::local;
+QWaitCondition tst_QWaitCondition::remote;
+tst_QWaitCondition::Turn tst_QWaitCondition::turn = tst_QWaitCondition::LocalTurn;
+
+class OscillateThread : public QThread
+{
+public:
+ bool m_done;
+ bool m_useMutex;
+ unsigned long m_timeout;
+ bool m_wakeOne;
+ int count;
+
+ OscillateThread(bool useMutex, unsigned long timeout, bool wakeOne)
+ : m_done(false), m_useMutex(useMutex), m_timeout(timeout), m_wakeOne(wakeOne)
+ {}
+ void run()
+ {
+ QMutex mtx;
+ QReadWriteLock rwl;
+ count = 0;
+
+ forever {
+ if (m_done)
+ break;
+ if (m_useMutex) {
+ mtx.lock();
+ while (tst_QWaitCondition::turn == tst_QWaitCondition::LocalTurn)
+ tst_QWaitCondition::remote.wait(&mtx, m_timeout);
+ mtx.unlock();
+ } else {
+ rwl.lockForWrite();
+ while (tst_QWaitCondition::turn == tst_QWaitCondition::LocalTurn)
+ tst_QWaitCondition::remote.wait(&rwl, m_timeout);
+ rwl.unlock();
+ }
+ tst_QWaitCondition::turn = tst_QWaitCondition::LocalTurn;
+ if (m_wakeOne)
+ tst_QWaitCondition::local.wakeOne();
+ else
+ tst_QWaitCondition::local.wakeAll();
+ count++;
+ }
+ }
+};
+
+void tst_QWaitCondition::oscillate_data()
+{
+ QTest::addColumn<bool>("useMutex");
+ QTest::addColumn<unsigned long>("timeout");
+ QTest::addColumn<bool>("wakeOne");
+
+ QTest::newRow("mutex, timeout, one") << true << 1000ul << true;
+ QTest::newRow("readWriteLock, timeout, one") << false << 1000ul << true;
+ QTest::newRow("mutex, timeout, all") << true << 1000ul << false;
+ QTest::newRow("readWriteLock, timeout, all") << false << 1000ul << false;
+ QTest::newRow("mutex, forever, one") << true << ULONG_MAX << true;
+ QTest::newRow("readWriteLock, forever, one") << false << ULONG_MAX << true;
+ QTest::newRow("mutex, forever, all") << true << ULONG_MAX << false;
+ QTest::newRow("readWriteLock, forever, all") << false << ULONG_MAX << false;
+}
+
+void tst_QWaitCondition::oscillate()
+{
+ QMutex mtx;
+ QReadWriteLock rwl;
+
+ QFETCH(bool, useMutex);
+ QFETCH(unsigned long, timeout);
+ QFETCH(bool, wakeOne);
+
+ turn = LocalTurn;
+ OscillateThread thrd(useMutex, timeout, wakeOne);
+ thrd.start();
+
+ QBENCHMARK {
+ if (useMutex)
+ mtx.lock();
+ else
+ rwl.lockForWrite();
+ turn = RemoteTurn;
+ if (wakeOne)
+ remote.wakeOne();
+ else
+ remote.wakeAll();
+ if (useMutex) {
+ while (turn == RemoteTurn)
+ local.wait(&mtx, timeout);
+ mtx.unlock();
+ } else {
+ while (turn == RemoteTurn)
+ local.wait(&rwl, timeout);
+ rwl.unlock();
+ }
+ }
+
+ thrd.m_done = true;
+ remote.wakeAll();
+ thrd.wait();
+
+ QCOMPARE(0, 0);
+}
+
+void tst_QWaitCondition::thrash_data()
+{
+ oscillate_data();
+}
+
+void tst_QWaitCondition::thrash()
+{
+ QMutex mtx;
+ mtx.lock();
+
+ QFETCH(bool, useMutex);
+ QFETCH(unsigned long, timeout);
+ QFETCH(bool, wakeOne);
+
+ turn = LocalTurn;
+ OscillateThread thrd(useMutex, timeout, wakeOne);
+ thrd.start();
+ local.wait(&mtx, 1000ul);
+ mtx.unlock();
+
+ QBENCHMARK {
+ turn = RemoteTurn;
+ if (wakeOne)
+ remote.wakeOne();
+ else
+ remote.wakeAll();
+ }
+
+ thrd.m_done = true;
+ turn = RemoteTurn;
+ remote.wakeAll();
+ thrd.wait();
+
+ QCOMPARE(0, 0);
+}
+
+QTEST_MAIN(tst_QWaitCondition)
+#include "tst_qwaitcondition.moc"