BodyLockMulti.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /// Destructor will unlock the bodies
  31. ~BodyLockMultiBase()
  32. {
  33. if (mMutexMask != 0)
  34. {
  35. if (Write)
  36. mBodyLockInterface.UnlockWrite(mMutexMask);
  37. else
  38. mBodyLockInterface.UnlockRead(mMutexMask);
  39. }
  40. }
  41. /// Access the body (returns null if body was not properly locked)
  42. inline BodyType * GetBody(int inBodyIndex) const
  43. {
  44. // Range check
  45. JPH_ASSERT(inBodyIndex >= 0 && inBodyIndex < mNumBodyIDs);
  46. // Get body ID
  47. const BodyID &body_id = mBodyIDs[inBodyIndex];
  48. if (body_id.IsInvalid())
  49. return nullptr;
  50. // Get a reference to the body or nullptr when it is no longer valid
  51. return mBodyLockInterface.TryGetBody(body_id);
  52. }
  53. private:
  54. const BodyLockInterface & mBodyLockInterface;
  55. MutexMask mMutexMask;
  56. const BodyID * mBodyIDs;
  57. int mNumBodyIDs;
  58. };
  59. /// A multi body lock takes a number of body IDs and locks the underlying bodies so that other threads cannot access its members
  60. ///
  61. /// The common usage pattern is:
  62. ///
  63. /// BodyLockInterface lock_interface = physics_system.GetBodyLockInterface(); // Or non-locking interface if the lock is already taken
  64. /// const BodyID *body_id = ...; // Obtain IDs to bodies
  65. /// int num_body_ids = ...;
  66. ///
  67. /// // Scoped lock
  68. /// {
  69. /// BodyLockMultiRead lock(lock_interface, body_ids, num_body_ids);
  70. /// for (int i = 0; i < num_body_ids; ++i)
  71. /// {
  72. /// const Body *body = lock.GetBody(i);
  73. /// if (body != nullptr)
  74. /// {
  75. /// const Body &body = lock.Body();
  76. ///
  77. /// // Do something with body
  78. /// ...
  79. /// }
  80. /// }
  81. /// }
  82. class BodyLockMultiRead : public BodyLockMultiBase<false, const Body>
  83. {
  84. using BodyLockMultiBase::BodyLockMultiBase;
  85. };
  86. /// Specialization that locks multiple bodies for writing to. @see BodyLockMultiRead for usage patterns.
  87. class BodyLockMultiWrite : public BodyLockMultiBase<true, Body>
  88. {
  89. using BodyLockMultiBase::BodyLockMultiBase;
  90. };
  91. JPH_NAMESPACE_END