diff options
Diffstat (limited to 'tests/benchmarks/corelib/thread')
13 files changed, 222 insertions, 211 deletions
diff --git a/tests/benchmarks/corelib/thread/CMakeLists.txt b/tests/benchmarks/corelib/thread/CMakeLists.txt index 8f7b829a40..48bf0572a6 100644 --- a/tests/benchmarks/corelib/thread/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/CMakeLists.txt @@ -1,4 +1,5 @@ -# Generated from thread.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause add_subdirectory(qfuture) add_subdirectory(qmutex) diff --git a/tests/benchmarks/corelib/thread/qfuture/CMakeLists.txt b/tests/benchmarks/corelib/thread/qfuture/CMakeLists.txt index d53e3f6728..5988b1c0b0 100644 --- a/tests/benchmarks/corelib/thread/qfuture/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/qfuture/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + ##################################################################### ## tst_bench_qfuture Binary: ##################################################################### @@ -6,6 +9,6 @@ qt_internal_add_benchmark(tst_bench_qfuture EXCEPTIONS SOURCES tst_bench_qfuture.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::Test ) diff --git a/tests/benchmarks/corelib/thread/qfuture/tst_bench_qfuture.cpp b/tests/benchmarks/corelib/thread/qfuture/tst_bench_qfuture.cpp index 17c2f20712..38b3f636c9 100644 --- a/tests/benchmarks/corelib/thread/qfuture/tst_bench_qfuture.cpp +++ b/tests/benchmarks/corelib/thread/qfuture/tst_bench_qfuture.cpp @@ -1,35 +1,11 @@ -/**************************************************************************** -** -** Copyright (C) 2021 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) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> #include <qexception.h> #include <qfuture.h> +#include <qpromise.h> #include <qsemaphore.h> class tst_QFuture : public QObject @@ -37,7 +13,7 @@ class tst_QFuture : public QObject Q_OBJECT private slots: - void makeReadyfuture(); + void makeReadyValueFuture(); #ifndef QT_NO_EXCEPTIONS void makeExceptionalFuture(); #endif @@ -67,10 +43,10 @@ private slots: void progressText(); }; -void tst_QFuture::makeReadyfuture() +void tst_QFuture::makeReadyValueFuture() { QBENCHMARK { - auto future = QtFuture::makeReadyFuture(42); + auto future = QtFuture::makeReadyValueFuture(42); Q_UNUSED(future); } } @@ -88,7 +64,7 @@ void tst_QFuture::makeExceptionalFuture() void tst_QFuture::result() { - auto future = QtFuture::makeReadyFuture(42); + auto future = QtFuture::makeReadyValueFuture(42); QBENCHMARK { auto value = future.result(); @@ -116,7 +92,7 @@ void tst_QFuture::results() void tst_QFuture::takeResult() { QBENCHMARK { - auto future = QtFuture::makeReadyFuture(42); + auto future = QtFuture::makeReadyValueFuture(42); auto value = future.takeResult(); Q_UNUSED(value); } @@ -164,7 +140,7 @@ void tst_QFuture::reportException() void tst_QFuture::then() { - auto f = QtFuture::makeReadyFuture(42); + auto f = QtFuture::makeReadyValueFuture(42); QBENCHMARK { auto future = f.then([](int value) { return value; }); Q_UNUSED(future); @@ -173,7 +149,7 @@ void tst_QFuture::then() void tst_QFuture::thenVoid() { - auto f = QtFuture::makeReadyFuture(); + auto f = QtFuture::makeReadyVoidFuture(); QBENCHMARK { auto future = f.then([] {}); Q_UNUSED(future); @@ -229,7 +205,7 @@ void tst_QFuture::onFailedVoid() void tst_QFuture::thenOnFailed() { - auto f = QtFuture::makeReadyFuture(42); + auto f = QtFuture::makeReadyValueFuture(42); QBENCHMARK { auto future = f.then([](int) { throw std::runtime_error("error"); }).onFailed([] { return 0; }); @@ -239,7 +215,7 @@ void tst_QFuture::thenOnFailed() void tst_QFuture::thenOnFailedVoid() { - auto f = QtFuture::makeReadyFuture(); + auto f = QtFuture::makeReadyVoidFuture(); QBENCHMARK { auto future = f.then([] { throw std::runtime_error("error"); }).onFailed([] {}); Q_UNUSED(future); diff --git a/tests/benchmarks/corelib/thread/qmutex/CMakeLists.txt b/tests/benchmarks/corelib/thread/qmutex/CMakeLists.txt index d056a10568..2d01f9ff1f 100644 --- a/tests/benchmarks/corelib/thread/qmutex/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/qmutex/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + ##################################################################### ## tst_bench_qmutex Binary: ##################################################################### @@ -5,7 +8,7 @@ qt_internal_add_benchmark(tst_bench_qmutex SOURCES tst_bench_qmutex.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::CorePrivate Qt::Test ) diff --git a/tests/benchmarks/corelib/thread/qmutex/tst_bench_qmutex.cpp b/tests/benchmarks/corelib/thread/qmutex/tst_bench_qmutex.cpp index 987612b7b2..6ea0c25c0b 100644 --- a/tests/benchmarks/corelib/thread/qmutex/tst_bench_qmutex.cpp +++ b/tests/benchmarks/corelib/thread/qmutex/tst_bench_qmutex.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** 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 <QTest> @@ -34,6 +9,8 @@ //#define USE_SEM_T +using namespace std::chrono_literals; + #if defined(Q_OS_UNIX) #if !defined(USE_SEM_T) # include <pthread.h> @@ -76,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) { @@ -254,7 +230,8 @@ 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; @@ -272,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); @@ -297,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(); } @@ -309,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); @@ -324,7 +301,8 @@ 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; @@ -342,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(); @@ -365,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(); } @@ -377,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); } @@ -389,7 +367,8 @@ 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; @@ -407,8 +386,8 @@ public: { QMutexLocker locker1(mutex1); QMutexLocker locker2(use2mutexes ? mutex2 : 0); - if (msleepDuration >= 0) - msleep(msleepDuration); + if (msleepDuration >= 0ms) + sleep(msleepDuration); } QThread::yieldCurrentThread(); @@ -428,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(); } @@ -440,11 +419,11 @@ 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); } diff --git a/tests/benchmarks/corelib/thread/qreadwritelock/CMakeLists.txt b/tests/benchmarks/corelib/thread/qreadwritelock/CMakeLists.txt index 901f9609bc..b56f484b48 100644 --- a/tests/benchmarks/corelib/thread/qreadwritelock/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/qreadwritelock/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + ##################################################################### ## tst_bench_qreadwritelock Binary: ##################################################################### @@ -5,7 +8,7 @@ qt_internal_add_benchmark(tst_bench_qreadwritelock SOURCES tst_bench_qreadwritelock.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::CorePrivate Qt::Test ) diff --git a/tests/benchmarks/corelib/thread/qreadwritelock/tst_bench_qreadwritelock.cpp b/tests/benchmarks/corelib/thread/qreadwritelock/tst_bench_qreadwritelock.cpp index be12877df1..e63ac6573f 100644 --- a/tests/benchmarks/corelib/thread/qreadwritelock/tst_bench_qreadwritelock.cpp +++ b/tests/benchmarks/corelib/thread/qreadwritelock/tst_bench_qreadwritelock.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com> -** 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 Olivier Goffart <ogoffart@woboq.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtCore/QtCore> #include <QTest> @@ -44,6 +19,49 @@ struct LockerWrapper : T } }; +struct QRecursiveReadWriteLock : QReadWriteLock +{ + QRecursiveReadWriteLock() : QReadWriteLock(Recursive) {} +}; + +template <typename T, size_t N> + // requires N = 2^M for some Integral M >= 0 +struct Recursive +{ + Recursive<T, N/2> r1, r2; + + template <typename...Args> + Q_ALWAYS_INLINE + explicit Recursive(Args &&...args) + : r1(args...), + r2(args...) + {} +}; + +template <typename T> +struct Recursive<T, 1> +{ + T t; + template <typename...Args> + Q_ALWAYS_INLINE + explicit Recursive(Args &&...args) + : t(args...) {} +}; + +template <typename T> +struct Recursive<T, 0> +{ + template <typename...Args> + Q_ALWAYS_INLINE + explicit Recursive(Args &&...) {} +}; + +template <size_t N> +using QRecursiveReadLocker = Recursive<QReadLocker, N>; + +template <size_t N> +using QRecursiveWriteLocker = Recursive<QWriteLocker, N>; + int threadCount; class tst_QReadWriteLock : public QObject @@ -105,6 +123,15 @@ void tst_QReadWriteLock::uncontended_data() << FunctionPtrHolder(testUncontended<QReadWriteLock, QReadLocker>); QTest::newRow("QReadWriteLock, write") << FunctionPtrHolder(testUncontended<QReadWriteLock, QWriteLocker>); +#define ROW(n) \ + QTest::addRow("QReadWriteLock, %s, recursive: %d", "read", n) \ + << FunctionPtrHolder(testUncontended<QRecursiveReadWriteLock, QRecursiveReadLocker<n>>); \ + QTest::addRow("QReadWriteLock, %s, recursive: %d", "write", n) \ + << FunctionPtrHolder(testUncontended<QRecursiveReadWriteLock, QRecursiveWriteLocker<n>>) + ROW(1); + ROW(2); + ROW(32); +#undef ROW QTest::newRow("std::mutex") << FunctionPtrHolder( testUncontended<std::mutex, LockerWrapper<std::unique_lock<std::mutex>>>); #ifdef __cpp_lib_shared_mutex @@ -172,6 +199,13 @@ void tst_QReadWriteLock::readOnly_data() QTest::newRow("nothing") << FunctionPtrHolder(testReadOnly<int, FakeLock>); QTest::newRow("QMutex") << FunctionPtrHolder(testReadOnly<QMutex, QMutexLocker<QMutex>>); QTest::newRow("QReadWriteLock") << FunctionPtrHolder(testReadOnly<QReadWriteLock, QReadLocker>); +#define ROW(n) \ + QTest::addRow("QReadWriteLock, recursive: %d", n) \ + << FunctionPtrHolder(testReadOnly<QRecursiveReadWriteLock, QRecursiveReadLocker<n>>) + ROW(1); + ROW(2); + ROW(32); +#undef ROW QTest::newRow("std::mutex") << FunctionPtrHolder( testReadOnly<std::mutex, LockerWrapper<std::unique_lock<std::mutex>>>); #ifdef __cpp_lib_shared_mutex @@ -233,6 +267,13 @@ void tst_QReadWriteLock::writeOnly_data() // QTest::newRow("nothing") << FunctionPtrHolder(testWriteOnly<int, FakeLock>); QTest::newRow("QMutex") << FunctionPtrHolder(testWriteOnly<QMutex, QMutexLocker<QMutex>>); QTest::newRow("QReadWriteLock") << FunctionPtrHolder(testWriteOnly<QReadWriteLock, QWriteLocker>); +#define ROW(n) \ + QTest::addRow("QReadWriteLock, recursive: %d", n) \ + << FunctionPtrHolder(testWriteOnly<QRecursiveReadWriteLock, QRecursiveWriteLocker<n>>) + ROW(1); + ROW(2); + ROW(32); +#undef ROW QTest::newRow("std::mutex") << FunctionPtrHolder( testWriteOnly<std::mutex, LockerWrapper<std::unique_lock<std::mutex>>>); #ifdef __cpp_lib_shared_mutex diff --git a/tests/benchmarks/corelib/thread/qthreadpool/CMakeLists.txt b/tests/benchmarks/corelib/thread/qthreadpool/CMakeLists.txt index 0d1f145e25..1353eaa2c4 100644 --- a/tests/benchmarks/corelib/thread/qthreadpool/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/qthreadpool/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + ##################################################################### ## tst_bench_qthreadpool Binary: ##################################################################### @@ -5,6 +8,6 @@ qt_internal_add_benchmark(tst_bench_qthreadpool SOURCES tst_bench_qthreadpool.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::Test ) diff --git a/tests/benchmarks/corelib/thread/qthreadpool/tst_bench_qthreadpool.cpp b/tests/benchmarks/corelib/thread/qthreadpool/tst_bench_qthreadpool.cpp index a64dbd15c9..e7e5636307 100644 --- a/tests/benchmarks/corelib/thread/qthreadpool/tst_bench_qthreadpool.cpp +++ b/tests/benchmarks/corelib/thread/qthreadpool/tst_bench_qthreadpool.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2013 David Faure <david.faure@kdab.com> -** 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) 2013 David Faure <david.faure@kdab.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <qtest.h> #include <QtCore> diff --git a/tests/benchmarks/corelib/thread/qthreadstorage/CMakeLists.txt b/tests/benchmarks/corelib/thread/qthreadstorage/CMakeLists.txt index 7f5de4e9ab..916a2d8dee 100644 --- a/tests/benchmarks/corelib/thread/qthreadstorage/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/qthreadstorage/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + ##################################################################### ## tst_bench_qthreadstorage Binary: ##################################################################### @@ -5,6 +8,6 @@ qt_internal_add_benchmark(tst_bench_qthreadstorage SOURCES tst_bench_qthreadstorage.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::Test ) diff --git a/tests/benchmarks/corelib/thread/qthreadstorage/tst_bench_qthreadstorage.cpp b/tests/benchmarks/corelib/thread/qthreadstorage/tst_bench_qthreadstorage.cpp index 6230cd3187..47db215465 100644 --- a/tests/benchmarks/corelib/thread/qthreadstorage/tst_bench_qthreadstorage.cpp +++ b/tests/benchmarks/corelib/thread/qthreadstorage/tst_bench_qthreadstorage.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** 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 <qtest.h> #include <QtCore> @@ -89,6 +64,7 @@ void tst_QThreadStorage::get() int *i = ts.localData(); count += *i; } + QVERIFY(count > 0); ts.setLocalData(0); } diff --git a/tests/benchmarks/corelib/thread/qwaitcondition/CMakeLists.txt b/tests/benchmarks/corelib/thread/qwaitcondition/CMakeLists.txt index 13814d7074..c7a15f9e8a 100644 --- a/tests/benchmarks/corelib/thread/qwaitcondition/CMakeLists.txt +++ b/tests/benchmarks/corelib/thread/qwaitcondition/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + ##################################################################### ## tst_bench_qwaitcondition Binary: ##################################################################### @@ -5,6 +8,6 @@ qt_internal_add_benchmark(tst_bench_qwaitcondition SOURCES tst_bench_qwaitcondition.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::Test ) diff --git a/tests/benchmarks/corelib/thread/qwaitcondition/tst_bench_qwaitcondition.cpp b/tests/benchmarks/corelib/thread/qwaitcondition/tst_bench_qwaitcondition.cpp index 19d3964c7a..0abd53fc93 100644 --- a/tests/benchmarks/corelib/thread/qwaitcondition/tst_bench_qwaitcondition.cpp +++ b/tests/benchmarks/corelib/thread/qwaitcondition/tst_bench_qwaitcondition.cpp @@ -1,36 +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 <QTest> #include <math.h> +#include <condition_variable> +#include <mutex> +#include <limits.h> + +using namespace std::chrono_literals; class tst_QWaitCondition : public QObject { @@ -42,24 +22,67 @@ public: } private slots: + void oscillate_QWaitCondition_QMutex_data() { oscillate_mutex_data(); } + void oscillate_QWaitCondition_QMutex(); + void oscillate_QWaitCondition_QReadWriteLock_data() { oscillate_mutex_data(); } + void oscillate_QWaitCondition_QReadWriteLock(); + void oscillate_std_condition_variable_std_mutex_data() { oscillate_mutex_data(); } + void oscillate_std_condition_variable_std_mutex(); + void oscillate_std_condition_variable_any_QMutex_data() { oscillate_mutex_data(); } + void oscillate_std_condition_variable_any_QMutex(); + void oscillate_std_condition_variable_any_QReadWriteLock_data() { oscillate_mutex_data(); } + void oscillate_std_condition_variable_any_QReadWriteLock(); + +private: void oscillate_mutex_data(); - void oscillate_mutex(); - void oscillate_writelock_data(); - void oscillate_writelock(); }; int turn; const int threadCount = 10; QWaitCondition cond; +std::condition_variable cv; +std::condition_variable_any cva; -template <class Mutex, class Locker> +template <typename Cond> +Cond *get(); + +template <> std::condition_variable *get() { return &cv; } +template <> std::condition_variable_any *get() { return &cva; } + +template <class Cond, class Mutex, class Locker> class OscillateThread : public QThread { public: Mutex *mutex; int m_threadid; - int timeout; + unsigned long timeout; + + void run() override + { + for (int count = 0; count < 5000; ++count) { + Locker lock(*mutex); + while (m_threadid != turn) { + if (timeout == ULONG_MAX) + get<Cond>()->wait(lock); + else if (timeout == 0) // Windows doesn't unlock the mutex with a zero timeout + get<Cond>()->wait_for(lock, 1ns); + else + get<Cond>()->wait_for(lock, timeout * 1ms); + } + turn = (turn+1) % threadCount; + get<Cond>()->notify_all(); + } + } +}; + +template <class Mutex, class Locker> +class OscillateThread<QWaitCondition, Mutex, Locker> : public QThread +{ +public: + Mutex *mutex; + int m_threadid; + unsigned long timeout; void run() override { @@ -75,10 +98,10 @@ public: } }; -template <class Mutex, class Locker> +template <class Cond, class Mutex, class Locker> void oscillate(unsigned long timeout) { - OscillateThread<Mutex, Locker> thrd[threadCount]; + OscillateThread<Cond, Mutex, Locker> thrd[threadCount]; Mutex m; for (int i = 0; i < threadCount; ++i) { thrd[i].mutex = &m; @@ -107,21 +130,43 @@ void tst_QWaitCondition::oscillate_mutex_data() QTest::newRow("forever") << ULONG_MAX; } -void tst_QWaitCondition::oscillate_mutex() +void tst_QWaitCondition::oscillate_QWaitCondition_QMutex() { QFETCH(unsigned long, timeout); - oscillate<QMutex, QMutexLocker<QMutex>>(timeout); + oscillate<QWaitCondition, QMutex, QMutexLocker<QMutex>>(timeout); } -void tst_QWaitCondition::oscillate_writelock_data() +void tst_QWaitCondition::oscillate_QWaitCondition_QReadWriteLock() { - oscillate_mutex_data(); + QFETCH(unsigned long, timeout); + oscillate<QWaitCondition, QReadWriteLock, QWriteLocker>(timeout); } -void tst_QWaitCondition::oscillate_writelock() +void tst_QWaitCondition::oscillate_std_condition_variable_std_mutex() { QFETCH(unsigned long, timeout); - oscillate<QReadWriteLock, QWriteLocker>(timeout); + oscillate<std::condition_variable, std::mutex, std::unique_lock<std::mutex>>(timeout); +} + + +void tst_QWaitCondition::oscillate_std_condition_variable_any_QMutex() +{ + QFETCH(unsigned long, timeout); + oscillate<std::condition_variable_any, QMutex, std::unique_lock<QMutex>>(timeout); +} + + +void tst_QWaitCondition::oscillate_std_condition_variable_any_QReadWriteLock() +{ + QFETCH(unsigned long, timeout); + + struct WriteLocker : QWriteLocker { + // adapt to BasicLockable + explicit WriteLocker(QReadWriteLock &m) : QWriteLocker{&m} {} + void lock() { relock(); } + }; + + oscillate<std::condition_variable_any, QReadWriteLock, WriteLocker>(timeout); } QTEST_MAIN(tst_QWaitCondition) |