Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
BodyLockMulti.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
10
12template <bool Write, class BodyType>
14{
15public:
18
20 BodyLockMultiBase(const BodyLockInterface &inBodyLockInterface, const BodyID *inBodyIDs, int inNumber) :
21 mBodyLockInterface(inBodyLockInterface),
22 mMutexMask(inBodyLockInterface.GetMutexMask(inBodyIDs, inNumber)),
23 mBodyIDs(inBodyIDs),
24 mNumBodyIDs(inNumber)
25 {
26 if (mMutexMask != 0)
27 {
28 // Get mutex
29 if (Write)
30 inBodyLockInterface.LockWrite(mMutexMask);
31 else
32 inBodyLockInterface.LockRead(mMutexMask);
33 }
34 }
35
37 inline void ReleaseLocks()
38 {
39 if (mMutexMask != 0)
40 {
41 if (Write)
42 mBodyLockInterface.UnlockWrite(mMutexMask);
43 else
44 mBodyLockInterface.UnlockRead(mMutexMask);
45
46 mMutexMask = 0;
47 mBodyIDs = nullptr;
48 mNumBodyIDs = 0;
49 }
50 }
51
54 {
56 }
57
59 inline int GetNumBodies() const
60 {
61 return mNumBodyIDs;
62 }
63
65 inline BodyType * GetBody(int inBodyIndex) const
66 {
67 // Range check
68 JPH_ASSERT(inBodyIndex >= 0 && inBodyIndex < mNumBodyIDs);
69
70 // Get body ID
71 const BodyID &body_id = mBodyIDs[inBodyIndex];
72 if (body_id.IsInvalid())
73 return nullptr;
74
75 // Get a reference to the body or nullptr when it is no longer valid
76 return mBodyLockInterface.TryGetBody(body_id);
77 }
78
79private:
80 const BodyLockInterface & mBodyLockInterface;
81 MutexMask mMutexMask;
82 const BodyID * mBodyIDs;
83 int mNumBodyIDs;
84};
85
109class BodyLockMultiRead : public BodyLockMultiBase<false, const Body>
110{
112};
113
115class BodyLockMultiWrite : public BodyLockMultiBase<true, Body>
116{
118};
119
#define JPH_NAMESPACE_END
Definition: Core.h:419
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:413
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition: BodyID.h:13
bool IsInvalid() const
Check if the ID is valid.
Definition: BodyID.h:65
Base class interface for locking a body. Usually you will use BodyLockRead / BodyLockWrite / BodyLock...
Definition: BodyLockInterface.h:17
virtual void UnlockRead(SharedMutex *inMutex) const =0
Body * TryGetBody(const BodyID &inBodyID) const
Convert body ID to body.
Definition: BodyLockInterface.h:50
BodyManager::MutexMask MutexMask
Redefine MutexMask.
Definition: BodyLockInterface.h:20
virtual void UnlockWrite(SharedMutex *inMutex) const =0
virtual SharedMutex * LockRead(const BodyID &inBodyID) const =0
virtual SharedMutex * LockWrite(const BodyID &inBodyID) const =0
Base class for locking multiple bodies for the duration of the scope of this class (do not use direct...
Definition: BodyLockMulti.h:14
BodyType * GetBody(int inBodyIndex) const
Access the body (returns null if body was not properly locked)
Definition: BodyLockMulti.h:65
~BodyLockMultiBase()
Destructor will unlock the bodies.
Definition: BodyLockMulti.h:53
BodyLockMultiBase(const BodyLockInterface &inBodyLockInterface, const BodyID *inBodyIDs, int inNumber)
Constructor will lock the bodies.
Definition: BodyLockMulti.h:20
int GetNumBodies() const
Returns the number of bodies that were locked.
Definition: BodyLockMulti.h:59
void ReleaseLocks()
Explicitly release the locks on all bodies (normally this is done in the destructor)
Definition: BodyLockMulti.h:37
BodyLockInterface::MutexMask MutexMask
Redefine MutexMask.
Definition: BodyLockMulti.h:17
Definition: BodyLockMulti.h:110
Specialization that locks multiple bodies for writing to.
Definition: BodyLockMulti.h:116
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11