ソースを参照

Added a ReleaseLocks function to BodyLockMultiBase (#1733)

This allows releasing the locks before the object is destroyed.

Fixes #1732
Jorrit Rouwe 3 週間 前
コミット
9fcfa0718c
2 ファイル変更46 行追加2 行削除
  1. 18 2
      Jolt/Physics/Body/BodyLockMulti.h
  2. 28 0
      UnitTests/Physics/PhysicsTests.cpp

+ 18 - 2
Jolt/Physics/Body/BodyLockMulti.h

@@ -33,8 +33,8 @@ public:
 		}
 	}
 
-	/// Destructor will unlock the bodies
-								~BodyLockMultiBase()
+	/// Explicitly release the locks on all bodies (normally this is done in the destructor)
+	inline void					ReleaseLocks()
 	{
 		if (mMutexMask != 0)
 		{
@@ -42,9 +42,25 @@ public:
 				mBodyLockInterface.UnlockWrite(mMutexMask);
 			else
 				mBodyLockInterface.UnlockRead(mMutexMask);
+
+			mMutexMask = 0;
+			mBodyIDs = nullptr;
+			mNumBodyIDs = 0;
 		}
 	}
 
+	/// Destructor will unlock the bodies
+								~BodyLockMultiBase()
+	{
+		ReleaseLocks();
+	}
+
+	/// Returns the number of bodies that were locked
+	inline int					GetNumBodies() const
+	{
+		return mNumBodyIDs;
+	}
+
 	/// Access the body (returns null if body was not properly locked)
 	inline BodyType *			GetBody(int inBodyIndex) const
 	{

+ 28 - 0
UnitTests/Physics/PhysicsTests.cpp

@@ -81,6 +81,20 @@ TEST_SUITE("PhysicsTests")
 				BodyLockRead lock1(c.GetSystem()->GetBodyLockInterface(), body1_id);
 				CHECK(lock1.Succeeded());
 				CHECK(lock1.SucceededAndIsInBroadPhase());
+
+				// Unlock automatically on going out of scope
+			}
+
+			// Check that we can lock the first box
+			{
+				BodyLockRead lock1(c.GetSystem()->GetBodyLockInterface(), body1_id);
+				CHECK(lock1.Succeeded());
+				CHECK(lock1.SucceededAndIsInBroadPhase());
+
+				// Release the lock early
+				lock1.ReleaseLock();
+				CHECK(!lock1.Succeeded());
+				CHECK(!lock1.SucceededAndIsInBroadPhase());
 			}
 
 			// Remove the first box
@@ -148,6 +162,20 @@ TEST_SUITE("PhysicsTests")
 				BodyLockMultiWrite lock(c.GetSystem()->GetBodyLockInterface(), bodies, 2);
 				CHECK(lock.GetBody(0) == &body1);
 				CHECK(lock.GetBody(1) == &body2);
+
+				// Unlock automatically on going out of scope
+			}
+
+			{
+				// Lock the bodies
+				BodyLockMultiWrite lock(c.GetSystem()->GetBodyLockInterface(), bodies, 2);
+				CHECK(lock.GetNumBodies() == 2);
+				CHECK(lock.GetBody(0) == &body1);
+				CHECK(lock.GetBody(1) == &body2);
+
+				// Release the locks early
+				lock.ReleaseLocks();
+				CHECK(lock.GetNumBodies() == 0);
 			}
 
 			// Destroy body 1