summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/corelib/thread/qmutex
diff options
context:
space:
mode:
Diffstat (limited to 'tests/benchmarks/corelib/thread/qmutex')
-rw-r--r--tests/benchmarks/corelib/thread/qmutex/CMakeLists.txt13
-rw-r--r--tests/benchmarks/corelib/thread/qmutex/qmutex.pro6
-rw-r--r--tests/benchmarks/corelib/thread/qmutex/tst_bench_qmutex.cpp (renamed from tests/benchmarks/corelib/thread/qmutex/tst_qmutex.cpp)94
3 files changed, 45 insertions, 68 deletions
diff --git a/tests/benchmarks/corelib/thread/qmutex/CMakeLists.txt b/tests/benchmarks/corelib/thread/qmutex/CMakeLists.txt
index 4ba55034d8..2d01f9ff1f 100644
--- a/tests/benchmarks/corelib/thread/qmutex/CMakeLists.txt
+++ b/tests/benchmarks/corelib/thread/qmutex/CMakeLists.txt
@@ -1,15 +1,14 @@
-# Generated from qmutex.pro.
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
#####################################################################
## tst_bench_qmutex Binary:
#####################################################################
-qt_add_benchmark(tst_bench_qmutex
+qt_internal_add_benchmark(tst_bench_qmutex
SOURCES
- tst_qmutex.cpp
- PUBLIC_LIBRARIES
+ tst_bench_qmutex.cpp
+ LIBRARIES
+ Qt::CorePrivate
Qt::Test
)
-
-#### Keys ignored in scope 1:.:.:qmutex.pro:<TRUE>:
-# TEMPLATE = "app"
diff --git a/tests/benchmarks/corelib/thread/qmutex/qmutex.pro b/tests/benchmarks/corelib/thread/qmutex/qmutex.pro
deleted file mode 100644
index a0b2ddeaa9..0000000000
--- a/tests/benchmarks/corelib/thread/qmutex/qmutex.pro
+++ /dev/null
@@ -1,6 +0,0 @@
-TEMPLATE = app
-CONFIG += benchmark
-QT = core testlib
-
-TARGET = tst_bench_qmutex
-SOURCES += tst_qmutex.cpp
diff --git a/tests/benchmarks/corelib/thread/qmutex/tst_qmutex.cpp b/tests/benchmarks/corelib/thread/qmutex/tst_bench_qmutex.cpp
index 1482c21a85..6ea0c25c0b 100644
--- a/tests/benchmarks/corelib/thread/qmutex/tst_qmutex.cpp
+++ b/tests/benchmarks/corelib/thread/qmutex/tst_bench_qmutex.cpp
@@ -1,38 +1,16 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtCore/QtCore>
-#include <QtTest/QtTest>
+#include <QTest>
+#include <QtCore/private/qvolatile_p.h>
#include <math.h>
//#define USE_SEM_T
+using namespace std::chrono_literals;
+
#if defined(Q_OS_UNIX)
#if !defined(USE_SEM_T)
# include <pthread.h>
@@ -75,8 +53,7 @@ void NativeMutexUnlock(NativeMutexType *mutex)
}
#endif
#elif defined(Q_OS_WIN)
-# define _WIN32_WINNT 0x0400
-# include <windows.h>
+# include <qt_windows.h>
typedef CRITICAL_SECTION NativeMutexType;
void NativeMutexInitialize(NativeMutexType *mutex)
{
@@ -158,7 +135,7 @@ void tst_QMutex::noThread()
QBENCHMARK {
count = 0;
for (int i = 0; i < N; i++) {
- count++;
+ QtPrivate::volatilePreIncrement(count);
}
}
break;
@@ -167,7 +144,7 @@ void tst_QMutex::noThread()
count = 0;
for (int i = 0; i < N; i++) {
mtx.lock();
- count++;
+ QtPrivate::volatilePreIncrement(count);
mtx.unlock();
}
}
@@ -177,7 +154,7 @@ void tst_QMutex::noThread()
count = 0;
for (int i = 0; i < N; i++) {
QMutexLocker locker(&mtx);
- count++;
+ QtPrivate::volatilePreIncrement(count);
}
}
break;
@@ -253,14 +230,16 @@ void tst_QMutex::contendedNative_data()
class NativeMutexThread : public QThread
{
NativeMutexType *mutex1, *mutex2;
- int iterations, msleepDuration;
+ int iterations;
+ std::chrono::milliseconds msleepDuration;
bool use2mutexes;
public:
bool done;
NativeMutexThread(NativeMutexType *mutex1, NativeMutexType *mutex2, int iterations, int msleepDuration, bool use2mutexes)
: mutex1(mutex1), mutex2(mutex2), iterations(iterations), msleepDuration(msleepDuration), use2mutexes(use2mutexes), done(false)
{ }
- void run() {
+ void run() override
+ {
forever {
tst_QMutex::semaphore1.release();
tst_QMutex::semaphore2.acquire();
@@ -270,8 +249,8 @@ public:
NativeMutexLock(mutex1);
if (use2mutexes)
NativeMutexLock(mutex2);
- if (msleepDuration >= 0)
- msleep(msleepDuration);
+ if (msleepDuration >= 0ms)
+ sleep(msleepDuration);
if (use2mutexes)
NativeMutexUnlock(mutex2);
NativeMutexUnlock(mutex1);
@@ -295,7 +274,7 @@ void tst_QMutex::contendedNative()
NativeMutexInitialize(&mutex2);
QList<NativeMutexThread *> threads(threadCount);
- for (int i = 0; i < threads.count(); ++i) {
+ for (int i = 0; i < threads.size(); ++i) {
threads[i] = new NativeMutexThread(&mutex1, &mutex2, iterations, msleepDuration, use2mutexes);
threads[i]->start();
}
@@ -307,11 +286,11 @@ void tst_QMutex::contendedNative()
semaphore4.release(threadCount);
}
- for (int i = 0; i < threads.count(); ++i)
+ for (int i = 0; i < threads.size(); ++i)
threads[i]->done = true;
semaphore1.acquire(threadCount);
semaphore2.release(threadCount);
- for (int i = 0; i < threads.count(); ++i)
+ for (int i = 0; i < threads.size(); ++i)
threads[i]->wait();
qDeleteAll(threads);
@@ -322,14 +301,16 @@ void tst_QMutex::contendedNative()
class QMutexThread : public QThread
{
QMutex *mutex1, *mutex2;
- int iterations, msleepDuration;
+ int iterations;
+ std::chrono::milliseconds msleepDuration;
bool use2mutexes;
public:
bool done;
QMutexThread(QMutex *mutex1, QMutex *mutex2, int iterations, int msleepDuration, bool use2mutexes)
: mutex1(mutex1), mutex2(mutex2), iterations(iterations), msleepDuration(msleepDuration), use2mutexes(use2mutexes), done(false)
{ }
- void run() {
+ void run() override
+ {
forever {
tst_QMutex::semaphore1.release();
tst_QMutex::semaphore2.acquire();
@@ -339,8 +320,8 @@ public:
mutex1->lock();
if (use2mutexes)
mutex2->lock();
- if (msleepDuration >= 0)
- msleep(msleepDuration);
+ if (msleepDuration >= 0ms)
+ sleep(msleepDuration);
if (use2mutexes)
mutex2->unlock();
mutex1->unlock();
@@ -362,7 +343,7 @@ void tst_QMutex::contendedQMutex()
QMutex mutex1, mutex2;
QList<QMutexThread *> threads(threadCount);
- for (int i = 0; i < threads.count(); ++i) {
+ for (int i = 0; i < threads.size(); ++i) {
threads[i] = new QMutexThread(&mutex1, &mutex2, iterations, msleepDuration, use2mutexes);
threads[i]->start();
}
@@ -374,11 +355,11 @@ void tst_QMutex::contendedQMutex()
semaphore4.release(threadCount);
}
- for (int i = 0; i < threads.count(); ++i)
+ for (int i = 0; i < threads.size(); ++i)
threads[i]->done = true;
semaphore1.acquire(threadCount);
semaphore2.release(threadCount);
- for (int i = 0; i < threads.count(); ++i)
+ for (int i = 0; i < threads.size(); ++i)
threads[i]->wait();
qDeleteAll(threads);
}
@@ -386,14 +367,16 @@ void tst_QMutex::contendedQMutex()
class QMutexLockerThread : public QThread
{
QMutex *mutex1, *mutex2;
- int iterations, msleepDuration;
+ int iterations;
+ std::chrono::milliseconds msleepDuration;
bool use2mutexes;
public:
bool done;
QMutexLockerThread(QMutex *mutex1, QMutex *mutex2, int iterations, int msleepDuration, bool use2mutexes)
: mutex1(mutex1), mutex2(mutex2), iterations(iterations), msleepDuration(msleepDuration), use2mutexes(use2mutexes), done(false)
{ }
- void run() {
+ void run() override
+ {
forever {
tst_QMutex::semaphore1.release();
tst_QMutex::semaphore2.acquire();
@@ -403,8 +386,8 @@ public:
{
QMutexLocker locker1(mutex1);
QMutexLocker locker2(use2mutexes ? mutex2 : 0);
- if (msleepDuration >= 0)
- msleep(msleepDuration);
+ if (msleepDuration >= 0ms)
+ sleep(msleepDuration);
}
QThread::yieldCurrentThread();
@@ -424,7 +407,7 @@ void tst_QMutex::contendedQMutexLocker()
QMutex mutex1, mutex2;
QList<QMutexLockerThread *> threads(threadCount);
- for (int i = 0; i < threads.count(); ++i) {
+ for (int i = 0; i < threads.size(); ++i) {
threads[i] = new QMutexLockerThread(&mutex1, &mutex2, iterations, msleepDuration, use2mutexes);
threads[i]->start();
}
@@ -436,14 +419,15 @@ void tst_QMutex::contendedQMutexLocker()
semaphore4.release(threadCount);
}
- for (int i = 0; i < threads.count(); ++i)
+ for (int i = 0; i < threads.size(); ++i)
threads[i]->done = true;
semaphore1.acquire(threadCount);
semaphore2.release(threadCount);
- for (int i = 0; i < threads.count(); ++i)
+ for (int i = 0; i < threads.size(); ++i)
threads[i]->wait();
qDeleteAll(threads);
}
QTEST_MAIN(tst_QMutex)
-#include "tst_qmutex.moc"
+
+#include "tst_bench_qmutex.moc"