internal void AgentLoop() { // int eIdx = _startIdx + _count; PathManager pm = _pfm._pathManagers[_pathIdx]; EAgentLoopStep eStep = _pfm._eCurrentAgentLoopStep; // for (int i = _startIdx; i < eIdx; i++) { { int aCount = pm._agentIdx.Count; for (int j = 0; j < aCount; j++) { pm._agentIdx[j].MultiThreadLoop(_pfm.DeltaTime, false, eStep); } } } }
public void OnDestroy() { #if !USING_DYNAMIC_TREE _dicQdTree.Clear(); _agents.Clear(); #else if (_dynamicTree != null) { _dynamicTree.Destroy(); } _dynamicTree = null; #endif TerminateThread(); // _destroyed = true; // _allAgents.Clear(); _hsSpecPos.Clear(); _flowManager = null; _allAgentCount = 0; // _mSIdx = 0; // _mEIdx = 0; _deltaTime = CurTime = 0; _pathManagers = null; // _taskEvents = null; if (_tasks != null) { int count = _tasks.Count; for (int i = 0; i < count; i++) { _tasks[i].terminate = true; } _tasks.Clear(); } _updateIdx = 0; _eCurrentAgentLoopStep = EAgentLoopStep.max; }
public void UpdateAgentsWithMultiThread(FP deltaTime) { int taskCount = _tasks.Count; //int agentCount = _allAgents.Count; //int perACount = agentCount / (taskCount + 1);//plus main thread //for (int i = 0; i < taskCount; i++) //{ // //int leftCount = agentCount - i * perACount; // _tasks[i].SetIdx(i * perACount, perACount); //} // _mSIdx = (taskCount) * perACount; // _mEIdx = agentCount - 1; // int pathMcount = taskCount + 1; for (int i = 0; i < pathMcount; i++) { _pathManagers[i].Reset(); } //--------------------------- int eCount = (int)EAgentLoopStep.max; for (int i = 0; i < eCount; i++) { _eCurrentAgentLoopStep = (EAgentLoopStep)i; // UnityEngine.Profiling.Profiler.BeginSample("Path finding update" +i); if (_eCurrentAgentLoopStep == EAgentLoopStep.checkAction // || _eCurrentAgentLoopStep == EAgentLoopStep.checkAvailabelRevs || _eCurrentAgentLoopStep == EAgentLoopStep.FinalMoveHandle // || _eCurrentAgentLoopStep == EAgentLoopStep.postFindPath ) { //main thread //for (int k = 0; k <= agentCount - 1; k++) for (int k = 0; k < pathMcount; k++) { int aCount = _pathManagers[k]._agentIdx.Count; for (int j = 0; j < aCount; j++) { _pathManagers[k]._agentIdx[j].MultiThreadLoop(deltaTime, false, _eCurrentAgentLoopStep); } } } else//multi thread:tasks +main thread { // _threadFinishedCount = 0; for (int j = 0; j < taskCount; j++) { _tasks[j].Execute(0); // ThreadPool.QueueUserWorkItem(_tasks[j].AgentLoop); } #if PATHMANAGER_DEBUG try { #endif PathManager pm = _pathManagers[pathMcount - 1]; int aCount = pm._agentIdx.Count; for (int j = 0; j < aCount; j++) { pm._agentIdx[j].MultiThreadLoop(deltaTime, false, _eCurrentAgentLoopStep); } #if PATHMANAGER_DEBUG } catch (Exception e) { #if UNITY_EDITOR UnityEngine.Debug.LogError(e.ToString() + e.StackTrace); #endif } #endif if (taskCount > 0) { // WaitHandle.WaitAll(_taskEvents); for (int j = 0; j < taskCount; j++) { _tasks[j]._finished.WaitOne(); } // CheckThread(taskCount); } } // UnityEngine.Profiling.Profiler.EndSample(); } // UnityEngine.Profiling.Profiler.BeginSample("_pathManagers loop"); // _threadFinishedCount = 0; for (int i = 0; i < pathMcount; i++) { if (i < taskCount) { _tasks[i].Execute(1); //ThreadPool.QueueUserWorkItem(_tasks[i].PathMangerUpdate); } else { _pathManagers[i].Loop(deltaTime); } } //if (taskCount > 0) //{ // WaitHandle.WaitAll(_taskEvents); //} if (taskCount > 0) { // WaitHandle.WaitAll(_taskEvents); for (int j = 0; j < taskCount; j++) { _tasks[j]._finished.WaitOne(); } // CheckThread(taskCount); } // UnityEngine.Profiling.Profiler.EndSample(); }