jolt_job_system.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**************************************************************************/
  2. /* jolt_job_system.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/os/spin_lock.h"
  32. #include "core/templates/hash_map.h"
  33. #include "Jolt/Jolt.h"
  34. #include "Jolt/Core/FixedSizeFreeList.h"
  35. #include "Jolt/Core/JobSystemWithBarrier.h"
  36. #include <atomic>
  37. class JoltJobSystem final : public JPH::JobSystemWithBarrier {
  38. class Job : public JPH::JobSystem::Job {
  39. inline static std::atomic<Job *> completed_head = nullptr;
  40. #ifdef DEBUG_ENABLED
  41. const char *name = nullptr;
  42. #endif
  43. int64_t task_id = -1;
  44. std::atomic<Job *> completed_next = nullptr;
  45. static void _execute(void *p_user_data);
  46. public:
  47. Job(const char *p_name, JPH::ColorArg p_color, JPH::JobSystem *p_job_system, const JPH::JobSystem::JobFunction &p_job_function, JPH::uint32 p_dependency_count);
  48. Job(const Job &p_other) = delete;
  49. Job(Job &&p_other) = delete;
  50. ~Job();
  51. static void push_completed(Job *p_job);
  52. static Job *pop_completed();
  53. void queue();
  54. Job &operator=(const Job &p_other) = delete;
  55. Job &operator=(Job &&p_other) = delete;
  56. };
  57. #ifdef DEBUG_ENABLED
  58. // We use `const void*` here to avoid the cost of hashing the actual string, since the job names
  59. // are always literals and as such will point to the same address every time.
  60. inline static HashMap<const void *, uint64_t> timings_by_job;
  61. // TODO: Check whether the usage of SpinLock is justified or if this should be a mutex instead.
  62. inline static SpinLock timings_lock;
  63. #endif
  64. JPH::FixedSizeFreeList<Job> jobs;
  65. int thread_count = 0;
  66. virtual int GetMaxConcurrency() const override;
  67. virtual JPH::JobHandle CreateJob(const char *p_name, JPH::ColorArg p_color, const JPH::JobSystem::JobFunction &p_job_function, JPH::uint32 p_dependency_count = 0) override;
  68. virtual void QueueJob(JPH::JobSystem::Job *p_job) override;
  69. virtual void QueueJobs(JPH::JobSystem::Job **p_jobs, JPH::uint p_job_count) override;
  70. virtual void FreeJob(JPH::JobSystem::Job *p_job) override;
  71. void _reclaim_jobs();
  72. public:
  73. JoltJobSystem();
  74. void pre_step();
  75. void post_step();
  76. #ifdef DEBUG_ENABLED
  77. void flush_timings();
  78. #endif
  79. };