From 742a07ece1c99db964617015c06a05b4cb0dd995 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Mon, 25 Mar 2019 15:27:39 +0100 Subject: Use QScopedValueRollback more as a reentrancy guard It is documented for the use, but we were rarely using it. Change-Id: I812b9e6c8fbf204aba43ce2b79eca308ca75de88 Reviewed-by: Thiago Macieira --- src/corelib/animation/qabstractanimation.cpp | 16 ++++++++-------- src/corelib/io/qprocess.cpp | 4 ++-- src/corelib/io/qwindowspipereader.cpp | 4 ++-- src/corelib/io/qwindowspipewriter.cpp | 4 ++-- src/gui/painting/qoutlinemapper.cpp | 5 ++--- src/gui/text/qsyntaxhighlighter.cpp | 7 ++++--- src/gui/text/qtextdocument_p.cpp | 18 ++++++++++-------- src/gui/text/qtextdocument_p.h | 2 +- 8 files changed, 31 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/corelib/animation/qabstractanimation.cpp b/src/corelib/animation/qabstractanimation.cpp index 9d7a256191..e37f9a0271 100644 --- a/src/corelib/animation/qabstractanimation.cpp +++ b/src/corelib/animation/qabstractanimation.cpp @@ -152,6 +152,7 @@ #include #include #include +#include #define DEFAULT_TIMER_INTERVAL 16 #define PAUSE_TIMER_COARSE_THRESHOLD 2000 @@ -315,14 +316,13 @@ void QUnifiedTimer::updateAnimationTimers(qint64 currentTick) //* it might happen in some cases that the delta is negative because the animation driver // advances faster than time.elapsed() if (delta > 0) { - insideTick = true; + QScopedValueRollback guard(insideTick, true); if (profilerCallback) profilerCallback(delta); for (currentAnimationIdx = 0; currentAnimationIdx < animationTimers.count(); ++currentAnimationIdx) { QAbstractAnimationTimer *animation = animationTimers.at(currentAnimationIdx); animation->updateAnimationsTime(delta); } - insideTick = false; currentAnimationIdx = 0; } } @@ -361,10 +361,11 @@ void QUnifiedTimer::localRestart() void QUnifiedTimer::restart() { - insideRestart = true; - for (int i = 0; i < animationTimers.count(); ++i) - animationTimers.at(i)->restartAnimationTimer(); - insideRestart = false; + { + QScopedValueRollback guard(insideRestart, true); + for (int i = 0; i < animationTimers.count(); ++i) + animationTimers.at(i)->restartAnimationTimer(); + } localRestart(); } @@ -599,14 +600,13 @@ void QAnimationTimer::updateAnimationsTime(qint64 delta) //it might happen in some cases that the time doesn't change because events are delayed //when the CPU load is high if (delta) { - insideTick = true; + QScopedValueRollback guard(insideTick, true); for (currentAnimationIdx = 0; currentAnimationIdx < animations.count(); ++currentAnimationIdx) { QAbstractAnimation *animation = animations.at(currentAnimationIdx); int elapsed = QAbstractAnimationPrivate::get(animation)->totalCurrentTime + (animation->direction() == QAbstractAnimation::Forward ? delta : -delta); animation->setCurrentTime(elapsed); } - insideTick = false; currentAnimationIdx = 0; } } diff --git a/src/corelib/io/qprocess.cpp b/src/corelib/io/qprocess.cpp index d5d67d86f3..c7635cc7b4 100644 --- a/src/corelib/io/qprocess.cpp +++ b/src/corelib/io/qprocess.cpp @@ -42,6 +42,7 @@ #include #include +#include #if defined(Q_OS_WIN) #include #endif @@ -1062,9 +1063,8 @@ bool QProcessPrivate::tryReadFromChannel(Channel *channel) if (currentReadChannel == channelIdx) { didRead = true; if (!emittedReadyRead) { - emittedReadyRead = true; + QScopedValueRollback guard(emittedReadyRead, true); emit q->readyRead(); - emittedReadyRead = false; } } emit q->channelReadyRead(int(channelIdx)); diff --git a/src/corelib/io/qwindowspipereader.cpp b/src/corelib/io/qwindowspipereader.cpp index 15c9f52cf3..1f03ac5d5a 100644 --- a/src/corelib/io/qwindowspipereader.cpp +++ b/src/corelib/io/qwindowspipereader.cpp @@ -40,6 +40,7 @@ #include "qwindowspipereader_p.h" #include "qiodevice_p.h" #include +#include QT_BEGIN_NAMESPACE @@ -301,9 +302,8 @@ void QWindowsPipeReader::emitPendingReadyRead() { if (readyReadPending) { readyReadPending = false; - inReadyRead = true; + QScopedValueRollback guard(inReadyRead, true); emit readyRead(); - inReadyRead = false; } } diff --git a/src/corelib/io/qwindowspipewriter.cpp b/src/corelib/io/qwindowspipewriter.cpp index 92e8b6db52..32536f495b 100644 --- a/src/corelib/io/qwindowspipewriter.cpp +++ b/src/corelib/io/qwindowspipewriter.cpp @@ -39,6 +39,7 @@ #include "qwindowspipewriter_p.h" #include "qiodevice_p.h" +#include QT_BEGIN_NAMESPACE @@ -111,9 +112,8 @@ void QWindowsPipeWriter::emitPendingBytesWrittenValue() emit canWrite(); if (!inBytesWritten) { - inBytesWritten = true; + QScopedValueRollback guard(inBytesWritten, true); emit bytesWritten(bytes); - inBytesWritten = false; } } } diff --git a/src/gui/painting/qoutlinemapper.cpp b/src/gui/painting/qoutlinemapper.cpp index b2d02182c3..2074f98069 100644 --- a/src/gui/painting/qoutlinemapper.cpp +++ b/src/gui/painting/qoutlinemapper.cpp @@ -42,6 +42,7 @@ #include "qbezier_p.h" #include "qmath.h" #include "qpainterpath_p.h" +#include "qscopedvaluerollback.h" #include @@ -354,7 +355,7 @@ void QOutlineMapper::clipElements(const QPointF *elements, // instead of going through convenience functionallity, but since // this part of code hardly every used, it shouldn't matter. - m_in_clip_elements = true; + QScopedValueRollback in_clip_elements(m_in_clip_elements, true); QPainterPath path; @@ -397,8 +398,6 @@ void QOutlineMapper::clipElements(const QPointF *elements, convertPath(clippedPath); m_transform = oldTransform; } - - m_in_clip_elements = false; } QT_END_NAMESPACE diff --git a/src/gui/text/qsyntaxhighlighter.cpp b/src/gui/text/qsyntaxhighlighter.cpp index 102a776ed3..cf584f6980 100644 --- a/src/gui/text/qsyntaxhighlighter.cpp +++ b/src/gui/text/qsyntaxhighlighter.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -68,14 +69,14 @@ public: void reformatBlocks(int from, int charsRemoved, int charsAdded); void reformatBlock(const QTextBlock &block); - inline void rehighlight(QTextCursor &cursor, QTextCursor::MoveOperation operation) { - inReformatBlocks = true; + inline void rehighlight(QTextCursor &cursor, QTextCursor::MoveOperation operation) + { + QScopedValueRollback bg(inReformatBlocks, true); cursor.beginEditBlock(); int from = cursor.position(); cursor.movePosition(operation); reformatBlocks(from, 0, cursor.position() - from); cursor.endEditBlock(); - inReformatBlocks = false; } inline void _q_delayedRehighlight() { diff --git a/src/gui/text/qtextdocument_p.cpp b/src/gui/text/qtextdocument_p.cpp index c0a0c1a177..0e3c8d0e83 100644 --- a/src/gui/text/qtextdocument_p.cpp +++ b/src/gui/text/qtextdocument_p.cpp @@ -40,6 +40,7 @@ #include #include +#include #include "qtextdocument_p.h" #include "qtextdocument.h" #include @@ -274,9 +275,10 @@ void QTextDocumentPrivate::clear() rtFrame = 0; init(); cursors = oldCursors; - inContentsChange = true; - emit q->contentsChange(0, len, 0); - inContentsChange = false; + { + QScopedValueRollback bg(inContentsChange, true); + emit q->contentsChange(0, len, 0); + } if (lout) lout->documentChanged(0, len, 0); } QT_CATCH(...) { @@ -309,9 +311,10 @@ void QTextDocumentPrivate::setLayout(QAbstractTextDocumentLayout *layout) it->free(); emit q->documentLayoutChanged(); - inContentsChange = true; - emit q->contentsChange(0, 0, length()); - inContentsChange = false; + { + QScopedValueRollback bg(inContentsChange, true); + emit q->contentsChange(0, 0, length()); + } if (lout) lout->documentChanged(0, 0, length()); } @@ -1213,9 +1216,8 @@ void QTextDocumentPrivate::finishEdit() if (lout && docChangeFrom >= 0) { if (!inContentsChange) { - inContentsChange = true; + QScopedValueRollback bg(inContentsChange, true); emit q->contentsChange(docChangeFrom, docChangeOldLength, docChangeLength); - inContentsChange = false; } lout->documentChanged(docChangeFrom, docChangeOldLength, docChangeLength); } diff --git a/src/gui/text/qtextdocument_p.h b/src/gui/text/qtextdocument_p.h index d95f5bef6a..a8e17bfc08 100644 --- a/src/gui/text/qtextdocument_p.h +++ b/src/gui/text/qtextdocument_p.h @@ -339,6 +339,7 @@ private: int lastBlockCount; public: + bool inContentsChange; QTextOption defaultTextOption; Qt::CursorMoveStyle defaultCursorMoveStyle; #ifndef QT_NO_CSSPARSER @@ -346,7 +347,6 @@ public: #endif int maximumBlockCount; uint needsEnsureMaximumBlockCount : 1; - uint inContentsChange : 1; uint blockCursorAdjustment : 1; QSizeF pageSize; QString title; -- cgit v1.2.3