Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
JobSystemThreadPool.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
10
12#include <thread>
14
16
17// Things we're using from STL
18using std::thread;
19
26{
27public:
29
32 JobSystemThreadPool(uint inMaxJobs, uint inMaxBarriers, int inNumThreads = -1);
34 virtual ~JobSystemThreadPool() override;
35
37 using InitExitFunction = function<void(int)>;
38 void SetThreadInitFunction(const InitExitFunction &inInitFunction) { mThreadInitFunction = inInitFunction; }
39 void SetThreadExitFunction(const InitExitFunction &inExitFunction) { mThreadExitFunction = inExitFunction; }
40
45 void Init(uint inMaxJobs, uint inMaxBarriers, int inNumThreads = -1);
46
47 // See JobSystem
48 virtual int GetMaxConcurrency() const override { return int(mThreads.size()) + 1; }
49 virtual JobHandle CreateJob(const char *inName, ColorArg inColor, const JobFunction &inJobFunction, uint32 inNumDependencies = 0) override;
50
52 void SetNumThreads(int inNumThreads) { StopThreads(); StartThreads(inNumThreads); }
53
54protected:
55 // See JobSystem
56 virtual void QueueJob(Job *inJob) override;
57 virtual void QueueJobs(Job **inJobs, uint inNumJobs) override;
58 virtual void FreeJob(Job *inJob) override;
59
60private:
62 void StartThreads(int inNumThreads);
63 void StopThreads();
64
66 void ThreadMain(int inThreadIndex);
67
69 inline uint GetHead() const;
70
72 inline void QueueJobInternal(Job *inJob);
73
75 InitExitFunction mThreadInitFunction = [](int) { };
76 InitExitFunction mThreadExitFunction = [](int) { };
77
79 using AvailableJobs = FixedSizeFreeList<Job>;
80 AvailableJobs mJobs;
81
83 Array<thread> mThreads;
84
85 // The job queue
86 static constexpr uint32 cQueueLength = 1024;
87 static_assert(IsPowerOf2(cQueueLength)); // We do bit operations and require queue length to be a power of 2
88 atomic<Job *> mQueue[cQueueLength];
89
90 // Head and tail of the queue, do this value modulo cQueueLength - 1 to get the element in the mQueue array
91 atomic<uint> * mHeads = nullptr;
92 alignas(JPH_CACHE_LINE_SIZE) atomic<uint> mTail = 0;
93
94 // Semaphore used to signal worker threads that there is new work
95 Semaphore mSemaphore;
96
98 atomic<bool> mQuit = false;
99};
100
#define JPH_EXPORT
Definition Core.h:271
#define JPH_CACHE_LINE_SIZE
Definition Core.h:521
#define JPH_SUPPRESS_WARNINGS_STD_BEGIN
Definition Core.h:419
#define JPH_SUPPRESS_WARNINGS_STD_END
Definition Core.h:431
unsigned int uint
Definition Core.h:481
#define JPH_NAMESPACE_END
Definition Core.h:414
std::uint32_t uint32
Definition Core.h:484
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
constexpr bool IsPowerOf2(T inV)
Check if inV is a power of 2.
Definition Math.h:76
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition Memory.h:31
Definition Array.h:36
Class that holds an RGBA color with 8-bits per component.
Definition Color.h:16
Definition FixedSizeFreeList.h:18
Definition JobSystem.h:80
virtual JobHandle CreateJob(const char *inName, ColorArg inColor, const JobFunction &inJobFunction, uint32 inNumDependencies=0)=0
virtual void QueueJobs(Job **inJobs, uint inNumJobs)=0
Adds a number of jobs at once to the job queue.
virtual void FreeJob(Job *inJob)=0
Frees a job.
virtual void QueueJob(Job *inJob)=0
Adds a job to the job queue.
Definition JobSystemThreadPool.h:26
void SetThreadExitFunction(const InitExitFunction &inExitFunction)
Definition JobSystemThreadPool.h:39
function< void(int)> InitExitFunction
Functions to call when a thread is initialized or exits, must be set before calling Init()
Definition JobSystemThreadPool.h:37
void SetNumThreads(int inNumThreads)
Change the max concurrency after initialization.
Definition JobSystemThreadPool.h:52
void SetThreadInitFunction(const InitExitFunction &inInitFunction)
Definition JobSystemThreadPool.h:38
JobSystemThreadPool()=default
virtual int GetMaxConcurrency() const override
Get maximum number of concurrently executing jobs.
Definition JobSystemThreadPool.h:48
Definition JobSystemWithBarrier.h:24
void Init(uint inMaxBarriers)
Definition JobSystemWithBarrier.cpp:170
Definition Semaphore.h:23