2
0
Эх сурвалжийг харах

Added Init() function to JobSystemThreadPool to allow initialization after construction (#140)

Jorrit Rouwe 3 жил өмнө
parent
commit
d30189ad22

+ 12 - 3
Jolt/Core/JobSystemThreadPool.cpp

@@ -228,10 +228,14 @@ void JobSystemThreadPool::BarrierImpl::Wait()
 	}
 	}
 }
 }
 
 
-JobSystemThreadPool::JobSystemThreadPool(uint inMaxJobs, uint inMaxBarriers, int inNumThreads) :
-	mMaxBarriers(inMaxBarriers),
-	mBarriers(new BarrierImpl [inMaxBarriers]) // Init freelist of barriers
+void JobSystemThreadPool::Init(uint inMaxJobs, uint inMaxBarriers, int inNumThreads)
 {
 {
+	JPH_ASSERT(mBarriers == nullptr); // Already initialized?
+
+	// Init freelist of barriers
+	mMaxBarriers = inMaxBarriers;
+	mBarriers = new BarrierImpl [inMaxBarriers];
+
 	// Init freelist of jobs
 	// Init freelist of jobs
 	mJobs.Init(inMaxJobs, inMaxJobs);
 	mJobs.Init(inMaxJobs, inMaxJobs);
 
 
@@ -243,6 +247,11 @@ JobSystemThreadPool::JobSystemThreadPool(uint inMaxJobs, uint inMaxBarriers, int
 	StartThreads(inNumThreads);
 	StartThreads(inNumThreads);
 }
 }
 
 
+JobSystemThreadPool::JobSystemThreadPool(uint inMaxJobs, uint inMaxBarriers, int inNumThreads)
+{
+	Init(inMaxJobs, inMaxBarriers, inNumThreads);
+}
+
 void JobSystemThreadPool::StartThreads(int inNumThreads)
 void JobSystemThreadPool::StartThreads(int inNumThreads)
 {
 {
 	// Auto detect number of threads
 	// Auto detect number of threads

+ 9 - 4
Jolt/Core/JobSystemThreadPool.h

@@ -23,11 +23,16 @@ class JobSystemThreadPool final : public JobSystem
 {
 {
 public:
 public:
 	/// Creates a thread pool.
 	/// Creates a thread pool.
+	/// @see JobSystemThreadPool::Init
+							JobSystemThreadPool(uint inMaxJobs, uint inMaxBarriers, int inNumThreads = -1);
+							JobSystemThreadPool() = default;
+	virtual					~JobSystemThreadPool() override;
+
+	/// Initialize the thread pool
 	/// @param inMaxJobs Max number of jobs that can be allocated at any time
 	/// @param inMaxJobs Max number of jobs that can be allocated at any time
 	/// @param inMaxBarriers Max number of barriers that can be allocated at any time
 	/// @param inMaxBarriers Max number of barriers that can be allocated at any time
 	/// @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.
 	/// @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.
-							JobSystemThreadPool(uint inMaxJobs, uint inMaxBarriers, int inNumThreads = -1);
-	virtual					~JobSystemThreadPool() override;
+	void					Init(uint inMaxJobs, uint inMaxBarriers, int inNumThreads = -1);
 
 
 	// See JobSystem
 	// See JobSystem
 	virtual int				GetMaxConcurrency() const override				{ return int(mThreads.size()) + 1; }
 	virtual int				GetMaxConcurrency() const override				{ return int(mThreads.size()) + 1; }
@@ -128,8 +133,8 @@ private:
 	AvailableJobs			mJobs;
 	AvailableJobs			mJobs;
 
 
 	/// Array of barriers (we keep them constructed all the time since constructing a semaphore/mutex is not cheap)
 	/// Array of barriers (we keep them constructed all the time since constructing a semaphore/mutex is not cheap)
-	uint					mMaxBarriers;									///< Max amount of barriers
-	BarrierImpl *			mBarriers;										///< List of the actual barriers
+	uint					mMaxBarriers = 0;								///< Max amount of barriers
+	BarrierImpl *			mBarriers = nullptr;							///< List of the actual barriers
 
 
 	/// Threads running jobs
 	/// Threads running jobs
 	vector<thread>			mThreads;
 	vector<thread>			mThreads;