QSignalSpy: make args member const

This means it's implicitly thread-safe now and we don't need to
protect accesses to it with the mutex.

Task-number: QTBUG-123544
Change-Id: I9f826003dca6fb81e7a75e283482c81ecff09be0
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
This commit is contained in:
Marc Mutz 2024-03-21 18:18:30 +01:00
parent 951bf6e3ec
commit 46ad7fe966
2 changed files with 4 additions and 10 deletions

View File

@ -258,7 +258,6 @@ bool QSignalSpy::connectToSignal(const QObject *sender, int sigIndex)
void QSignalSpy::appendArgs(void **a)
{
QMutexLocker locker(&m_mutex);
QList<QVariant> list;
list.reserve(args.size());
for (qsizetype i = 0; i < args.size(); ++i) {
@ -268,6 +267,7 @@ void QSignalSpy::appendArgs(void **a)
else
list << QVariant(QMetaType(type), a[i + 1]);
}
QMutexLocker locker(&m_mutex);
append(std::move(list));
if (m_waiting) {

View File

@ -12,8 +12,6 @@
#include <QtCore/qvariant.h>
#include <QtCore/qmutex.h>
#include <mutex>
QT_BEGIN_NAMESPACE
@ -65,15 +63,11 @@ public:
private:
explicit QSignalSpy(ObjectSignal os)
: args(os.obj ? makeArgs(os.sig, os.obj) : QList<int>{})
{
if (!os.obj)
return;
auto tmp = makeArgs(os.sig, os.obj);
{
const auto lock = std::scoped_lock(m_mutex);
args = std::move(tmp);
}
if (!connectToSignal(os.obj, os.sig.methodIndex()))
return;
@ -92,11 +86,11 @@ private:
// the full, normalized signal name
QByteArray sig;
// holds the QMetaType types for the argument list of the signal
QList<int> args;
const QList<int> args;
QTestEventLoop m_loop;
bool m_waiting = false;
QMutex m_mutex; // protects m_waiting, args and the QList base class, between appendArgs() and wait()
QMutex m_mutex; // protects m_waiting and the QList base class, between appendArgs() and wait()
};
QT_END_NAMESPACE