From e45e6efa723297e8aaa1132251ef55b65201d174 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 23 Aug 2019 09:51:39 +0200 Subject: QTouchDevice: don't play ping-pong with q*PostRoutine() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removing a qPostRoutine is an O(N) operation. It's perfectly ok to keep calling it even if the list has become empty before. Just qAddPostRoutine in the deviceList's ctor and never remove it again. Add a missing qAsConst as a drive-by. Change-Id: I25f824b74012146214568cfccb22c5dba3ca38ef Reviewed-by: Jan Arve Sæther --- src/gui/kernel/qtouchdevice.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/gui/kernel/qtouchdevice.cpp b/src/gui/kernel/qtouchdevice.cpp index 511e92566e..ea187f54aa 100644 --- a/src/gui/kernel/qtouchdevice.cpp +++ b/src/gui/kernel/qtouchdevice.cpp @@ -44,6 +44,7 @@ #include #include +#include QT_BEGIN_NAMESPACE @@ -201,15 +202,20 @@ void QTouchDevice::setName(const QString &name) d->name = name; } -typedef QList TouchDevices; -Q_GLOBAL_STATIC(TouchDevices, deviceList) static QBasicMutex devicesMutex; -static void cleanupDevicesList() +struct TouchDevices { + TouchDevices(); + QList list; +}; +Q_GLOBAL_STATIC(TouchDevices, deviceList) + +TouchDevices::TouchDevices() { - QMutexLocker lock(&devicesMutex); - qDeleteAll(*deviceList()); - deviceList()->clear(); + qAddPostRoutine([]{ + const auto locker = qt_scoped_lock(devicesMutex); + qDeleteAll(qExchange(deviceList->list, {})); + }); } /*! @@ -223,7 +229,7 @@ static void cleanupDevicesList() QList QTouchDevice::devices() { QMutexLocker lock(&devicesMutex); - return *deviceList(); + return deviceList->list; } /*! @@ -232,13 +238,13 @@ QList QTouchDevice::devices() bool QTouchDevicePrivate::isRegistered(const QTouchDevice *dev) { QMutexLocker locker(&devicesMutex); - return deviceList()->contains(dev); + return deviceList->list.contains(dev); } const QTouchDevice *QTouchDevicePrivate::deviceById(quint8 id) { QMutexLocker locker(&devicesMutex); - for (const QTouchDevice *dev : *deviceList()) + for (const QTouchDevice *dev : qAsConst(deviceList->list)) if (QTouchDevicePrivate::get(const_cast(dev))->id == id) return dev; return nullptr; @@ -250,9 +256,7 @@ const QTouchDevice *QTouchDevicePrivate::deviceById(quint8 id) void QTouchDevicePrivate::registerDevice(const QTouchDevice *dev) { QMutexLocker lock(&devicesMutex); - if (deviceList()->isEmpty()) - qAddPostRoutine(cleanupDevicesList); - deviceList()->append(dev); + deviceList->list.append(dev); } /*! @@ -261,9 +265,7 @@ void QTouchDevicePrivate::registerDevice(const QTouchDevice *dev) void QTouchDevicePrivate::unregisterDevice(const QTouchDevice *dev) { QMutexLocker lock(&devicesMutex); - bool wasRemoved = deviceList()->removeOne(dev); - if (wasRemoved && deviceList()->isEmpty()) - qRemovePostRoutine(cleanupDevicesList); + deviceList->list.removeOne(dev); } #ifndef QT_NO_DEBUG_STREAM -- cgit v1.2.3