From fd785c3899d21dd05fd013336bbc63ce818dc2a6 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 22 Aug 2019 10:17:12 +0200 Subject: QtCore: port all QMutexLocker users to qt_{scoped,unique}_lock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ... except four instances in QCoreApplication that would conflict with another change. Replace a locally-defined MutexUnlocker with a call to unlock() + qScopedGuard'ed lock() to avoid having to spell out the locker type while we can't depend on C++17 CTAD, yet. In QSettings, move the new mutex locker into and out of initDefaultPaths(), such as is idiomatic for std::unique_lock, but wasn't possible with QMutexLocker (which is not movable). Change-Id: I23056e13ecaa76159db583c7dccc6e05715e0788 Reviewed-by: MÃ¥rten Nordheim --- src/corelib/animation/qpropertyanimation.cpp | 3 +- src/corelib/animation/qvariantanimation.cpp | 5 ++-- src/corelib/global/qglobal.cpp | 19 +++++++------ src/corelib/global/qlogging.cpp | 5 ++-- src/corelib/io/qfileselector.cpp | 6 ++-- src/corelib/io/qfilesystemwatcher_win.cpp | 13 +++++---- src/corelib/io/qloggingregistry.cpp | 7 +++-- src/corelib/io/qprocess_unix.cpp | 3 +- src/corelib/io/qsettings.cpp | 41 +++++++++++++++------------- src/corelib/kernel/qcore_mac.cpp | 3 +- src/corelib/kernel/qcoreapplication.cpp | 29 +++++++++----------- src/corelib/kernel/qcoreapplication_win.cpp | 3 +- src/corelib/text/qregexp.cpp | 5 ++-- 13 files changed, 76 insertions(+), 66 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/animation/qpropertyanimation.cpp b/src/corelib/animation/qpropertyanimation.cpp index a1baa112fc..2a3572d441 100644 --- a/src/corelib/animation/qpropertyanimation.cpp +++ b/src/corelib/animation/qpropertyanimation.cpp @@ -85,6 +85,7 @@ #include "qpropertyanimation_p.h" #include +#include QT_BEGIN_NAMESPACE @@ -261,7 +262,7 @@ void QPropertyAnimation::updateState(QAbstractAnimation::State newState, QPropertyAnimation *animToStop = 0; { static QBasicMutex mutex; - QMutexLocker locker(&mutex); + auto locker = qt_unique_lock(mutex); typedef QPair QPropertyAnimationPair; typedef QHash QPropertyAnimationHash; static QPropertyAnimationHash hash; diff --git a/src/corelib/animation/qvariantanimation.cpp b/src/corelib/animation/qvariantanimation.cpp index 01a699c5dc..216c015732 100644 --- a/src/corelib/animation/qvariantanimation.cpp +++ b/src/corelib/animation/qvariantanimation.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include @@ -426,7 +427,7 @@ void QVariantAnimation::registerInterpolator(QVariantAnimation::Interpolator fun // in such an order that we get here with interpolators == NULL, // to continue causes the app to crash on exit with a SEGV if (interpolators) { - QMutexLocker locker(®isteredInterpolatorsMutex); + const auto locker = qt_scoped_lock(registeredInterpolatorsMutex); if (int(interpolationType) >= interpolators->count()) interpolators->resize(int(interpolationType) + 1); interpolators->replace(interpolationType, func); @@ -443,7 +444,7 @@ QVariantAnimation::Interpolator QVariantAnimationPrivate::getInterpolator(int in { { QInterpolatorVector *interpolators = registeredInterpolators(); - QMutexLocker locker(®isteredInterpolatorsMutex); + const auto locker = qt_scoped_lock(registeredInterpolatorsMutex); QVariantAnimation::Interpolator ret = 0; if (interpolationType < interpolators->count()) { ret = interpolators->at(interpolationType); diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index dd54ef61dc..dfd5137703 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -52,6 +52,7 @@ #include #include +#include #include #include @@ -3351,7 +3352,7 @@ static QBasicMutex environmentMutex; */ void qTzSet() { - QMutexLocker locker(&environmentMutex); + const auto locker = qt_scoped_lock(environmentMutex); #if defined(Q_OS_WIN) _tzset(); #else @@ -3365,7 +3366,7 @@ void qTzSet() */ time_t qMkTime(struct tm *when) { - QMutexLocker locker(&environmentMutex); + const auto locker = qt_scoped_lock(environmentMutex); return mktime(when); } @@ -3397,7 +3398,7 @@ time_t qMkTime(struct tm *when) */ QByteArray qgetenv(const char *varName) { - QMutexLocker locker(&environmentMutex); + const auto locker = qt_scoped_lock(environmentMutex); #ifdef Q_CC_MSVC size_t requiredSize = 0; QByteArray buffer; @@ -3465,7 +3466,7 @@ QByteArray qgetenv(const char *varName) QString qEnvironmentVariable(const char *varName, const QString &defaultValue) { #if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) - QMutexLocker locker(&environmentMutex); + const auto locker = qt_scoped_lock(environmentMutex); QVarLengthArray wname(int(strlen(varName)) + 1); for (int i = 0; i < wname.size(); ++i) // wname.size() is correct: will copy terminating null wname[i] = uchar(varName[i]); @@ -3513,7 +3514,7 @@ QString qEnvironmentVariable(const char *varName) */ bool qEnvironmentVariableIsEmpty(const char *varName) noexcept { - QMutexLocker locker(&environmentMutex); + const auto locker = qt_scoped_lock(environmentMutex); #ifdef Q_CC_MSVC // we provide a buffer that can only hold the empty string, so // when the env.var isn't empty, we'll get an ERANGE error (buffer @@ -3552,7 +3553,7 @@ int qEnvironmentVariableIntValue(const char *varName, bool *ok) noexcept static const int MaxDigitsForOctalInt = (std::numeric_limits::digits + NumBinaryDigitsPerOctalDigit - 1) / NumBinaryDigitsPerOctalDigit; - QMutexLocker locker(&environmentMutex); + const auto locker = qt_scoped_lock(environmentMutex); #ifdef Q_CC_MSVC // we provide a buffer that can hold any int value: char buffer[MaxDigitsForOctalInt + 2]; // +1 for NUL +1 for optional '-' @@ -3617,7 +3618,7 @@ int qEnvironmentVariableIntValue(const char *varName, bool *ok) noexcept */ bool qEnvironmentVariableIsSet(const char *varName) noexcept { - QMutexLocker locker(&environmentMutex); + const auto locker = qt_scoped_lock(environmentMutex); #ifdef Q_CC_MSVC size_t requiredSize = 0; (void)getenv_s(&requiredSize, 0, 0, varName); @@ -3647,7 +3648,7 @@ bool qEnvironmentVariableIsSet(const char *varName) noexcept */ bool qputenv(const char *varName, const QByteArray& value) { - QMutexLocker locker(&environmentMutex); + const auto locker = qt_scoped_lock(environmentMutex); #if defined(Q_CC_MSVC) return _putenv_s(varName, value.constData()) == 0; #elif (defined(_POSIX_VERSION) && (_POSIX_VERSION-0) >= 200112L) || defined(Q_OS_HAIKU) @@ -3678,7 +3679,7 @@ bool qputenv(const char *varName, const QByteArray& value) */ bool qunsetenv(const char *varName) { - QMutexLocker locker(&environmentMutex); + const auto locker = qt_scoped_lock(environmentMutex); #if defined(Q_CC_MSVC) return _putenv_s(varName, "") == 0; #elif (defined(_POSIX_VERSION) && (_POSIX_VERSION-0) >= 200112L) || defined(Q_OS_BSD4) || defined(Q_OS_HAIKU) diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index 621b6d7d13..491823f217 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -49,6 +49,7 @@ #include "qvarlengtharray.h" #include "qdebug.h" #include "qmutex.h" +#include #include "qloggingcategory.h" #ifndef QT_BOOTSTRAPPED #include "qelapsedtimer.h" @@ -1375,7 +1376,7 @@ QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, con { QString message; - QMutexLocker lock(&QMessagePattern::mutex); + const auto locker = qt_scoped_lock(QMessagePattern::mutex); QMessagePattern *pattern = qMessagePattern(); if (!pattern) { @@ -2091,7 +2092,7 @@ QtMsgHandler qInstallMsgHandler(QtMsgHandler h) void qSetMessagePattern(const QString &pattern) { - QMutexLocker lock(&QMessagePattern::mutex); + const auto locker = qt_scoped_lock(QMessagePattern::mutex); if (!qMessagePattern()->fromEnvironment) qMessagePattern()->setPattern(pattern); diff --git a/src/corelib/io/qfileselector.cpp b/src/corelib/io/qfileselector.cpp index 500b475d1d..31c490de66 100644 --- a/src/corelib/io/qfileselector.cpp +++ b/src/corelib/io/qfileselector.cpp @@ -44,7 +44,7 @@ #include #include #include -#include +#include #include #include #include @@ -312,7 +312,7 @@ void QFileSelector::setExtraSelectors(const QStringList &list) QStringList QFileSelector::allSelectors() const { Q_D(const QFileSelector); - QMutexLocker locker(&sharedDataMutex); + const auto locker = qt_scoped_lock(sharedDataMutex); QFileSelectorPrivate::updateSelectors(); return d->extras + sharedData->staticSelectors; } @@ -371,7 +371,7 @@ QStringList QFileSelectorPrivate::platformSelectors() void QFileSelectorPrivate::addStatics(const QStringList &statics) { - QMutexLocker locker(&sharedDataMutex); + const auto locker = qt_scoped_lock(sharedDataMutex); sharedData->preloadedStatics << statics; sharedData->staticSelectors.clear(); } diff --git a/src/corelib/io/qfilesystemwatcher_win.cpp b/src/corelib/io/qfilesystemwatcher_win.cpp index c22a003931..5f91ce5e3d 100644 --- a/src/corelib/io/qfilesystemwatcher_win.cpp +++ b/src/corelib/io/qfilesystemwatcher_win.cpp @@ -48,6 +48,7 @@ #include #include #include +#include #include @@ -423,7 +424,7 @@ QStringList QWindowsFileSystemWatcherEngine::addPaths(const QStringList &paths, end = threads.constEnd(); for(jt = threads.constBegin(); jt != end; ++jt) { thread = *jt; - QMutexLocker locker(&(thread->mutex)); + const auto locker = qt_scoped_lock(thread->mutex); const auto hit = thread->handleForDir.find(QFileSystemWatcherPathKey(absolutePath)); if (hit != thread->handleForDir.end() && hit.value().flags < flags) { @@ -478,7 +479,7 @@ QStringList QWindowsFileSystemWatcherEngine::addPaths(const QStringList &paths, // now look for a thread to insert bool found = false; for (QWindowsFileSystemWatcherEngineThread *thread : qAsConst(threads)) { - QMutexLocker locker(&(thread->mutex)); + const auto locker = qt_scoped_lock(thread->mutex); if (thread->handles.count() < MAXIMUM_WAIT_OBJECTS) { DEBUG() << "Added handle" << handle.handle << "for" << absolutePath << "to watch" << fileInfo.absoluteFilePath() << "to existing thread " << thread; @@ -554,7 +555,7 @@ QStringList QWindowsFileSystemWatcherEngine::removePaths(const QStringList &path if (*jt == 0) continue; - QMutexLocker locker(&(thread->mutex)); + auto locker = qt_unique_lock(thread->mutex); QWindowsFileSystemWatcherEngine::Handle handle = thread->handleForDir.value(QFileSystemWatcherPathKey(absolutePath)); if (handle.handle == INVALID_HANDLE_VALUE) { @@ -587,7 +588,7 @@ QStringList QWindowsFileSystemWatcherEngine::removePaths(const QStringList &path locker.unlock(); thread->stop(); thread->wait(); - locker.relock(); + locker.lock(); // We can't delete the thread until the mutex locker is // out of scope } @@ -652,13 +653,13 @@ static QString msgFindNextFailed(const QWindowsFileSystemWatcherEngineThread::Pa void QWindowsFileSystemWatcherEngineThread::run() { - QMutexLocker locker(&mutex); + auto locker = qt_unique_lock(mutex); forever { QVector handlesCopy = handles; locker.unlock(); DEBUG() << "QWindowsFileSystemWatcherThread" << this << "waiting on" << handlesCopy.count() << "handles"; DWORD r = WaitForMultipleObjects(handlesCopy.count(), handlesCopy.constData(), false, INFINITE); - locker.relock(); + locker.lock(); do { if (r == WAIT_OBJECT_0) { int m = msg; diff --git a/src/corelib/io/qloggingregistry.cpp b/src/corelib/io/qloggingregistry.cpp index 7849dfd14c..e8eb18b4c1 100644 --- a/src/corelib/io/qloggingregistry.cpp +++ b/src/corelib/io/qloggingregistry.cpp @@ -41,6 +41,7 @@ #include #include +#include #include #include #include @@ -356,7 +357,7 @@ void QLoggingRegistry::initializeRules() */ void QLoggingRegistry::registerCategory(QLoggingCategory *cat, QtMsgType enableForLevel) { - QMutexLocker locker(®istryMutex); + const auto locker = qt_scoped_lock(registryMutex); if (!categories.contains(cat)) { categories.insert(cat, enableForLevel); @@ -370,7 +371,7 @@ void QLoggingRegistry::registerCategory(QLoggingCategory *cat, QtMsgType enableF */ void QLoggingRegistry::unregisterCategory(QLoggingCategory *cat) { - QMutexLocker locker(®istryMutex); + const auto locker = qt_scoped_lock(registryMutex); categories.remove(cat); } @@ -413,7 +414,7 @@ void QLoggingRegistry::updateRules() QLoggingCategory::CategoryFilter QLoggingRegistry::installFilter(QLoggingCategory::CategoryFilter filter) { - QMutexLocker locker(®istryMutex); + const auto locker = qt_scoped_lock(registryMutex); if (!filter) filter = defaultCategoryFilter; diff --git a/src/corelib/io/qprocess_unix.cpp b/src/corelib/io/qprocess_unix.cpp index 951fc4ccaa..0c80daa024 100644 --- a/src/corelib/io/qprocess_unix.cpp +++ b/src/corelib/io/qprocess_unix.cpp @@ -89,6 +89,7 @@ QT_END_NAMESPACE #include "qprocess_p.h" #include "qstandardpaths.h" #include "private/qcore_unix_p.h" +#include "private/qlocking_p.h" #ifdef Q_OS_MAC #include @@ -404,7 +405,7 @@ void QProcessPrivate::startProcess() // CFBundle is not reentrant, since CFBundleCreate might return a reference // to a cached bundle object. Protect the bundle calls with a mutex lock. static QBasicMutex cfbundleMutex; - QMutexLocker lock(&cfbundleMutex); + const auto locker = qt_scoped_lock(cfbundleMutex); QCFType bundle = CFBundleCreate(0, url); // 'executableURL' can be either relative or absolute ... QCFType executableURL = CFBundleCopyExecutableURL(bundle); diff --git a/src/corelib/io/qsettings.cpp b/src/corelib/io/qsettings.cpp index accde01f16..4a119a1e2f 100644 --- a/src/corelib/io/qsettings.cpp +++ b/src/corelib/io/qsettings.cpp @@ -47,6 +47,7 @@ #include "qdir.h" #include "qfileinfo.h" #include "qmutex.h" +#include "private/qlocking_p.h" #include "qlibraryinfo.h" #include "qtemporaryfile.h" #include "qstandardpaths.h" @@ -210,7 +211,7 @@ QConfFile *QConfFile::fromName(const QString &fileName, bool _userPerms) ConfFileCache *unusedCache = unusedCacheFunc(); QConfFile *confFile = 0; - QMutexLocker locker(&settingsGlobalMutex); + const auto locker = qt_scoped_lock(settingsGlobalMutex); if (!(confFile = usedHash->value(absPath))) { if ((confFile = unusedCache->take(absPath))) @@ -225,7 +226,7 @@ QConfFile *QConfFile::fromName(const QString &fileName, bool _userPerms) void QConfFile::clearCache() { - QMutexLocker locker(&settingsGlobalMutex); + const auto locker = qt_scoped_lock(settingsGlobalMutex); unusedCacheFunc()->clear(); } @@ -937,7 +938,7 @@ void QConfFileSettingsPrivate::initFormat() #endif if (format > QSettings::IniFormat) { - QMutexLocker locker(&settingsGlobalMutex); + const auto locker = qt_scoped_lock(settingsGlobalMutex); const CustomFormatVector *customFormatVector = customFormatVectorFunc(); int i = (int)format - (int)QSettings::CustomFormat1; @@ -1052,11 +1053,11 @@ static QString make_user_path() } #endif // !Q_OS_WIN -static void initDefaultPaths(QMutexLocker *locker) +static std::unique_lock initDefaultPaths(std::unique_lock locker) { PathHash *pathHash = pathHashFunc(); - locker->unlock(); + locker.unlock(); /* QLibraryInfo::location() uses QSettings, so in order to @@ -1065,7 +1066,7 @@ static void initDefaultPaths(QMutexLocker *locker) */ QString systemPath = QLibraryInfo::location(QLibraryInfo::SettingsPath) + QLatin1Char('/'); - locker->relock(); + locker.lock(); if (pathHash->isEmpty()) { /* Lazy initialization of pathHash. We initialize the @@ -1096,6 +1097,8 @@ static void initDefaultPaths(QMutexLocker *locker) #endif #endif // Q_OS_WIN } + + return locker; } static Path getPath(QSettings::Format format, QSettings::Scope scope) @@ -1103,10 +1106,10 @@ static Path getPath(QSettings::Format format, QSettings::Scope scope) Q_ASSERT((int)QSettings::NativeFormat == 0); Q_ASSERT((int)QSettings::IniFormat == 1); - QMutexLocker locker(&settingsGlobalMutex); + auto locker = qt_unique_lock(settingsGlobalMutex); PathHash *pathHash = pathHashFunc(); if (pathHash->isEmpty()) - initDefaultPaths(&locker); + locker = initDefaultPaths(std::move(locker)); Path result = pathHash->value(pathHashKey(format, scope)); if (!result.path.isEmpty()) @@ -1120,7 +1123,7 @@ static Path getPath(QSettings::Format format, QSettings::Scope scope) // Note: Suitable only for autotests. void Q_AUTOTEST_EXPORT clearDefaultPaths() { - QMutexLocker locker(&settingsGlobalMutex); + const auto locker = qt_scoped_lock(settingsGlobalMutex); pathHashFunc()->clear(); } #endif // QT_BUILD_INTERNAL && Q_XDG_PLATFORM && !QT_NO_STANDARDPATHS @@ -1200,7 +1203,7 @@ QConfFileSettingsPrivate::QConfFileSettingsPrivate(const QString &fileName, QConfFileSettingsPrivate::~QConfFileSettingsPrivate() { - QMutexLocker locker(&settingsGlobalMutex); + const auto locker = qt_scoped_lock(settingsGlobalMutex); ConfFileHash *usedHash = usedHashFunc(); ConfFileCache *unusedCache = unusedCacheFunc(); @@ -1239,7 +1242,7 @@ void QConfFileSettingsPrivate::remove(const QString &key) QSettingsKey theKey(key, caseSensitivity); QSettingsKey prefix(key + QLatin1Char('/'), caseSensitivity); - QMutexLocker locker(&confFile->mutex); + const auto locker = qt_scoped_lock(confFile->mutex); ensureSectionParsed(confFile, theKey); ensureSectionParsed(confFile, prefix); @@ -1267,7 +1270,7 @@ void QConfFileSettingsPrivate::set(const QString &key, const QVariant &value) QConfFile *confFile = confFiles.at(0); QSettingsKey theKey(key, caseSensitivity, nextPosition++); - QMutexLocker locker(&confFile->mutex); + const auto locker = qt_scoped_lock(confFile->mutex); confFile->removedKeys.remove(theKey); confFile->addedKeys.insert(theKey, value); } @@ -1279,7 +1282,7 @@ bool QConfFileSettingsPrivate::get(const QString &key, QVariant *value) const bool found = false; for (auto confFile : qAsConst(confFiles)) { - QMutexLocker locker(&confFile->mutex); + const auto locker = qt_scoped_lock(confFile->mutex); if (!confFile->addedKeys.isEmpty()) { j = confFile->addedKeys.constFind(theKey); @@ -1312,7 +1315,7 @@ QStringList QConfFileSettingsPrivate::children(const QString &prefix, ChildSpec int startPos = prefix.size(); for (auto confFile : qAsConst(confFiles)) { - QMutexLocker locker(&confFile->mutex); + const auto locker = qt_scoped_lock(confFile->mutex); if (thePrefix.isEmpty()) ensureAllSectionsParsed(confFile); @@ -1351,7 +1354,7 @@ void QConfFileSettingsPrivate::clear() // Note: First config file is always the most specific. QConfFile *confFile = confFiles.at(0); - QMutexLocker locker(&confFile->mutex); + const auto locker = qt_scoped_lock(confFile->mutex); ensureAllSectionsParsed(confFile); confFile->addedKeys.clear(); confFile->removedKeys = confFile->originalKeys; @@ -1363,7 +1366,7 @@ void QConfFileSettingsPrivate::sync() // error we just try to go on and make the best of it for (auto confFile : qAsConst(confFiles)) { - QMutexLocker locker(&confFile->mutex); + const auto locker = qt_scoped_lock(confFile->mutex); syncConfFile(confFile); } } @@ -3521,10 +3524,10 @@ void QSettings::setUserIniPath(const QString &dir) */ void QSettings::setPath(Format format, Scope scope, const QString &path) { - QMutexLocker locker(&settingsGlobalMutex); + auto locker = qt_unique_lock(settingsGlobalMutex); PathHash *pathHash = pathHashFunc(); if (pathHash->isEmpty()) - initDefaultPaths(&locker); + locker = initDefaultPaths(std::move(locker)); pathHash->insert(pathHashKey(format, scope), Path(path + QDir::separator(), true)); } @@ -3604,7 +3607,7 @@ QSettings::Format QSettings::registerFormat(const QString &extension, ReadFunc r Q_ASSERT(caseSensitivity == Qt::CaseSensitive); #endif - QMutexLocker locker(&settingsGlobalMutex); + const auto locker = qt_scoped_lock(settingsGlobalMutex); CustomFormatVector *customFormatVector = customFormatVectorFunc(); int index = customFormatVector->size(); if (index == 16) // the QSettings::Format enum has room for 16 custom formats diff --git a/src/corelib/kernel/qcore_mac.cpp b/src/corelib/kernel/qcore_mac.cpp index b048576f5b..e59835be48 100644 --- a/src/corelib/kernel/qcore_mac.cpp +++ b/src/corelib/kernel/qcore_mac.cpp @@ -44,6 +44,7 @@ #include "qpair.h" #include "qmutex.h" #include "qvarlengtharray.h" +#include "private/qlocking_p.h" QT_BEGIN_NAMESPACE @@ -135,7 +136,7 @@ os_log_type_t AppleUnifiedLogger::logTypeForMessageType(QtMsgType msgType) os_log_t AppleUnifiedLogger::cachedLog(const QString &subsystem, const QString &category) { static QBasicMutex mutex; - QMutexLocker locker(&mutex); + const auto locker = qt_scoped_lock(mutex); static QHash, os_log_t> logs; const auto cacheKey = qMakePair(subsystem, category); diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 87dae896fa..6647ea99f5 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -55,6 +55,7 @@ #include #include #include +#include #include #ifndef QT_NO_QOBJECT #include @@ -70,6 +71,7 @@ #include #include #include +#include #include #ifndef QT_NO_QOBJECT @@ -294,7 +296,7 @@ void qAddPreRoutine(QtStartUpFunction p) // Due to C++11 parallel dynamic initialization, this can be called // from multiple threads. - QMutexLocker locker(&globalRoutinesMutex); + const auto locker = qt_scoped_lock(globalRoutinesMutex); list->prepend(p); // in case QCoreApplication is re-created, see qt_call_pre_routines } @@ -303,7 +305,7 @@ void qAddPostRoutine(QtCleanUpFunction p) QVFuncList *list = postRList(); if (!list) return; - QMutexLocker locker(&globalRoutinesMutex); + const auto locker = qt_scoped_lock(globalRoutinesMutex); list->prepend(p); } @@ -312,7 +314,7 @@ void qRemovePostRoutine(QtCleanUpFunction p) QVFuncList *list = postRList(); if (!list) return; - QMutexLocker locker(&globalRoutinesMutex); + const auto locker = qt_scoped_lock(globalRoutinesMutex); list->removeAll(p); } @@ -323,7 +325,7 @@ static void qt_call_pre_routines() QVFuncList list; { - QMutexLocker locker(&globalRoutinesMutex); + const auto locker = qt_scoped_lock(globalRoutinesMutex); // Unlike qt_call_post_routines, we don't empty the list, because // Q_COREAPP_STARTUP_FUNCTION is a macro, so the user expects // the function to be executed every time QCoreApplication is created. @@ -342,7 +344,7 @@ void Q_CORE_EXPORT qt_call_post_routines() QVFuncList list; { // extract the current list and make the stored list empty - QMutexLocker locker(&globalRoutinesMutex); + const auto locker = qt_scoped_lock(globalRoutinesMutex); qSwap(*postRList, list); } @@ -522,7 +524,7 @@ void QCoreApplicationPrivate::cleanupThreadData() #endif // need to clear the state of the mainData, just in case a new QCoreApplication comes along. - QMutexLocker locker(&threadData->postEventList.mutex); + const auto locker = qt_scoped_lock(threadData->postEventList.mutex); for (int i = 0; i < threadData->postEventList.size(); ++i) { const QPostEvent &pe = threadData->postEventList.at(i); if (pe.event) { @@ -1705,7 +1707,7 @@ void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver, int event_type ++data->postEventList.recursion; - QMutexLocker locker(&data->postEventList.mutex); + auto locker = qt_unique_lock(data->postEventList.mutex); // by default, we assume that the event dispatcher can go to sleep after // processing all events. if any new events are posted while we send @@ -1821,13 +1823,8 @@ void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver, int event_type // for the next event. const_cast(pe).event = 0; - struct MutexUnlocker - { - QMutexLocker &m; - MutexUnlocker(QMutexLocker &m) : m(m) { m.unlock(); } - ~MutexUnlocker() { m.relock(); } - }; - MutexUnlocker unlocker(locker); + locker.unlock(); + const auto relocker = qScopeGuard([&locker] { locker.lock(); }); QScopedPointer event_deleter(e); // will delete the event (with the mutex unlocked) @@ -1864,7 +1861,7 @@ void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver, int event_type void QCoreApplication::removePostedEvents(QObject *receiver, int eventType) { QThreadData *data = receiver ? receiver->d_func()->threadData : QThreadData::current(); - QMutexLocker locker(&data->postEventList.mutex); + auto locker = qt_unique_lock(data->postEventList.mutex); // the QObject destructor calls this function directly. this can // happen while the event loop is in the middle of posting events, @@ -1927,7 +1924,7 @@ void QCoreApplicationPrivate::removePostedEvent(QEvent * event) QThreadData *data = QThreadData::current(); - QMutexLocker locker(&data->postEventList.mutex); + const auto locker = qt_scoped_lock(data->postEventList.mutex); if (data->postEventList.size() == 0) { #if defined(QT_DEBUG) diff --git a/src/corelib/kernel/qcoreapplication_win.cpp b/src/corelib/kernel/qcoreapplication_win.cpp index 822b68cf62..961b96710e 100644 --- a/src/corelib/kernel/qcoreapplication_win.cpp +++ b/src/corelib/kernel/qcoreapplication_win.cpp @@ -47,6 +47,7 @@ #ifndef QT_NO_QOBJECT #include "qmutex.h" #include +#include #endif #include "qtextstream.h" #include @@ -919,7 +920,7 @@ void QCoreApplicationPrivate::removePostedTimerEvent(QObject *object, int timerI { QThreadData *data = object->d_func()->threadData; - QMutexLocker locker(&data->postEventList.mutex); + const auto locker = qt_scoped_lock(data->postEventList.mutex); if (data->postEventList.size() == 0) return; for (int i = 0; i < data->postEventList.size(); ++i) { diff --git a/src/corelib/text/qregexp.cpp b/src/corelib/text/qregexp.cpp index d81826b47b..41f3508ff8 100644 --- a/src/corelib/text/qregexp.cpp +++ b/src/corelib/text/qregexp.cpp @@ -52,6 +52,7 @@ #include "qstringlist.h" #include "qstringmatcher.h" #include "qvector.h" +#include "private/qlocking_p.h" #include #include @@ -3825,7 +3826,7 @@ static QBasicMutex engineCacheMutex; static void derefEngine(QRegExpEngine *eng, const QRegExpEngineKey &key) { #if !defined(QT_NO_REGEXP_OPTIM) - QMutexLocker locker(&engineCacheMutex); + const auto locker = qt_scoped_lock(engineCacheMutex); if (!eng->ref.deref()) { if (QRECache *c = engineCache()) { c->unusedEngines.insert(key, eng, 4 + key.pattern.length() / 4); @@ -3846,7 +3847,7 @@ static void prepareEngine_helper(QRegExpPrivate *priv) Q_ASSERT(!priv->eng); #if !defined(QT_NO_REGEXP_OPTIM) - QMutexLocker locker(&engineCacheMutex); + const auto locker = qt_scoped_lock(engineCacheMutex); if (QRECache *c = engineCache()) { priv->eng = c->unusedEngines.take(priv->engineKey); if (!priv->eng) -- cgit v1.2.3