Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
JobSystemWithBarrier.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2023 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
9
11
24{
25public:
27
30 explicit JobSystemWithBarrier(uint inMaxBarriers);
32 virtual ~JobSystemWithBarrier() override;
33
36 void Init(uint inMaxBarriers);
37
38 // See JobSystem
39 virtual Barrier * CreateBarrier() override;
40 virtual void DestroyBarrier(Barrier *inBarrier) override;
41 virtual void WaitForJobs(Barrier *inBarrier) override;
42
43private:
44 class BarrierImpl : public Barrier
45 {
46 public:
48
50 BarrierImpl();
51 virtual ~BarrierImpl() override;
52
53 // See Barrier
54 virtual void AddJob(const JobHandle &inJob) override;
55 virtual void AddJobs(const JobHandle *inHandles, uint inNumHandles) override;
56
58 inline bool IsEmpty() const { return mJobReadIndex == mJobWriteIndex; }
59
61 void Wait();
62
64 atomic<bool> mInUse { false };
65
66 protected:
68 virtual void OnJobFinished(Job *inJob) override;
69
71 static constexpr uint cMaxJobs = 2048;
72 static_assert(IsPowerOf2(cMaxJobs)); // We do bit operations and require max jobs to be a power of 2
73 atomic<Job *> mJobs[cMaxJobs];
74 alignas(JPH_CACHE_LINE_SIZE) atomic<uint> mJobReadIndex { 0 };
75 alignas(JPH_CACHE_LINE_SIZE) atomic<uint> mJobWriteIndex { 0 };
76 atomic<int> mNumToAcquire { 0 };
77 Semaphore mSemaphore;
78 };
79
81 uint mMaxBarriers = 0;
82 BarrierImpl * mBarriers = nullptr;
83};
84
#define JPH_EXPORT
Definition: Core.h:227
#define JPH_CACHE_LINE_SIZE
Definition: Core.h:466
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
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:29
A job barrier keeps track of a number of jobs and allows waiting until they are all completed.
Definition: JobSystem.h:123
Definition: JobSystem.h:80
Definition: JobSystem.h:70
virtual Barrier * CreateBarrier()=0
Create a new barrier, used to wait on jobs.
virtual void WaitForJobs(Barrier *inBarrier)=0
Wait for a set of jobs to be finished, note that only 1 thread can be waiting on a barrier at a time.
virtual void DestroyBarrier(Barrier *inBarrier)=0
Destroy a barrier when it is no longer used. The barrier should be empty at this point.
Definition: JobSystemWithBarrier.h:24
JobSystemWithBarrier()=default
Definition: Semaphore.h:23