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/kernel/qcore_mac.cpp | 3 ++- src/corelib/kernel/qcoreapplication.cpp | 29 +++++++++++++---------------- src/corelib/kernel/qcoreapplication_win.cpp | 3 ++- 3 files changed, 17 insertions(+), 18 deletions(-) (limited to 'src/corelib/kernel') 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) { -- cgit v1.2.3