Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Body/BodyLockMulti.h
9912 views
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
7
#include <Jolt/Physics/Body/BodyLockInterface.h>
8
9
JPH_NAMESPACE_BEGIN
10
11
/// Base class for locking multiple bodies for the duration of the scope of this class (do not use directly)
12
template <bool Write, class BodyType>
13
class BodyLockMultiBase : public NonCopyable
14
{
15
public:
16
/// Redefine MutexMask
17
using MutexMask = BodyLockInterface::MutexMask;
18
19
/// Constructor will lock the bodies
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
36
/// Destructor will unlock the bodies
37
~BodyLockMultiBase()
38
{
39
if (mMutexMask != 0)
40
{
41
if (Write)
42
mBodyLockInterface.UnlockWrite(mMutexMask);
43
else
44
mBodyLockInterface.UnlockRead(mMutexMask);
45
}
46
}
47
48
/// Access the body (returns null if body was not properly locked)
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
63
private:
64
const BodyLockInterface & mBodyLockInterface;
65
MutexMask mMutexMask;
66
const BodyID * mBodyIDs;
67
int mNumBodyIDs;
68
};
69
70
/// A multi body lock takes a number of body IDs and locks the underlying bodies so that other threads cannot access its members
71
///
72
/// The common usage pattern is:
73
///
74
/// BodyLockInterface lock_interface = physics_system.GetBodyLockInterface(); // Or non-locking interface if the lock is already taken
75
/// const BodyID *body_id = ...; // Obtain IDs to bodies
76
/// int num_body_ids = ...;
77
///
78
/// // Scoped lock
79
/// {
80
/// BodyLockMultiRead lock(lock_interface, body_ids, num_body_ids);
81
/// for (int i = 0; i < num_body_ids; ++i)
82
/// {
83
/// const Body *body = lock.GetBody(i);
84
/// if (body != nullptr)
85
/// {
86
/// const Body &body = lock.Body();
87
///
88
/// // Do something with body
89
/// ...
90
/// }
91
/// }
92
/// }
93
class BodyLockMultiRead : public BodyLockMultiBase<false, const Body>
94
{
95
using BodyLockMultiBase::BodyLockMultiBase;
96
};
97
98
/// Specialization that locks multiple bodies for writing to. @see BodyLockMultiRead for usage patterns.
99
class BodyLockMultiWrite : public BodyLockMultiBase<true, Body>
100
{
101
using BodyLockMultiBase::BodyLockMultiBase;
102
};
103
104
JPH_NAMESPACE_END
105
106