From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001
From: chai <chaifix@163.com>
Date: Wed, 14 Aug 2019 22:50:43 +0800
Subject: +Unity Runtime code

---
 Runtime/Threads/JobGroupRecycler.h | 59 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 Runtime/Threads/JobGroupRecycler.h

(limited to 'Runtime/Threads/JobGroupRecycler.h')

diff --git a/Runtime/Threads/JobGroupRecycler.h b/Runtime/Threads/JobGroupRecycler.h
new file mode 100644
index 0000000..9305582
--- /dev/null
+++ b/Runtime/Threads/JobGroupRecycler.h
@@ -0,0 +1,59 @@
+#ifndef JOB_GROUP_RECYCLER_H
+#define JOB_GROUP_RECYCLER_H
+
+#include "JobScheduler.h"
+
+#if ENABLE_MULTITHREADED_CODE
+
+template<int MaxGroups>
+class JobGroupRecycler
+{
+public:
+	// Limits the amount of groups used by finishing older ones first.
+	// Restrictions: Must always be called from the same thread.
+	// All jobs must be submitted before beginning a new group.
+
+	JobGroupRecycler()
+	{
+		memset( m_SlotOwners, 0, sizeof(m_SlotOwners) );
+		memset( m_JobGroups, 0, sizeof(m_JobGroups) );
+		m_NextSlot = 0;
+	}
+
+	int BeginGroup( void* owner, int maxJobs )
+	{
+		int slot = m_NextSlot;
+		if( m_SlotOwners[slot] )
+			GetJobScheduler().WaitForGroup( m_JobGroups[slot] );
+		m_JobGroups[slot] = GetJobScheduler().BeginGroup( maxJobs );
+		m_SlotOwners[slot] = owner;
+		m_NextSlot = (m_NextSlot + 1) % MaxGroups;
+		return slot;
+	}
+
+	bool SubmitJob( void* owner, int slot, JobScheduler::JobFunction func, void *data, JobScheduler::ReturnCode *returnCode )
+	{
+		Assert( slot >= 0 && slot < MaxGroups );
+		Assert( m_SlotOwners[slot] == owner );
+		return GetJobScheduler().SubmitJob( m_JobGroups[slot], func, data, returnCode );
+	}
+
+	void WaitForGroup( void* owner, int slot )
+	{
+		// Maybe someone else reused the job group
+		if( m_SlotOwners[slot] != owner )
+			return;
+		GetJobScheduler().WaitForGroup( m_JobGroups[slot] );
+		m_SlotOwners[slot] = NULL;
+	}
+
+private:
+	JobScheduler::JobGroupID m_JobGroups[MaxGroups];
+	void* m_SlotOwners[MaxGroups];
+	int m_NextSlot;
+};
+
+
+#endif // ENABLE_MULTITHREADED_CODE
+
+#endif
-- 
cgit v1.1-26-g67d0