From 2abfecc3a03974630f5240590769ebf5d23ed94d Mon Sep 17 00:00:00 2001 From: Christoph Hart Date: Thu, 14 Sep 2023 12:35:17 +0200 Subject: [PATCH] - fix multithreading issues with scripttablemodel --- .../scripting/api/ScriptTableListModel.cpp | 91 ++++++++++++++++--- .../scripting/api/ScriptTableListModel.h | 3 + 2 files changed, 82 insertions(+), 12 deletions(-) diff --git a/hi_scripting/scripting/api/ScriptTableListModel.cpp b/hi_scripting/scripting/api/ScriptTableListModel.cpp index e3440fff0..ba8d97c67 100644 --- a/hi_scripting/scripting/api/ScriptTableListModel.cpp +++ b/hi_scripting/scripting/api/ScriptTableListModel.cpp @@ -286,9 +286,13 @@ Component* ScriptTableListModel::refreshComponentForCell(int rowNumber, int colu var vToUse = value; + if(rangeSet != RangeHelpers::IdSet::scriptnode) + { + SimpleReadWriteLock::ScopedReadLock sl(rowLock); vToUse = rowData[rowNumber]; - + } + ComponentUpdateHelpers::updateSliderProperties(s, vToUse, rangeSet, shouldSendCallOnDrag()); ComponentUpdateHelpers::updateValue(s, value); } @@ -345,6 +349,8 @@ Component* ScriptTableListModel::refreshComponentForCell(int rowNumber, int colu auto r = (int)b->getProperties()["RowIndex"]; + SimpleReadWriteLock::ScopedReadLock sl(rowLock); + if (auto obj = rowData[r].getDynamicObject()) { obj->setProperty(id, b->getToggleState()); @@ -380,6 +386,8 @@ Component* ScriptTableListModel::refreshComponentForCell(int rowNumber, int colu { auto id = columnMetadata[c - 1][PropertyIds::ID].toString(); + SimpleReadWriteLock::ScopedReadLock sl(rowLock); + auto r_ = (int)s_->getProperties()["RowIndex"]; if (auto obj = rowData[r_].getDynamicObject()) @@ -397,7 +405,11 @@ Component* ScriptTableListModel::refreshComponentForCell(int rowNumber, int colu var vToUse = value; if (rangeSet != RangeHelpers::IdSet::scriptnode) + { + SimpleReadWriteLock::ScopedReadLock sl(rowLock); vToUse = rowData[rowNumber]; + } + if (!ComponentUpdateHelpers::updateSliderProperties(s, vToUse, rangeSet, shouldSendCallOnDrag())) ComponentUpdateHelpers::updateSliderProperties(s, cd, rangeSet, shouldSendCallOnDrag()); @@ -434,6 +446,8 @@ Component* ScriptTableListModel::refreshComponentForCell(int rowNumber, int colu var value = ComponentUpdateHelpers::getValue(cb_, valueMode); + SimpleReadWriteLock::ScopedReadLock sl(rowLock); + if (auto obj = rowData[r_].getDynamicObject()) { obj->setProperty(id, value); @@ -520,6 +534,7 @@ ScriptTableListModel::LookAndFeelMethods::~LookAndFeelMethods() int ScriptTableListModel::getNumRows() { + SimpleReadWriteLock::ScopedReadLock sl(rowLock); return rowData.size(); } @@ -534,6 +549,8 @@ var ScriptTableListModel::getCellValue(int rowIndex, int columnIndex) const { if(isPositiveAndBelow(columnIndex, columnMetadata.size())) { + SimpleReadWriteLock::ScopedReadLock sl(rowLock); + auto id = columnMetadata[columnIndex][scriptnode::PropertyIds::ID].toString(); if(isPositiveAndBelow(rowIndex, rowData.size())) @@ -572,7 +589,11 @@ void ScriptTableListModel::addAdditionalCallback(const std::function newData; + originalRowData = rd.clone(); - if (auto a = originalRowData.getArray()) - newData.addArray(*a); + Array newData; - rowData = var(newData); + if (auto a = originalRowData.getArray()) + newData.addArray(*a); + + rowData = var(newData); + } if (d.sortColumnId != 0) { @@ -1037,6 +1095,9 @@ void ScriptTableListModel::sendCallback(int rowId, int columnId, var value, Even lastClickedCell = newCell; + + SimpleReadWriteLock::ScopedReadLock sl(rowLock); + if(rowData.isArray() && isPositiveAndBelow(rowId, rowData.size())) value = rowData[rowId]; } @@ -1107,7 +1168,13 @@ bool ScriptTableListModel::TableRepainter::keyPressed(const KeyPress& key, Compo { if(parent.processSpaceKey) { - parent.sendCallback(parent.lastClickedCell.x, parent.lastClickedCell.y, parent.rowData[parent.lastClickedCell.y], EventType::SpaceKey); + var c; + { + SimpleReadWriteLock::ScopedReadLock sl(parent.rowLock); + c = parent.rowData[parent.lastClickedCell.y]; + } + + parent.sendCallback(parent.lastClickedCell.x, parent.lastClickedCell.y, c, EventType::SpaceKey); return true; } diff --git a/hi_scripting/scripting/api/ScriptTableListModel.h b/hi_scripting/scripting/api/ScriptTableListModel.h index d46dff9e8..9f8f933ff 100644 --- a/hi_scripting/scripting/api/ScriptTableListModel.h +++ b/hi_scripting/scripting/api/ScriptTableListModel.h @@ -225,6 +225,9 @@ struct ScriptTableListModel : public juce::TableListBoxModel, var tableMetadata; var columnMetadata; + + mutable hise::SimpleReadWriteLock rowLock; + var rowData; var originalRowData; WeakCallbackHolder cellCallback;