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
40 void Init(uint inMaxJobs, uint inMaxBarriers, int inNumThreads = -1);
41
42 // See JobSystem
43 virtual int GetMaxConcurrency() const override { return int(mThreads.size()) + 1; }
44 virtual JobHandle CreateJob(const char *inName, ColorArg inColor, const JobFunction &inJobFunction, uint32 inNumDependencies = 0) override;
45
47 void SetNumThreads(int inNumThreads) { StopThreads(); StartThreads(inNumThreads); }
48
49protected:
50 // See JobSystem
51 virtual void QueueJob(Job *inJob) override;
52 virtual void QueueJobs(Job **inJobs, uint inNumJobs) override;
53 virtual void FreeJob(Job *inJob) override;
54
55private:
57 void StartThreads(int inNumThreads);
58 void StopThreads();
59
61 void ThreadMain(int inThreadIndex);
62
64 inline uint GetHead() const;
65
67 inline void QueueJobInternal(Job *inJob);
68
70 using AvailableJobs = FixedSizeFreeList<Job>;
71 AvailableJobs mJobs;
72
74 Array<thread> mThreads;
75
76 // The job queue
77 static constexpr uint32 cQueueLength = 1024;
78 static_assert(IsPowerOf2(cQueueLength)); // We do bit operations and require queue length to be a power of 2
79 atomic<Job *> mQueue[cQueueLength];
80
81 // Head and tail of the queue, do this value modulo cQueueLength - 1 to get the element in the mQueue array
82 atomic<uint> * mHeads = nullptr;
83 alignas(JPH_CACHE_LINE_SIZE) atomic<uint> mTail = 0;
84
85 // Semaphore used to signal worker threads that there is new work
86 Semaphore mSemaphore;
87
89 atomic<bool> mQuit = false;
90};
91
#define JPH_EXPORT
Definition: Core.h:227
#define JPH_CACHE_LINE_SIZE
Definition: Core.h:466
#define JPH_SUPPRESS_WARNINGS_STD_BEGIN
Definition: Core.h:372
#define JPH_SUPPRESS_WARNINGS_STD_END
Definition: Core.h:384
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
std::uint32_t uint32
Definition: Core.h:442
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
constexpr bool IsPowerOf2(T inV)
Check if inV is a power of 2.
Definition: Math.h:73
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
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 SetNumThreads(int inNumThreads)
Change the max concurrency after initialization.
Definition: JobSystemThreadPool.h:47
JobSystemThreadPool()=default
virtual int GetMaxConcurrency() const override
Get maximum number of concurrently executing jobs.
Definition: JobSystemThreadPool.h:43
Definition: JobSystemWithBarrier.h:24
void Init(uint inMaxBarriers)
Definition: JobSystemWithBarrier.cpp:167
Definition: Semaphore.h:23