Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
BodyLockInterface.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
6#pragma once
7
11#include <Jolt/Core/Mutex.h>
12
14
17{
18public:
21
23 explicit BodyLockInterface(BodyManager &inBodyManager) : mBodyManager(inBodyManager) { }
24 virtual ~BodyLockInterface() = default;
25
28 virtual SharedMutex * LockRead(const BodyID &inBodyID) const = 0;
29 virtual void UnlockRead(SharedMutex *inMutex) const = 0;
30 virtual SharedMutex * LockWrite(const BodyID &inBodyID) const = 0;
31 virtual void UnlockWrite(SharedMutex *inMutex) const = 0;
33
36 {
38 }
39
42 virtual MutexMask GetMutexMask(const BodyID *inBodies, int inNumber) const = 0;
43 virtual void LockRead(MutexMask inMutexMask) const = 0;
44 virtual void UnlockRead(MutexMask inMutexMask) const = 0;
45 virtual void LockWrite(MutexMask inMutexMask) const = 0;
46 virtual void UnlockWrite(MutexMask inMutexMask) const = 0;
48
50 inline Body * TryGetBody(const BodyID &inBodyID) const { return mBodyManager.TryGetBody(inBodyID); }
51
52protected:
54};
55
58{
59public:
61
63 virtual SharedMutex * LockRead([[maybe_unused]] const BodyID &inBodyID) const override { return nullptr; }
64 virtual void UnlockRead([[maybe_unused]] SharedMutex *inMutex) const override { /* Nothing to do */ }
65 virtual SharedMutex * LockWrite([[maybe_unused]] const BodyID &inBodyID) const override { return nullptr; }
66 virtual void UnlockWrite([[maybe_unused]] SharedMutex *inMutex) const override { /* Nothing to do */ }
67
69 virtual MutexMask GetMutexMask([[maybe_unused]] const BodyID *inBodies, [[maybe_unused]] int inNumber) const override { return 0; }
70 virtual void LockRead([[maybe_unused]] MutexMask inMutexMask) const override { /* Nothing to do */ }
71 virtual void UnlockRead([[maybe_unused]] MutexMask inMutexMask) const override { /* Nothing to do */ }
72 virtual void LockWrite([[maybe_unused]] MutexMask inMutexMask) const override { /* Nothing to do */ }
73 virtual void UnlockWrite([[maybe_unused]] MutexMask inMutexMask) const override { /* Nothing to do */ }
74};
75
78{
79public:
81
83 virtual SharedMutex * LockRead(const BodyID &inBodyID) const override
84 {
85 SharedMutex &mutex = mBodyManager.GetMutexForBody(inBodyID);
86 PhysicsLock::sLockShared(mutex JPH_IF_ENABLE_ASSERTS(, &mBodyManager, EPhysicsLockTypes::PerBody));
87 return &mutex;
88 }
89
90 virtual void UnlockRead(SharedMutex *inMutex) const override
91 {
92 PhysicsLock::sUnlockShared(*inMutex JPH_IF_ENABLE_ASSERTS(, &mBodyManager, EPhysicsLockTypes::PerBody));
93 }
94
95 virtual SharedMutex * LockWrite(const BodyID &inBodyID) const override
96 {
97 SharedMutex &mutex = mBodyManager.GetMutexForBody(inBodyID);
98 PhysicsLock::sLock(mutex JPH_IF_ENABLE_ASSERTS(, &mBodyManager, EPhysicsLockTypes::PerBody));
99 return &mutex;
100 }
101
102 virtual void UnlockWrite(SharedMutex *inMutex) const override
103 {
104 PhysicsLock::sUnlock(*inMutex JPH_IF_ENABLE_ASSERTS(, &mBodyManager, EPhysicsLockTypes::PerBody));
105 }
106
108 virtual MutexMask GetMutexMask(const BodyID *inBodies, int inNumber) const override
109 {
110 return mBodyManager.GetMutexMask(inBodies, inNumber);
111 }
112
113 virtual void LockRead(MutexMask inMutexMask) const override
114 {
115 mBodyManager.LockRead(inMutexMask);
116 }
117
118 virtual void UnlockRead(MutexMask inMutexMask) const override
119 {
120 mBodyManager.UnlockRead(inMutexMask);
121 }
122
123 virtual void LockWrite(MutexMask inMutexMask) const override
124 {
125 mBodyManager.LockWrite(inMutexMask);
126 }
127
128 virtual void UnlockWrite(MutexMask inMutexMask) const override
129 {
130 mBodyManager.UnlockWrite(inMutexMask);
131 }
132};
133
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#define JPH_IF_ENABLE_ASSERTS(...)
Definition: IssueReporting.h:35
Definition: Body.h:35
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition: BodyID.h:13
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
BodyManager & mBodyManager
Definition: BodyLockInterface.h:53
virtual MutexMask GetMutexMask(const BodyID *inBodies, int inNumber) const =0
BodyLockInterface(BodyManager &inBodyManager)
Constructor.
Definition: BodyLockInterface.h:23
Body * TryGetBody(const BodyID &inBodyID) const
Convert body ID to body.
Definition: BodyLockInterface.h:50
virtual void UnlockRead(MutexMask inMutexMask) const =0
MutexMask GetAllBodiesMutexMask() const
Get the mask needed to lock all bodies.
Definition: BodyLockInterface.h:35
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 void LockWrite(MutexMask inMutexMask) const =0
virtual SharedMutex * LockWrite(const BodyID &inBodyID) const =0
virtual ~BodyLockInterface()=default
virtual void LockRead(MutexMask inMutexMask) const =0
virtual void UnlockWrite(MutexMask inMutexMask) const =0
Implementation that uses the body manager to lock the correct mutex for a body.
Definition: BodyLockInterface.h:78
virtual void UnlockWrite(SharedMutex *inMutex) const override
Definition: BodyLockInterface.h:102
virtual SharedMutex * LockWrite(const BodyID &inBodyID) const override
Definition: BodyLockInterface.h:95
virtual void UnlockRead(MutexMask inMutexMask) const override
Definition: BodyLockInterface.h:118
virtual void UnlockWrite(MutexMask inMutexMask) const override
Definition: BodyLockInterface.h:128
virtual void LockWrite(MutexMask inMutexMask) const override
Definition: BodyLockInterface.h:123
virtual void UnlockRead(SharedMutex *inMutex) const override
Definition: BodyLockInterface.h:90
virtual SharedMutex * LockRead(const BodyID &inBodyID) const override
Definition: BodyLockInterface.h:83
virtual MutexMask GetMutexMask(const BodyID *inBodies, int inNumber) const override
Definition: BodyLockInterface.h:108
virtual void LockRead(MutexMask inMutexMask) const override
Definition: BodyLockInterface.h:113
Implementation that performs no locking (assumes the lock has already been taken)
Definition: BodyLockInterface.h:58
virtual void LockWrite(MutexMask inMutexMask) const override
Definition: BodyLockInterface.h:72
virtual MutexMask GetMutexMask(const BodyID *inBodies, int inNumber) const override
Definition: BodyLockInterface.h:69
virtual void UnlockRead(MutexMask inMutexMask) const override
Definition: BodyLockInterface.h:71
virtual SharedMutex * LockRead(const BodyID &inBodyID) const override
Definition: BodyLockInterface.h:63
virtual void UnlockWrite(SharedMutex *inMutex) const override
Definition: BodyLockInterface.h:66
virtual SharedMutex * LockWrite(const BodyID &inBodyID) const override
Definition: BodyLockInterface.h:65
virtual void UnlockWrite(MutexMask inMutexMask) const override
Definition: BodyLockInterface.h:73
virtual void UnlockRead(SharedMutex *inMutex) const override
Definition: BodyLockInterface.h:64
virtual void LockRead(MutexMask inMutexMask) const override
Definition: BodyLockInterface.h:70
Class that contains all bodies.
Definition: BodyManager.h:32
void LockWrite(MutexMask inMutexMask) const
Definition: BodyManager.cpp:711
void UnlockWrite(MutexMask inMutexMask) const
Definition: BodyManager.cpp:721
void UnlockRead(MutexMask inMutexMask) const
Definition: BodyManager.cpp:701
MutexMask GetMutexMask(const BodyID *inBodies, int inNumber) const
Definition: BodyManager.cpp:669
MutexMask GetAllBodiesMutexMask() const
Definition: BodyManager.h:171
SharedMutex & GetMutexForBody(const BodyID &inID) const
Access the mutex for a single body.
Definition: BodyManager.h:164
void LockRead(MutexMask inMutexMask) const
Definition: BodyManager.cpp:691
const Body * TryGetBody(const BodyID &inID) const
Access a body, will return a nullptr if the body ID is no longer valid (not protected by lock)
Definition: BodyManager.h:136
uint64 MutexMask
Bodies are protected using an array of mutexes (so a fixed number, not 1 per body)....
Definition: BodyManager.h:167
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11
static void sLockShared(LockType &inMutex JPH_IF_ENABLE_ASSERTS(, PhysicsLockContext inContext, EPhysicsLockTypes inType))
Definition: PhysicsLock.h:68
static void sUnlock(LockType &inMutex JPH_IF_ENABLE_ASSERTS(, PhysicsLockContext inContext, EPhysicsLockTypes inType))
Definition: PhysicsLock.h:61
static void sUnlockShared(LockType &inMutex JPH_IF_ENABLE_ASSERTS(, PhysicsLockContext inContext, EPhysicsLockTypes inType))
Definition: PhysicsLock.h:75
static void sLock(LockType &inMutex JPH_IF_ENABLE_ASSERTS(, PhysicsLockContext inContext, EPhysicsLockTypes inType))
Definition: PhysicsLock.h:54
Definition: Mutex.h:166