JobSystem.inl 2.1 KB

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