Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
JobSystem.inl
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
6
8{
9 JPH_IF_ENABLE_ASSERTS(uint32 old_value =) mNumDependencies.fetch_add(inCount, memory_order_relaxed);
10 JPH_ASSERT(old_value > 0 && old_value != cExecutingState && old_value != cDoneState, "Job is queued, running or done, it is not allowed to add a dependency to a running job");
11}
12
14{
15 uint32 old_value = mNumDependencies.fetch_sub(inCount, memory_order_release);
16 JPH_ASSERT(old_value != cExecutingState && old_value != cDoneState, "Job is running or done, it is not allowed to add a dependency to a running job");
17 uint32 new_value = old_value - inCount;
18 JPH_ASSERT(old_value > new_value, "Test wrap around, this is a logic error");
19 return new_value == 0;
20}
21
23{
24 if (RemoveDependency(inCount))
25 mJobSystem->QueueJob(this);
26}
27
28void JobSystem::JobHandle::sRemoveDependencies(const JobHandle *inHandles, uint inNumHandles, int inCount)
29{
31
32 JPH_ASSERT(inNumHandles > 0);
33
34 // Get the job system, all jobs should be part of the same job system
35 JobSystem *job_system = inHandles->GetPtr()->GetJobSystem();
36
37 // Allocate a buffer to store the jobs that need to be queued
38 Job **jobs_to_queue = (Job **)JPH_STACK_ALLOC(inNumHandles * sizeof(Job *));
39 Job **next_job = jobs_to_queue;
40
41 // Remove the dependencies on all jobs
42 for (const JobHandle *handle = inHandles, *handle_end = inHandles + inNumHandles; handle < handle_end; ++handle)
43 {
44 Job *job = handle->GetPtr();
45 JPH_ASSERT(job->GetJobSystem() == job_system); // All jobs should belong to the same job system
46 if (job->RemoveDependency(inCount))
47 *(next_job++) = job;
48 }
49
50 // If any jobs need to be scheduled, schedule them as a batch
51 uint num_jobs_to_queue = uint(next_job - jobs_to_queue);
52 if (num_jobs_to_queue != 0)
53 job_system->QueueJobs(jobs_to_queue, num_jobs_to_queue);
54}
55
#define JPH_STACK_ALLOC(n)
Definition Core.h:506
unsigned int uint
Definition Core.h:453
#define JPH_NAMESPACE_END
Definition Core.h:379
std::uint32_t uint32
Definition Core.h:456
#define JPH_NAMESPACE_BEGIN
Definition Core.h:373
#define JPH_IF_ENABLE_ASSERTS(...)
Definition IssueReporting.h:35
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
#define JPH_PROFILE_FUNCTION()
Scope profiling for function.
Definition Profiler.h:271
Definition JobSystem.h:80
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
A class that contains information for a single unit of work.
Definition JobSystem.h:171
bool RemoveDependency(int inCount)
Definition JobSystem.inl:13
void AddDependency(int inCount)
Add to the dependency counter.
Definition JobSystem.inl:7
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
static constexpr uint32 cExecutingState
Value of mNumDependencies when job is executing.
Definition JobSystem.h:280
void RemoveDependencyAndQueue(int inCount)
Definition JobSystem.inl:22
Definition JobSystem.h:70
virtual void QueueJobs(Job **inJobs, uint inNumJobs)=0
Adds a number of jobs at once to the job queue.