Get rid of recursive mutex.

This commit is contained in:
Adam Treat 2023-06-25 20:22:38 -04:00
parent 7f01b153b3
commit 27f25d5878
2 changed files with 100 additions and 90 deletions

View File

@ -166,23 +166,26 @@ bool ModelList::lessThan(const ModelInfo* a, const ModelInfo* b)
void ModelList::addModel(const QString &filename)
{
QMutexLocker locker(&m_mutex);
Q_ASSERT(!m_modelMap.contains(filename));
if (m_modelMap.contains(filename)) {
const bool hasModel = contains(filename);
Q_ASSERT(!hasModel);
if (hasModel) {
qWarning() << "ERROR: model list already contains" << filename;
return;
}
beginInsertRows(QModelIndex(), m_models.size(), m_models.size());
int modelSizeAfter = 0;
{
QMutexLocker locker(&m_mutex);
ModelInfo *info = new ModelInfo;
info->filename = filename;
m_models.append(info);
m_modelMap.insert(filename, info);
endInsertRows();
std::stable_sort(m_models.begin(), m_models.end(), ModelList::lessThan);
emit dataChanged(index(0, 0), index(m_models.size() - 1, 0));
modelSizeAfter = m_models.size();
}
endInsertRows();
emit dataChanged(index(0, 0), index(modelSizeAfter - 1, 0));
emit userDefaultModelListChanged();
}
@ -270,6 +273,11 @@ QVariant ModelList::data(const QModelIndex &index, int role) const
}
void ModelList::updateData(const QString &filename, int role, const QVariant &value)
{
int modelSize;
bool updateInstalled;
bool updateIncomplete;
int index;
{
QMutexLocker locker(&m_mutex);
if (!m_modelMap.contains(filename)) {
@ -278,7 +286,7 @@ void ModelList::updateData(const QString &filename, int role, const QVariant &va
}
ModelInfo *info = m_modelMap.value(filename);
const int index = m_models.indexOf(info);
index = m_models.indexOf(info);
if (index == -1) {
qWarning() << "ERROR: cannot update as model list does not contain" << filename;
return;
@ -343,16 +351,18 @@ void ModelList::updateData(const QString &filename, int role, const QVariant &va
QFileInfo fileInfo(info->dirpath + info->filename);
if (info->installed != fileInfo.exists()) {
info->installed = fileInfo.exists();
emit dataChanged(createIndex(index, 0), createIndex(index, 0), {InstalledRole});
updateInstalled = true;
}
QFileInfo incompleteInfo(incompleteDownloadPath(info->filename));
if (info->isIncomplete != incompleteInfo.exists()) {
info->isIncomplete = incompleteInfo.exists();
emit dataChanged(createIndex(index, 0), createIndex(index, 0), {IncompleteRole});
updateIncomplete = true;
}
std::stable_sort(m_models.begin(), m_models.end(), ModelList::lessThan);
emit dataChanged(createIndex(0, 0), createIndex(m_models.size() - 1, 0));
modelSize = m_models.size();
}
emit dataChanged(createIndex(0, 0), createIndex(modelSize - 1, 0));
emit userDefaultModelListChanged();
}

View File

@ -229,7 +229,7 @@ private:
static bool lessThan(const ModelInfo* a, const ModelInfo* b);
private:
mutable QRecursiveMutex m_mutex;
mutable QMutex m_mutex;
InstalledModels *m_installedModels;
DownloadableModels *m_downloadableModels;
QList<ModelInfo*> m_models;