delete is now thread safe
authorPatrik Gornicz <Gornicz.P@gmail.com>
Sun, 30 Nov 2008 22:32:58 +0000 (17:32 -0500)
committerPatrik Gornicz <Gornicz.P@gmail.com>
Sun, 30 Nov 2008 22:32:58 +0000 (17:32 -0500)
src/entityManager.cpp

index 1664210..e091b51 100644 (file)
@@ -35,6 +35,8 @@
 
 void updateParticles(float);
 void updatePhysics(float);
+void clearUpParticles();
+void clearUpPhysics();
 
 /// ***** Private Variables *****
 
@@ -95,6 +97,7 @@ void manager::add(Entity* e)
 void manager::remove(Entity* e)
 {
     {
+        Autolock lock( particleSetLock );
         Particle* p = dynamic_cast<Particle*>(e);
         if( p != 0 )
         {
@@ -104,6 +107,7 @@ void manager::remove(Entity* e)
     }
 
     {
+        Autolock lock( physicsEntitySetLock );
         PhysicsEntity* p = dynamic_cast<PhysicsEntity*>(e);
         if( p != 0 )
         {
@@ -128,20 +132,11 @@ void manager::update(float time_step)
 }
 void manager::draw()
 {
+    clearUpParticles();
     {
         Autolock lock( particleSetLock );
         DPF(0, "Particle Draw Start");
 
-        // remove dead Particle*s from Active
-        for( setPart::iterator it = particles_To_Remove.begin();
-             it != particles_To_Remove.end();
-             it++ )
-        {
-            DPF(0, "Particle Delete");
-            active_Particles.erase(*it);
-        }
-        particles_To_Remove.clear();
-
         // update active Particle*s
         for( setPart::iterator it = active_Particles.begin();
              it != active_Particles.end();
@@ -153,20 +148,11 @@ void manager::draw()
         DPF(0, "Particle Draw End");
     }
 
+    clearUpPhysics();
     {
         Autolock lock( physicsEntitySetLock );
         DPF(0, "Physics Draw Start");
 
-        // remove dead PhysicsEntity*s from Active
-        for( setPhys::iterator it = physics_To_Remove.begin();
-             it != physics_To_Remove.end();
-             it++ )
-        {
-            DPF(0, "Physics Delete");
-            active_Physics.erase(*it);
-        }
-        physics_To_Remove.clear();
-
         // update active PhysicsEntity*s
         for( setPhys::iterator it = active_Physics.begin();
              it != active_Physics.end();
@@ -183,30 +169,7 @@ void manager::draw()
 
 void updateParticles(float time_step)
 {
-    {
-        Autolock lock( particleSetLock );
-
-        // add new Particle*s to Active
-        for( setPart::iterator it = particles_To_Add.begin();
-             it != particles_To_Add.end();
-             it++ )
-        {
-            DPF(0, "Particle Insert");
-            active_Particles.insert(*it);
-        }
-
-        // remove dead Particle*s from Active
-        for( setPart::iterator it = particles_To_Remove.begin();
-             it != particles_To_Remove.end();
-             it++ )
-        {
-            DPF(0, "Particle Delete");
-            active_Particles.erase(*it);
-        }
-        particles_To_Remove.clear();
-    }
-
-    particles_To_Add.clear();
+    clearUpParticles();
 
     // update active Particle*s
     for( setPart::iterator it = active_Particles.begin();
@@ -218,30 +181,7 @@ void updateParticles(float time_step)
 }
 void updatePhysics(float time_step)
 {
-    {
-        Autolock lock( physicsEntitySetLock );
-
-        // add new PhysicsEntity*s to Active
-        for( setPhys::iterator it = physics_To_Add.begin();
-             it != physics_To_Add.end();
-             it++ )
-        {
-            DPF(0, "Physics Insert");
-            active_Physics.insert(*it);
-        }
-
-        // remove dead PhysicsEntity*s from Active
-        for( setPhys::iterator it = physics_To_Remove.begin();
-             it != physics_To_Remove.end();
-             it++ )
-        {
-            DPF(0, "Physics Delete");
-            active_Physics.erase(*it);
-        }
-        physics_To_Remove.clear();
-    }
-
-    physics_To_Add.clear();
+    clearUpPhysics();
 
     // apply collision math
     collision::update(active_Physics);
@@ -254,3 +194,51 @@ void updatePhysics(float time_step)
         (*it)->update(time_step);
     }
 }
+void clearUpParticles()
+{
+    Autolock lock( particleSetLock );
+
+    // add new Particle*s to Active
+    for( setPart::iterator it = particles_To_Add.begin();
+         it != particles_To_Add.end();
+         it++ )
+    {
+        DPF(0, "Particle Insert");
+        active_Particles.insert(*it);
+    }
+    particles_To_Add.clear();
+
+    // remove dead Particle*s from Active
+    for( setPart::iterator it = particles_To_Remove.begin();
+         it != particles_To_Remove.end();
+         it++ )
+    {
+        DPF(0, "Particle Delete");
+        active_Particles.erase(*it);
+    }
+    particles_To_Remove.clear();
+}
+void clearUpPhysics()
+{
+    Autolock lock( physicsEntitySetLock );
+
+    // add new PhysicsEntity*s to Active
+    for( setPhys::iterator it = physics_To_Add.begin();
+         it != physics_To_Add.end();
+         it++ )
+    {
+        DPF(0, "Physics Insert");
+        active_Physics.insert(*it);
+    }
+    physics_To_Add.clear();
+
+    // remove dead PhysicsEntity*s from Active
+    for( setPhys::iterator it = physics_To_Remove.begin();
+         it != physics_To_Remove.end();
+         it++ )
+    {
+        DPF(0, "Physics Delete");
+        active_Physics.erase(*it);
+    }
+    physics_To_Remove.clear();
+}