Browse Source

Added option to get a callback when a job system thread starts/stops

Jorrit Rouwe 1 year ago
parent
commit
417b4dba99
2 changed files with 15 additions and 0 deletions
  1. 6 0
      Jolt/Core/JobSystemThreadPool.cpp
  2. 9 0
      Jolt/Core/JobSystemThreadPool.h

+ 6 - 0
Jolt/Core/JobSystemThreadPool.cpp

@@ -318,6 +318,9 @@ void JobSystemThreadPool::ThreadMain(int inThreadIndex)
 
 	JPH_PROFILE_THREAD_START(name);
 
+	// Call the thread init function
+	mThreadInitFunction(inThreadIndex);
+
 	atomic<uint> &head = mHeads[inThreadIndex];
 
 	while (!mQuit)
@@ -348,6 +351,9 @@ void JobSystemThreadPool::ThreadMain(int inThreadIndex)
 		}
 	}
 
+	// Call the thread exit function
+	mThreadExitFunction(inThreadIndex);
+
 	JPH_PROFILE_THREAD_END();
 }
 

+ 9 - 0
Jolt/Core/JobSystemThreadPool.h

@@ -33,6 +33,11 @@ public:
 							JobSystemThreadPool() = default;
 	virtual					~JobSystemThreadPool() override;
 
+	/// Functions to call when a thread is initialized or exits, must be set before calling Init()
+	using InitExitFunction = function<void(int)>;
+	void					SetThreadInitFunction(const InitExitFunction &inInitFunction)	{ mThreadInitFunction = inInitFunction; }
+	void					SetThreadExitFunction(const InitExitFunction &inExitFunction)	{ mThreadExitFunction = inExitFunction; }
+
 	/// Initialize the thread pool
 	/// @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
@@ -66,6 +71,10 @@ private:
 	/// Internal helper function to queue a job
 	inline void				QueueJobInternal(Job *inJob);
 
+	/// Functions to call when initializing or exiting a thread
+	InitExitFunction		mThreadInitFunction = [](int) { };
+	InitExitFunction		mThreadExitFunction = [](int) { };
+
 	/// Array of jobs (fixed size)
 	using AvailableJobs = FixedSizeFreeList<Job>;
 	AvailableJobs			mJobs;