I'm locking something wrong

This commit is contained in:
Zach Fox 2017-08-24 12:21:19 -07:00
parent d370eeea2f
commit baad5f195e
3 changed files with 22 additions and 14 deletions

View file

@ -46,6 +46,7 @@ bool GameplayObjects::removeFromGameplayObjects(const OverlayID& overlayID) {
return true; return true;
} }
SelectionScriptingInterface::SelectionScriptingInterface() { SelectionScriptingInterface::SelectionScriptingInterface() {
} }
@ -71,8 +72,9 @@ bool SelectionScriptingInterface::removeFromSelectedItemsList(const QString& lis
} }
template <class T> bool SelectionScriptingInterface::addToGameplayObjects(const QString& listName, T idToAdd) { template <class T> bool SelectionScriptingInterface::addToGameplayObjects(const QString& listName, T idToAdd) {
if (_selectedItemsListMap.contains(listName)) { QWriteLocker writeLock(&_mapLock);
auto currentList = _selectedItemsListMap.take(listName); auto currentList = _selectedItemsListMap.value(listName);
if (currentList.getContainsData()) {
currentList.addToGameplayObjects(idToAdd); currentList.addToGameplayObjects(idToAdd);
_selectedItemsListMap.insert(listName, currentList); _selectedItemsListMap.insert(listName, currentList);
@ -84,8 +86,9 @@ template <class T> bool SelectionScriptingInterface::addToGameplayObjects(const
} }
} }
template <class T> bool SelectionScriptingInterface::removeFromGameplayObjects(const QString& listName, T idToRemove) { template <class T> bool SelectionScriptingInterface::removeFromGameplayObjects(const QString& listName, T idToRemove) {
if (_selectedItemsListMap.contains(listName)) { QWriteLocker writeLock(&_mapLock);
auto currentList = _selectedItemsListMap.take(listName); auto currentList = _selectedItemsListMap.value(listName);
if (currentList.getContainsData()) {
currentList.removeFromGameplayObjects(idToRemove); currentList.removeFromGameplayObjects(idToRemove);
_selectedItemsListMap.insert(listName, currentList); _selectedItemsListMap.insert(listName, currentList);
@ -100,12 +103,14 @@ template <class T> bool SelectionScriptingInterface::removeFromGameplayObjects(c
// //
GameplayObjects SelectionScriptingInterface::getList(const QString& listName) { GameplayObjects SelectionScriptingInterface::getList(const QString& listName) {
QReadLocker readLock(&_mapLock);
return _selectedItemsListMap.value(listName); return _selectedItemsListMap.value(listName);
} }
void SelectionScriptingInterface::printList(const QString& listName) { void SelectionScriptingInterface::printList(const QString& listName) {
if (_selectedItemsListMap.contains(listName)) { QReadLocker readLock(&_mapLock);
auto currentList = _selectedItemsListMap.value(listName); auto currentList = _selectedItemsListMap.value(listName);
if (currentList.getContainsData()) {
qDebug() << "Avatar IDs:"; qDebug() << "Avatar IDs:";
for (auto i : currentList.getAvatarIDs()) { for (auto i : currentList.getAvatarIDs()) {
@ -130,6 +135,7 @@ void SelectionScriptingInterface::printList(const QString& listName) {
} }
bool SelectionScriptingInterface::removeListFromMap(const QString& listName) { bool SelectionScriptingInterface::removeListFromMap(const QString& listName) {
QWriteLocker writeLock(&_mapLock);
if (_selectedItemsListMap.remove(listName)) { if (_selectedItemsListMap.remove(listName)) {
emit selectedItemsListChanged(listName); emit selectedItemsListChanged(listName);
return true; return true;
@ -142,8 +148,7 @@ bool SelectionScriptingInterface::removeListFromMap(const QString& listName) {
SelectionToSceneHandler::SelectionToSceneHandler() { SelectionToSceneHandler::SelectionToSceneHandler() {
} }
void SelectionToSceneHandler::initialize(render::ScenePointer mainScene, const QString& listName) { void SelectionToSceneHandler::initialize(const QString& listName) {
_mainScene = mainScene;
_listName = listName; _listName = listName;
} }
@ -154,11 +159,12 @@ void SelectionToSceneHandler::selectedItemsListChanged(const QString& listName)
} }
void SelectionToSceneHandler::updateSceneFromSelectedList() { void SelectionToSceneHandler::updateSceneFromSelectedList() {
if (_mainScene) { auto mainScene = qApp->getMain3DScene();
if (mainScene) {
GameplayObjects thisList = DependencyManager::get<SelectionScriptingInterface>()->getList(_listName); GameplayObjects thisList = DependencyManager::get<SelectionScriptingInterface>()->getList(_listName);
render::Transaction transaction;
if (thisList.getContainsData()) { if (thisList.getContainsData()) {
render::Transaction transaction;
render::ItemIDs finalList; render::ItemIDs finalList;
render::ItemID currentID; render::ItemID currentID;
auto entityTree = qApp->getEntities()->getTree(); auto entityTree = qApp->getEntities()->getTree();
@ -202,7 +208,7 @@ void SelectionToSceneHandler::updateSceneFromSelectedList() {
render::Selection selection(_listName.toStdString(), finalList); render::Selection selection(_listName.toStdString(), finalList);
transaction.resetSelection(selection); transaction.resetSelection(selection);
_mainScene->enqueueTransaction(transaction); mainScene->enqueueTransaction(transaction);
} else { } else {
qWarning() << "List of GameplayObjects doesn't exist in thisList"; qWarning() << "List of GameplayObjects doesn't exist in thisList";
} }

View file

@ -47,6 +47,7 @@ private:
std::vector<OverlayID> _overlayIDs; std::vector<OverlayID> _overlayIDs;
}; };
class SelectionScriptingInterface : public QObject, public Dependency { class SelectionScriptingInterface : public QObject, public Dependency {
Q_OBJECT Q_OBJECT
@ -67,6 +68,8 @@ signals:
private: private:
QMap<QString, GameplayObjects> _selectedItemsListMap; QMap<QString, GameplayObjects> _selectedItemsListMap;
QReadWriteLock _mapLock;
template <class T> bool addToGameplayObjects(const QString& listName, T idToAdd); template <class T> bool addToGameplayObjects(const QString& listName, T idToAdd);
template <class T> bool removeFromGameplayObjects(const QString& listName, T idToRemove); template <class T> bool removeFromGameplayObjects(const QString& listName, T idToRemove);
}; };
@ -76,7 +79,7 @@ class SelectionToSceneHandler : public QObject {
Q_OBJECT Q_OBJECT
public: public:
SelectionToSceneHandler(); SelectionToSceneHandler();
void initialize(render::ScenePointer mainScene, const QString& listName); void initialize(const QString& listName);
void updateSceneFromSelectedList(); void updateSceneFromSelectedList();
@ -84,7 +87,6 @@ public slots:
void selectedItemsListChanged(const QString& listName); void selectedItemsListChanged(const QString& listName);
private: private:
render::ScenePointer _mainScene;
QString _listName { "" }; QString _listName { "" };
}; };

View file

@ -29,7 +29,7 @@ ContextOverlayInterface::ContextOverlayInterface() {
_tabletScriptingInterface = DependencyManager::get<TabletScriptingInterface>(); _tabletScriptingInterface = DependencyManager::get<TabletScriptingInterface>();
_selectionScriptingInterface = DependencyManager::get<SelectionScriptingInterface>(); _selectionScriptingInterface = DependencyManager::get<SelectionScriptingInterface>();
_selectionToSceneHandler.initialize(qApp->getMain3DScene(), "contextOverlayHighlightList"); _selectionToSceneHandler.initialize("contextOverlayHighlightList");
_entityPropertyFlags += PROP_POSITION; _entityPropertyFlags += PROP_POSITION;
_entityPropertyFlags += PROP_ROTATION; _entityPropertyFlags += PROP_ROTATION;