summaryrefslogtreecommitdiffstats
path: root/src/network
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2019-07-05 12:39:57 +0200
committerMarc Mutz <marc.mutz@kdab.com>2019-08-13 05:05:59 +0000
commit09b0038513bb738a89d9a0f1e64f7084f2dd5da0 (patch)
treefa88d6046ffd4da8a74d8201fcf2924faab3a292 /src/network
parentd45908e24292a41ff7838366b34be7340bf9fda5 (diff)
Short live qt_unique_lock/qt_scoped_lock! (until C++17)
Now that QRecursiveMutex is getting split off of QMutex, QMutexLocker will stop working on QRecursiveMutex once the split has been finalized in Qt 6. Even today, QMutexLocker contains casts from QBasicMutex to QMutex that some reviewers are uncomfortable with. One way to carry QMutexLocker forward is to template it on the mutex type, possibly with aliases like QBasicMutexLocker and QRecursiveMutexLocker. C++17 code would then not require a port, thanks to CTAD. But we have the problem now, and we can't template QMutexLocker in Qt 5. The alternative is to look at std and realize that they have surpassed QMutexLocker in expressiveness already. A scoped_lock cannot be unlocked again, a unique_lock can be moved around. QMutexLocker doesn't do either. The only "problem" is that the std lock classes are already templates, but we can't, yet, rely on C++17 CTAD to make them look as if they weren't. So, prepare for a future with C++17 CTAD by writing factory functions, qt_scoped_lock and qt_unique_lock, which will later port mechanically to their C++17 equivalents (mostly). The functions are added to a new private qlocking_p.h becauee we don't want to make them public. These are for use in Qt's own implementation, or for users that don't care about compatibility and will not mind them to be removed once we depend on C++17. Originally, I planned to use qmutex_p.h instead, but that header is not self-contained and causes build errors when we started to include it into libraries other than QtCore. Regarding the return value of qt_scoped_lock: Ideally, we'd like to return a std::scoped_lock, but two things stand in the way: First, scoped_lock was only added in C++17 (we fall back to lock_guard if scoped_lock is not available). Second, returning one from a function requires C++17 guaranteed copy elision, because neither scoped_lock not lock_guard have a copy ctor. In order for code not to come to depend on a particular lock class, we return any of lock_guard, unique_lock or scoped_guard, depending on what the compiler supports, and therefore wrap the functions in the unnamed namespace to avoid running into ODR if (private) headers are used from different projects (autotests, e.g.). By the time we can drop them, however, qt_*_lock will be semantically 100% identical to their replacements. Port some initial users. Change-Id: I2a208ef2a4a533ee8e675812273986460e6b4d00 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/network')
-rw-r--r--src/network/bearer/qbearerengine.cpp4
-rw-r--r--src/network/bearer/qnetworkconfigmanager_p.cpp69
2 files changed, 39 insertions, 34 deletions
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;
}