diff options
Diffstat (limited to 'tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp')
-rw-r--r-- | tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp | 91 |
1 files changed, 84 insertions, 7 deletions
diff --git a/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp b/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp index fcf600a059..1d47d98657 100644 --- a/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp +++ b/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp @@ -28,12 +28,14 @@ #include <QtCore/QtCore> #include <QtTest/QtTest> +#include <QtCore/private/qmemory_p.h> #include <mutex> #if QT_HAS_INCLUDE(<shared_mutex>) #if __cplusplus > 201103L #include <shared_mutex> #endif #endif +#include <vector> // Wrapers that take pointers instead of reference to have the same interface as Qt template <typename T> @@ -63,6 +65,8 @@ private slots: void uncontended(); void readOnly_data(); void readOnly(); + void writeOnly_data(); + void writeOnly(); // void readWrite(); }; @@ -106,6 +110,14 @@ void tst_QReadWriteLock::uncontended_data() << FunctionPtrHolder(testUncontended<QReadWriteLock, QWriteLocker>); QTest::newRow("std::mutex") << FunctionPtrHolder( testUncontended<std::mutex, LockerWrapper<std::unique_lock<std::mutex>>>); +#ifdef __cpp_lib_shared_mutex + QTest::newRow("std::shared_mutex, read") << FunctionPtrHolder( + testUncontended<std::shared_mutex, + LockerWrapper<std::shared_lock<std::shared_mutex>>>); + QTest::newRow("std::shared_mutex, write") << FunctionPtrHolder( + testUncontended<std::shared_mutex, + LockerWrapper<std::unique_lock<std::shared_mutex>>>); +#endif #if defined __cpp_lib_shared_timed_mutex QTest::newRow("std::shared_timed_mutex, read") << FunctionPtrHolder( testUncontended<std::shared_timed_mutex, @@ -130,7 +142,7 @@ void testReadOnly() struct Thread : QThread { Mutex *lock; - void run() + void run() override { for (int i = 0; i < Iterations; ++i) { QString s = QString::number(i); // Do something outside the lock @@ -140,21 +152,20 @@ void testReadOnly() } }; Mutex lock; - QVector<QThread *> threads; + std::vector<std::unique_ptr<Thread>> threads; for (int i = 0; i < threadCount; ++i) { - auto t = new Thread; + auto t = qt_make_unique<Thread>(); t->lock = &lock; - threads.append(t); + threads.push_back(std::move(t)); } QBENCHMARK { - for (auto t : threads) { + for (auto &t : threads) { t->start(); } - for (auto t : threads) { + for (auto &t : threads) { t->wait(); } } - qDeleteAll(threads); } void tst_QReadWriteLock::readOnly_data() @@ -166,6 +177,11 @@ void tst_QReadWriteLock::readOnly_data() QTest::newRow("QReadWriteLock") << FunctionPtrHolder(testReadOnly<QReadWriteLock, QReadLocker>); QTest::newRow("std::mutex") << FunctionPtrHolder( testReadOnly<std::mutex, LockerWrapper<std::unique_lock<std::mutex>>>); +#ifdef __cpp_lib_shared_mutex + QTest::newRow("std::shared_mutex") << FunctionPtrHolder( + testReadOnly<std::shared_mutex, + LockerWrapper<std::shared_lock<std::shared_mutex>>>); +#endif #if defined __cpp_lib_shared_timed_mutex QTest::newRow("std::shared_timed_mutex") << FunctionPtrHolder( testReadOnly<std::shared_timed_mutex, @@ -179,5 +195,66 @@ void tst_QReadWriteLock::readOnly() holder.value(); } +static QString global_string; + +template <typename Mutex, typename Locker> +void testWriteOnly() +{ + struct Thread : QThread + { + Mutex *lock; + void run() override + { + for (int i = 0; i < Iterations; ++i) { + QString s = QString::number(i); // Do something outside the lock + Locker locker(lock); + global_string = s; + } + } + }; + Mutex lock; + std::vector<std::unique_ptr<Thread>> threads; + for (int i = 0; i < threadCount; ++i) { + auto t = qt_make_unique<Thread>(); + t->lock = &lock; + threads.push_back(std::move(t)); + } + QBENCHMARK { + for (auto &t : threads) { + t->start(); + } + for (auto &t : threads) { + t->wait(); + } + } +} + +void tst_QReadWriteLock::writeOnly_data() +{ + QTest::addColumn<FunctionPtrHolder>("holder"); + + // QTest::newRow("nothing") << FunctionPtrHolder(testWriteOnly<int, FakeLock>); + QTest::newRow("QMutex") << FunctionPtrHolder(testWriteOnly<QMutex, QMutexLocker>); + QTest::newRow("QReadWriteLock") << FunctionPtrHolder(testWriteOnly<QReadWriteLock, QWriteLocker>); + QTest::newRow("std::mutex") << FunctionPtrHolder( + testWriteOnly<std::mutex, LockerWrapper<std::unique_lock<std::mutex>>>); +#ifdef __cpp_lib_shared_mutex + QTest::newRow("std::shared_mutex") << FunctionPtrHolder( + testWriteOnly<std::shared_mutex, + LockerWrapper<std::unique_lock<std::shared_mutex>>>); +#endif +#if defined __cpp_lib_shared_timed_mutex + QTest::newRow("std::shared_timed_mutex") << FunctionPtrHolder( + testWriteOnly<std::shared_timed_mutex, + LockerWrapper<std::unique_lock<std::shared_timed_mutex>>>); +#endif +} + +void tst_QReadWriteLock::writeOnly() +{ + QFETCH(FunctionPtrHolder, holder); + holder.value(); +} + QTEST_MAIN(tst_QReadWriteLock) #include "tst_qreadwritelock.moc" |