JobSystem.inl 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. JPH_NAMESPACE_BEGIN
  4. void JobSystem::Job::AddDependency(int inCount)
  5. {
  6. JPH_IF_ENABLE_ASSERTS(uint32 old_value =) mNumDependencies.fetch_add(inCount, memory_order_relaxed);
  7. 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");
  8. }
  9. bool JobSystem::Job::RemoveDependency(int inCount)
  10. {
  11. uint32 old_value = mNumDependencies.fetch_sub(inCount, memory_order_release);
  12. 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");
  13. uint32 new_value = old_value - inCount;
  14. JPH_ASSERT(old_value > new_value, "Test wrap around, this is a logic error");
  15. return new_value == 0;
  16. }
  17. void JobSystem::Job::RemoveDependencyAndQueue(int inCount)
  18. {
  19. if (RemoveDependency(inCount))
  20. mJobSystem->QueueJob(this);
  21. }
  22. void JobSystem::JobHandle::sRemoveDependencies(JobHandle *inHandles, uint inNumHandles, int inCount)
  23. {
  24. JPH_PROFILE_FUNCTION();
  25. JPH_ASSERT(inNumHandles > 0);
  26. // Get the job system, all jobs should be part of the same job system
  27. JobSystem *job_system = inHandles->GetPtr()->GetJobSystem();
  28. // Allocate a buffer to store the jobs that need to be queued
  29. Job **jobs_to_queue = (Job **)JPH_STACK_ALLOC(inNumHandles * sizeof(Job *));
  30. Job **next_job = jobs_to_queue;
  31. // Remove the dependencies on all jobs
  32. for (const JobHandle *handle = inHandles, *handle_end = inHandles + inNumHandles; handle < handle_end; ++handle)
  33. {
  34. Job *job = handle->GetPtr();
  35. JPH_ASSERT(job->GetJobSystem() == job_system); // All jobs should belong to the same job system
  36. if (job->RemoveDependency(inCount))
  37. *(next_job++) = job;
  38. }
  39. // If any jobs need to be scheduled, schedule them as a batch
  40. uint num_jobs_to_queue = uint(next_job - jobs_to_queue);
  41. if (num_jobs_to_queue != 0)
  42. job_system->QueueJobs(jobs_to_queue, num_jobs_to_queue);
  43. }
  44. JPH_NAMESPACE_END