2
0
Эх сурвалжийг харах

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 жил өмнө
parent
commit
d0028ba103

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

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

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

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

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

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