OSDN Git Service

assume const methods are thread-safe and avoid locking where possible
authorIvailo Monev <xakepa10@laimg.moc>
Tue, 25 Jun 2019 12:52:02 +0000 (12:52 +0000)
committerIvailo Monev <xakepa10@laimg.moc>
Tue, 25 Jun 2019 12:52:02 +0000 (12:52 +0000)
Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
src/core/concurrent/qfutureinterface.cpp
src/core/concurrent/qfutureinterface_p.h
src/core/concurrent/qthreadpool.cpp
src/core/concurrent/qthreadpool_p.h
src/core/io/qurl.cpp
src/core/thread/qthread.cpp
src/core/thread/qthread_p.h
src/dbus/qdbuspendingcall.cpp
src/dbus/qdbuspendingcall_p.h
src/gui/graphicsview/qgraphicswidget.cpp

index 666f792..27db374 100644 (file)
@@ -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));
 }
 
index c3e51a1..78c0891 100644 (file)
@@ -122,7 +122,7 @@ public:
     QFutureInterfaceBasePrivate(QFutureInterfaceBase::State initialState);
 
     QAtomicInt refCount;
-    mutable QMutex m_mutex;
+    QMutex m_mutex;
     QWaitCondition waitCondition;
     QList<QFutureCallOutInterface *> outputConnections;
     int m_progressValue;
index 53b2fd8..f393754 100644 (file)
@@ -579,7 +579,6 @@ void QThreadPool::setMaxThreadCount(int maxThreadCount)
 int QThreadPool::activeThreadCount() const
 {
     Q_D(const QThreadPool);
-    QMutexLocker locker(&d->mutex);
     return d->activeThreadCount();
 }
 
index 5087079..a5399f5 100644 (file)
@@ -78,7 +78,7 @@ public:
     bool startFrontRunnable();
     void stealRunnable(QRunnable *);
 
-    mutable QMutex mutex;
+    QMutex mutex;
     QSet<QThreadPoolThread *> allThreads;
     QQueue<QThreadPoolThread *> waitingThreads;
     QQueue<QThreadPoolThread *> expiredThreads;
index 62c0c08..cb33fd6 100644 (file)
@@ -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<QPair<QString, QString> > QUrl::queryItems() const
 {
     if (!d) return QList<QPair<QString, QString> >();
-    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<QPair<QString, QString> > itemMap;
 
@@ -5228,8 +5231,10 @@ QList<QPair<QString, QString> > QUrl::queryItems() const
 QList<QPair<QByteArray, QByteArray> > QUrl::encodedQueryItems() const
 {
     if (!d) return QList<QPair<QByteArray, 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();
+    }
 
     QList<QPair<QByteArray, QByteArray> > 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<QByteArray> QUrl::allEncodedQueryItemValues(const QByteArray &key) const
 {
     if (!d) return QList<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();
+    }
 
     QList<QByteArray> 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();
 
index ce95925..4780191 100644 (file)
@@ -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);
 }
index 63c11bc..1d4d403 100644 (file)
@@ -129,7 +129,7 @@ public:
     QThreadPrivate(QThreadData *d = Q_NULLPTR);
     ~QThreadPrivate();
 
-    mutable QMutex mutex;
+    QMutex mutex;
 
     bool running;
     bool finished;
index a35a132..6b5ca29 100644 (file)
@@ -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;
 }
 
index 066122d..8c87491 100644 (file)
@@ -78,7 +78,7 @@ public:
 
     // }
 
-    mutable QMutex mutex;
+    QMutex mutex;
     QWaitCondition waitForFinishedCondition;
 
     // {
index 91f9820..ef4bc16 100644 (file)
@@ -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<const QGraphicsWidget *, QStyle *> styles;
-    mutable QMutex mutex;
+    QMutex mutex;
 };
 Q_GLOBAL_STATIC(QGraphicsWidgetStyles, widgetStyles)