QBuffer: exterminate three thread-safe static guard variables

Instead of duplicating two dynamically-initialized statics each in two
functions, causing four threading guards to be emitted by the
compiler, pack the two variables into a static struct and the struct
into an Extracted Method, leaving just a single threading guard.

https://godbolt.org/z/a9e5o848f

Uses C++14 NSDMI-in-aggregates, so doesn't work in 5.15.

Pick-to: 6.3 6.2
Change-Id: I98e053df48aafd5720ceffd514d6811fd3b28b1a
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
bb10
Marc Mutz 2022-02-02 16:31:01 +01:00
parent bb9be22133
commit 1e62f08998
1 changed files with 11 additions and 6 deletions

View File

@ -444,15 +444,22 @@ qint64 QBuffer::writeData(const char *data, qint64 len)
}
#ifndef QT_NO_QOBJECT
static bool is_tracked_signal(const QMetaMethod &signal)
{
// dynamic initialization: minimize the number of guard variables:
static const struct {
QMetaMethod readyReadSignal = QMetaMethod::fromSignal(&QBuffer::readyRead);
QMetaMethod bytesWrittenSignal = QMetaMethod::fromSignal(&QBuffer::bytesWritten);
} sigs;
return signal == sigs.readyReadSignal || signal == sigs.bytesWrittenSignal;
}
/*!
\reimp
\internal
*/
void QBuffer::connectNotify(const QMetaMethod &signal)
{
static const QMetaMethod readyReadSignal = QMetaMethod::fromSignal(&QBuffer::readyRead);
static const QMetaMethod bytesWrittenSignal = QMetaMethod::fromSignal(&QBuffer::bytesWritten);
if (signal == readyReadSignal || signal == bytesWrittenSignal)
if (is_tracked_signal(signal))
d_func()->signalConnectionCount++;
}
@ -463,9 +470,7 @@ void QBuffer::connectNotify(const QMetaMethod &signal)
void QBuffer::disconnectNotify(const QMetaMethod &signal)
{
if (signal.isValid()) {
static const QMetaMethod readyReadSignal = QMetaMethod::fromSignal(&QBuffer::readyRead);
static const QMetaMethod bytesWrittenSignal = QMetaMethod::fromSignal(&QBuffer::bytesWritten);
if (signal == readyReadSignal || signal == bytesWrittenSignal)
if (is_tracked_signal(signal))
d_func()->signalConnectionCount--;
} else {
d_func()->signalConnectionCount = 0;