summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/corelib/thread/qreadwritelock/tst_qreadwritelock.cpp
blob: fcf600a059f7d5d201cc5eeba51cd59181d167be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/****************************************************************************
**
** 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$
**
****************************************************************************/

#include <QtCore/QtCore>
#include <QtTest/QtTest>
#include <mutex>
#if QT_HAS_INCLUDE(<shared_mutex>)
#if __cplusplus > 201103L
#include <shared_mutex>
#endif
#endif

// Wrapers that take pointers instead of reference to have the same interface as Qt
template <typename T>
struct LockerWrapper : T
{
    LockerWrapper(typename T::mutex_type *mtx)
        : T(*mtx)
    {
    }
};

int threadCount;

class tst_QReadWriteLock : public QObject
{
    Q_OBJECT
public:
    tst_QReadWriteLock()
    {
        // at least 2 threads, even on single cpu/core machines
        threadCount = qMax(2, QThread::idealThreadCount());
        qDebug("thread count: %d", threadCount);
    }

private slots:
    void uncontended_data();
    void uncontended();
    void readOnly_data();
    void readOnly();
    // void readWrite();
};

struct FunctionPtrHolder
{
    FunctionPtrHolder(QFunctionPointer value = nullptr)
        : value(value)
    {
    }
    QFunctionPointer value;
};
Q_DECLARE_METATYPE(FunctionPtrHolder)

struct FakeLock
{
    FakeLock(volatile int *i) { *i = 0; }
};

enum { Iterations = 1000000 };

template <typename Mutex, typename Locker>
void testUncontended()
{
    Mutex lock;
    QBENCHMARK {
        for (int i = 0; i < Iterations; ++i) {
            Locker locker(&lock);
        }
    }
}

void tst_QReadWriteLock::uncontended_data()
{
    QTest::addColumn<FunctionPtrHolder>("holder");

    QTest::newRow("nothing") << FunctionPtrHolder(testUncontended<int, FakeLock>);
    QTest::newRow("QMutex") << FunctionPtrHolder(testUncontended<QMutex, QMutexLocker>);
    QTest::newRow("QReadWriteLock, read")
        << FunctionPtrHolder(testUncontended<QReadWriteLock, QReadLocker>);
    QTest::newRow("QReadWriteLock, write")
        << FunctionPtrHolder(testUncontended<QReadWriteLock, QWriteLocker>);
    QTest::newRow("std::mutex") << FunctionPtrHolder(
        testUncontended<std::mutex, LockerWrapper<std::unique_lock<std::mutex>>>);
#if defined __cpp_lib_shared_timed_mutex
    QTest::newRow("std::shared_timed_mutex, read") << FunctionPtrHolder(
        testUncontended<std::shared_timed_mutex,
                        LockerWrapper<std::shared_lock<std::shared_timed_mutex>>>);
    QTest::newRow("std::shared_timed_mutex, write") << FunctionPtrHolder(
        testUncontended<std::shared_timed_mutex,
                        LockerWrapper<std::unique_lock<std::shared_timed_mutex>>>);
#endif
}

void tst_QReadWriteLock::uncontended()
{
    QFETCH(FunctionPtrHolder, holder);
    holder.value();
}

static QHash<QString, QString> global_hash;

template <typename Mutex, typename Locker>
void testReadOnly()
{
    struct Thread : QThread
    {
        Mutex *lock;
        void run()
        {
            for (int i = 0; i < Iterations; ++i) {
                QString s = QString::number(i); // Do something outside the lock
                Locker locker(lock);
                global_hash.contains(s);
            }
        }
    };
    Mutex lock;
    QVector<QThread *> threads;
    for (int i = 0; i < threadCount; ++i) {
        auto t = new Thread;
        t->lock = &lock;
        threads.append(t);
    }
    QBENCHMARK {
        for (auto t : threads) {
            t->start();
        }
        for (auto t : threads) {
            t->wait();
        }
    }
    qDeleteAll(threads);
}

void tst_QReadWriteLock::readOnly_data()
{
    QTest::addColumn<FunctionPtrHolder>("holder");

    QTest::newRow("nothing") << FunctionPtrHolder(testReadOnly<int, FakeLock>);
    QTest::newRow("QMutex") << FunctionPtrHolder(testReadOnly<QMutex, QMutexLocker>);
    QTest::newRow("QReadWriteLock") << FunctionPtrHolder(testReadOnly<QReadWriteLock, QReadLocker>);
    QTest::newRow("std::mutex") << FunctionPtrHolder(
        testReadOnly<std::mutex, LockerWrapper<std::unique_lock<std::mutex>>>);
#if defined __cpp_lib_shared_timed_mutex
    QTest::newRow("std::shared_timed_mutex") << FunctionPtrHolder(
        testReadOnly<std::shared_timed_mutex,
                     LockerWrapper<std::shared_lock<std::shared_timed_mutex>>>);
#endif
}

void tst_QReadWriteLock::readOnly()
{
    QFETCH(FunctionPtrHolder, holder);
    holder.value();
}

QTEST_MAIN(tst_QReadWriteLock)
#include "tst_qreadwritelock.moc"