diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2022-01-18 15:45:56 +0100 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2022-01-21 01:51:20 +0100 |
commit | b292928e35b9864c2c3a023f79b892f40b08cd86 (patch) | |
tree | f961dc90ad54bcf452bd9bcf393d73236df492d0 /src/corelib/thread | |
parent | afdae3618a08da9f2c41b7c4ea57219f444ee324 (diff) |
Optimize ContinuationWrapper used for support of move-only continuations
After QFuture continuations became non-copyable (see earlier commits),
we have to always use ContinuationWrapper to save the continuations
inside std::function, since it requires the callable to be copyable.
Optimize the wrapper, by storing the callable directly (instead of using
a ref-counted QSharedPointer) and introducing a fake copy-constructor
that makes sure that it's never called.
Pick-to: 6.3 6.2
Change-Id: I0ed5f90ad62ede3b5c6d6e56ef58eb6377122920
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib/thread')
-rw-r--r-- | src/corelib/thread/qfuture_impl.h | 14 | ||||
-rw-r--r-- | src/corelib/thread/qfutureinterface.cpp | 2 |
2 files changed, 12 insertions, 4 deletions
diff --git a/src/corelib/thread/qfuture_impl.h b/src/corelib/thread/qfuture_impl.h index 69f57652ea..b09d84a6d3 100644 --- a/src/corelib/thread/qfuture_impl.h +++ b/src/corelib/thread/qfuture_impl.h @@ -538,11 +538,19 @@ bool Continuation<Function, ResultType, ParentResultType>::execute() template<class Function> struct ContinuationWrapper { - ContinuationWrapper(Function &&f) : function(QSharedPointer<Function>::create(std::move(f))) { } - void operator()(const QFutureInterfaceBase &parentData) { (*function)(parentData); } + ContinuationWrapper(Function &&f) : function(std::move(f)) { } + ContinuationWrapper(const ContinuationWrapper &other) + : function(std::move(const_cast<ContinuationWrapper &>(other).function)) + { + Q_ASSERT_X(false, "QFuture", "Continuation shouldn't be copied"); + } + ContinuationWrapper(ContinuationWrapper &&other) = default; + ContinuationWrapper &operator=(ContinuationWrapper &&) = default; + + void operator()(const QFutureInterfaceBase &parentData) { function(parentData); } private: - QSharedPointer<Function> function; + Function function; }; template<typename Function, typename ResultType, typename ParentResultType> diff --git a/src/corelib/thread/qfutureinterface.cpp b/src/corelib/thread/qfutureinterface.cpp index dfc905e828..cad6111ab9 100644 --- a/src/corelib/thread/qfutureinterface.cpp +++ b/src/corelib/thread/qfutureinterface.cpp @@ -837,7 +837,7 @@ void QFutureInterfaceBasePrivate::setState(QFutureInterfaceBase::State newState) void QFutureInterfaceBase::setContinuation(std::function<void(const QFutureInterfaceBase &)> func) { - setContinuation(func, nullptr); + setContinuation(std::move(func), nullptr); } void QFutureInterfaceBase::setContinuation(std::function<void(const QFutureInterfaceBase &)> func, |