BodyLockMulti.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Body/BodyLockInterface.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// Base class for locking multiple bodies for the duration of the scope of this class (do not use directly)
  8. template <bool Write, class BodyType>
  9. class BodyLockMultiBase : public NonCopyable
  10. {
  11. public:
  12. /// Redefine MutexMask
  13. using MutexMask = BodyLockInterface::MutexMask;
  14. /// Constructor will lock the bodies
  15. BodyLockMultiBase(const BodyLockInterface &inBodyLockInterface, const BodyID *inBodyIDs, int inNumber) :
  16. mBodyLockInterface(inBodyLockInterface),
  17. mMutexMask(inBodyLockInterface.GetMutexMask(inBodyIDs, inNumber)),
  18. mBodyIDs(inBodyIDs),
  19. mNumBodyIDs(inNumber)
  20. {
  21. if (mMutexMask != 0)
  22. {
  23. // Get mutex
  24. if (Write)
  25. inBodyLockInterface.LockWrite(mMutexMask);
  26. else
  27. inBodyLockInterface.LockRead(mMutexMask);
  28. }
  29. }
  30. /// Explicitly release the locks on all bodies (normally this is done in the destructor)
  31. inline void ReleaseLocks()
  32. {
  33. if (mMutexMask != 0)
  34. {
  35. if (Write)
  36. mBodyLockInterface.UnlockWrite(mMutexMask);
  37. else
  38. mBodyLockInterface.UnlockRead(mMutexMask);
  39. mMutexMask = 0;
  40. mBodyIDs = nullptr;
  41. mNumBodyIDs = 0;
  42. }
  43. }
  44. /// Destructor will unlock the bodies
  45. ~BodyLockMultiBase()
  46. {
  47. ReleaseLocks();
  48. }
  49. /// Returns the number of bodies that were locked
  50. inline int GetNumBodies() const
  51. {
  52. return mNumBodyIDs;
  53. }
  54. /// Access the body (returns null if body was not properly locked)
  55. inline BodyType * GetBody(int inBodyIndex) const
  56. {
  57. // Range check
  58. JPH_ASSERT(inBodyIndex >= 0 && inBodyIndex < mNumBodyIDs);
  59. // Get body ID
  60. const BodyID &body_id = mBodyIDs[inBodyIndex];
  61. if (body_id.IsInvalid())
  62. return nullptr;
  63. // Get a reference to the body or nullptr when it is no longer valid
  64. return mBodyLockInterface.TryGetBody(body_id);
  65. }
  66. private:
  67. const BodyLockInterface & mBodyLockInterface;
  68. MutexMask mMutexMask;
  69. const BodyID * mBodyIDs;
  70. int mNumBodyIDs;
  71. };
  72. /// A multi body lock takes a number of body IDs and locks the underlying bodies so that other threads cannot access its members
  73. ///
  74. /// The common usage pattern is:
  75. ///
  76. /// BodyLockInterface lock_interface = physics_system.GetBodyLockInterface(); // Or non-locking interface if the lock is already taken
  77. /// const BodyID *body_id = ...; // Obtain IDs to bodies
  78. /// int num_body_ids = ...;
  79. ///
  80. /// // Scoped lock
  81. /// {
  82. /// BodyLockMultiRead lock(lock_interface, body_ids, num_body_ids);
  83. /// for (int i = 0; i < num_body_ids; ++i)
  84. /// {
  85. /// const Body *body = lock.GetBody(i);
  86. /// if (body != nullptr)
  87. /// {
  88. /// const Body &body = lock.Body();
  89. ///
  90. /// // Do something with body
  91. /// ...
  92. /// }
  93. /// }
  94. /// }
  95. class BodyLockMultiRead : public BodyLockMultiBase<false, const Body>
  96. {
  97. using BodyLockMultiBase::BodyLockMultiBase;
  98. };
  99. /// Specialization that locks multiple bodies for writing to. @see BodyLockMultiRead for usage patterns.
  100. class BodyLockMultiWrite : public BodyLockMultiBase<true, Body>
  101. {
  102. using BodyLockMultiBase::BodyLockMultiBase;
  103. };
  104. JPH_NAMESPACE_END