summaryrefslogtreecommitdiffstats
path: root/src/network/kernel
diff options
context:
space:
mode:
authorLiang Qi <liang.qi@qt.io>2016-05-12 07:31:50 +0200
committerLiang Qi <liang.qi@qt.io>2016-05-12 08:33:08 +0200
commit990969655c5fb4d03682e96df9b12101f5ee9815 (patch)
treeb8fb5c50285105c8bc5a938fb50f93ff9f24889d /src/network/kernel
parenta213011a53f12f101d08a04afc8fdacd2d54a232 (diff)
parente64b2234e829cc47872225debcf80d6c06db18f0 (diff)
Merge remote-tracking branch 'origin/5.7' into dev
Conflicts: config_help.txt configure src/corelib/io/qprocess_wince.cpp src/plugins/platforms/windows/qwindowstheme.cpp src/plugins/platforms/xcb/qxcbbackingstore.cpp tests/auto/corelib/tools/qtimezone/BLACKLIST tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp Change-Id: I26644d1cb3b78412c8ff285e2a55bea1bd641c01
Diffstat (limited to 'src/network/kernel')
-rw-r--r--src/network/kernel/qdnslookup.cpp22
-rw-r--r--src/network/kernel/qdnslookup_unix.cpp17
-rw-r--r--src/network/kernel/qdnslookup_winrt.cpp2
-rw-r--r--src/network/kernel/qhostinfo.cpp85
-rw-r--r--src/network/kernel/qhostinfo_unix.cpp17
-rw-r--r--src/network/kernel/qhostinfo_win.cpp14
-rw-r--r--src/network/kernel/qnetworkinterface.cpp14
-rw-r--r--src/network/kernel/qnetworkproxy.cpp2
-rw-r--r--src/network/kernel/qnetworkproxy_generic.cpp2
-rw-r--r--src/network/kernel/qnetworkproxy_libproxy.cpp4
-rw-r--r--src/network/kernel/qnetworkproxy_win.cpp20
11 files changed, 96 insertions, 103 deletions
diff --git a/src/network/kernel/qdnslookup.cpp b/src/network/kernel/qdnslookup.cpp
index d8f8d78a0f..02df00a590 100644
--- a/src/network/kernel/qdnslookup.cpp
+++ b/src/network/kernel/qdnslookup.cpp
@@ -76,11 +76,11 @@ static void qt_qdnsmailexchangerecord_sort(QList<QDnsMailExchangeRecord> &record
// Determine the slice of records with the current preference.
QList<QDnsMailExchangeRecord> slice;
- const quint16 slicePreference = records[i].preference();
+ const quint16 slicePreference = records.at(i).preference();
for (int j = i; j < records.size(); ++j) {
- if (records[j].preference() != slicePreference)
+ if (records.at(j).preference() != slicePreference)
break;
- slice << records[j];
+ slice << records.at(j);
}
// Randomize the slice of records.
@@ -119,13 +119,13 @@ static void qt_qdnsservicerecord_sort(QList<QDnsServiceRecord> &records)
// Determine the slice of records with the current priority.
QList<QDnsServiceRecord> slice;
- const quint16 slicePriority = records[i].priority();
+ const quint16 slicePriority = records.at(i).priority();
unsigned int sliceWeight = 0;
for (int j = i; j < records.size(); ++j) {
- if (records[j].priority() != slicePriority)
+ if (records.at(j).priority() != slicePriority)
break;
- sliceWeight += records[j].weight();
- slice << records[j];
+ sliceWeight += records.at(j).weight();
+ slice << records.at(j);
}
#ifdef QDNSLOOKUP_DEBUG
qDebug("qt_qdnsservicerecord_sort() : priority %i (size: %i, total weight: %i)",
@@ -137,15 +137,15 @@ static void qt_qdnsservicerecord_sort(QList<QDnsServiceRecord> &records)
const unsigned int weightThreshold = qrand() % (sliceWeight + 1);
unsigned int summedWeight = 0;
for (int j = 0; j < slice.size(); ++j) {
- summedWeight += slice[j].weight();
+ summedWeight += slice.at(j).weight();
if (summedWeight >= weightThreshold) {
#ifdef QDNSLOOKUP_DEBUG
qDebug("qt_qdnsservicerecord_sort() : adding %s %i (weight: %i)",
- qPrintable(slice[j].target()), slice[j].port(),
- slice[j].weight());
+ qPrintable(slice.at(j).target()), slice.at(j).port(),
+ slice.at(j).weight());
#endif
// Adjust the slice weight and take the current record.
- sliceWeight -= slice[j].weight();
+ sliceWeight -= slice.at(j).weight();
records[i++] = slice.takeAt(j);
break;
}
diff --git a/src/network/kernel/qdnslookup_unix.cpp b/src/network/kernel/qdnslookup_unix.cpp
index d2c5542139..35981a2f2c 100644
--- a/src/network/kernel/qdnslookup_unix.cpp
+++ b/src/network/kernel/qdnslookup_unix.cpp
@@ -42,7 +42,6 @@
#include <qlibrary.h>
#include <qscopedpointer.h>
#include <qurl.h>
-#include <private/qmutexpool_p.h>
#include <private/qnativesocketengine_p.h>
#include <sys/types.h>
@@ -78,7 +77,7 @@ struct QDnsLookupStateDeleter
}
};
-static void resolveLibrary()
+static bool resolveLibraryInternal()
{
QLibrary lib;
#ifdef LIBRESOLV_SO
@@ -88,7 +87,7 @@ static void resolveLibrary()
{
lib.setFileName(QLatin1String("resolv"));
if (!lib.load())
- return;
+ return false;
}
local_dn_expand = dn_expand_proto(lib.resolve("__dn_expand"));
@@ -112,19 +111,15 @@ static void resolveLibrary()
local_res_nquery = res_nquery_proto(lib.resolve("res_9_nquery"));
if (!local_res_nquery)
local_res_nquery = res_nquery_proto(lib.resolve("res_nquery"));
+
+ return true;
}
+Q_GLOBAL_STATIC_WITH_ARGS(bool, resolveLibrary, (resolveLibraryInternal()))
void QDnsLookupRunnable::query(const int requestType, const QByteArray &requestName, const QHostAddress &nameserver, QDnsLookupReply *reply)
{
// Load dn_expand, res_ninit and res_nquery on demand.
- static QBasicAtomicInt triedResolve = Q_BASIC_ATOMIC_INITIALIZER(false);
- if (!triedResolve.loadAcquire()) {
- QMutexLocker locker(QMutexPool::globalInstanceGet(&local_res_ninit));
- if (!triedResolve.load()) {
- resolveLibrary();
- triedResolve.storeRelease(true);
- }
- }
+ resolveLibrary();
// If dn_expand, res_ninit or res_nquery is missing, fail.
if (!local_dn_expand || !local_res_nclose || !local_res_ninit || !local_res_nquery) {
diff --git a/src/network/kernel/qdnslookup_winrt.cpp b/src/network/kernel/qdnslookup_winrt.cpp
index b459deb1ed..30510d89fc 100644
--- a/src/network/kernel/qdnslookup_winrt.cpp
+++ b/src/network/kernel/qdnslookup_winrt.cpp
@@ -146,7 +146,7 @@ void QDnsLookupRunnable::query(const int requestType, const QByteArray &requestN
PCWSTR rawString = name.GetRawBuffer(&length);
addresses.insert(QHostAddress(QString::fromWCharArray(rawString, length)));
}
- foreach (const QHostAddress &address, addresses) {
+ for (const QHostAddress &address : qAsConst(addresses)) {
QDnsHostAddressRecord record;
record.d->name = aceHostname;
record.d->value = address;
diff --git a/src/network/kernel/qhostinfo.cpp b/src/network/kernel/qhostinfo.cpp
index 293633d6bc..88df65dbcb 100644
--- a/src/network/kernel/qhostinfo.cpp
+++ b/src/network/kernel/qhostinfo.cpp
@@ -79,6 +79,22 @@ bool any_of(InputIt first, InputIt last, UnaryPredicate p)
{
return std::find_if(first, last, p) != last;
}
+
+template <typename InputIt, typename OutputIt1, typename OutputIt2, typename UnaryPredicate>
+std::pair<OutputIt1, OutputIt2> separate_if(InputIt first, InputIt last, OutputIt1 dest1, OutputIt2 dest2, UnaryPredicate p)
+{
+ while (first != last) {
+ if (p(*first)) {
+ *dest1 = *first;
+ ++dest1;
+ } else {
+ *dest2 = *first;
+ ++dest2;
+ }
+ ++first;
+ }
+ return std::make_pair(dest1, dest2);
+}
}
/*!
@@ -587,46 +603,37 @@ void QHostInfoLookupManager::work()
finishedLookups.clear();
}
- if (!postponedLookups.isEmpty()) {
- // try to start the postponed ones
-
- QMutableListIterator<QHostInfoRunnable*> iterator(postponedLookups);
- while (iterator.hasNext()) {
- QHostInfoRunnable* postponed = iterator.next();
-
- // check if none of the postponed hostnames is currently running
- const bool alreadyRunning = any_of(currentLookups.cbegin(), currentLookups.cend(), ToBeLookedUpEquals(postponed->toBeLookedUp));
- if (!alreadyRunning) {
- iterator.remove();
- scheduledLookups.prepend(postponed); // prepend! we want to finish it ASAP
- }
+ auto isAlreadyRunning = [this](QHostInfoRunnable *lookup) {
+ return any_of(currentLookups.cbegin(), currentLookups.cend(), ToBeLookedUpEquals(lookup->toBeLookedUp));
+ };
+
+ // Transfer any postponed lookups that aren't currently running to the scheduled list, keeping already-running lookups:
+ postponedLookups.erase(separate_if(postponedLookups.begin(),
+ postponedLookups.end(),
+ postponedLookups.begin(),
+ std::front_inserter(scheduledLookups), // prepend! we want to finish it ASAP
+ isAlreadyRunning).first,
+ postponedLookups.end());
+
+ // Unschedule and postpone any that are currently running:
+ scheduledLookups.erase(separate_if(scheduledLookups.begin(),
+ scheduledLookups.end(),
+ std::back_inserter(postponedLookups),
+ scheduledLookups.begin(),
+ isAlreadyRunning).second,
+ scheduledLookups.end());
+
+ const int availableThreads = threadPool.maxThreadCount() - currentLookups.size();
+ if (availableThreads > 0) {
+ int readyToStartCount = qMin(availableThreads, scheduledLookups.size());
+ auto it = scheduledLookups.begin();
+ while (readyToStartCount--) {
+ // runnable now running in new thread, track this in currentLookups
+ threadPool.start(*it);
+ currentLookups.push_back(std::move(*it));
+ ++it;
}
- }
-
- if (!scheduledLookups.isEmpty()) {
- // try to start the new ones
- QMutableListIterator<QHostInfoRunnable*> iterator(scheduledLookups);
- while (iterator.hasNext()) {
- QHostInfoRunnable *scheduled = iterator.next();
-
- // check if a lookup for this host is already running, then postpone
- const bool alreadyRunning = any_of(currentLookups.cbegin(), currentLookups.cend(), ToBeLookedUpEquals(scheduled->toBeLookedUp));
- if (alreadyRunning) {
- iterator.remove();
- postponedLookups.append(scheduled);
- scheduled = 0;
- }
-
- if (scheduled && currentLookups.size() < threadPool.maxThreadCount()) {
- // runnable now running in new thread, track this in currentLookups
- threadPool.start(scheduled);
- iterator.remove();
- currentLookups.append(scheduled);
- } else {
- // was postponed, continue iterating
- continue;
- }
- };
+ scheduledLookups.erase(scheduledLookups.begin(), it);
}
}
diff --git a/src/network/kernel/qhostinfo_unix.cpp b/src/network/kernel/qhostinfo_unix.cpp
index 8a3f3fa5c9..7af8db73e0 100644
--- a/src/network/kernel/qhostinfo_unix.cpp
+++ b/src/network/kernel/qhostinfo_unix.cpp
@@ -49,7 +49,6 @@
#include <qbasicatomic.h>
#include <qurl.h>
#include <qfile.h>
-#include <private/qmutexpool_p.h>
#include <private/qnet_unix_p.h>
#include <sys/types.h>
@@ -92,7 +91,7 @@ typedef void (*res_nclose_proto)(res_state_ptr);
static res_nclose_proto local_res_nclose = 0;
static res_state_ptr local_res = 0;
-static void resolveLibrary()
+static bool resolveLibraryInternal()
{
#if !defined(QT_NO_LIBRARY) && !defined(Q_OS_QNX)
QLibrary lib;
@@ -103,7 +102,7 @@ static void resolveLibrary()
{
lib.setFileName(QLatin1String("resolv"));
if (!lib.load())
- return;
+ return false;
}
local_res_init = res_init_proto(lib.resolve("__res_init"));
@@ -125,7 +124,10 @@ static void resolveLibrary()
local_res_ninit = 0;
}
#endif
+
+ return true;
}
+Q_GLOBAL_STATIC_WITH_ARGS(bool, resolveLibrary, (resolveLibraryInternal()))
QHostInfo QHostInfoAgent::fromName(const QString &hostName)
{
@@ -137,14 +139,7 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName)
#endif
// Load res_init on demand.
- static QBasicAtomicInt triedResolve = Q_BASIC_ATOMIC_INITIALIZER(false);
- if (!triedResolve.loadAcquire()) {
- QMutexLocker locker(QMutexPool::globalInstanceGet(&local_res_init));
- if (!triedResolve.load()) {
- resolveLibrary();
- triedResolve.storeRelease(true);
- }
- }
+ resolveLibrary();
// If res_init is available, poll it.
if (local_res_init)
diff --git a/src/network/kernel/qhostinfo_win.cpp b/src/network/kernel/qhostinfo_win.cpp
index 1d34ae8277..9e5d556f2b 100644
--- a/src/network/kernel/qhostinfo_win.cpp
+++ b/src/network/kernel/qhostinfo_win.cpp
@@ -45,7 +45,6 @@
#include <qmutex.h>
#include <qbasicatomic.h>
#include <qurl.h>
-#include <private/qmutexpool_p.h>
QT_BEGIN_NAMESPACE
@@ -78,7 +77,7 @@ static getnameinfoProto local_getnameinfo = 0;
static getaddrinfoProto local_getaddrinfo = 0;
static freeaddrinfoProto local_freeaddrinfo = 0;
-static void resolveLibrary()
+static bool resolveLibraryInternal()
{
// Attempt to resolve getaddrinfo(); without it we'll have to fall
// back to gethostbyname(), which has no IPv6 support.
@@ -91,7 +90,9 @@ static void resolveLibrary()
local_freeaddrinfo = (freeaddrinfoProto) QSystemLibrary::resolve(QLatin1String("ws2_32"), "freeaddrinfo");
local_getnameinfo = (getnameinfoProto) QSystemLibrary::resolve(QLatin1String("ws2_32"), "getnameinfo");
#endif
+ return true;
}
+Q_GLOBAL_STATIC_WITH_ARGS(bool, resolveLibrary, (resolveLibraryInternal()))
static void translateWSAError(int error, QHostInfo *results)
{
@@ -114,14 +115,7 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName)
QSysInfo::machineHostName(); // this initializes ws2_32.dll
// Load res_init on demand.
- static QBasicAtomicInt triedResolve = Q_BASIC_ATOMIC_INITIALIZER(false);
- if (!triedResolve.loadAcquire()) {
- QMutexLocker locker(QMutexPool::globalInstanceGet(&local_getaddrinfo));
- if (!triedResolve.load()) {
- resolveLibrary();
- triedResolve.storeRelease(true);
- }
- }
+ resolveLibrary();
QHostInfo results;
diff --git a/src/network/kernel/qnetworkinterface.cpp b/src/network/kernel/qnetworkinterface.cpp
index e55e113619..c64d8e13cd 100644
--- a/src/network/kernel/qnetworkinterface.cpp
+++ b/src/network/kernel/qnetworkinterface.cpp
@@ -120,11 +120,11 @@ QSharedDataPointer<QNetworkInterfacePrivate> QNetworkInterfaceManager::interface
QList<QSharedDataPointer<QNetworkInterfacePrivate> > QNetworkInterfaceManager::allInterfaces()
{
- QList<QNetworkInterfacePrivate *> list = postProcess(scan());
+ const QList<QNetworkInterfacePrivate *> list = postProcess(scan());
QList<QSharedDataPointer<QNetworkInterfacePrivate> > result;
result.reserve(list.size());
- foreach (QNetworkInterfacePrivate *ptr, list)
+ for (QNetworkInterfacePrivate *ptr : list)
result << QSharedDataPointer<QNetworkInterfacePrivate>(ptr);
return result;
@@ -611,10 +611,10 @@ QString QNetworkInterface::interfaceNameFromIndex(int index)
*/
QList<QNetworkInterface> QNetworkInterface::allInterfaces()
{
- QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
+ const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
QList<QNetworkInterface> result;
result.reserve(privs.size());
- foreach (const QSharedDataPointer<QNetworkInterfacePrivate> &p, privs) {
+ for (const auto &p : privs) {
QNetworkInterface item;
item.d = p;
result << item;
@@ -631,10 +631,10 @@ QList<QNetworkInterface> QNetworkInterface::allInterfaces()
*/
QList<QHostAddress> QNetworkInterface::allAddresses()
{
- QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
+ const QList<QSharedDataPointer<QNetworkInterfacePrivate> > privs = manager()->allInterfaces();
QList<QHostAddress> result;
- foreach (const QSharedDataPointer<QNetworkInterfacePrivate> &p, privs) {
- foreach (const QNetworkAddressEntry &entry, p->addressEntries)
+ for (const auto &p : privs) {
+ for (const QNetworkAddressEntry &entry : qAsConst(p->addressEntries))
result += entry.ip();
}
diff --git a/src/network/kernel/qnetworkproxy.cpp b/src/network/kernel/qnetworkproxy.cpp
index 22400ef9ab..ad78c48fd8 100644
--- a/src/network/kernel/qnetworkproxy.cpp
+++ b/src/network/kernel/qnetworkproxy.cpp
@@ -297,7 +297,7 @@ public:
QNetworkProxy applicationProxy()
{
- return proxyForQuery(QNetworkProxyQuery()).first();
+ return proxyForQuery(QNetworkProxyQuery()).constFirst();
}
QList<QNetworkProxy> proxyForQuery(const QNetworkProxyQuery &query);
diff --git a/src/network/kernel/qnetworkproxy_generic.cpp b/src/network/kernel/qnetworkproxy_generic.cpp
index db1083f3e0..e69870a98c 100644
--- a/src/network/kernel/qnetworkproxy_generic.cpp
+++ b/src/network/kernel/qnetworkproxy_generic.cpp
@@ -59,7 +59,7 @@ static bool ignoreProxyFor(const QNetworkProxyQuery &query)
const QList<QByteArray> noProxyTokens = noProxy.split(',');
- foreach (const QByteArray &rawToken, noProxyTokens) {
+ for (const QByteArray &rawToken : noProxyTokens) {
QByteArray token = rawToken.trimmed();
QString peerHostName = query.peerHostName();
diff --git a/src/network/kernel/qnetworkproxy_libproxy.cpp b/src/network/kernel/qnetworkproxy_libproxy.cpp
index e89df79226..184dc6469d 100644
--- a/src/network/kernel/qnetworkproxy_libproxy.cpp
+++ b/src/network/kernel/qnetworkproxy_libproxy.cpp
@@ -122,10 +122,10 @@ QList<QNetworkProxy> QNetworkProxyFactory::systemProxyForQuery(const QNetworkPro
return proxyList;
}
- QList<QUrl> rawProxies = libProxyWrapper()->getProxies(queryUrl);
+ const QList<QUrl> rawProxies = libProxyWrapper()->getProxies(queryUrl);
bool haveDirectConnection = false;
- foreach (const QUrl& url, rawProxies) {
+ for (const QUrl& url : rawProxies) {
QNetworkProxy::ProxyType type;
const QString scheme = url.scheme();
if (scheme == QLatin1String("http")) {
diff --git a/src/network/kernel/qnetworkproxy_win.cpp b/src/network/kernel/qnetworkproxy_win.cpp
index a37585b5cd..03109b4e46 100644
--- a/src/network/kernel/qnetworkproxy_win.cpp
+++ b/src/network/kernel/qnetworkproxy_win.cpp
@@ -191,14 +191,16 @@ static bool isBypassed(const QString &host, const QStringList &bypassList)
return true;
// does it match the list of exclusions?
- foreach (const QString &entry, bypassList) {
+ for (const QString &entry : bypassList) {
if (entry == QLatin1String("<local>")) {
if (isSimple)
return true;
if (isIpAddress) {
//exclude all local subnets
- foreach (const QNetworkInterface &iface, QNetworkInterface::allInterfaces()) {
- foreach (const QNetworkAddressEntry netaddr, iface.addressEntries()) {
+ const auto ifaces = QNetworkInterface::allInterfaces();
+ for (const QNetworkInterface &iface : ifaces) {
+ const auto netaddrs = iface.addressEntries();
+ for (const QNetworkAddressEntry &netaddr : netaddrs) {
if (ipAddress.isInSubnet(netaddr.ip(), netaddr.prefixLength())) {
return true;
}
@@ -238,7 +240,7 @@ static QList<QNetworkProxy> filterProxyListByCapabilities(const QList<QNetworkPr
break;
}
QList<QNetworkProxy> result;
- foreach (const QNetworkProxy& proxy, proxyList) {
+ for (const QNetworkProxy &proxy : proxyList) {
if (proxy.capabilities() & requiredCaps)
result.append(proxy);
}
@@ -248,7 +250,7 @@ static QList<QNetworkProxy> filterProxyListByCapabilities(const QList<QNetworkPr
static QList<QNetworkProxy> removeDuplicateProxies(const QList<QNetworkProxy> &proxyList)
{
QList<QNetworkProxy> result;
- foreach (QNetworkProxy proxy, proxyList) {
+ for (const QNetworkProxy &proxy : proxyList) {
bool append = true;
for (int i=0; i < result.count(); i++) {
if (proxy.hostName() == result.at(i).hostName()
@@ -280,7 +282,7 @@ static QList<QNetworkProxy> parseServerList(const QNetworkProxyQuery &query, con
QHash<QString, QNetworkProxy> taggedProxies;
const QString requiredTag = query.protocolTag();
bool checkTags = !requiredTag.isEmpty() && query.queryType() != QNetworkProxyQuery::TcpServer; //windows tags are only for clients
- foreach (const QString &entry, proxyList) {
+ for (const QString &entry : proxyList) {
int server = 0;
QNetworkProxy::ProxyType proxyType = QNetworkProxy::HttpProxy;
@@ -329,7 +331,7 @@ static QList<QNetworkProxy> parseServerList(const QNetworkProxyQuery &query, con
result << QNetworkProxy(proxyType, entry.mid(server, pos - server), port);
if (!protocolTag.isEmpty())
- taggedProxies.insert(protocolTag.toString(), result.last());
+ taggedProxies.insert(protocolTag.toString(), result.constLast());
}
if (checkTags && taggedProxies.contains(requiredTag)) {
@@ -389,9 +391,9 @@ public:
}
void clear() {
- foreach (HANDLE event, m_watchEvents)
+ for (HANDLE event : qAsConst(m_watchEvents))
CloseHandle(event);
- foreach (HKEY key, m_registryHandles)
+ for (HKEY key : qAsConst(m_registryHandles))
RegCloseKey(key);
m_watchEvents.clear();