108 static inline void sRemoveDependencies(
const JobHandle *inHandles,
uint inNumHandles,
int inCount = 1);
177 #if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED)
181 mJobSystem(inJobSystem),
182 mJobFunction(inJobFunction),
183 mNumDependencies(inNumDependencies)
194 mReferenceCount.fetch_add(1, memory_order_relaxed);
198 #ifndef JPH_TSAN_ENABLED
200 if (mReferenceCount.fetch_sub(1, memory_order_release) == 1)
203 atomic_thread_fence(memory_order_acquire);
204 mJobSystem->FreeJob(
this);
208 if (mReferenceCount.fetch_sub(1, memory_order_acq_rel) == 1)
209 mJobSystem->FreeJob(
this);
214 inline void AddDependency(
int inCount);
218 inline bool RemoveDependency(
int inCount);
222 inline void RemoveDependencyAndQueue(
int inCount);
227 intptr_t barrier = 0;
228 if (mBarrier.compare_exchange_strong(barrier,
reinterpret_cast<intptr_t
>(inBarrier), memory_order_relaxed))
239 if (!mNumDependencies.compare_exchange_strong(state,
cExecutingState, memory_order_acquire))
249 intptr_t barrier = mBarrier.load(memory_order_relaxed);
252 if (mBarrier.compare_exchange_weak(barrier,
cBarrierDoneState, memory_order_relaxed))
259 mNumDependencies.compare_exchange_strong(state,
cDoneState, memory_order_relaxed);
264 reinterpret_cast<Barrier *
>(barrier)->OnJobFinished(
this);
270 inline bool CanBeExecuted()
const {
return mNumDependencies.load(memory_order_relaxed) == 0; }
273 inline bool IsDone()
const {
return mNumDependencies.load(memory_order_relaxed) ==
cDoneState; }
275 #if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED)
277 const char *
GetName()
const {
return mJobName; }
286 #if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED)
287 const char * mJobName;
291 atomic<intptr_t> mBarrier = 0;
293 atomic<uint32> mReferenceCount = 0;
294 atomic<uint32> mNumDependencies;
Color ColorArg
Type to use for passing arguments to a function.
Definition Color.h:12
#define JPH_EXPORT
Definition Core.h:275
unsigned int uint
Definition Core.h:500
#define JPH_NAMESPACE_END
Definition Core.h:425
std::uint32_t uint32
Definition Core.h:503
#define JPH_NAMESPACE_BEGIN
Definition Core.h:419
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
JobSystem::JobHandle JobHandle
Definition JobSystem.h:307
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:50
#define JPH_PROFILE(...)
Definition Profiler.h:268
Class that holds an RGBA color with 8-bits per component.
Definition Color.h:16
A job barrier keeps track of a number of jobs and allows waiting until they are all completed.
Definition JobSystem.h:123
friend class Job
Job needs to be able to call OnJobFinished.
Definition JobSystem.h:137
virtual ~Barrier()=default
Destructor, you should call JobSystem::DestroyBarrier instead of destructing this object directly.
virtual void OnJobFinished(Job *inJob)=0
Called by a Job to mark that it is finished.
virtual void AddJobs(const JobHandle *inHandles, uint inNumHandles)=0
virtual JPH_OVERRIDE_NEW_DELETE void AddJob(const JobHandle &inJob)=0
Definition JobSystem.h:80
JobHandle()=default
Constructor.
JobHandle(Job *inJob)
Constructor, only to be used by JobSystem.
Definition JobSystem.h:88
static void sRemoveDependencies(const JobHandle *inHandles, uint inNumHandles, int inCount=1)
Remove a dependency from a batch of jobs at once, this can be more efficient than removing them one b...
Definition JobSystem.inl:28
void RemoveDependency(int inCount=1) const
Definition JobSystem.h:105
JobHandle(JobHandle &&inHandle) noexcept
Definition JobSystem.h:85
bool IsDone() const
Check if this job has finished executing.
Definition JobSystem.h:98
static void sRemoveDependencies(StaticArray< JobHandle, N > &inHandles, int inCount=1)
Helper function to remove dependencies on a static array of job handles.
Definition JobSystem.h:112
bool IsValid() const
Check if this handle contains a job.
Definition JobSystem.h:95
JobHandle(const JobHandle &inHandle)=default
void AddDependency(int inCount=1) const
Add to the dependency counter.
Definition JobSystem.h:101
A class that contains information for a single unit of work.
Definition JobSystem.h:171
static constexpr intptr_t cBarrierDoneState
Value to use when the barrier has been triggered.
Definition JobSystem.h:283
const char * GetName() const
Get the name of the job.
Definition JobSystem.h:277
void Release()
Definition JobSystem.h:196
JPH_OVERRIDE_NEW_DELETE Job(const char *inJobName, ColorArg inColor, JobSystem *inJobSystem, const JobFunction &inJobFunction, uint32 inNumDependencies)
Constructor.
Definition JobSystem.h:176
uint32 Execute()
Run the job function, returns the number of dependencies that this job still has or cExecutingState o...
Definition JobSystem.h:235
bool SetBarrier(Barrier *inBarrier)
Set the job barrier that this job belongs to and returns false if this was not possible because the j...
Definition JobSystem.h:225
static constexpr uint32 cDoneState
Value of mNumDependencies when job is done executing.
Definition JobSystem.h:281
JobSystem * GetJobSystem()
Get the jobs system to which this job belongs.
Definition JobSystem.h:188
bool CanBeExecuted() const
Test if the job can be executed.
Definition JobSystem.h:270
bool IsDone() const
Test if the job finished executing.
Definition JobSystem.h:273
void AddRef()
Add or release a reference to this object.
Definition JobSystem.h:191
static constexpr uint32 cExecutingState
Value of mNumDependencies when job is executing.
Definition JobSystem.h:280
Definition JobSystem.h:70
virtual JobHandle CreateJob(const char *inName, ColorArg inColor, const JobFunction &inJobFunction, uint32 inNumDependencies=0)=0
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.
virtual void QueueJobs(Job **inJobs, uint inNumJobs)=0
Adds a number of jobs at once to the job queue.
virtual int GetMaxConcurrency() const =0
Get maximum number of concurrently executing jobs.
virtual void FreeJob(Job *inJob)=0
Frees a job.
virtual ~JobSystem()=default
Destructor.
function< void()> JobFunction
Main function of the job.
Definition JobSystem.h:147
virtual void QueueJob(Job *inJob)=0
Adds a job to the job queue.
Definition Reference.h:107
Job * GetPtr() const
Definition Reference.h:135
Ref()
Definition Reference.h:110
Simple variable length array backed by a fixed size buffer.
Definition StaticArray.h:14
const T * data() const
Definition StaticArray.h:138
size_type size() const
Returns amount of elements in the array.
Definition StaticArray.h:89