diff --git a/libraries/octree/src/Octree.cpp b/libraries/octree/src/Octree.cpp index 0847590720..05760ef675 100644 --- a/libraries/octree/src/Octree.cpp +++ b/libraries/octree/src/Octree.cpp @@ -39,12 +39,13 @@ float boundaryDistanceForRenderLevel(unsigned int renderLevel, float voxelSizeSc } Octree::Octree(bool shouldReaverage) : + _rootNode(NULL), _isDirty(true), _shouldReaverage(shouldReaverage), _stopImport(false), - _lock(QReadWriteLock::Recursive) { - _rootNode = NULL; - _isViewing = false; + _lock(), + _isViewing(false) +{ } Octree::~Octree() { @@ -552,10 +553,7 @@ OctreeElement* Octree::getOctreeElementAt(float x, float y, float z, float s) co OctreeElement* Octree::getOrCreateChildElementAt(float x, float y, float z, float s) { - lockForWrite(); - OctreeElement* result = getRoot()->getOrCreateChildElementAt(x, y, z, s); - unlock(); - return result; + return getRoot()->getOrCreateChildElementAt(x, y, z, s); } diff --git a/libraries/particles/src/Particle.cpp b/libraries/particles/src/Particle.cpp index b493ec6bc7..d9f0beb81a 100644 --- a/libraries/particles/src/Particle.cpp +++ b/libraries/particles/src/Particle.cpp @@ -385,7 +385,7 @@ Particle Particle::fromEditPacket(const unsigned char* data, int length, int& pr } else { // look up the existing particle - const Particle* existingParticle = tree->findParticleByID(editID); + const Particle* existingParticle = tree->findParticleByID(editID, true); // copy existing properties before over-writing with new properties if (existingParticle) { diff --git a/libraries/particles/src/ParticleTree.cpp b/libraries/particles/src/ParticleTree.cpp index 30270b8fcf..a254305789 100644 --- a/libraries/particles/src/ParticleTree.cpp +++ b/libraries/particles/src/ParticleTree.cpp @@ -370,12 +370,16 @@ bool ParticleTree::findByIDOperation(OctreeElement* element, void* extraData) { } -const Particle* ParticleTree::findParticleByID(uint32_t id) { +const Particle* ParticleTree::findParticleByID(uint32_t id, bool alreadyLocked) { FindByIDArgs args = { id, false, NULL }; - lockForRead(); + if (!alreadyLocked) { + lockForRead(); + } recurseTreeWithOperation(findByIDOperation, &args); - unlock(); + if (!alreadyLocked) { + unlock(); + } return args.foundParticle; } @@ -455,6 +459,7 @@ bool ParticleTree::pruneOperation(OctreeElement* element, void* extraData) { } void ParticleTree::update() { + lockForWrite(); _isDirty = true; ParticleTreeUpdateArgs args = { }; @@ -469,9 +474,7 @@ void ParticleTree::update() { AABox treeBounds = getRoot()->getAABox(); if (!shouldDie && treeBounds.contains(args._movingParticles[i].getPosition())) { - lockForWrite(); storeParticle(args._movingParticles[i]); - unlock(); } else { uint32_t particleID = args._movingParticles[i].getID(); quint64 deletedAt = usecTimestampNow(); @@ -482,7 +485,6 @@ void ParticleTree::update() { } // prune the tree... - lockForWrite(); recurseTreeWithOperation(pruneOperation, NULL); unlock(); } diff --git a/libraries/particles/src/ParticleTree.h b/libraries/particles/src/ParticleTree.h index 08e8e51a05..f3b8f5183d 100644 --- a/libraries/particles/src/ParticleTree.h +++ b/libraries/particles/src/ParticleTree.h @@ -44,7 +44,7 @@ public: void addParticle(const ParticleID& particleID, const ParticleProperties& properties); void deleteParticle(const ParticleID& particleID); const Particle* findClosestParticle(glm::vec3 position, float targetRadius); - const Particle* findParticleByID(uint32_t id); + const Particle* findParticleByID(uint32_t id, bool alreadyLocked = false); /// finds all particles that touch a sphere /// \param center the center of the sphere diff --git a/libraries/particles/src/ParticlesScriptingInterface.cpp b/libraries/particles/src/ParticlesScriptingInterface.cpp index c2376e8d72..a25dde1b9e 100644 --- a/libraries/particles/src/ParticlesScriptingInterface.cpp +++ b/libraries/particles/src/ParticlesScriptingInterface.cpp @@ -33,7 +33,9 @@ ParticleID ParticlesScriptingInterface::addParticle(const ParticleProperties& pr // If we have a local particle tree set, then also update it. if (_particleTree) { + _particleTree->lockForWrite(); _particleTree->addParticle(id, properties); + _particleTree->unlock(); } return id; @@ -64,7 +66,7 @@ ParticleProperties ParticlesScriptingInterface::getParticleProperties(ParticleID } if (_particleTree) { _particleTree->lockForRead(); - const Particle* particle = _particleTree->findParticleByID(identity.id); + const Particle* particle = _particleTree->findParticleByID(identity.id, true); if (particle) { results.copyFromParticle(*particle); } else {