Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
MutexArray.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
15template <class MutexType>
16class MutexArray : public NonCopyable
17{
18public:
20
22 MutexArray() = default;
23
25 explicit MutexArray(uint inNumMutexes) { Init(inNumMutexes); }
26
28 ~MutexArray() { delete [] mMutexStorage; }
29
32 void Init(uint inNumMutexes)
33 {
34 JPH_ASSERT(mMutexStorage == nullptr);
35 JPH_ASSERT(inNumMutexes > 0 && IsPowerOf2(inNumMutexes));
36
37 mMutexStorage = new MutexStorage[inNumMutexes];
38 mNumMutexes = inNumMutexes;
39 }
40
42 inline uint GetNumMutexes() const
43 {
44 return mNumMutexes;
45 }
46
48 inline uint32 GetMutexIndex(uint32 inObjectIndex) const
49 {
50 Hash<uint32> hasher;
51 return hasher(inObjectIndex) & (mNumMutexes - 1);
52 }
53
55 inline MutexType & GetMutexByObjectIndex(uint32 inObjectIndex)
56 {
57 return mMutexStorage[GetMutexIndex(inObjectIndex)].mMutex;
58 }
59
61 inline MutexType & GetMutexByIndex(uint32 inMutexIndex)
62 {
63 return mMutexStorage[inMutexIndex].mMutex;
64 }
65
67 void LockAll()
68 {
70
71 MutexStorage *end = mMutexStorage + mNumMutexes;
72 for (MutexStorage *m = mMutexStorage; m < end; ++m)
73 m->mMutex.lock();
74 }
75
77 void UnlockAll()
78 {
80
81 MutexStorage *end = mMutexStorage + mNumMutexes;
82 for (MutexStorage *m = mMutexStorage; m < end; ++m)
83 m->mMutex.unlock();
84 }
85
86private:
88 struct alignas(JPH_CACHE_LINE_SIZE) MutexStorage
89 {
91
92 MutexType mMutex;
93 };
94
95 MutexStorage * mMutexStorage = nullptr;
96 uint mNumMutexes = 0;
97};
98
100
unsigned int uint
Definition Core.h:564
#define JPH_NAMESPACE_END
Definition Core.h:487
std::uint32_t uint32
Definition Core.h:567
#define JPH_NAMESPACE_BEGIN
Definition Core.h:481
#define JPH_ASSERT(...)
Definition IssueReporting.h:28
constexpr bool IsPowerOf2(T inV)
Check if inV is a power of 2.
Definition Math.h:161
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:51
#define JPH_PROFILE_FUNCTION()
Scope profiling for function.
Definition Profiler.h:270
MutexArray(uint inNumMutexes)
Constructor, constructs an array with inNumMutexes entries.
Definition MutexArray.h:25
uint32 GetMutexIndex(uint32 inObjectIndex) const
Convert an object index to a mutex index.
Definition MutexArray.h:48
~MutexArray()
Destructor.
Definition MutexArray.h:28
JPH_OVERRIDE_NEW_DELETE MutexArray()=default
Constructor, constructs an empty mutex array that you need to initialize with Init().
MutexType & GetMutexByObjectIndex(uint32 inObjectIndex)
Get the mutex belonging to a certain object by index.
Definition MutexArray.h:55
void UnlockAll()
Unlock all mutexes.
Definition MutexArray.h:77
void LockAll()
Lock all mutexes.
Definition MutexArray.h:67
void Init(uint inNumMutexes)
Definition MutexArray.h:32
MutexType & GetMutexByIndex(uint32 inMutexIndex)
Get a mutex by index in the array.
Definition MutexArray.h:61
uint GetNumMutexes() const
Get the number of mutexes that were allocated.
Definition MutexArray.h:42
NonCopyable()=default
Fallback hash function that calls T::GetHash().
Definition HashCombine.h:59