diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2019-08-23 09:51:39 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2019-08-30 22:38:37 +0200 |
commit | e45e6efa723297e8aaa1132251ef55b65201d174 (patch) | |
tree | 0adf0a2365a68a3790fd455568f1b9766315fae6 | |
parent | 65dfc485adc1c5de4840f217c47c6ad79d26ae82 (diff) | |
download | qtbase-e45e6efa723297e8aaa1132251ef55b65201d174.tar.gz |
QTouchDevice: don't play ping-pong with q*PostRoutine()
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 <jan-arve.saether@qt.io>
-rw-r--r-- | src/gui/kernel/qtouchdevice.cpp | 32 |
1 files 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 <QCoreApplication> #include <private/qdebug_p.h> +#include <private/qlocking_p.h> QT_BEGIN_NAMESPACE @@ -201,15 +202,20 @@ void QTouchDevice::setName(const QString &name) d->name = name; } -typedef QList<const QTouchDevice *> TouchDevices; -Q_GLOBAL_STATIC(TouchDevices, deviceList) static QBasicMutex devicesMutex; -static void cleanupDevicesList() +struct TouchDevices { + TouchDevices(); + QList<const QTouchDevice *> 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<const QTouchDevice *> QTouchDevice::devices() { QMutexLocker lock(&devicesMutex); - return *deviceList(); + return deviceList->list; } /*! @@ -232,13 +238,13 @@ QList<const QTouchDevice *> 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<QTouchDevice *>(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 |