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 MutexArray() = default;
21
23 explicit MutexArray(uint inNumMutexes) { Init(inNumMutexes); }
24
26 ~MutexArray() { delete [] mMutexStorage; }
27
30 void Init(uint inNumMutexes)
31 {
32 JPH_ASSERT(mMutexStorage == nullptr);
33 JPH_ASSERT(inNumMutexes > 0 && IsPowerOf2(inNumMutexes));
34
35 mMutexStorage = new MutexStorage[inNumMutexes];
36 mNumMutexes = inNumMutexes;
37 }
38
40 inline uint GetNumMutexes() const
41 {
42 return mNumMutexes;
43 }
44
46 inline uint32 GetMutexIndex(uint32 inObjectIndex) const
47 {
48 std::hash<uint32> hasher;
49 return hasher(inObjectIndex) & (mNumMutexes - 1);
50 }
51
53 inline MutexType & GetMutexByObjectIndex(uint32 inObjectIndex)
54 {
55 return mMutexStorage[GetMutexIndex(inObjectIndex)].mMutex;
56 }
57
59 inline MutexType & GetMutexByIndex(uint32 inMutexIndex)
60 {
61 return mMutexStorage[inMutexIndex].mMutex;
62 }
63
65 void LockAll()
66 {
68
69 MutexStorage *end = mMutexStorage + mNumMutexes;
70 for (MutexStorage *m = mMutexStorage; m < end; ++m)
71 m->mMutex.lock();
72 }
73
75 void UnlockAll()
76 {
78
79 MutexStorage *end = mMutexStorage + mNumMutexes;
80 for (MutexStorage *m = mMutexStorage; m < end; ++m)
81 m->mMutex.unlock();
82 }
83
84private:
86 struct alignas(JPH_CACHE_LINE_SIZE) MutexStorage
87 {
89
90 MutexType mMutex;
91 };
92
93 MutexStorage * mMutexStorage = nullptr;
94 uint mNumMutexes = 0;
95};
96
98
unsigned int uint
Definition Core.h:448
#define JPH_NAMESPACE_END
Definition Core.h:377
std::uint32_t uint32
Definition Core.h:451
#define JPH_NAMESPACE_BEGIN
Definition Core.h:371
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
constexpr bool IsPowerOf2(T inV)
Check if inV is a power of 2.
Definition Math.h:73
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:31
#define JPH_PROFILE_FUNCTION()
Scope profiling for function.
Definition Profiler.h:254
Definition MutexArray.h:17
MutexArray(uint inNumMutexes)
Constructor, constructs an array with inNumMutexes entries.
Definition MutexArray.h:23
uint32 GetMutexIndex(uint32 inObjectIndex) const
Convert an object index to a mutex index.
Definition MutexArray.h:46
~MutexArray()
Destructor.
Definition MutexArray.h:26
MutexType & GetMutexByObjectIndex(uint32 inObjectIndex)
Get the mutex belonging to a certain object by index.
Definition MutexArray.h:53
void UnlockAll()
Unlock all mutexes.
Definition MutexArray.h:75
void LockAll()
Lock all mutexes.
Definition MutexArray.h:65
MutexArray()=default
Constructor, constructs an empty mutex array that you need to initialize with Init()
void Init(uint inNumMutexes)
Definition MutexArray.h:30
MutexType & GetMutexByIndex(uint32 inMutexIndex)
Get a mutex by index in the array.
Definition MutexArray.h:59
uint GetNumMutexes() const
Get the number of mutexes that were allocated.
Definition MutexArray.h:40
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition NonCopyable.h:11