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 | |
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>
-rw-r--r-- | src/qml/qml/qqmlincubator.cpp | 31 | ||||
-rw-r--r-- | src/qml/qml/qqmlincubator.h | 5 | ||||
-rw-r--r-- | src/qml/qml/qqmlvme_p.h | 35 | ||||
-rw-r--r-- | tests/auto/qml/qqmlincubator/tst_qqmlincubator.cpp | 66 |
4 files changed, 90 insertions, 47 deletions
diff --git a/src/qml/qml/qqmlincubator.cpp b/src/qml/qml/qqmlincubator.cpp index f0ef5360b0..d9c965d3ea 100644 --- a/src/qml/qml/qqmlincubator.cpp +++ b/src/qml/qml/qqmlincubator.cpp @@ -410,14 +410,39 @@ void QQmlIncubationController::incubateFor(int msecs) } while (d && d->incubatorCount != 0 && !i.shouldInterrupt()); } +#if QT_DEPRECATED_SINCE(5, 15) /*! -Incubate objects while the bool pointed to by \a flag is true, or until there are no -more objects to incubate, or up to \a msecs if \a msecs is not zero. +\obsolete + +\warning Do not use this function. +Use the overload taking a \c{std::atomic<bool>} instead. +*/ +void QQmlIncubationController::incubateWhile(volatile bool *flag, int msecs) +{ + if (!d || !d->incubatorCount) + return; + + QQmlInstantiationInterrupt i(flag, msecs * 1000000); + i.reset(); + do { + static_cast<QQmlIncubatorPrivate*>(d->incubatorList.first())->incubate(i); + } while (d && d->incubatorCount != 0 && !i.shouldInterrupt()); +} +#endif + +/*! +\since 5.15 + +Incubate objects while the atomic bool pointed to by \a flag is true, +or until there are no more objects to incubate, or up to \a msecs if \a +msecs is not zero. Generally this method is used in conjunction with a thread or a UNIX signal that sets the bool pointed to by \a flag to false when it wants incubation to be interrupted. + +\note \a flag is read using acquire memory ordering. */ -void QQmlIncubationController::incubateWhile(volatile bool *flag, int msecs) +void QQmlIncubationController::incubateWhile(std::atomic<bool> *flag, int msecs) { if (!d || !d->incubatorCount) return; diff --git a/src/qml/qml/qqmlincubator.h b/src/qml/qml/qqmlincubator.h index f075407e73..6e47ca2173 100644 --- a/src/qml/qml/qqmlincubator.h +++ b/src/qml/qml/qqmlincubator.h @@ -42,6 +42,7 @@ #include <QtQml/qtqmlglobal.h> #include <QtQml/qqmlerror.h> +#include <atomic> QT_BEGIN_NAMESPACE @@ -112,7 +113,11 @@ public: int incubatingObjectCount() const; void incubateFor(int msecs); +#if QT_DEPRECATED_SINCE(5, 15) + QT_DEPRECATED_VERSION_X(5, 15, "Use the overload that takes a std::atomic<bool>") void incubateWhile(volatile bool *flag, int msecs=0); +#endif + void incubateWhile(std::atomic<bool> *flag, int msecs = 0); protected: virtual void incubatingObjectCountChanged(int); 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 diff --git a/tests/auto/qml/qqmlincubator/tst_qqmlincubator.cpp b/tests/auto/qml/qqmlincubator/tst_qqmlincubator.cpp index 756b3b1d7c..549aae8c2b 100644 --- a/tests/auto/qml/qqmlincubator/tst_qqmlincubator.cpp +++ b/tests/auto/qml/qqmlincubator/tst_qqmlincubator.cpp @@ -149,7 +149,7 @@ void tst_qqmlincubator::objectDeleted() QVERIFY(!SelfRegisteringType::me()); while (SelfRegisteringOuterType::me() == nullptr && incubator.isLoading()) { - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -157,14 +157,14 @@ void tst_qqmlincubator::objectDeleted() QVERIFY(incubator.isLoading()); while (SelfRegisteringType::me() == nullptr && incubator.isLoading()) { - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } delete SelfRegisteringType::me(); { - bool b = true; + std::atomic<bool> b{true}; controller.incubateWhile(&b); } @@ -205,7 +205,7 @@ void tst_qqmlincubator::clear() component.create(incubator); while (SelfRegisteringType::me() == nullptr && incubator.isLoading()) { - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -224,7 +224,7 @@ void tst_qqmlincubator::clear() component.create(incubator); { - bool b = true; + std::atomic<bool> b{true}; controller.incubateWhile(&b); } @@ -318,7 +318,7 @@ void tst_qqmlincubator::forceCompletion() QVERIFY(incubator.isLoading()); while (SelfRegisteringType::me() == nullptr && incubator.isLoading()) { - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -377,7 +377,7 @@ void tst_qqmlincubator::setInitialState() MyIncubator incubator(QQmlIncubator::Asynchronous); component.create(incubator); QVERIFY(incubator.isLoading()); - bool b = true; + std::atomic<bool> b{true}; controller.incubateWhile(&b); QVERIFY(incubator.isReady()); QVERIFY(incubator.object()); @@ -414,7 +414,7 @@ void tst_qqmlincubator::clearDuringCompletion() QVERIFY(!CompletionRegisteringType::me()); while (CompletionRegisteringType::me() == nullptr && incubator.isLoading()) { - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -453,7 +453,7 @@ void tst_qqmlincubator::objectDeletionAfterInit() component.create(incubator); while (!incubator.obj && incubator.isLoading()) { - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -552,7 +552,7 @@ void tst_qqmlincubator::statusChanged() QCOMPARE(incubator.statuses.at(0), int(QQmlIncubator::Loading)); { - bool b = true; + std::atomic<bool> b{true}; controller.incubateWhile(&b); } @@ -574,7 +574,7 @@ void tst_qqmlincubator::statusChanged() QCOMPARE(incubator.statuses.at(0), int(QQmlIncubator::Loading)); { - bool b = true; + std::atomic<bool> b{true}; controller.incubateWhile(&b); } @@ -622,7 +622,7 @@ void tst_qqmlincubator::asynchronousIfNested() QVERIFY(incubator.isLoading()); QVERIFY(!SelfRegisteringType::me()); while (SelfRegisteringType::me() == nullptr && incubator.isLoading()) { - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -635,7 +635,7 @@ void tst_qqmlincubator::asynchronousIfNested() while (nested.isLoading()) { QVERIFY(incubator.isLoading()); - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -643,7 +643,7 @@ void tst_qqmlincubator::asynchronousIfNested() QVERIFY(incubator.isLoading()); { - bool b = true; + std::atomic<bool> b{true}; controller.incubateWhile(&b); } @@ -742,7 +742,7 @@ void tst_qqmlincubator::chainedAsynchronousIfNested() QVERIFY(!SelfRegisteringType::me()); while (SelfRegisteringType::me() == nullptr && incubator.isLoading()) { - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -779,7 +779,7 @@ void tst_qqmlincubator::chainedAsynchronousIfNested() QVERIFY(incubator1.isLoading()); QVERIFY(incubator2.isNull()); - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -792,14 +792,14 @@ void tst_qqmlincubator::chainedAsynchronousIfNested() QVERIFY(incubator1.isReady()); QVERIFY(incubator2.isLoading()); - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } QVERIFY(incubator1.isReady()); QVERIFY(incubator2.isReady()); if (incubator.isLoading()) { - bool b = true; + std::atomic<bool> b{true}; controller.incubateWhile(&b); } @@ -856,7 +856,7 @@ void tst_qqmlincubator::chainedAsynchronousIfNestedOnCompleted() QVERIFY(!SelfRegisteringType::me()); while (SelfRegisteringType::me() == nullptr && incubator.isLoading()) { - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -876,7 +876,7 @@ void tst_qqmlincubator::chainedAsynchronousIfNestedOnCompleted() QVERIFY(incubator2.isNull()); QVERIFY(incubator3.isNull()); - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -891,7 +891,7 @@ void tst_qqmlincubator::chainedAsynchronousIfNestedOnCompleted() QVERIFY(incubator2.isNull()); QVERIFY(incubator3.isNull()); - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -906,7 +906,7 @@ void tst_qqmlincubator::chainedAsynchronousIfNestedOnCompleted() QVERIFY(incubator2.isLoading()); QVERIFY(incubator3.isNull()); - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -921,12 +921,12 @@ void tst_qqmlincubator::chainedAsynchronousIfNestedOnCompleted() QVERIFY(incubator2.isReady()); QVERIFY(incubator3.isLoading()); - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } { - bool b = true; + std::atomic<bool> b{true}; controller.incubateWhile(&b); } @@ -984,7 +984,7 @@ void tst_qqmlincubator::chainedAsynchronousClear() QVERIFY(!SelfRegisteringType::me()); while (SelfRegisteringType::me() == nullptr && incubator.isLoading()) { - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -1004,7 +1004,7 @@ void tst_qqmlincubator::chainedAsynchronousClear() QVERIFY(incubator2.isNull()); QVERIFY(incubator3.isNull()); - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -1019,7 +1019,7 @@ void tst_qqmlincubator::chainedAsynchronousClear() QVERIFY(incubator2.isNull()); QVERIFY(incubator3.isNull()); - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -1034,7 +1034,7 @@ void tst_qqmlincubator::chainedAsynchronousClear() QVERIFY(incubator2.isLoading()); QVERIFY(incubator3.isNull()); - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -1078,7 +1078,7 @@ void tst_qqmlincubator::selfDelete() #define DELETE_TEST(status, mode) { \ bool done = false; \ component.create(*(new MyIncubator(&done, status, mode))); \ - bool True = true; \ + std::atomic<bool> True{true}; \ controller.incubateWhile(&True); \ QVERIFY(done == true); \ } @@ -1107,7 +1107,7 @@ void tst_qqmlincubator::selfDelete() QVERIFY(!SelfRegisteringType::me()); while (SelfRegisteringType::me() == nullptr && incubator->isLoading()) { - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } @@ -1121,7 +1121,7 @@ void tst_qqmlincubator::selfDelete() delete SelfRegisteringType::me(); { - bool b = true; + std::atomic<bool> b{true}; controller.incubateWhile(&b); } @@ -1142,7 +1142,7 @@ void tst_qqmlincubator::contextDelete() delete context; { - bool b = false; + std::atomic<bool> b{false}; controller.incubateWhile(&b); } } @@ -1155,7 +1155,7 @@ void tst_qqmlincubator::garbageCollection() engine.collectGarbage(); - bool b = true; + std::atomic<bool> b{true}; controller.incubateWhile(&b); // verify incubation completed (the incubator was not prematurely collected) |