Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
BodyLock.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:
17 BodyLockBase(const BodyLockInterface &inBodyLockInterface, const BodyID &inBodyID) :
18 mBodyLockInterface(inBodyLockInterface)
19 {
20 if (inBodyID == BodyID())
21 {
22 // Invalid body id
23 mBodyLockMutex = nullptr;
24 mBody = nullptr;
25 }
26 else
27 {
28 // Get mutex
29 mBodyLockMutex = Write? inBodyLockInterface.LockWrite(inBodyID) : inBodyLockInterface.LockRead(inBodyID);
30
31 // Get a reference to the body or nullptr when it is no longer valid
32 mBody = inBodyLockInterface.TryGetBody(inBodyID);
33 }
34 }
35
37 inline void ReleaseLock()
38 {
39 if (mBodyLockMutex != nullptr)
40 {
41 if (Write)
42 mBodyLockInterface.UnlockWrite(mBodyLockMutex);
43 else
44 mBodyLockInterface.UnlockRead(mBodyLockMutex);
45
46 mBodyLockMutex = nullptr;
47 mBody = nullptr;
48 }
49 }
50
53 {
55 }
56
58 inline bool Succeeded() const
59 {
60 return mBody != nullptr;
61 }
62
64 inline bool SucceededAndIsInBroadPhase() const
65 {
66 return mBody != nullptr && mBody->IsInBroadPhase();
67 }
68
70 inline BodyType & GetBody() const
71 {
72 JPH_ASSERT(mBody != nullptr, "Should check Succeeded() first");
73 return *mBody;
74 }
75
76private:
77 const BodyLockInterface & mBodyLockInterface;
78 SharedMutex * mBodyLockMutex;
79 BodyType * mBody;
80};
81
100class BodyLockRead : public BodyLockBase<false, const Body>
101{
103};
104
106class BodyLockWrite : public BodyLockBase<true, Body>
107{
109};
110
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#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
Base class for locking bodies for the duration of the scope of this class (do not use directly)
Definition: BodyLock.h:14
~BodyLockBase()
Destructor will unlock the body.
Definition: BodyLock.h:52
BodyLockBase(const BodyLockInterface &inBodyLockInterface, const BodyID &inBodyID)
Constructor will lock the body.
Definition: BodyLock.h:17
bool Succeeded() const
Test if the lock was successful (if the body ID was valid)
Definition: BodyLock.h:58
bool SucceededAndIsInBroadPhase() const
Test if the lock was successful (if the body ID was valid) and the body is still in the broad phase.
Definition: BodyLock.h:64
BodyType & GetBody() const
Access the body.
Definition: BodyLock.h:70
void ReleaseLock()
Explicitly release the lock (normally this is done in the destructor)
Definition: BodyLock.h:37
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
virtual void UnlockWrite(SharedMutex *inMutex) const =0
virtual SharedMutex * LockRead(const BodyID &inBodyID) const =0
virtual SharedMutex * LockWrite(const BodyID &inBodyID) const =0
Definition: BodyLock.h:101
Specialization that locks a body for writing to.
Definition: BodyLock.h:107
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11
Definition: Mutex.h:166