diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/thread/qlocking_p.h | 119 | ||||
-rw-r--r-- | src/corelib/thread/thread.pri | 1 | ||||
-rw-r--r-- | src/network/bearer/qbearerengine.cpp | 4 | ||||
-rw-r--r-- | src/network/bearer/qnetworkconfigmanager_p.cpp | 69 | ||||
-rw-r--r-- | src/plugins/bearer/connman/qconnmanengine.cpp | 45 |
5 files changed, 181 insertions, 57 deletions
diff --git a/src/corelib/thread/qlocking_p.h b/src/corelib/thread/qlocking_p.h new file mode 100644 index 0000000000..9a796cf7f7 --- /dev/null +++ b/src/corelib/thread/qlocking_p.h @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtCore module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** 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-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QLOCKING_P_H +#define QLOCKING_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of qmutex.cpp, qmutex_unix.cpp, and qmutex_win.cpp. This header +// file may change from version to version without notice, or even be +// removed. +// +// We mean it. +// + +#include <QtCore/qmutex.h> + +#include <mutex> + +QT_BEGIN_NAMESPACE + +// +// This API is bridging the time until we can depend on C++17: +// +// - qt_scoped_lock returns a lock that cannot be unlocked again before the end of the scope +// - qt_unique_lock returns a lock that can be unlock()ed and moved around +// - for compat with QMutexLocker, qt_unique_lock supports passing by pointer. +// Do NOT use this overload lightly; it's only for cases such as where a Q_GLOBAL_STATIC +// may have already been deleted. In particular, do NOT port from +// QMutexLocker locker(&mutex); +// to +// auto locker = qt_unique_lock(&mutex); +// as this will not port automatically to std::unique_lock come C++17! +// +// The intent, come C++17, is to replace +// qt_scoped_lock(mutex); +// qt_unique_lock(mutex); // except qt_unique_lock(&mutex) +// with +// std::scoped_lock(mutex); +// std::unique_lock(mutex); +// resp. (C++17 meaning CTAD, guaranteed copy elision + scoped_lock available on all platforms), +// so please use these functions only in ways which don't break this mechanical search & replace. +// + +namespace { + +template <typename Mutex, typename Lock = +#if defined(__cpp_guaranteed_copy_elision) && __cpp_guaranteed_copy_elision >= 201606L +# if defined(__cpp_lib_scoped_lock) && __cpp_lib_scoped_lock >= 201703L + std::scoped_lock +# else + std::lock_guard +# endif +#else + std::unique_lock +#endif + <typename std::decay<Mutex>::type> +> +Lock qt_scoped_lock(Mutex &mutex) +{ + return Lock(mutex); +} + +template <typename Mutex, typename Lock = std::unique_lock<typename std::decay<Mutex>::type>> +Lock qt_unique_lock(Mutex &mutex) +{ + return Lock(mutex); +} + +template <typename Mutex, typename Lock = std::unique_lock<typename std::decay<Mutex>::type>> +Lock qt_unique_lock(Mutex *mutex) +{ + return mutex ? Lock(*mutex) : Lock() ; +} + +} // unnamed namespace + +QT_END_NAMESPACE + +#endif // QLOCKING_P_H diff --git a/src/corelib/thread/thread.pri b/src/corelib/thread/thread.pri index d11e6500ff..9fc9af0e65 100644 --- a/src/corelib/thread/thread.pri +++ b/src/corelib/thread/thread.pri @@ -28,6 +28,7 @@ qtConfig(thread) { thread/qbasicatomic.h \ thread/qfutex_p.h \ thread/qgenericatomic.h \ + thread/qlocking_p.h \ thread/qmutex_p.h \ thread/qorderedmutexlocker_p.h \ thread/qreadwritelock_p.h \ diff --git a/src/network/bearer/qbearerengine.cpp b/src/network/bearer/qbearerengine.cpp index a7c139fe63..06bf449611 100644 --- a/src/network/bearer/qbearerengine.cpp +++ b/src/network/bearer/qbearerengine.cpp @@ -38,6 +38,8 @@ ****************************************************************************/ #include "qbearerengine_p.h" +#include <QtCore/private/qlocking_p.h> + #include <algorithm> #ifndef QT_NO_BEARERMANAGEMENT @@ -86,7 +88,7 @@ bool QBearerEngine::requiresPolling() const */ bool QBearerEngine::configurationsInUse() const { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); return hasUsedConfiguration(accessPointConfigurations) || hasUsedConfiguration(snapConfigurations) || hasUsedConfiguration(userChoiceConfigurations); diff --git a/src/network/bearer/qnetworkconfigmanager_p.cpp b/src/network/bearer/qnetworkconfigmanager_p.cpp index 91ea063af1..b432444669 100644 --- a/src/network/bearer/qnetworkconfigmanager_p.cpp +++ b/src/network/bearer/qnetworkconfigmanager_p.cpp @@ -45,6 +45,7 @@ #include <QtCore/qstringlist.h> #include <QtCore/qthread.h> #include <QtCore/private/qcoreapplication_p.h> +#include <QtCore/private/qlocking_p.h> #include <QtCore/private/qthread_p.h> #include <QtCore/qbytearray.h> @@ -115,10 +116,10 @@ QNetworkConfiguration QNetworkConfigurationManagerPrivate::defaultConfiguration( QNetworkConfigurationPrivatePointer defaultConfiguration; for (QBearerEngine *engine : sessionEngines) { - QMutexLocker locker(&engine->mutex); + const auto locker = qt_scoped_lock(engine->mutex); for (const auto &ptr : qAsConst(engine->snapConfigurations)) { - QMutexLocker configLocker(&ptr->mutex); + const auto locker = qt_scoped_lock(ptr->mutex); if ((ptr->state & QNetworkConfiguration::Active) == QNetworkConfiguration::Active) { QNetworkConfiguration config; @@ -211,11 +212,11 @@ QList<QNetworkConfiguration> QNetworkConfigurationManagerPrivate::allConfigurati for (QBearerEngine *engine : sessionEngines) { - QMutexLocker locker(&engine->mutex); + const auto locker = qt_scoped_lock(engine->mutex); //find all InternetAccessPoints for (const auto &ptr : qAsConst(engine->accessPointConfigurations)) { - QMutexLocker configLocker(&ptr->mutex); + const auto locker = qt_scoped_lock(ptr->mutex); if ((ptr->state & filter) == filter) { QNetworkConfiguration pt; @@ -226,7 +227,7 @@ QList<QNetworkConfiguration> QNetworkConfigurationManagerPrivate::allConfigurati //find all service networks for (const auto &ptr : qAsConst(engine->snapConfigurations)) { - QMutexLocker configLocker(&ptr->mutex); + const auto locker = qt_scoped_lock(ptr->mutex); if ((ptr->state & filter) == filter) { QNetworkConfiguration pt; @@ -243,10 +244,10 @@ QNetworkConfiguration QNetworkConfigurationManagerPrivate::configurationFromIden { QNetworkConfiguration item; - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); for (QBearerEngine *engine : sessionEngines) { - QMutexLocker locker(&engine->mutex); + const auto locker = qt_scoped_lock(engine->mutex); if (auto ptr = engine->accessPointConfigurations.value(identifier)) { item.d = std::move(ptr); break; @@ -266,7 +267,7 @@ QNetworkConfiguration QNetworkConfigurationManagerPrivate::configurationFromIden bool QNetworkConfigurationManagerPrivate::isOnline() const { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); // We need allConfigurations since onlineConfigurations is filled with queued connections // and thus is not always (more importantly just after creation) up to date @@ -275,7 +276,7 @@ bool QNetworkConfigurationManagerPrivate::isOnline() const QNetworkConfigurationManager::Capabilities QNetworkConfigurationManagerPrivate::capabilities() const { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); QNetworkConfigurationManager::Capabilities capFlags; @@ -287,7 +288,7 @@ QNetworkConfigurationManager::Capabilities QNetworkConfigurationManagerPrivate:: void QNetworkConfigurationManagerPrivate::configurationAdded(QNetworkConfigurationPrivatePointer ptr) { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); if (!firstUpdate) { QNetworkConfiguration item; @@ -295,24 +296,24 @@ void QNetworkConfigurationManagerPrivate::configurationAdded(QNetworkConfigurati emit configurationAdded(item); } - ptr->mutex.lock(); + auto ptrLocker = qt_unique_lock(ptr->mutex); if (ptr->state == QNetworkConfiguration::Active) { - ptr->mutex.unlock(); - onlineConfigurations.insert(ptr->id); + const auto id = ptr->id; + ptrLocker.unlock(); + onlineConfigurations.insert(id); if (!firstUpdate && onlineConfigurations.count() == 1) emit onlineStateChanged(true); - } else { - ptr->mutex.unlock(); } } void QNetworkConfigurationManagerPrivate::configurationRemoved(QNetworkConfigurationPrivatePointer ptr) { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); - ptr->mutex.lock(); - ptr->isValid = false; - ptr->mutex.unlock(); + { + const auto locker = qt_scoped_lock(ptr->mutex); + ptr->isValid = false; + } if (!firstUpdate) { QNetworkConfiguration item; @@ -327,7 +328,7 @@ void QNetworkConfigurationManagerPrivate::configurationRemoved(QNetworkConfigura void QNetworkConfigurationManagerPrivate::configurationChanged(QNetworkConfigurationPrivatePointer ptr) { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); if (!firstUpdate) { QNetworkConfiguration item; @@ -337,12 +338,13 @@ void QNetworkConfigurationManagerPrivate::configurationChanged(QNetworkConfigura bool previous = !onlineConfigurations.isEmpty(); - ptr->mutex.lock(); - if (ptr->state == QNetworkConfiguration::Active) - onlineConfigurations.insert(ptr->id); - else - onlineConfigurations.remove(ptr->id); - ptr->mutex.unlock(); + { + const auto locker = qt_scoped_lock(ptr->mutex); + if (ptr->state == QNetworkConfiguration::Active) + onlineConfigurations.insert(ptr->id); + else + onlineConfigurations.remove(ptr->id); + } bool online = !onlineConfigurations.isEmpty(); @@ -354,7 +356,8 @@ void QNetworkConfigurationManagerPrivate::updateConfigurations() { typedef QMultiMap<int, QString> PluginKeyMap; typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator; - QMutexLocker locker(&mutex); + + auto locker = qt_unique_lock(mutex); if (firstUpdate) { if (qobject_cast<QBearerEngine *>(sender())) @@ -432,7 +435,7 @@ void QNetworkConfigurationManagerPrivate::updateConfigurations() void QNetworkConfigurationManagerPrivate::performAsyncConfigurationUpdate() { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); if (sessionEngines.isEmpty()) { emit configurationUpdateComplete(); @@ -449,14 +452,14 @@ void QNetworkConfigurationManagerPrivate::performAsyncConfigurationUpdate() QList<QBearerEngine *> QNetworkConfigurationManagerPrivate::engines() const { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); return sessionEngines; } void QNetworkConfigurationManagerPrivate::startPolling() { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); if (!pollTimer) { pollTimer = new QTimer(this); bool ok; @@ -482,7 +485,7 @@ void QNetworkConfigurationManagerPrivate::startPolling() void QNetworkConfigurationManagerPrivate::pollEngines() { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); for (QBearerEngine *engine : qAsConst(sessionEngines)) { if (engine->requiresPolling() && (forcedPolling || engine->configurationsInUse())) { @@ -494,7 +497,7 @@ void QNetworkConfigurationManagerPrivate::pollEngines() void QNetworkConfigurationManagerPrivate::enablePolling() { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); ++forcedPolling; @@ -504,7 +507,7 @@ void QNetworkConfigurationManagerPrivate::enablePolling() void QNetworkConfigurationManagerPrivate::disablePolling() { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); --forcedPolling; } diff --git a/src/plugins/bearer/connman/qconnmanengine.cpp b/src/plugins/bearer/connman/qconnmanengine.cpp index eabae5a07b..8b2076bd18 100644 --- a/src/plugins/bearer/connman/qconnmanengine.cpp +++ b/src/plugins/bearer/connman/qconnmanengine.cpp @@ -46,6 +46,7 @@ #include <QtNetwork/qnetworksession.h> #include <QtCore/qdebug.h> +#include <QtCore/private/qlocking_p.h> #include <QtDBus/QtDBus> #include <QtDBus/QDBusConnection> @@ -74,13 +75,13 @@ QConnmanEngine::~QConnmanEngine() bool QConnmanEngine::connmanAvailable() const { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); return connmanManager->isValid(); } void QConnmanEngine::initialize() { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); connect(ofonoManager,SIGNAL(modemChanged()),this,SLOT(changedModem())); ofonoNetwork = new QOfonoNetworkRegistrationInterface(ofonoManager->currentModem(),this); @@ -101,7 +102,7 @@ void QConnmanEngine::initialize() void QConnmanEngine::changedModem() { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); if (ofonoNetwork) delete ofonoNetwork; @@ -114,7 +115,7 @@ void QConnmanEngine::changedModem() void QConnmanEngine::servicesReady(const QStringList &list) { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); for (const QString &servPath : list) addServiceConfiguration(servPath); @@ -123,7 +124,7 @@ void QConnmanEngine::servicesReady(const QStringList &list) QList<QNetworkConfigurationPrivate *> QConnmanEngine::getConfigurations() { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); QList<QNetworkConfigurationPrivate *> fetchedConfigurations; QNetworkConfigurationPrivate* cpPriv = 0; const int numFoundConfigurations = foundConfigurations.count(); @@ -150,19 +151,19 @@ QList<QNetworkConfigurationPrivate *> QConnmanEngine::getConfigurations() QString QConnmanEngine::getInterfaceFromId(const QString &id) { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); return configInterfaces.value(id); } bool QConnmanEngine::hasIdentifier(const QString &id) { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); return accessPointConfigurations.contains(id); } void QConnmanEngine::connectToId(const QString &id) { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); QConnmanServiceInterface *serv = connmanServiceInterfaces.value(id); @@ -184,7 +185,7 @@ void QConnmanEngine::connectToId(const QString &id) void QConnmanEngine::disconnectFromId(const QString &id) { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); QConnmanServiceInterface *serv = connmanServiceInterfaces.value(id); if (!serv || !serv->isValid()) { @@ -196,7 +197,7 @@ void QConnmanEngine::disconnectFromId(const QString &id) void QConnmanEngine::requestUpdate() { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); QTimer::singleShot(0, this, SLOT(doRequestUpdate())); } @@ -215,7 +216,7 @@ void QConnmanEngine::finishedScan(bool error) void QConnmanEngine::updateServices(const ConnmanMapList &changed, const QList<QDBusObjectPath> &removed) { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); foreach (const QDBusObjectPath &objectPath, removed) { removeConfiguration(objectPath.path()); @@ -234,7 +235,7 @@ void QConnmanEngine::updateServices(const ConnmanMapList &changed, const QList<Q QNetworkSession::State QConnmanEngine::sessionStateForId(const QString &id) { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); QNetworkConfigurationPrivatePointer ptr = accessPointConfigurations.value(id); @@ -275,7 +276,7 @@ QNetworkSession::State QConnmanEngine::sessionStateForId(const QString &id) quint64 QConnmanEngine::bytesWritten(const QString &id) {//TODO use connman counter API - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); quint64 result = 0; QString devFile = getInterfaceFromId(id); QFile tx("/sys/class/net/"+devFile+"/statistics/tx_bytes"); @@ -290,7 +291,7 @@ quint64 QConnmanEngine::bytesWritten(const QString &id) quint64 QConnmanEngine::bytesReceived(const QString &id) {//TODO use connman counter API - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); quint64 result = 0; QString devFile = getInterfaceFromId(id); QFile rx("/sys/class/net/"+devFile+"/statistics/rx_bytes"); @@ -305,7 +306,7 @@ quint64 QConnmanEngine::bytesReceived(const QString &id) quint64 QConnmanEngine::startTime(const QString &/*id*/) { // TODO - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); if (activeTime.isNull()) { return 0; } @@ -327,7 +328,7 @@ QNetworkSessionPrivate *QConnmanEngine::createSessionBackend() QNetworkConfigurationPrivatePointer QConnmanEngine::defaultConfiguration() { - const QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); const auto servPaths = connmanManager->getServices(); for (const QString &servPath : servPaths) { if (connmanServiceInterfaces.contains(servPath)) { @@ -352,7 +353,7 @@ void QConnmanEngine::configurationChange(QConnmanServiceInterface *serv) { if (!serv) return; - QMutexLocker locker(&mutex); + auto locker = qt_unique_lock(mutex); QString id = serv->path(); if (accessPointConfigurations.contains(id)) { @@ -381,7 +382,7 @@ void QConnmanEngine::configurationChange(QConnmanServiceInterface *serv) if (changed) { locker.unlock(); emit configurationChanged(ptr); - locker.relock(); + locker.lock(); } } @@ -391,7 +392,7 @@ void QConnmanEngine::configurationChange(QConnmanServiceInterface *serv) QNetworkConfiguration::StateFlags QConnmanEngine::getStateForService(const QString &service) { - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); QConnmanServiceInterface *serv = connmanServiceInterfaces.value(service); if (!serv) return QNetworkConfiguration::Undefined; @@ -472,7 +473,7 @@ bool QConnmanEngine::isRoamingAllowed(const QString &context) void QConnmanEngine::removeConfiguration(const QString &id) { - QMutexLocker locker(&mutex); + auto locker = qt_unique_lock(mutex); if (accessPointConfigurations.contains(id)) { @@ -485,13 +486,12 @@ void QConnmanEngine::removeConfiguration(const QString &id) foundConfigurations.removeOne(ptr.data()); locker.unlock(); emit configurationRemoved(ptr); - locker.relock(); } } void QConnmanEngine::addServiceConfiguration(const QString &servicePath) { - QMutexLocker locker(&mutex); + auto locker = qt_unique_lock(mutex); if (!connmanServiceInterfaces.contains(servicePath)) { QConnmanServiceInterface *serv = new QConnmanServiceInterface(servicePath, this); connmanServiceInterfaces.insert(serv->path(),serv); @@ -547,7 +547,6 @@ void QConnmanEngine::addServiceConfiguration(const QString &servicePath) locker.unlock(); Q_EMIT configurationAdded(ptr); - locker.relock(); } } |