QtCore: port all QMutexLocker users to qt_{scoped,unique}_lock

... 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 <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2019-08-22 10:17:12 +02:00
parent 0d336ab313
commit fd785c3899
13 changed files with 76 additions and 66 deletions

View File

@ -85,6 +85,7 @@
#include "qpropertyanimation_p.h"
#include <QtCore/QMutex>
#include <QtCore/private/qlocking_p.h>
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<QObject *, QByteArray> QPropertyAnimationPair;
typedef QHash<QPropertyAnimationPair, QPropertyAnimation*> QPropertyAnimationHash;
static QPropertyAnimationHash hash;

View File

@ -43,6 +43,7 @@
#include <QtCore/qrect.h>
#include <QtCore/qline.h>
#include <QtCore/qmutex.h>
#include <QtCore/private/qlocking_p.h>
#include <algorithm>
@ -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(&registeredInterpolatorsMutex);
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(&registeredInterpolatorsMutex);
const auto locker = qt_scoped_lock(registeredInterpolatorsMutex);
QVariantAnimation::Interpolator ret = 0;
if (interpolationType < interpolators->count()) {
ret = interpolators->at(interpolationType);

View File

@ -52,6 +52,7 @@
#include <private/qlocale_tools_p.h>
#include <qmutex.h>
#include <QtCore/private/qlocking_p.h>
#include <stdlib.h>
#include <limits.h>
@ -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<wchar_t, 32> 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<uint>::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)

View File

@ -49,6 +49,7 @@
#include "qvarlengtharray.h"
#include "qdebug.h"
#include "qmutex.h"
#include <QtCore/private/qlocking_p.h>
#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);

View File

@ -44,7 +44,7 @@
#include <QtCore/QFile>
#include <QtCore/QDir>
#include <QtCore/QMutex>
#include <QtCore/QMutexLocker>
#include <QtCore/private/qlocking_p.h>
#include <QtCore/QUrl>
#include <QtCore/QFileInfo>
#include <QtCore/QLocale>
@ -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();
}

View File

@ -48,6 +48,7 @@
#include <qdatetime.h>
#include <qdir.h>
#include <qtextstream.h>
#include <private/qlocking_p.h>
#include <qt_windows.h>
@ -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<HANDLE> 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;

View File

@ -41,6 +41,7 @@
#include <QtCore/qfile.h>
#include <QtCore/qlibraryinfo.h>
#include <QtCore/private/qlocking_p.h>
#include <QtCore/qstandardpaths.h>
#include <QtCore/qtextstream.h>
#include <QtCore/qdir.h>
@ -356,7 +357,7 @@ void QLoggingRegistry::initializeRules()
*/
void QLoggingRegistry::registerCategory(QLoggingCategory *cat, QtMsgType enableForLevel)
{
QMutexLocker locker(&registryMutex);
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(&registryMutex);
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(&registryMutex);
const auto locker = qt_scoped_lock(registryMutex);
if (!filter)
filter = defaultCategoryFilter;

View File

@ -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 <private/qcore_mac_p.h>
@ -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<CFBundleRef> bundle = CFBundleCreate(0, url);
// 'executableURL' can be either relative or absolute ...
QCFType<CFURLRef> executableURL = CFBundleCopyExecutableURL(bundle);

View File

@ -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<QBasicMutex> initDefaultPaths(std::unique_lock<QBasicMutex> 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

View File

@ -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<QPair<QString, QString>, os_log_t> logs;
const auto cacheKey = qMakePair(subsystem, category);

View File

@ -55,6 +55,7 @@
#include <qfileinfo.h>
#include <qmutex.h>
#include <private/qloggingregistry_p.h>
#include <qscopeguard.h>
#include <qstandardpaths.h>
#ifndef QT_NO_QOBJECT
#include <qthread.h>
@ -70,6 +71,7 @@
#include <private/qfactoryloader_p.h>
#include <private/qfunctions_p.h>
#include <private/qlocale_p.h>
#include <private/qlocking_p.h>
#include <private/qhooks_p.h>
#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<QPostEvent &>(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<QEvent> 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)

View File

@ -47,6 +47,7 @@
#ifndef QT_NO_QOBJECT
#include "qmutex.h"
#include <private/qthread_p.h>
#include <private/qlocking_p.h>
#endif
#include "qtextstream.h"
#include <ctype.h>
@ -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) {

View File

@ -52,6 +52,7 @@
#include "qstringlist.h"
#include "qstringmatcher.h"
#include "qvector.h"
#include "private/qlocking_p.h"
#include <limits.h>
#include <algorithm>
@ -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)