Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/JobSystem.inl

raw
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 JPH_NAMESPACE_BEGIN
6
7 void JobSystem::Job::AddDependency(int inCount)
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
13 bool JobSystem::Job::RemoveDependency(int inCount)
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
22 void JobSystem::Job::RemoveDependencyAndQueue(int inCount)
23 {
24 if (RemoveDependency(inCount))
25 mJobSystem->QueueJob(this);
26 }
27
28 void JobSystem::JobHandle::sRemoveDependencies(const JobHandle *inHandles, uint inNumHandles, int inCount)
29 {
30 JPH_PROFILE_FUNCTION();
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
56 JPH_NAMESPACE_END