assume const methods are thread-safe and avoid locking where possible

Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
This commit is contained in:
Ivailo Monev 2019-06-25 12:52:02 +00:00
parent d63af47325
commit 2afcd5ba4d
10 changed files with 56 additions and 50 deletions

View file

@ -175,19 +175,16 @@ int QFutureInterfaceBase::progressMaximum() const
int QFutureInterfaceBase::resultCount() const int QFutureInterfaceBase::resultCount() const
{ {
QMutexLocker lock(&d->m_mutex);
return d->internal_resultCount(); return d->internal_resultCount();
} }
QString QFutureInterfaceBase::progressText() const QString QFutureInterfaceBase::progressText() const
{ {
QMutexLocker locker(&d->m_mutex);
return d->m_progressText; return d->m_progressText;
} }
bool QFutureInterfaceBase::isProgressUpdateNeeded() const bool QFutureInterfaceBase::isProgressUpdateNeeded() const
{ {
QMutexLocker locker(&d->m_mutex);
return !d->progressTime.isValid() || (d->progressTime.elapsed() > (1000 / MaxProgressEmitsPerSecond)); return !d->progressTime.isValid() || (d->progressTime.elapsed() > (1000 / MaxProgressEmitsPerSecond));
} }

View file

@ -122,7 +122,7 @@ public:
QFutureInterfaceBasePrivate(QFutureInterfaceBase::State initialState); QFutureInterfaceBasePrivate(QFutureInterfaceBase::State initialState);
QAtomicInt refCount; QAtomicInt refCount;
mutable QMutex m_mutex; QMutex m_mutex;
QWaitCondition waitCondition; QWaitCondition waitCondition;
QList<QFutureCallOutInterface *> outputConnections; QList<QFutureCallOutInterface *> outputConnections;
int m_progressValue; int m_progressValue;

View file

@ -579,7 +579,6 @@ void QThreadPool::setMaxThreadCount(int maxThreadCount)
int QThreadPool::activeThreadCount() const int QThreadPool::activeThreadCount() const
{ {
Q_D(const QThreadPool); Q_D(const QThreadPool);
QMutexLocker locker(&d->mutex);
return d->activeThreadCount(); return d->activeThreadCount();
} }

View file

@ -78,7 +78,7 @@ public:
bool startFrontRunnable(); bool startFrontRunnable();
void stealRunnable(QRunnable *); void stealRunnable(QRunnable *);
mutable QMutex mutex; QMutex mutex;
QSet<QThreadPoolThread *> allThreads; QSet<QThreadPoolThread *> allThreads;
QQueue<QThreadPoolThread *> waitingThreads; QQueue<QThreadPoolThread *> waitingThreads;
QQueue<QThreadPoolThread *> expiredThreads; QQueue<QThreadPoolThread *> expiredThreads;

View file

