diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2019-10-18 17:13:53 +0200 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2019-10-21 23:20:35 +0200 |
commit | a9bf6e652aa99784343c94b0181c9443205e2a82 (patch) | |
tree | 6ff4a5c358f1f1bd0523a66733c75c31ca4a3cc5 /src/qml/qml/qqmlvme_p.h | |
parent | fecfa43cfac329a5b50663cd04bb74bf5a63ca6f (diff) |
QQmlIncubationController::incubateWhile: port to atomic<bool>
"volatile bool" does not guarantee synchronization, and as such,
the documentation (and the intended use case, having another
thread stopping incubation) are wrong. Add a different overload
that takes a std::atomic<bool> as a replacement.
Change-Id: I05e2d608385d075ef67989839d6c0a634da7a64b
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/qml/qml/qqmlvme_p.h')
-rw-r--r-- | src/qml/qml/qqmlvme_p.h | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/src/qml/qml/qqmlvme_p.h b/src/qml/qml/qqmlvme_p.h index 13c5524d96..32b1631271 100644 --- a/src/qml/qml/qqmlvme_p.h +++ b/src/qml/qml/qqmlvme_p.h @@ -64,6 +64,8 @@ #include <private/qqmlengine_p.h> #include <private/qfinitestack_p.h> +#include <atomic> + QT_BEGIN_NAMESPACE class QObject; @@ -97,17 +99,20 @@ class QTypeInfo<QQmlVMETypes::State> : public QTypeInfoMerger<QQmlVMETypes::Stat class QQmlInstantiationInterrupt { public: inline QQmlInstantiationInterrupt(); + // ### Qt 6: remove inline QQmlInstantiationInterrupt(volatile bool *runWhile, int nsecs=0); + inline QQmlInstantiationInterrupt(std::atomic<bool> *runWhile, int nsecs = 0); inline QQmlInstantiationInterrupt(int nsecs); inline void reset(); inline bool shouldInterrupt() const; private: - enum Mode { None, Time, Flag }; + enum Mode { None, Time, LegacyFlag, Flag }; // ### Qt 6: remove LegacyFlag Mode mode; QElapsedTimer timer; - int nsecs; - volatile bool *runWhile; + int nsecs = 0; + volatile bool *runWhileLegacy = nullptr; // ### Qt 6: remove + std::atomic<bool> *runWhile = nullptr; }; class Q_QML_PRIVATE_EXPORT QQmlVME @@ -147,17 +152,22 @@ private: }; QQmlInstantiationInterrupt::QQmlInstantiationInterrupt() - : mode(None), nsecs(0), runWhile(nullptr) + : mode(None) { } QQmlInstantiationInterrupt::QQmlInstantiationInterrupt(volatile bool *runWhile, int nsecs) + : mode(LegacyFlag), nsecs(nsecs), runWhileLegacy(runWhile) +{ +} + +QQmlInstantiationInterrupt::QQmlInstantiationInterrupt(std::atomic<bool> *runWhile, int nsecs) : mode(Flag), nsecs(nsecs), runWhile(runWhile) { } QQmlInstantiationInterrupt::QQmlInstantiationInterrupt(int nsecs) - : mode(Time), nsecs(nsecs), runWhile(nullptr) + : mode(Time), nsecs(nsecs) { } @@ -169,15 +179,18 @@ void QQmlInstantiationInterrupt::reset() bool QQmlInstantiationInterrupt::shouldInterrupt() const { - if (mode == None) { + switch (mode) { + case None: return false; - } else if (mode == Time) { + case Time: return timer.nsecsElapsed() > nsecs; - } else if (mode == Flag) { - return !*runWhile || (nsecs && timer.nsecsElapsed() > nsecs); - } else { - return false; + case LegacyFlag: + return !*runWhileLegacy || (nsecs && timer.nsecsElapsed() > nsecs); + case Flag: + return !runWhile->load(std::memory_order_acquire) || (nsecs && timer.nsecsElapsed() > nsecs); } + Q_UNREACHABLE(); + return false; } QT_END_NAMESPACE |