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
38 {
39 if (mMutexMask != 0)
40 {
41 if (Write)
42 mBodyLockInterface.UnlockWrite(mMutexMask);
43 else
44 mBodyLockInterface.UnlockRead(mMutexMask);
45 }
46 }
47
49 inline BodyType * GetBody(int inBodyIndex) const
50 {
51 // Range check
52 JPH_ASSERT(inBodyIndex >= 0 && inBodyIndex < mNumBodyIDs);
53
54 // Get body ID
55 const BodyID &body_id = mBodyIDs[inBodyIndex];
56 if (body_id.IsInvalid())
57 return nullptr;
58
59 // Get a reference to the body or nullptr when it is no longer valid
60 return mBodyLockInterface.TryGetBody(body_id);
61 }
62
63private:
64 const BodyLockInterface & mBodyLockInterface;
65 MutexMask mMutexMask;
66 const BodyID * mBodyIDs;
67 int mNumBodyIDs;
68};
69
93class BodyLockMultiRead : public BodyLockMultiBase<false, const Body>
94{
96};
97
99class BodyLockMultiWrite : public BodyLockMultiBase<true, Body>
100{
102};
103
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
#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:64
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:49
~BodyLockMultiBase()
Destructor will unlock the bodies.
Definition: BodyLockMulti.h:37
BodyLockMultiBase(const BodyLockInterface &inBodyLockInterface, const BodyID *inBodyIDs, int inNumber)
Constructor will lock the bodies.
Definition: BodyLockMulti.h:20
BodyLockInterface::MutexMask MutexMask
Redefine MutexMask.
Definition: BodyLockMulti.h:17
Definition: BodyLockMulti.h:94
Specialization that locks multiple bodies for writing to.
Definition: BodyLockMulti.h:100
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11