summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qbasicfuturewatcher.cpp
diff options
context:
space:
mode:
authorArno Rehn <a.rehn@menlosystems.com>2023-05-08 15:28:11 +0200
committerArno Rehn <a.rehn@menlosystems.com>2023-05-30 21:42:46 +0200
commit07d6d31a4c0c17d8c897d783a9b0841df6834b02 (patch)
tree851fa5478368fb537523eed5ced7fb7581e5cc96 /src/corelib/thread/qbasicfuturewatcher.cpp
parent86c044176f16674616a20cd8e6a88b5a2dcf3775 (diff)
QFuture: Gracefully handle a destroyed context in continuations
This patch relaxes the requirements on the context object of continuations. Instead of having to stay alive during execution of the whole chain, it now only has to stay alive during setup of the chain. If the context object is destroyed before the chain finishes, the respective future is canceled. This patch works by using QFutureCallOutInterface and signals instead of direct invocation of the continuation by the parent future, similar to how QFutureWatcher is implemented. If a continuation is used with a context object, a QBasicFutureWatcher is connected to the QFuture via a QFutureCallOutInterface. When the future finishes, QBasicFutureWatcher::finished() triggers the continuation with a signal/slot connection. This way, we require the context object to stay alive only during setup; the required synchronization is guaranteed by the existing event and signal-slot mechanisms. The continuation itself does not need to know about the context object anymore. [ChangeLog][QtCore][QFuture] Added support for context objects of continuations being destroyed before the continuation finishes. In these cases the future is cancelled immediately. Fixes: QTBUG-112958 Change-Id: Ie0ef3470b2a0ccfa789d2ae7604b92e509c14591 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Diffstat (limited to 'src/corelib/thread/qbasicfuturewatcher.cpp')
-rw-r--r--src/corelib/thread/qbasicfuturewatcher.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/corelib/thread/qbasicfuturewatcher.cpp b/src/corelib/thread/qbasicfuturewatcher.cpp
new file mode 100644
index 00000000000..2602995e8fa
--- /dev/null
+++ b/src/corelib/thread/qbasicfuturewatcher.cpp
@@ -0,0 +1,80 @@
+// Copyright (C) 2023 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+
+#include "qbasicfuturewatcher.h"
+#include "qcoreapplication.h"
+#include "qfutureinterface.h"
+#include "qfutureinterface_p.h"
+
+#include <QtCore/private/qobject_p.h>
+
+QT_BEGIN_NAMESPACE
+
+namespace QtPrivate {
+
+class QBasicFutureWatcherPrivate : public QObjectPrivate, QFutureCallOutInterface
+{
+public:
+ Q_DECLARE_PUBLIC(QBasicFutureWatcher)
+
+ QFutureInterfaceBase future;
+
+ void postCallOutEvent(const QFutureCallOutEvent &event) override;
+ void callOutInterfaceDisconnected() override;
+};
+
+void QBasicFutureWatcherPrivate::postCallOutEvent(const QFutureCallOutEvent &event)
+{
+ Q_Q(QBasicFutureWatcher);
+ if (q->thread() == QThread::currentThread()) {
+ // If we are in the same thread, don't queue up anything.
+ std::unique_ptr<QFutureCallOutEvent> clonedEvent(event.clone());
+ QCoreApplication::sendEvent(q, clonedEvent.get());
+ } else {
+ QCoreApplication::postEvent(q, event.clone());
+ }
+}
+
+void QBasicFutureWatcherPrivate::callOutInterfaceDisconnected()
+{
+ Q_Q(QBasicFutureWatcher);
+ QCoreApplication::removePostedEvents(q, QEvent::FutureCallOut);
+}
+
+/*
+ * QBasicFutureWatcher is a more lightweight version of QFutureWatcher for internal use
+ */
+QBasicFutureWatcher::QBasicFutureWatcher(QObject *parent)
+ : QObject(*new QBasicFutureWatcherPrivate, parent)
+{
+}
+
+QBasicFutureWatcher::~QBasicFutureWatcher()
+{
+ Q_D(QBasicFutureWatcher);
+ d->future.d->disconnectOutputInterface(d);
+}
+
+void QBasicFutureWatcher::setFuture(QFutureInterfaceBase &fi)
+{
+ Q_D(QBasicFutureWatcher);
+ d->future = fi;
+ d->future.d->connectOutputInterface(d);
+}
+
+bool QtPrivate::QBasicFutureWatcher::event(QEvent *event)
+{
+ if (event->type() == QEvent::FutureCallOut) {
+ QFutureCallOutEvent *callOutEvent = static_cast<QFutureCallOutEvent *>(event);
+ if (callOutEvent->callOutType == QFutureCallOutEvent::Finished)
+ emit finished();
+ return true;
+ }
+ return QObject::event(event);
+}
+
+} // namespace QtPrivate
+
+QT_END_NAMESPACE
+
+#include "moc_qbasicfuturewatcher.cpp"