From 2afcd5ba4d919e0ed8ba17af0b1d2c09adaa4548 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Tue, 25 Jun 2019 12:52:02 +0000 Subject: [PATCH] assume const methods are thread-safe and avoid locking where possible Signed-off-by: Ivailo Monev --- src/core/concurrent/qfutureinterface.cpp | 3 -- src/core/concurrent/qfutureinterface_p.h | 2 +- src/core/concurrent/qthreadpool.cpp | 1 - src/core/concurrent/qthreadpool_p.h | 2 +- src/core/io/qurl.cpp | 74 +++++++++++++++++++++----------- src/core/thread/qthread.cpp | 5 --- src/core/thread/qthread_p.h | 2 +- src/dbus/qdbuspendingcall.cpp | 10 +---- src/dbus/qdbuspendingcall_p.h | 2 +- src/gui/graphicsview/qgraphicswidget.cpp | 5 +-- 10 files changed, 56 insertions(+), 50 deletions(-) diff --git a/src/core/concurrent/qfutureinterface.cpp b/src/core/concurrent/qfutureinterface.cpp index 666f79263..27db37477 100644 --- a/src/core/concurrent/qfutureinterface.cpp +++ b/src/core/concurrent/qfutureinterface.cpp @@ -175,19 +175,16 @@ int QFutureInterfaceBase::progressMaximum() const int QFutureInterfaceBase::resultCount() const { - QMutexLocker lock(&d->m_mutex); return d->internal_resultCount(); } QString QFutureInterfaceBase::progressText() const { - QMutexLocker locker(&d->m_mutex); return d->m_progressText; } bool QFutureInterfaceBase::isProgressUpdateNeeded() const { - QMutexLocker locker(&d->m_mutex); return !d->progressTime.isValid() || (d->progressTime.elapsed() > (1000 / MaxProgressEmitsPerSecond)); } diff --git a/src/core/concurrent/qfutureinterface_p.h b/src/core/concurrent/qfutureinterface_p.h index c3e51a108..78c0891fb 100644 --- a/src/core/concurrent/qfutureinterface_p.h +++ b/src/core/concurrent/qfutureinterface_p.h @@ -122,7 +122,7 @@ public: QFutureInterfaceBasePrivate(QFutureInterfaceBase::State initialState); QAtomicInt refCount; - mutable QMutex m_mutex; + QMutex m_mutex; QWaitCondition waitCondition; QList outputConnections; int m_progressValue; diff --git a/src/core/concurrent/qthreadpool.cpp b/src/core/concurrent/qthreadpool.cpp index 53b2fd872..f393754c8 100644 --- a/src/core/concurrent/qthreadpool.cpp +++ b/src/core/concurrent/qthreadpool.cpp @@ -579,7 +579,6 @@ void QThreadPool::setMaxThreadCount(int maxThreadCount) int QThreadPool::activeThreadCount() const { Q_D(const QThreadPool); - QMutexLocker locker(&d->mutex); return d->activeThreadCount(); } diff --git a/src/core/concurrent/qthreadpool_p.h b/src/core/concurrent/qthreadpool_p.h index 508707909..a5399f536 100644 --- a/src/core/concurrent/qthreadpool_p.h +++ b/src/core/concurrent/qthreadpool_p.h @@ -78,7 +78,7 @@ public: bool startFrontRunnable(); void stealRunnable(QRunnable *); - mutable QMutex mutex; + QMutex mutex; QSet allThreads; QQueue waitingThreads; QQueue expiredThreads; diff --git a/src/core/io/qurl.cpp b/src/core/io/qurl.cpp index 62c0c0834..cb33fd628 100644 --- a/src/core/io/qurl.cpp +++ b/src/core/io/qurl.cpp @@ -4281,7 +4281,6 @@ bool QUrl::isEmpty() const { if (!d) return true; - QMutexLocker lock(&d->mutex); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) return d->encodedOriginal.isEmpty(); else @@ -4953,8 +4952,10 @@ QByteArray QUrl::encodedPath() const bool QUrl::hasQuery() const { if (!d) return false; - QMutexLocker lock(&d->mutex); - if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) { + QMutexLocker lock(&d->mutex); + d->parse(); + } return d->hasQuery; } @@ -5194,8 +5195,10 @@ void QUrl::addEncodedQueryItem(const QByteArray &key, const QByteArray &value) QList > QUrl::queryItems() const { if (!d) return QList >(); - QMutexLocker lock(&d->mutex); - if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) { + QMutexLocker lock(&d->mutex); + d->parse(); + } QList > itemMap; @@ -5228,8 +5231,10 @@ QList > QUrl::queryItems() const QList > QUrl::encodedQueryItems() const { if (!d) return QList >(); - QMutexLocker lock(&d->mutex); - if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) { + QMutexLocker lock(&d->mutex); + d->parse(); + } QList > itemMap; @@ -5277,8 +5282,10 @@ bool QUrl::hasQueryItem(const QString &key) const bool QUrl::hasEncodedQueryItem(const QByteArray &key) const { if (!d) return false; - QMutexLocker lock(&d->mutex); - if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) { + QMutexLocker lock(&d->mutex); + d->parse(); + } int pos = 0; const char *query = d->query.constData(); @@ -5325,8 +5332,10 @@ QString QUrl::queryItemValue(const QString &key) const QByteArray QUrl::encodedQueryItemValue(const QByteArray &key) const { if (!d) return QByteArray(); - QMutexLocker lock(&d->mutex); - if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) { + QMutexLocker lock(&d->mutex); + d->parse(); + } int pos = 0; const char *query = d->query.constData(); @@ -5354,8 +5363,10 @@ QByteArray QUrl::encodedQueryItemValue(const QByteArray &key) const QStringList QUrl::allQueryItemValues(const QString &key) const { if (!d) return QStringList(); - QMutexLocker lock(&d->mutex); - if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) { + QMutexLocker lock(&d->mutex); + d->parse(); + } QByteArray encodedKey = toPercentEncoding(key, queryExcludeChars); QStringList values; @@ -5393,8 +5404,10 @@ QStringList QUrl::allQueryItemValues(const QString &key) const QList QUrl::allEncodedQueryItemValues(const QByteArray &key) const { if (!d) return QList(); - QMutexLocker lock(&d->mutex); - if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) { + QMutexLocker lock(&d->mutex); + d->parse(); + } QList values; @@ -5514,8 +5527,10 @@ void QUrl::removeAllEncodedQueryItems(const QByteArray &key) QByteArray QUrl::encodedQuery() const { if (!d) return QByteArray(); - QMutexLocker lock(&d->mutex); - if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) { + QMutexLocker lock(&d->mutex); + d->parse(); + } return d->query; } @@ -5635,8 +5650,10 @@ QByteArray QUrl::encodedFragment() const bool QUrl::hasFragment() const { if (!d) return false; - QMutexLocker lock(&d->mutex); - if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) { + QMutexLocker lock(&d->mutex); + d->parse(); + } return d->hasFragment; } @@ -5739,8 +5756,10 @@ QUrl QUrl::resolved(const QUrl &relative) const bool QUrl::isRelative() const { if (!d) return true; - QMutexLocker lock(&d->mutex); - if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) { + QMutexLocker lock(&d->mutex); + d->parse(); + } return d->scheme.isEmpty(); } @@ -6228,8 +6247,10 @@ QString QUrl::toLocalFile() const bool QUrl::isLocalFile() const { if (!d) return false; - QMutexLocker lock(&d->mutex); - if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) { + QMutexLocker lock(&d->mutex); + d->parse(); + } if (d->scheme.compare(QLatin1String("file"), Qt::CaseInsensitive) != 0) return false; // not file @@ -6250,9 +6271,10 @@ bool QUrl::isParentOf(const QUrl &childUrl) const && (childUrl.authority().isEmpty()) && childPath.length() > 0 && childPath.at(0) == QLatin1Char('/')); - QMutexLocker lock(&d->mutex); - if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); - lock.unlock(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) { + QMutexLocker lock(&d->mutex); + d->parse(); + } QString ourPath = path(); diff --git a/src/core/thread/qthread.cpp b/src/core/thread/qthread.cpp index ce959257b..478019111 100644 --- a/src/core/thread/qthread.cpp +++ b/src/core/thread/qthread.cpp @@ -425,7 +425,6 @@ QThread::~QThread() bool QThread::isFinished() const { Q_D(const QThread); - QMutexLocker locker(&d->mutex); return d->finished || d->isInFinish; } @@ -437,7 +436,6 @@ bool QThread::isFinished() const bool QThread::isRunning() const { Q_D(const QThread); - QMutexLocker locker(&d->mutex); return d->running && !d->isInFinish; } @@ -471,7 +469,6 @@ void QThread::setStackSize(uint stackSize) uint QThread::stackSize() const { Q_D(const QThread); - QMutexLocker locker(&d->mutex); return d->stackSize; } @@ -610,8 +607,6 @@ void QThread::run() QThread::Priority QThread::priority() const { Q_D(const QThread); - QMutexLocker locker(&d->mutex); - // mask off the high bits that are used for flags return Priority(d->priority & 0xffff); } diff --git a/src/core/thread/qthread_p.h b/src/core/thread/qthread_p.h index 63c11bc70..1d4d40311 100644 --- a/src/core/thread/qthread_p.h +++ b/src/core/thread/qthread_p.h @@ -129,7 +129,7 @@ public: QThreadPrivate(QThreadData *d = Q_NULLPTR); ~QThreadPrivate(); - mutable QMutex mutex; + QMutex mutex; bool running; bool finished; diff --git a/src/dbus/qdbuspendingcall.cpp b/src/dbus/qdbuspendingcall.cpp index a35a13290..6b5ca2932 100644 --- a/src/dbus/qdbuspendingcall.cpp +++ b/src/dbus/qdbuspendingcall.cpp @@ -326,7 +326,6 @@ bool QDBusPendingCall::isFinished() const if (!d) return true; // considered finished - QMutexLocker locker(&d->mutex); return d->replyMessage.type() != QDBusMessage::InvalidMessage; } @@ -348,7 +347,6 @@ bool QDBusPendingCall::isValid() const { if (!d) return false; - QMutexLocker locker(&d->mutex); return d->replyMessage.type() == QDBusMessage::ReplyMessage; } @@ -365,7 +363,6 @@ bool QDBusPendingCall::isError() const { if (!d) return true; // considered finished and an error - QMutexLocker locker(&d->mutex); return d->replyMessage.type() == QDBusMessage::ErrorMessage; } @@ -380,14 +377,12 @@ bool QDBusPendingCall::isError() const QDBusError QDBusPendingCall::error() const { if (d) { - QMutexLocker locker(&d->mutex); return d->replyMessage; } // not connected, return an error - QDBusError err = QDBusError(QDBusError::Disconnected, - QLatin1String("Not connected to D-Bus server")); - return err; + return QDBusError(QDBusError::Disconnected, + QLatin1String("Not connected to D-Bus server")); } /*! @@ -405,7 +400,6 @@ QDBusMessage QDBusPendingCall::reply() const { if (!d) return QDBusMessage::createError(error()); - QMutexLocker locker(&d->mutex); return d->replyMessage; } diff --git a/src/dbus/qdbuspendingcall_p.h b/src/dbus/qdbuspendingcall_p.h index 066122daa..8c8749166 100644 --- a/src/dbus/qdbuspendingcall_p.h +++ b/src/dbus/qdbuspendingcall_p.h @@ -78,7 +78,7 @@ public: // } - mutable QMutex mutex; + QMutex mutex; QWaitCondition waitForFinishedCondition; // { diff --git a/src/gui/graphicsview/qgraphicswidget.cpp b/src/gui/graphicsview/qgraphicswidget.cpp index 91f982041..ef4bc16e6 100644 --- a/src/gui/graphicsview/qgraphicswidget.cpp +++ b/src/gui/graphicsview/qgraphicswidget.cpp @@ -207,8 +207,7 @@ class QGraphicsWidgetStyles public: QStyle *styleForWidget(const QGraphicsWidget *widget) const { - QMutexLocker locker(&mutex); - return styles.value(widget, 0); + return styles.value(widget, Q_NULLPTR); } void setStyleForWidget(QGraphicsWidget *widget, QStyle *style) @@ -222,7 +221,7 @@ public: private: QMap styles; - mutable QMutex mutex; + QMutex mutex; }; Q_GLOBAL_STATIC(QGraphicsWidgetStyles, widgetStyles) -- 2.11.0