// Called when the thread path request is completed and the movement path is drawn public void FinishedProcessingThreadPath(PathResultData pathResult) { // Get back on main thread before callback - Reach them from update which runs on main thread lock (_threadResults) // Thread safety - lock for preventing reaching this method at the same time with concurrent threads { _threadResults.Enqueue(pathResult); } }
private IEnumerator CheckForJobCompletionEnumerator() { while (true) { bool tryToResolveQueue = false; for (var i = runningQueriesJobHandle.Count - 1; i >= 0; i--) { JobHandle jobHandle = runningQueriesJobHandle[i]; if (!jobHandle.IsCompleted) { continue; } jobHandle.Complete(); PathQueryJob pathQueryJob = runningJobs[i]; PathQueryData queryData = GetPathQueryById(pathQueryJob.QueryID); //cannot find the query by the path ID if (queryData.QueryID == -1) { continue; } PathResultData pathResult = new PathResultData( (PathQueryResult)pathQueryJob.PathResult[0], queryData.StartPosition, queryData.EndPosition, pathQueryJob.ResultPath.ToArray()); pathQueryJob.ResultPath.Dispose(); pathQueryJob.PathResult.Dispose(); runningQueriesJobHandle.RemoveAtSwapBack(i); runningJobs.RemoveAtSwapBack(i); runningQueries.RemoveAtSwapBack(i); gridNodesPool.Enqueue(pathQueryJob.Grid.ToArray()); queryIDs.Add(pathQueryJob.QueryID); queryData.Callback?.Invoke(pathResult); tryToResolveQueue = true; } if (tryToResolveQueue) { ResolveQueue(); } yield return(null); } }
private void Update() { // Check whether a path calculation on a thread is finished. If it is, perform the callback lock (_threadResults) { if (_threadResults.Count > 0) { _itemsInQueue = _threadResults.Count; lock (_threadResults) { for (int i = 0; i < _itemsInQueue; i++) { PathResultData newResult = _threadResults.Dequeue(); newResult.CallbackPathResult(newResult.PathResult, newResult.PathNodes, newResult.SuccessResult); } } } } }