JobSystemThreadPool.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Core/JobSystem.h>
  5. #include <Jolt/Core/FixedSizeFreeList.h>
  6. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  7. #include <thread>
  8. #include <mutex>
  9. #include <condition_variable>
  10. JPH_SUPPRESS_WARNINGS_STD_END
  11. JPH_NAMESPACE_BEGIN
  12. /// Implementation of a JobSystem using a thread pool
  13. ///
  14. /// Note that this is considered an example implementation. It is expected that when you integrate
  15. /// the physics engine into your own project that you'll provide your own implementation of the
  16. /// JobSystem built on top of whatever job system your project uses.
  17. class JobSystemThreadPool final : public JobSystem
  18. {
  19. public:
  20. JPH_OVERRIDE_NEW_DELETE
  21. /// Creates a thread pool.
  22. /// @see JobSystemThreadPool::Init
  23. JobSystemThreadPool(uint inMaxJobs, uint inMaxBarriers, int inNumThreads = -1);
  24. JobSystemThreadPool() = default;
  25. virtual ~JobSystemThreadPool() override;
  26. /// Initialize the thread pool
  27. /// @param inMaxJobs Max number of jobs that can be allocated at any time
  28. /// @param inMaxBarriers Max number of barriers that can be allocated at any time
  29. /// @param inNumThreads Number of threads to start (the number of concurrent jobs is 1 more because the main thread will also run jobs while waiting for a barrier to complete). Use -1 to autodetect the amount of CPU's.
  30. void Init(uint inMaxJobs, uint inMaxBarriers, int inNumThreads = -1);
  31. // See JobSystem
  32. virtual int GetMaxConcurrency() const override { return int(mThreads.size()) + 1; }
  33. virtual JobHandle CreateJob(const char *inName, ColorArg inColor, const JobFunction &inJobFunction, uint32 inNumDependencies = 0) override;
  34. virtual Barrier * CreateBarrier() override;
  35. virtual void DestroyBarrier(Barrier *inBarrier) override;
  36. virtual void WaitForJobs(Barrier *inBarrier) override;
  37. /// Change the max concurrency after initialization
  38. void SetNumThreads(int inNumThreads) { StopThreads(); StartThreads(inNumThreads); }
  39. protected:
  40. // See JobSystem
  41. virtual void QueueJob(Job *inJob) override;
  42. virtual void QueueJobs(Job **inJobs, uint inNumJobs) override;
  43. virtual void FreeJob(Job *inJob) override;
  44. private:
  45. /// When we switch to C++20 we can use counting_semaphore to unify this
  46. class Semaphore
  47. {
  48. public:
  49. /// Constructor
  50. inline Semaphore();
  51. inline ~Semaphore();
  52. /// Release the semaphore, signalling the thread waiting on the barrier that there may be work
  53. inline void Release(uint inNumber = 1);
  54. /// Acquire the semaphore inNumber times
  55. inline void Acquire(uint inNumber = 1);
  56. /// Get the current value of the semaphore
  57. inline int GetValue() const { return mCount; }
  58. private:
  59. #ifdef JPH_PLATFORM_WINDOWS
  60. // On windows we use a semaphore object since it is more efficient than a lock and a condition variable
  61. alignas(JPH_CACHE_LINE_SIZE) atomic<int> mCount { 0 }; ///< We increment mCount for every release, to acquire we decrement the count. If the count is negative we know that we are waiting on the actual semaphore.
  62. void * mSemaphore; ///< The semaphore is an expensive construct so we only acquire/release it if we know that we need to wait/have waiting threads
  63. #else
  64. // Other platforms: Emulate a semaphore using a mutex, condition variable and count
  65. mutex mLock;
  66. condition_variable mWaitVariable;
  67. int mCount = 0;
  68. #endif
  69. };
  70. class BarrierImpl : public Barrier
  71. {
  72. public:
  73. JPH_OVERRIDE_NEW_DELETE
  74. /// Constructor
  75. BarrierImpl();
  76. virtual ~BarrierImpl() override;
  77. // See Barrier
  78. virtual void AddJob(const JobHandle &inJob) override;
  79. virtual void AddJobs(const JobHandle *inHandles, uint inNumHandles) override;
  80. /// Check if there are any jobs in the job barrier
  81. inline bool IsEmpty() const { return mJobReadIndex == mJobWriteIndex; }
  82. /// Wait for all jobs in this job barrier, while waiting, execute jobs that are part of this barrier on the current thread
  83. void Wait();
  84. /// Flag to indicate if a barrier has been handed out
  85. atomic<bool> mInUse { false };
  86. protected:
  87. /// Called by a Job to mark that it is finished
  88. virtual void OnJobFinished(Job *inJob) override;
  89. /// Jobs queue for the barrier
  90. static constexpr uint cMaxJobs = 2048;
  91. static_assert(IsPowerOf2(cMaxJobs)); // We do bit operations and require max jobs to be a power of 2
  92. atomic<Job *> mJobs[cMaxJobs]; ///< List of jobs that are part of this barrier, nullptrs for empty slots
  93. alignas(JPH_CACHE_LINE_SIZE) atomic<uint> mJobReadIndex { 0 }; ///< First job that could be valid (modulo cMaxJobs), can be nullptr if other thread is still working on adding the job
  94. alignas(JPH_CACHE_LINE_SIZE) atomic<uint> mJobWriteIndex { 0 }; ///< First job that can be written (modulo cMaxJobs)
  95. atomic<int> mNumToAcquire { 0 }; ///< Number of times the semaphore has been released, the barrier should acquire the semaphore this many times (written at the same time as mJobWriteIndex so ok to put in same cache line)
  96. Semaphore mSemaphore; ///< Semaphore used by finishing jobs to signal the barrier that they're done
  97. };
  98. /// Start/stop the worker threads
  99. void StartThreads(int inNumThreads);
  100. void StopThreads();
  101. /// Entry point for a thread
  102. void ThreadMain(int inThreadIndex);
  103. /// Get the head of the thread that has processed the least amount of jobs
  104. inline uint GetHead() const;
  105. /// Internal helper function to queue a job
  106. inline void QueueJobInternal(Job *inJob);
  107. /// Array of jobs (fixed size)
  108. using AvailableJobs = FixedSizeFreeList<Job>;
  109. AvailableJobs mJobs;
  110. /// Array of barriers (we keep them constructed all the time since constructing a semaphore/mutex is not cheap)
  111. uint mMaxBarriers = 0; ///< Max amount of barriers
  112. BarrierImpl * mBarriers = nullptr; ///< List of the actual barriers
  113. /// Threads running jobs
  114. Array<thread> mThreads;
  115. // The job queue
  116. static constexpr uint32 cQueueLength = 1024;
  117. static_assert(IsPowerOf2(cQueueLength)); // We do bit operations and require queue length to be a power of 2
  118. atomic<Job *> mQueue[cQueueLength];
  119. // Head and tail of the queue, do this value modulo cQueueLength - 1 to get the element in the mQueue array
  120. atomic<uint> * mHeads = nullptr; ///< Per executing thread the head of the current queue
  121. alignas(JPH_CACHE_LINE_SIZE) atomic<uint> mTail = 0; ///< Tail (write end) of the queue
  122. // Semaphore used to signal worker threads that there is new work
  123. Semaphore mSemaphore;
  124. /// Boolean to indicate that we want to stop the job system
  125. atomic<bool> mQuit = false;
  126. };
  127. JPH_NAMESPACE_END