JobSystemThreadPool.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/JobSystemWithBarrier.h>
  6. #include <Jolt/Core/FixedSizeFreeList.h>
  7. #include <Jolt/Core/Semaphore.h>
  8. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  9. #include <thread>
  10. JPH_SUPPRESS_WARNINGS_STD_END
  11. JPH_NAMESPACE_BEGIN
  12. // Things we're using from STL
  13. using std::thread;
  14. /// Implementation of a JobSystem using a thread pool
  15. ///
  16. /// Note that this is considered an example implementation. It is expected that when you integrate
  17. /// the physics engine into your own project that you'll provide your own implementation of the
  18. /// JobSystem built on top of whatever job system your project uses.
  19. class JPH_EXPORT JobSystemThreadPool final : public JobSystemWithBarrier
  20. {
  21. public:
  22. JPH_OVERRIDE_NEW_DELETE
  23. /// Creates a thread pool.
  24. /// @see JobSystemThreadPool::Init
  25. JobSystemThreadPool(uint inMaxJobs, uint inMaxBarriers, int inNumThreads = -1);
  26. JobSystemThreadPool() = default;
  27. virtual ~JobSystemThreadPool() override;
  28. /// Initialize the thread pool
  29. /// @param inMaxJobs Max number of jobs that can be allocated at any time
  30. /// @param inMaxBarriers Max number of barriers that can be allocated at any time
  31. /// @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.
  32. void Init(uint inMaxJobs, uint inMaxBarriers, int inNumThreads = -1);
  33. // See JobSystem
  34. virtual int GetMaxConcurrency() const override { return int(mThreads.size()) + 1; }
  35. virtual JobHandle CreateJob(const char *inName, ColorArg inColor, const JobFunction &inJobFunction, uint32 inNumDependencies = 0) override;
  36. /// Change the max concurrency after initialization
  37. void SetNumThreads(int inNumThreads) { StopThreads(); StartThreads(inNumThreads); }
  38. protected:
  39. // See JobSystem
  40. virtual void QueueJob(Job *inJob) override;
  41. virtual void QueueJobs(Job **inJobs, uint inNumJobs) override;
  42. virtual void FreeJob(Job *inJob) override;
  43. private:
  44. /// Start/stop the worker threads
  45. void StartThreads(int inNumThreads);
  46. void StopThreads();
  47. /// Entry point for a thread
  48. void ThreadMain(int inThreadIndex);
  49. /// Get the head of the thread that has processed the least amount of jobs
  50. inline uint GetHead() const;
  51. /// Internal helper function to queue a job
  52. inline void QueueJobInternal(Job *inJob);
  53. /// Array of jobs (fixed size)
  54. using AvailableJobs = FixedSizeFreeList<Job>;
  55. AvailableJobs mJobs;
  56. /// Threads running jobs
  57. Array<thread> mThreads;
  58. // The job queue
  59. static constexpr uint32 cQueueLength = 1024;
  60. static_assert(IsPowerOf2(cQueueLength)); // We do bit operations and require queue length to be a power of 2
  61. atomic<Job *> mQueue[cQueueLength];
  62. // Head and tail of the queue, do this value modulo cQueueLength - 1 to get the element in the mQueue array
  63. atomic<uint> * mHeads = nullptr; ///< Per executing thread the head of the current queue
  64. alignas(JPH_CACHE_LINE_SIZE) atomic<uint> mTail = 0; ///< Tail (write end) of the queue
  65. // Semaphore used to signal worker threads that there is new work
  66. Semaphore mSemaphore;
  67. /// Boolean to indicate that we want to stop the job system
  68. atomic<bool> mQuit = false;
  69. };
  70. JPH_NAMESPACE_END