Re-do the usage of QWriteLocker

Only lock if necessary to avoid dead-lock situations.

Change-Id: I39d1158749131a72805e55f28aba79e1827f158b
Reviewed-by: Christian Stenger <christian.stenger@theqtcompany.com>
This commit is contained in:
Christian Stenger
2014-12-09 14:02:41 +01:00
parent ac24550a22
commit cda4227a62

View File

@@ -54,7 +54,7 @@ QModelIndex TestResultModel::parent(const QModelIndex &) const
int TestResultModel::rowCount(const QModelIndex &parent) const int TestResultModel::rowCount(const QModelIndex &parent) const
{ {
// do not use the QReadLocker here or this will produce a deadlock QReadLocker lock(&m_rwLock);
return parent.isValid() ? 0 : m_testResults.size(); return parent.isValid() ? 0 : m_testResults.size();
} }
@@ -122,19 +122,21 @@ void TestResultModel::addTestResult(const TestResult &testResult)
int position = m_testResults.size(); int position = m_testResults.size();
rLock.unlock(); rLock.unlock();
QWriteLocker wLock(&m_rwLock);
if (hasCurrentTestMssg && isCurrentTestMssg) { if (hasCurrentTestMssg && isCurrentTestMssg) {
beginRemoveRows(QModelIndex(), position, position); beginRemoveRows(QModelIndex(), position, position);
QWriteLocker wLock(&m_rwLock);
m_testResults.replace(position - 1, testResult); m_testResults.replace(position - 1, testResult);
wLock.unlock();
endRemoveRows(); endRemoveRows();
} else { } else {
if (!isCurrentTestMssg && position) // decrement only if at least one other item if (!isCurrentTestMssg && position) // decrement only if at least one other item
--position; --position;
beginInsertRows(QModelIndex(), position, position); beginInsertRows(QModelIndex(), position, position);
QWriteLocker wLock(&m_rwLock);
m_testResults.insert(position, testResult); m_testResults.insert(position, testResult);
wLock.unlock();
endInsertRows(); endInsertRows();
} }
wLock.unlock();
if (!isCurrentTestMssg) { if (!isCurrentTestMssg) {
int count = m_testResultCount.value(testResult.result(), 0); int count = m_testResultCount.value(testResult.result(), 0);
@@ -148,10 +150,11 @@ void TestResultModel::removeCurrentTestMessage()
{ {
QReadLocker rLock(&m_rwLock); QReadLocker rLock(&m_rwLock);
if (m_availableResultTypes.contains(ResultType::MESSAGE_CURRENT_TEST)) { if (m_availableResultTypes.contains(ResultType::MESSAGE_CURRENT_TEST)) {
beginRemoveRows(QModelIndex(), m_testResults.size() - 1, m_testResults.size() - 1);
rLock.unlock(); rLock.unlock();
QWriteLocker wLock(&m_rwLock); QWriteLocker wLock(&m_rwLock);
beginRemoveRows(QModelIndex(), m_testResults.size() - 1, m_testResults.size() - 1);
m_testResults.removeLast(); m_testResults.removeLast();
wLock.unlock();
endRemoveRows(); endRemoveRows();
m_availableResultTypes.remove(ResultType::MESSAGE_CURRENT_TEST); m_availableResultTypes.remove(ResultType::MESSAGE_CURRENT_TEST);
} }