summaryrefslogtreecommitdiffstats
path: root/src/network/kernel
diff options
context:
space:
mode:
authorQt Forward Merge Bot <qt_forward_merge_bot@qt-project.org>2019-07-29 01:00:11 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2019-07-29 11:56:00 +0200
commit154155f588c89d43362abb58bcf91449c78e3ff8 (patch)
treee72e8040684ab1c51c7ef67d9369a253730da6e4 /src/network/kernel
parent28af6e97e5fe7f844bc85df7af22ce8b7414f5a6 (diff)
parente24a4976bebd7ca90deac2b40c08900625773a99 (diff)
Merge remote-tracking branch 'origin/5.12' into 5.13
Conflicts: src/corelib/io/qresource.cpp Change-Id: I54917f72444a621bd08aeaa15f5d17415993144d
Diffstat (limited to 'src/network/kernel')
-rw-r--r--src/network/kernel/qhostinfo.cpp50
-rw-r--r--src/network/kernel/qhostinfo_p.h9
2 files changed, 49 insertions, 10 deletions
diff --git a/src/network/kernel/qhostinfo.cpp b/src/network/kernel/qhostinfo.cpp
index 12fd257c11..f4348b690e 100644
--- a/src/network/kernel/qhostinfo.cpp
+++ b/src/network/kernel/qhostinfo.cpp
@@ -106,11 +106,39 @@ int get_signal_index()
return signal_index + QMetaObjectPrivate::signalOffset(senderMetaObject);
}
-void emit_results_ready(const QHostInfo &hostInfo, const QObject *receiver,
- QtPrivate::QSlotObjectBase *slotObj)
+}
+
+/*
+ The calling thread is likely the one that executes the lookup via
+ QHostInfoRunnable. Unless we operate with a queued connection already,
+ posts the QHostInfo to a dedicated QHostInfoResult object that lives in
+ the same thread as the user-provided receiver, or (if there is none) in
+ the thread that made the call to lookupHost. That QHostInfoResult object
+ then calls the user code in the correct thread.
+
+ The 'result' object deletes itself (via deleteLater) when the metacall
+ event is received.
+*/
+void QHostInfoResult::postResultsReady(const QHostInfo &info)
{
+ // queued connection will take care of dispatching to right thread
+ if (!slotObj) {
+ emitResultsReady(info);
+ return;
+ }
static const int signal_index = get_signal_index();
+
+ // we used to have a context object, but it's already destroyed
+ if (withContextObject && !receiver)
+ return;
+
+ /* QHostInfoResult c'tor moves the result object to the thread of receiver.
+ If we don't have a receiver, then the result object will not live in a
+ thread that runs an event loop - so move it to this' thread, which is the thread
+ that initiated the lookup, and required to have a running event loop. */
auto result = new QHostInfoResult(receiver, slotObj);
+ if (!receiver)
+ result->moveToThread(thread());
Q_CHECK_PTR(result);
const int nargs = 2;
auto types = reinterpret_cast<int *>(malloc(nargs * sizeof(int)));
@@ -120,15 +148,13 @@ void emit_results_ready(const QHostInfo &hostInfo, const QObject *receiver,
auto args = reinterpret_cast<void **>(malloc(nargs * sizeof(void *)));
Q_CHECK_PTR(args);
args[0] = 0;
- args[1] = QMetaType::create(types[1], &hostInfo);
+ args[1] = QMetaType::create(types[1], &info);
Q_CHECK_PTR(args[1]);
auto metaCallEvent = new QMetaCallEvent(slotObj, nullptr, signal_index, nargs, types, args);
Q_CHECK_PTR(metaCallEvent);
qApp->postEvent(result, metaCallEvent);
}
-}
-
/*!
\class QHostInfo
\brief The QHostInfo class provides static functions for host name lookups.
@@ -316,6 +342,10 @@ int QHostInfo::lookupHost(const QString &name, QObject *receiver,
ready, the \a functor is called with a QHostInfo argument. The
QHostInfo object can then be inspected to get the results of the
lookup.
+
+ The \a functor will be run in the thread that makes the call to lookupHost;
+ that thread must have a running Qt event loop.
+
\note There is no guarantee on the order the signals will be emitted
if you start multiple requests with lookupHost().
@@ -623,7 +653,8 @@ int QHostInfo::lookupHostImpl(const QString &name,
QHostInfo hostInfo(id);
hostInfo.setError(QHostInfo::HostNotFound);
hostInfo.setErrorString(QCoreApplication::translate("QHostInfo", "No host name given"));
- emit_results_ready(hostInfo, receiver, slotObj);
+ QHostInfoResult result(receiver, slotObj);
+ result.postResultsReady(hostInfo);
return id;
}
@@ -637,7 +668,8 @@ int QHostInfo::lookupHostImpl(const QString &name,
QHostInfo info = manager->cache.get(name, &valid);
if (valid) {
info.setLookupId(id);
- emit_results_ready(info, receiver, slotObj);
+ QHostInfoResult result(receiver, slotObj);
+ result.postResultsReady(info);
return id;
}
}
@@ -698,7 +730,7 @@ void QHostInfoRunnable::run()
// signal emission
hostInfo.setLookupId(id);
- resultEmitter.emitResultsReady(hostInfo);
+ resultEmitter.postResultsReady(hostInfo);
#if QT_CONFIG(thread)
// now also iterate through the postponed ones
@@ -711,7 +743,7 @@ void QHostInfoRunnable::run()
QHostInfoRunnable* postponed = *it;
// we can now emit
hostInfo.setLookupId(postponed->id);
- postponed->resultEmitter.emitResultsReady(hostInfo);
+ postponed->resultEmitter.postResultsReady(hostInfo);
delete postponed;
}
manager->postponedLookups.erase(partitionBegin, partitionEnd);
diff --git a/src/network/kernel/qhostinfo_p.h b/src/network/kernel/qhostinfo_p.h
index 8898d6ff50..bbf4cc36d1 100644
--- a/src/network/kernel/qhostinfo_p.h
+++ b/src/network/kernel/qhostinfo_p.h
@@ -84,12 +84,14 @@ class QHostInfoResult : public QObject
QPointer<const QObject> receiver = nullptr;
QtPrivate::QSlotObjectBase *slotObj = nullptr;
+ const bool withContextObject = false;
public:
QHostInfoResult() = default;
QHostInfoResult(const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj) :
receiver(receiver),
- slotObj(slotObj)
+ slotObj(slotObj),
+ withContextObject(slotObj && receiver)
{
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, this,
&QObject::deleteLater);
@@ -97,10 +99,15 @@ public:
moveToThread(receiver->thread());
}
+ void postResultsReady(const QHostInfo &info);
+
public Q_SLOTS:
inline void emitResultsReady(const QHostInfo &info)
{
if (slotObj) {
+ // we used to have a context object, but it's already destroyed
+ if (withContextObject && !receiver)
+ return;
QHostInfo copy = info;
void *args[2] = { 0, reinterpret_cast<void *>(&copy) };
slotObj->call(const_cast<QObject*>(receiver.data()), args);