Skip to content

Commit

Permalink
Improved mutex locker naming and style
Browse files Browse the repository at this point in the history
  • Loading branch information
JoergAtGithub committed Feb 17, 2022
1 parent ca3e246 commit 3ba0f1c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/controllers/hid/hidiooutputreport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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);

Expand All @@ -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
Expand Down
29 changes: 14 additions & 15 deletions src/controllers/hid/hidiothread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -173,7 +173,7 @@ QByteArray HidIoThread::getInputReport(unsigned int reportID) {
reinterpret_cast<char*>(m_pPollData[m_pollingBufferIndex]),
bytesRead);

lock.unlock();
hidDeviceLock.unlock();

qCDebug(m_logInput) << bytesRead << "bytes received by hid_get_input_report"
<< m_deviceInfo.formatName() << "serial #"
Expand All @@ -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<HidIoOutputReport> pNewOutputReport;
m_outputReports[reportID] = std::make_unique<HidIoOutputReport>(
Expand All @@ -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);
}
Expand All @@ -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<Key,T,Compare,Allocator>::operator[]
Expand All @@ -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<const unsigned char*>(dataArray.constData()),
dataArray.size());
Expand All @@ -260,7 +259,7 @@ void HidIoThread::sendFeatureReport(
return;
}

lock.unlock();
hidDeviceLock.unlock();

qCDebug(m_logOutput)
<< result << "bytes sent by sendFeatureReport to"
Expand All @@ -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);
Expand All @@ -294,7 +293,7 @@ QByteArray HidIoThread::getFeatureReport(
return {};
}

lock.unlock();
hidDeviceLock.unlock();

qCDebug(m_logInput)
<< bytesRead << "bytes received by getFeatureReport from"
Expand Down

0 comments on commit 3ba0f1c

Please sign in to comment.