BodyLockMulti.h 2.8 KB

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