4444#include " util/logging.h"
4545#include " util/object_counter.h"
4646#include " util/ordered_weak_ptr.h"
47+ #include " util/lock.h"
4748
4849namespace programmerjake
4950{
@@ -292,7 +293,8 @@ class PhysicsWorld final : public std::enable_shared_from_this<PhysicsWorld>
292293{
293294 friend class PhysicsObject ;
294295private:
295- mutable std::recursive_mutex theLock; // lock for all state except for this->chunks
296+ mutable checked_recursive_lock<200 > theLockImp;
297+ mutable generic_lock_wrapper theLock; // lock for all state except for this->chunks
296298 double currentTime = 0 ;
297299 int variableSetIndex = 0 ;
298300private:
@@ -303,6 +305,10 @@ class PhysicsWorld final : public std::enable_shared_from_this<PhysicsWorld>
303305 return b.descriptor ->blockShape ;
304306 }
305307public:
308+ PhysicsWorld ()
309+ : theLock(theLockImp)
310+ {
311+ }
306312 BlockChunkMap chunks; // not locked by theLock
307313 BlockIterator getBlockIterator (PositionI pos)
308314 {
@@ -335,29 +341,29 @@ class PhysicsWorld final : public std::enable_shared_from_this<PhysicsWorld>
335341 static constexpr float timeEPS = eps;
336342 inline double getCurrentTime () const
337343 {
338- std::unique_lock<std::recursive_mutex > lockIt (theLock);
344+ std::unique_lock<generic_lock_wrapper > lockIt (theLock);
339345 return currentTime;
340346 }
341347 inline int getOldVariableSetIndex () const
342348 {
343- std::unique_lock<std::recursive_mutex > lockIt (theLock);
349+ std::unique_lock<generic_lock_wrapper > lockIt (theLock);
344350 return variableSetIndex;
345351 }
346352 inline int getNewVariableSetIndex () const
347353 {
348- std::unique_lock<std::recursive_mutex > lockIt (theLock);
354+ std::unique_lock<generic_lock_wrapper > lockIt (theLock);
349355 return 1 - variableSetIndex;
350356 }
351357private:
352358 std::unordered_set<ordered_weak_ptr<PhysicsObject>> objects;
353359 void addObject (std::shared_ptr<PhysicsObject> o)
354360 {
355- std::unique_lock<std::recursive_mutex > lockIt (theLock);
361+ std::unique_lock<generic_lock_wrapper > lockIt (theLock);
356362 objects.insert (o);
357363 }
358364 void removeObject (std::shared_ptr<PhysicsObject> o)
359365 {
360- std::unique_lock<std::recursive_mutex > lockIt (theLock);
366+ std::unique_lock<generic_lock_wrapper > lockIt (theLock);
361367 objects.erase (o);
362368 }
363369 struct CollisionEvent final
@@ -419,8 +425,8 @@ inline void PhysicsObject::transferToNewWorld(std::shared_ptr<PhysicsWorld> newW
419425 std::shared_ptr<PhysicsWorld> world = getWorld ();
420426 if (world == newWorld)
421427 return ;
422- std::unique_lock<std::recursive_mutex > worldLock (world->theLock , std::defer_lock);
423- std::unique_lock<std::recursive_mutex > newWorldLock (newWorld->theLock , std::defer_lock);
428+ std::unique_lock<generic_lock_wrapper > worldLock (world->theLock , std::defer_lock);
429+ std::unique_lock<generic_lock_wrapper > newWorldLock (newWorld->theLock , std::defer_lock);
424430 std::lock (worldLock, newWorldLock);
425431 double deltaTime = newWorld->getCurrentTime () - world->getCurrentTime ();
426432 for (double &v : objectTime)
@@ -655,7 +661,7 @@ inline PhysicsObject::~PhysicsObject()
655661inline PositionF PhysicsObject::getPosition () const
656662{
657663 auto world = getWorld ();
658- std::unique_lock<std::recursive_mutex > lockIt (world->theLock );
664+ std::unique_lock<generic_lock_wrapper > lockIt (world->theLock );
659665 int variableSetIndex = world->getOldVariableSetIndex ();
660666 float deltaTime = world->getCurrentTime () - objectTime[variableSetIndex];
661667 VectorF gravityVector = getProperties ().gravity ;
@@ -667,7 +673,7 @@ inline PositionF PhysicsObject::getPosition() const
667673inline VectorF PhysicsObject::getVelocity () const
668674{
669675 auto world = getWorld ();
670- std::unique_lock<std::recursive_mutex > lockIt (world->theLock );
676+ std::unique_lock<generic_lock_wrapper > lockIt (world->theLock );
671677 int variableSetIndex = world->getOldVariableSetIndex ();
672678 VectorF gravityVector = getProperties ().gravity ;
673679 if (!affectedByGravity || isSupported ())
@@ -679,7 +685,7 @@ inline VectorF PhysicsObject::getVelocity() const
679685inline void PhysicsObject::setNewState (PositionF newPosition, VectorF newVelocity)
680686{
681687 auto world = getWorld ();
682- std::unique_lock<std::recursive_mutex > lockIt (world->theLock );
688+ std::unique_lock<generic_lock_wrapper > lockIt (world->theLock );
683689 int variableSetIndex = world->getNewVariableSetIndex ();
684690 objectTime[variableSetIndex] = world->getCurrentTime ();
685691 newPosition += position[variableSetIndex] * newStateCount;
@@ -696,7 +702,7 @@ inline void PhysicsObject::setNewState(PositionF newPosition, VectorF newVelocit
696702inline void PhysicsObject::setupNewState ()
697703{
698704 auto world = getWorld ();
699- std::unique_lock<std::recursive_mutex > lockIt (world->theLock );
705+ std::unique_lock<generic_lock_wrapper > lockIt (world->theLock );
700706 int oldVariableSetIndex = world->getOldVariableSetIndex ();
701707 int newVariableSetIndex = world->getNewVariableSetIndex ();
702708 objectTime[newVariableSetIndex] = objectTime[oldVariableSetIndex];
0 commit comments