Browse Source

some thread-safe locks

David Rose 20 years ago
parent
commit
40e8313771

+ 1 - 0
panda/src/collide/collisionPolygon.cxx

@@ -771,6 +771,7 @@ fill_viz_geom() {
     collide_cat.debug()
       << "Recomputing viz for " << *this << "\n";
   }
+  nassertv(_viz_geom != (GeomNode *)NULL && _bounds_viz_geom != (GeomNode *)NULL);
   draw_polygon(_viz_geom, _bounds_viz_geom, _points);
 }
 

+ 11 - 1
panda/src/collide/collisionSolid.I

@@ -29,12 +29,13 @@
 ////////////////////////////////////////////////////////////////////
 INLINE void CollisionSolid::
 set_tangible(bool tangible) {
+  MutexHolder holder(_lock);
   if (tangible) {
     _flags |= F_tangible;
   } else {
     _flags &= ~F_tangible;
   }
-  mark_viz_stale();
+  _flags |= F_viz_geom_stale;
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -48,6 +49,7 @@ set_tangible(bool tangible) {
 ////////////////////////////////////////////////////////////////////
 INLINE bool CollisionSolid::
 is_tangible() const {
+  MutexHolder holder(_lock);
   return (_flags & F_tangible) != 0;
 }
 
@@ -65,6 +67,7 @@ is_tangible() const {
 ////////////////////////////////////////////////////////////////////
 INLINE void CollisionSolid::
 set_effective_normal(const LVector3f &effective_normal) {
+  MutexHolder holder(_lock);
   _effective_normal = effective_normal;
   _flags |= F_effective_normal;
 }
@@ -77,6 +80,7 @@ set_effective_normal(const LVector3f &effective_normal) {
 ////////////////////////////////////////////////////////////////////
 INLINE void CollisionSolid::
 clear_effective_normal() {
+  MutexHolder holder(_lock);
   _flags &= ~F_effective_normal;
 }
 
@@ -88,6 +92,7 @@ clear_effective_normal() {
 ////////////////////////////////////////////////////////////////////
 INLINE bool CollisionSolid::
 has_effective_normal() const {
+  MutexHolder holder(_lock);
   return respect_effective_normal && (_flags & F_effective_normal) != 0;
 }
 
@@ -101,6 +106,7 @@ has_effective_normal() const {
 INLINE const LVector3f &CollisionSolid::
 get_effective_normal() const {
   nassertr(has_effective_normal(), LVector3f::zero());
+  MutexHolder holder(_lock);
   return _effective_normal;
 }
 
@@ -116,6 +122,7 @@ get_effective_normal() const {
 ////////////////////////////////////////////////////////////////////
 INLINE void CollisionSolid::
 set_respect_effective_normal(bool respect_effective_normal) {
+  MutexHolder holder(_lock);
   // For historical reasons, the bit we store is the opposite of the
   // bool flag we present.
   if (respect_effective_normal) {
@@ -132,6 +139,7 @@ set_respect_effective_normal(bool respect_effective_normal) {
 ////////////////////////////////////////////////////////////////////
 INLINE bool CollisionSolid::
 get_respect_effective_normal() const {
+  MutexHolder holder(_lock);
   return (_flags & F_ignore_effective_normal) == 0;
 }
 
@@ -145,6 +153,7 @@ get_respect_effective_normal() const {
 ////////////////////////////////////////////////////////////////////
 INLINE void CollisionSolid::
 mark_internal_bounds_stale() {
+  MutexHolder holder(_lock);
   _flags |= F_internal_bounds_stale;
 }
 
@@ -158,5 +167,6 @@ mark_internal_bounds_stale() {
 ////////////////////////////////////////////////////////////////////
 INLINE void CollisionSolid::
 mark_viz_stale() {
+  MutexHolder holder(_lock);
   _flags |= F_viz_geom_stale;
 }

+ 5 - 2
panda/src/collide/collisionSolid.cxx

@@ -76,6 +76,7 @@ CollisionSolid::
 ////////////////////////////////////////////////////////////////////
 CPT(BoundingVolume) CollisionSolid::
 get_bounds() const {
+  MutexHolder holder(_lock);
   if (_flags & F_internal_bounds_stale) {
     ((CollisionSolid *)this)->_internal_bounds = compute_internal_bounds();
     ((CollisionSolid *)this)->_flags &= ~F_internal_bounds_stale;
@@ -105,13 +106,13 @@ test_intersection(const CollisionEntry &) const {
 ////////////////////////////////////////////////////////////////////
 void CollisionSolid::
 xform(const LMatrix4f &mat) {
+  MutexHolder holder(_lock);
   if ((_flags & F_effective_normal) != 0) {
     _effective_normal = _effective_normal * mat;
     _effective_normal.normalize();
   }
 
-  mark_viz_stale();
-  mark_internal_bounds_stale();
+  _flags |= F_viz_geom_stale | F_internal_bounds_stale;
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -124,6 +125,7 @@ xform(const LMatrix4f &mat) {
 ////////////////////////////////////////////////////////////////////
 PT(PandaNode) CollisionSolid::
 get_viz(const CullTraverser *, const CullTraverserData &, bool bounds_only) const {
+  MutexHolder holder(_lock);
   if ((_flags & F_viz_geom_stale) != 0) {
     if (_viz_geom == (GeomNode *)NULL) {
       ((CollisionSolid *)this)->_viz_geom = new GeomNode("viz");
@@ -310,6 +312,7 @@ void CollisionSolid::
 write_datagram(BamWriter *, Datagram &me) {
   // For now, we need only 8 bits of flags.  If we need to expand this
   // later, we will have to increase the bam version.
+  MutexHolder holder(_lock);
   me.add_uint8(_flags);
   if ((_flags & F_effective_normal) != 0) {
     _effective_normal.write_datagram(me);

+ 4 - 0
panda/src/collide/collisionSolid.h

@@ -27,6 +27,8 @@
 #include "pointerTo.h"
 #include "renderState.h"
 #include "geomNode.h"
+#include "pmutex.h"
+#include "mutexHolder.h"
 
 class CollisionHandler;
 class CollisionEntry;
@@ -130,6 +132,8 @@ private:
   };
   int _flags;
 
+  Mutex _lock;
+
 public:
   virtual void write_datagram(BamWriter* manager, Datagram &me);
 

+ 4 - 2
panda/src/collide/collisionVisualizer.cxx

@@ -296,10 +296,12 @@ collision_tested(const CollisionEntry &entry, bool detected) {
 
   NodePath node_path = entry.get_into_node_path();
   CPT(TransformState) net_transform = node_path.get_net_transform();
+  const CollisionSolid *solid = entry.get_into();
+  nassertv(solid != (CollisionSolid *)NULL);
 
   VizInfo &viz_info = _data[net_transform];
   if (detected) {
-    viz_info._solids[entry.get_into()]._detected_count++;
+    viz_info._solids[solid]._detected_count++;
 
     if (entry.has_surface_point()) {
       CollisionPoint p;
@@ -309,7 +311,7 @@ collision_tested(const CollisionEntry &entry, bool detected) {
     }
 
   } else {
-    viz_info._solids[entry.get_into()]._missed_count++;
+    viz_info._solids[solid]._missed_count++;
   }
 }