From 33cde412b13d9a8f7a5a0f5bc47917165e10d557 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 18 Jan 2017 17:43:57 -0800 Subject: [PATCH] make getJSONParameters thread-safe --- assignment-client/src/scripts/EntityScriptServer.cpp | 1 + libraries/entities/src/EntityItem.cpp | 2 +- libraries/octree/src/OctreeQuery.cpp | 5 ++++- libraries/octree/src/OctreeQuery.h | 7 +++++-- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/assignment-client/src/scripts/EntityScriptServer.cpp b/assignment-client/src/scripts/EntityScriptServer.cpp index f06d1a5b6f..ff3c0b09c5 100644 --- a/assignment-client/src/scripts/EntityScriptServer.cpp +++ b/assignment-client/src/scripts/EntityScriptServer.cpp @@ -246,6 +246,7 @@ void EntityScriptServer::checkAndCallPreload(const EntityItemID& entityID, const if (entity && entity->shouldPreloadServerScript() && _entitiesScriptEngine) { QString scriptUrl = entity->getServerScripts(); scriptUrl = ResourceManager::normalizeURL(scriptUrl); + qDebug() << "Loading entity server script" << scriptUrl << "for" << entityID; ScriptEngine::loadEntityScript(_entitiesScriptEngine, entityID, scriptUrl, reload); entity->serverScriptHasPreloaded(); } diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index f9038fd547..0b2e7fd5ca 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -2246,7 +2246,7 @@ bool EntityItem::matchesJSONFilters(const QJsonObject& jsonFilters) const { static const QString SERVER_SCRIPTS_PROPERTY = "serverScripts"; - for (auto& property : jsonFilters.keys()) { + foreach(const auto& property, jsonFilters.keys()) { if (property == SERVER_SCRIPTS_PROPERTY && jsonFilters[property] == EntityQueryFilterSymbol::NonDefault) { // check if this entity has a non-default value for serverScripts if (_serverScripts != ENTITY_ITEM_DEFAULT_SERVER_SCRIPTS) { diff --git a/libraries/octree/src/OctreeQuery.cpp b/libraries/octree/src/OctreeQuery.cpp index d9decf03e7..a639eccaba 100644 --- a/libraries/octree/src/OctreeQuery.cpp +++ b/libraries/octree/src/OctreeQuery.cpp @@ -132,7 +132,10 @@ int OctreeQuery::parseData(ReceivedMessage& message) { sourceBuffer += binaryParametersBytes; // grab the parameter object from the packed binary representation of JSON - _jsonParameters = QJsonDocument::fromBinaryData(binaryJSONParameters).object(); + auto newJsonDocument = QJsonDocument::fromBinaryData(binaryJSONParameters); + + QWriteLocker jsonParameterLocker { &_jsonParametersLock }; + _jsonParameters = newJsonDocument.object(); } return sourceBuffer - startPosition; diff --git a/libraries/octree/src/OctreeQuery.h b/libraries/octree/src/OctreeQuery.h index 79285697a5..058c1dc585 100644 --- a/libraries/octree/src/OctreeQuery.h +++ b/libraries/octree/src/OctreeQuery.h @@ -32,6 +32,7 @@ typedef unsigned long long quint64; #include #include +#include #include @@ -69,8 +70,9 @@ public: void setCameraCenterRadius(float radius) { _cameraCenterRadius = radius; } // getters/setters for JSON filter - QJsonObject getJSONParameters() { return _jsonParameters; } - void setJSONParameters(const QJsonObject& jsonParameters) { _jsonParameters = jsonParameters; } + QJsonObject getJSONParameters() { QReadLocker locker { &_jsonParametersLock }; return _jsonParameters; } + void setJSONParameters(const QJsonObject& jsonParameters) + { QWriteLocker locker { &_jsonParametersLock }; _jsonParameters = jsonParameters; } // related to Octree Sending strategies int getMaxQueryPacketsPerSecond() const { return _maxQueryPPS; } @@ -104,6 +106,7 @@ protected: uint8_t _usesFrustum = true; QJsonObject _jsonParameters; + QReadWriteLock _jsonParametersLock; private: // privatize the copy constructor and assignment operator so they cannot be called