From 3ba0f1c05cce7cae10198ea9e63f09198791ef22 Mon Sep 17 00:00:00 2001 From: JoergAtGithub Date: Thu, 17 Feb 2022 23:55:25 +0100 Subject: [PATCH] Improved mutex locker naming and style --- src/controllers/hid/hidiooutputreport.cpp | 10 ++++---- src/controllers/hid/hidiothread.cpp | 29 +++++++++++------------ 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/controllers/hid/hidiooutputreport.cpp b/src/controllers/hid/hidiooutputreport.cpp index 4344993938e..ed17c64d132 100644 --- a/src/controllers/hid/hidiooutputreport.cpp +++ b/src/controllers/hid/hidiooutputreport.cpp @@ -29,7 +29,7 @@ HidIoOutputReport::HidIoOutputReport( void HidIoOutputReport::updateCachedData(const QByteArray& data, const mixxx::hid::DeviceInfo& deviceInfo, const RuntimeLoggingCategory& logOutput) { - auto lock = lockMutex(&m_cachedDataMutex); + auto cacheLock = lockMutex(&m_cachedDataMutex); if (!m_lastCachedDataSize) { // First call updateCachedData for this report @@ -73,7 +73,7 @@ bool HidIoOutputReport::sendCachedData(QMutex* pHidDeviceMutex, const RuntimeLoggingCategory& logOutput) { auto startOfHidWrite = mixxx::Time::elapsed(); - auto reportCacheLock = lockMutex(&m_cachedDataMutex); + auto cacheLock = lockMutex(&m_cachedDataMutex); if (!m_possiblyUnsentDataCached) { // Return with false, to signal the caller, that no time consuming IO operation was necessary @@ -91,7 +91,7 @@ bool HidIoOutputReport::sendCachedData(QMutex* pHidDeviceMutex, // that the byte array compare operation is executed for the same data again m_possiblyUnsentDataCached = false; - reportCacheLock.unlock(); + cacheLock.unlock(); qCDebug(logOutput) << "t:" << startOfHidWrite.formatMillisWithUnit() << " Skipped identical Output Report for" @@ -111,7 +111,7 @@ bool HidIoOutputReport::sendCachedData(QMutex* pHidDeviceMutex, m_lastSentData.swap(m_cachedData); m_possiblyUnsentDataCached = false; - reportCacheLock.unlock(); + cacheLock.unlock(); auto hidDeviceLock = lockMutex(pHidDeviceMutex); @@ -130,7 +130,7 @@ bool HidIoOutputReport::sendCachedData(QMutex* pHidDeviceMutex, hidDeviceLock.unlock(); if (result == -1) { - reportCacheLock.relock(); + cacheLock.relock(); // Clear the m_lastSentData because the last send data are not reliable known. // These error should not occur in normal operation, // therefore the performance impact of additional memory allocation diff --git a/src/controllers/hid/hidiothread.cpp b/src/controllers/hid/hidiothread.cpp index c22105d9647..3fa82c9fca3 100644 --- a/src/controllers/hid/hidiothread.cpp +++ b/src/controllers/hid/hidiothread.cpp @@ -84,7 +84,7 @@ void HidIoThread::run() { void HidIoThread::pollBufferedInputReports() { Trace hidRead("HidIoThread pollBufferedInputReports"); - auto lock = lockMutex(&m_hidDeviceMutex); + auto hidDeviceLock = lockMutex(&m_hidDeviceMutex); // This function reads the available HID Input Reports using hidapi. // Important to know is, that this reading is not a hardware operation, // instead it reads previously received HID Input Reports from a ring buffer. @@ -145,7 +145,7 @@ void HidIoThread::processInputReport(int bytesRead) { QByteArray HidIoThread::getInputReport(unsigned int reportID) { auto startOfHidGetInputReport = mixxx::Time::elapsed(); - auto lock = lockMutex(&m_hidDeviceMutex); + auto hidDeviceLock = lockMutex(&m_hidDeviceMutex); m_pPollData[m_pollingBufferIndex][0] = reportID; // FIXME: implement upstream for hidraw backend on Linux @@ -173,7 +173,7 @@ QByteArray HidIoThread::getInputReport(unsigned int reportID) { reinterpret_cast(m_pPollData[m_pollingBufferIndex]), bytesRead); - lock.unlock(); + hidDeviceLock.unlock(); qCDebug(m_logInput) << bytesRead << "bytes received by hid_get_input_report" << m_deviceInfo.formatName() << "serial #" @@ -190,7 +190,7 @@ QByteArray HidIoThread::getInputReport(unsigned int reportID) { } void HidIoThread::updateCachedOutputReportData(const QByteArray& data, unsigned int reportID) { - auto lock = lockMutex(&m_outputReportMapMutex); + auto mapLock = lockMutex(&m_outputReportMapMutex); if (m_outputReports.find(reportID) == m_outputReports.end()) { std::unique_ptr pNewOutputReport; m_outputReports[reportID] = std::make_unique( @@ -203,7 +203,7 @@ void HidIoThread::updateCachedOutputReportData(const QByteArray& data, unsigned // Therefore actualOutputReportIterator doesn't require Mutex protection. auto actualOutputReportIterator = m_outputReports.find(reportID); - lock.unlock(); + mapLock.unlock(); actualOutputReportIterator->second->updateCachedData(data, m_deviceInfo, m_logOutput); } @@ -214,13 +214,12 @@ bool HidIoThread::sendNextCachedOutputReport() { // If the map size increases, this loop will execute one iteration more, // which only has the effect, that one additional lookup operation for unsent data will be executed. for (unsigned char i = 0; i < m_outputReports.size(); i++) { - { - auto lock = lockMutex(&m_outputReportMapMutex); - m_outputReportIterator++; - if (m_outputReportIterator == m_outputReports.end()) { - m_outputReportIterator = m_outputReports.begin(); - } + auto mapLock = lockMutex(&m_outputReportMapMutex); + m_outputReportIterator++; + if (m_outputReportIterator == m_outputReports.end()) { + m_outputReportIterator = m_outputReports.begin(); } + mapLock.unlock(); // The only mutable operation on m_outputReports is insert // by std::map::operator[] @@ -246,7 +245,7 @@ void HidIoThread::sendFeatureReport( dataArray.append(reportID); dataArray.append(reportData); - auto lock = lockMutex(&m_hidDeviceMutex); + auto hidDeviceLock = lockMutex(&m_hidDeviceMutex); int result = hid_send_feature_report(m_pHidDevice, reinterpret_cast(dataArray.constData()), dataArray.size()); @@ -260,7 +259,7 @@ void HidIoThread::sendFeatureReport( return; } - lock.unlock(); + hidDeviceLock.unlock(); qCDebug(m_logOutput) << result << "bytes sent by sendFeatureReport to" @@ -277,7 +276,7 @@ QByteArray HidIoThread::getFeatureReport( unsigned char dataRead[kReportIdSize + kBufferSize]; dataRead[0] = reportID; - auto lock = lockMutex(&m_hidDeviceMutex); + auto hidDeviceLock = lockMutex(&m_hidDeviceMutex); int bytesRead = hid_get_feature_report(m_pHidDevice, dataRead, kReportIdSize + kBufferSize); @@ -294,7 +293,7 @@ QByteArray HidIoThread::getFeatureReport( return {}; } - lock.unlock(); + hidDeviceLock.unlock(); qCDebug(m_logInput) << bytesRead << "bytes received by getFeatureReport from"