Browse Source

physics: Make PhysicalNode class thread-safe

It is, however, not yet pipeline-cycled, and the Physical object itself is not yet thread-safe.
rdb 4 years ago
parent
commit
d0028ba103

+ 4 - 0
panda/src/physics/physicalNode.I

@@ -16,6 +16,7 @@
  */
 INLINE void PhysicalNode::
 clear() {
+  LightMutexHolder holder(_lock);
   for (Physical *physical : _physicals) {
     nassertd(physical->_physical_node == this) continue;
     physical->_physical_node = nullptr;
@@ -28,6 +29,7 @@ clear() {
  */
 INLINE Physical *PhysicalNode::
 get_physical(size_t index) const {
+  LightMutexHolder holder(_lock);
   nassertr(index < _physicals.size(), nullptr);
   return _physicals[index];
 }
@@ -37,6 +39,7 @@ get_physical(size_t index) const {
  */
 INLINE size_t PhysicalNode::
 get_num_physicals() const {
+  LightMutexHolder holder(_lock);
   return _physicals.size();
 }
 
@@ -46,6 +49,7 @@ get_num_physicals() const {
  */
 INLINE void PhysicalNode::
 add_physical(Physical *physical) {
+  LightMutexHolder holder(_lock);
   if (physical->_physical_node != this) {
     nassertv(physical->_physical_node == nullptr);
     _physicals.push_back(physical);

+ 6 - 0
panda/src/physics/physicalNode.cxx

@@ -58,6 +58,7 @@ PhysicalNode::
  */
 PandaNode *PhysicalNode::
 make_copy() const {
+  LightMutexHolder holder(_lock);
   if (!_physicals.empty() && !warned_copy_physical_node.test_and_set()) {
     // This is a problem, because a Physical can only be on one PhysicalNode.
     //FIXME: Figure out a solution.
@@ -72,6 +73,7 @@ make_copy() const {
  */
 void PhysicalNode::
 add_physicals_from(const PhysicalNode &other) {
+  LightMutexHolder holder(_lock);
   size_t num_physicals = _physicals.size();
   _physicals.insert(_physicals.end(),
                     other._physicals.begin(), other._physicals.end());
@@ -86,6 +88,7 @@ add_physicals_from(const PhysicalNode &other) {
  */
 void PhysicalNode::
 set_physical(size_t index, Physical *physical) {
+  LightMutexHolder holder(_lock);
   nassertv(index < _physicals.size());
 
   _physicals[index]->_physical_node = nullptr;
@@ -98,6 +101,7 @@ set_physical(size_t index, Physical *physical) {
  */
 void PhysicalNode::
 insert_physical(size_t index, Physical *physical) {
+  LightMutexHolder holder(_lock);
   if (index > _physicals.size()) {
     index = _physicals.size();
   }
@@ -111,6 +115,7 @@ insert_physical(size_t index, Physical *physical) {
  */
 void PhysicalNode::
 remove_physical(Physical *physical) {
+  LightMutexHolder holder(_lock);
   pvector< PT(Physical) >::iterator found;
   PT(Physical) ptp = physical;
   found = find(_physicals.begin(), _physicals.end(), ptp);
@@ -128,6 +133,7 @@ remove_physical(Physical *physical) {
  */
 void PhysicalNode::
 remove_physical(size_t index) {
+  LightMutexHolder holder(_lock);
   nassertv(index <= _physicals.size());
 
   pvector< PT(Physical) >::iterator remove;

+ 3 - 0
panda/src/physics/physicalNode.h

@@ -21,6 +21,8 @@
 
 #include "physical.h"
 #include "config_physics.h"
+#include "lightMutex.h"
+#include "lightMutexHolder.h"
 
 /**
  * Graph node that encapsulated a series of physical objects
@@ -54,6 +56,7 @@ protected:
   PhysicalNode(const PhysicalNode &copy);
 
 private:
+  LightMutex _lock;
   typedef pvector<PT(Physical)> PhysicalsVector;
   PhysicalsVector _physicals;