@ -4281,7 +4281,6 @@ bool QUrl::isEmpty() const
{ {
if (!d) return true; if (!d) return true;
QMutexLocker lock(&d->mutex);
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed))
return d->encodedOriginal.isEmpty(); return d->encodedOriginal.isEmpty();
else else
@ -4953,8 +4952,10 @@ QByteArray QUrl::encodedPath() const
bool QUrl::hasQuery() const bool QUrl::hasQuery() const
{ {
if (!d) return false; if (!d) return false;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) {
QMutexLocker lock(&d->mutex); QMutexLocker lock(&d->mutex);
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->parse();
}
return d->hasQuery; return d->hasQuery;
} }
@ -5194,8 +5195,10 @@ void QUrl::addEncodedQueryItem(const QByteArray &key, const QByteArray &value)
QList<QPair<QString, QString> > QUrl::queryItems() const QList<QPair<QString, QString> > QUrl::queryItems() const
{ {
if (!d) return QList<QPair<QString, QString> >(); if (!d) return QList<QPair<QString, QString> >();
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) {
QMutexLocker lock(&d->mutex); QMutexLocker lock(&d->mutex);
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->parse();
}
QList<QPair<QString, QString> > itemMap; QList<QPair<QString, QString> > itemMap;
@ -5228,8 +5231,10 @@ QList<QPair<QString, QString> > QUrl::queryItems() const
QList<QPair<QByteArray, QByteArray> > QUrl::encodedQueryItems() const QList<QPair<QByteArray, QByteArray> > QUrl::encodedQueryItems() const
{ {
if (!d) return QList<QPair<QByteArray, QByteArray> >(); if (!d) return QList<QPair<QByteArray, QByteArray> >();
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) {
QMutexLocker lock(&d->mutex); QMutexLocker lock(&d->mutex);
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->parse();
}
QList<QPair<QByteArray, QByteArray> > itemMap; QList<QPair<QByteArray, QByteArray> > itemMap;
@ -5277,8 +5282,10 @@ bool QUrl::hasQueryItem(const QString &key) const
bool QUrl::hasEncodedQueryItem(const QByteArray &key) const bool QUrl::hasEncodedQueryItem(const QByteArray &key) const
{ {
if (!d) return false; if (!d) return false;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) {
QMutexLocker lock(&d->mutex); QMutexLocker lock(&d->mutex);
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->parse();
}
int pos = 0; int pos = 0;
const char *query = d->query.constData(); const char *query = d->query.constData();
@ -5325,8 +5332,10 @@ QString QUrl::queryItemValue(const QString &key) const
QByteArray QUrl::encodedQueryItemValue(const QByteArray &key) const QByteArray QUrl::encodedQueryItemValue(const QByteArray &key) const
{ {
if (!d) return QByteArray(); if (!d) return QByteArray();
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) {
QMutexLocker lock(&d->mutex); QMutexLocker lock(&d->mutex);
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->parse();
}
int pos = 0; int pos = 0;
const char *query = d->query.constData(); const char *query = d->query.constData();
@ -5354,8 +5363,10 @@ QByteArray QUrl::encodedQueryItemValue(const QByteArray &key) const
QStringList QUrl::allQueryItemValues(const QString &key) const QStringList QUrl::allQueryItemValues(const QString &key) const
{ {
if (!d) return QStringList(); if (!d) return QStringList();
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) {
QMutexLocker lock(&d->mutex); QMutexLocker lock(&d->mutex);
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->parse();
}
QByteArray encodedKey = toPercentEncoding(key, queryExcludeChars); QByteArray encodedKey = toPercentEncoding(key, queryExcludeChars);
QStringList values; QStringList values;
@ -5393,8 +5404,10 @@ QStringList QUrl::allQueryItemValues(const QString &key) const
QList<QByteArray> QUrl::allEncodedQueryItemValues(const QByteArray &key) const QList<QByteArray> QUrl::allEncodedQueryItemValues(const QByteArray &key) const
{ {
if (!d) return QList<QByteArray>(); if (!d) return QList<QByteArray>();
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) {
QMutexLocker lock(&d->mutex); QMutexLocker lock(&d->mutex);
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->parse();
}
QList<QByteArray> values; QList<QByteArray> values;
@ -5514,8 +5527,10 @@ void QUrl::removeAllEncodedQueryItems(const QByteArray &key)
QByteArray QUrl::encodedQuery() const QByteArray QUrl::encodedQuery() const
{ {
if (!d) return QByteArray(); if (!d) return QByteArray();
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) {
QMutexLocker lock(&d->mutex); QMutexLocker lock(&d->mutex);
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->parse();
}
return d->query; return d->query;
} }
@ -5635,8 +5650,10 @@ QByteArray QUrl::encodedFragment() const
bool QUrl::hasFragment() const bool QUrl::hasFragment() const
{ {
if (!d) return false; if (!d) return false;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) {
QMutexLocker lock(&d->mutex); QMutexLocker lock(&d->mutex);
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->parse();
}
return d->hasFragment; return d->hasFragment;
} }
@ -5739,8 +5756,10 @@ QUrl QUrl::resolved(const QUrl &relative) const
bool QUrl::isRelative() const bool QUrl::isRelative() const
{ {
if (!d) return true; if (!d) return true;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) {
QMutexLocker lock(&d->mutex); QMutexLocker lock(&d->mutex);
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->parse();
}
return d->scheme.isEmpty(); return d->scheme.isEmpty();
} }
@ -6228,8 +6247,10 @@ QString QUrl::toLocalFile() const
bool QUrl::isLocalFile() const bool QUrl::isLocalFile() const
{ {
if (!d) return false; if (!d) return false;
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) {
QMutexLocker lock(&d->mutex); QMutexLocker lock(&d->mutex);
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->parse();
}
if (d->scheme.compare(QLatin1String("file"), Qt::CaseInsensitive) != 0) if (d->scheme.compare(QLatin1String("file"), Qt::CaseInsensitive) != 0)
return false; // not file return false; // not file
@ -6250,9 +6271,10 @@ bool QUrl::isParentOf(const QUrl &childUrl) const
&& (childUrl.authority().isEmpty()) && (childUrl.authority().isEmpty())
&& childPath.length() > 0 && childPath.at(0) == QLatin1Char('/')); && childPath.length() > 0 && childPath.at(0) == QLatin1Char('/'));
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) {
QMutexLocker lock(&d->mutex); QMutexLocker lock(&d->mutex);
if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->parse();
lock.unlock(); }
QString ourPath = path(); QString ourPath = path();

View file

@ -425,7 +425,6 @@ QThread::~QThread()
bool QThread::isFinished() const bool QThread::isFinished() const
{ {
Q_D(const QThread); Q_D(const QThread);
QMutexLocker locker(&d->mutex);
return d->finished || d->isInFinish; return d->finished || d->isInFinish;
} }
@ -437,7 +436,6 @@ bool QThread::isFinished() const
bool QThread::isRunning() const bool QThread::isRunning() const
{ {
Q_D(const QThread); Q_D(const QThread);
QMutexLocker locker(&d->mutex);
return d->running && !d->isInFinish; return d->running && !d->isInFinish;
} }
@ -471,7 +469,6 @@ void QThread::setStackSize(uint stackSize)
uint QThread::stackSize() const uint QThread::stackSize() const
{ {
Q_D(const QThread); Q_D(const QThread);
QMutexLocker locker(&d->mutex);
return d->stackSize; return d->stackSize;
} }
@ -610,8 +607,6 @@ void QThread::run()
QThread::Priority QThread::priority() const QThread::Priority QThread::priority() const
{ {
Q_D(const QThread); Q_D(const QThread);
QMutexLocker locker(&d->mutex);
// mask off the high bits that are used for flags // mask off the high bits that are used for flags
return Priority(d->priority & 0xffff); return Priority(d->priority & 0xffff);
} }

View file

@ -129,7 +129,7 @@ public:
QThreadPrivate(QThreadData *d = Q_NULLPTR); QThreadPrivate(QThreadData *d = Q_NULLPTR);
~QThreadPrivate(); ~QThreadPrivate();
mutable QMutex mutex; QMutex mutex;
bool running; bool running;
bool finished; bool finished;

View file

@ -326,7 +326,6 @@ bool QDBusPendingCall::isFinished() const
if (!d) if (!d)
return true; // considered finished return true; // considered finished
QMutexLocker locker(&d->mutex);
return d->replyMessage.type() != QDBusMessage::InvalidMessage; return d->replyMessage.type() != QDBusMessage::InvalidMessage;
} }
@ -348,7 +347,6 @@ bool QDBusPendingCall::isValid() const
{ {
if (!d) if (!d)
return false; return false;
QMutexLocker locker(&d->mutex);
return d->replyMessage.type() == QDBusMessage::ReplyMessage; return d->replyMessage.type() == QDBusMessage::ReplyMessage;
} }
@ -365,7 +363,6 @@ bool QDBusPendingCall::isError() const
{ {
if (!d) if (!d)
return true; // considered finished and an error return true; // considered finished and an error
QMutexLocker locker(&d->mutex);
return d->replyMessage.type() == QDBusMessage::ErrorMessage; return d->replyMessage.type() == QDBusMessage::ErrorMessage;
} }
@ -380,14 +377,12 @@ bool QDBusPendingCall::isError() const
QDBusError QDBusPendingCall::error() const QDBusError QDBusPendingCall::error() const
{ {
if (d) { if (d) {
QMutexLocker locker(&d->mutex);
return d->replyMessage; return d->replyMessage;
} }
// not connected, return an error // not connected, return an error
QDBusError err = QDBusError(QDBusError::Disconnected, return QDBusError(QDBusError::Disconnected,
QLatin1String("Not connected to D-Bus server")); QLatin1String("Not connected to D-Bus server"));
return err;
} }
/*! /*!
@ -405,7 +400,6 @@ QDBusMessage QDBusPendingCall::reply() const
{ {
if (!d) if (!d)
return QDBusMessage::createError(error()); return QDBusMessage::createError(error());
QMutexLocker locker(&d->mutex);
return d->replyMessage; return d->replyMessage;
} }

View file

@ -78,7 +78,7 @@ public:
// } // }
mutable QMutex mutex; QMutex mutex;
QWaitCondition waitForFinishedCondition; QWaitCondition waitForFinishedCondition;
// { // {

View file

@ -207,8 +207,7 @@ class QGraphicsWidgetStyles
public: public:
QStyle *styleForWidget(const QGraphicsWidget *widget) const QStyle *styleForWidget(const QGraphicsWidget *widget) const
{ {
QMutexLocker locker(&mutex); return styles.value(widget, Q_NULLPTR);
return styles.value(widget, 0);
} }
void setStyleForWidget(QGraphicsWidget *widget, QStyle *style) void setStyleForWidget(QGraphicsWidget *widget, QStyle *style)
@ -222,7 +221,7 @@ public:
private: private:
QMap<const QGraphicsWidget *, QStyle *> styles; QMap<const QGraphicsWidget *, QStyle *> styles;
mutable QMutex mutex; QMutex mutex;
}; };
Q_GLOBAL_STATIC(QGraphicsWidgetStyles, widgetStyles) Q_GLOBAL_STATIC(QGraphicsWidgetStyles, widgetStyles)