Exemplo n.º 1
0
        public void asyncRequest(AsyncPathRequest request)
        {
            // Get the worker
            WorkerThread thread = FindSuitableWorker();

            // Push the request
            thread.AsyncRequest(request);
        }
Exemplo n.º 2
0
 public void AsyncRequest(AsyncPathRequest request)
 {
     // Lock the queue
     lock (incoming)
     {
         // Push the request
         incoming.Enqueue(request);
     }
 }
Exemplo n.º 3
0
        public void FindPath(Index start, Index end, DiagonalMode diagonal, BaseTraversal2D traversal2D, PathRequestDelegate callback)
        {
            if (!IsValid(start) || !IsValid(end))
            {
                return;
            }
            searchGrid.maxPathLength = maxPathLength;
            bool useThreading = allowThreading;

            if (useThreading == true)
            {
                AsyncPathRequest request = new AsyncPathRequest(searchGrid, start, end, diagonal, traversal2D, (Path path, PathRequestStatus status) =>
                {
                    PathView.setRenderPath(this, path);
                    callback(path, status);
                });
                ThreadManager.Active.asyncRequest(request);
            }
            else
            {
                PathRequestStatus status;
                Path result = FindPathImmediate(start, end, out status, diagonal);
                PathView.setRenderPath(this, result);
                callback(result, status);
            }

            Path FindPathImmediate(Index subStart, Index subEnd, out PathRequestStatus subStatus, DiagonalMode subDiagonal)
            {
                searchGrid.maxPathLength = maxPathLength;
                Path path = null;
                PathRequestStatus temp = PathRequestStatus.InvalidIndex;

                searchGrid.FindPath(subStart, subEnd, subDiagonal, traversal2D, (Path result, PathRequestStatus resultStatus) =>
                {
                    temp = resultStatus;
                    if (resultStatus == PathRequestStatus.PathFound)
                    {
                        path = result;
                        PathView.setRenderPath(this, path);
                    }
                });

                subStatus = temp;
                return(path);
            }
        }
Exemplo n.º 4
0
        private void ThreadMain()
        {
            // Set the flag
            isRunning = true;

            // Used to calcualte the average
            Stopwatch timer = new Stopwatch();

            try
            {
                // Loop forever
                while (isRunning == true)
                {
                    // Get a request
                    AsyncPathRequest request = null;

                    // Lock the input queue
                    lock (incoming)
                    {
                        // Calcualte the current thread load
                        CalcualteLoad();

                        // Get a request
                        if (incoming.Count > 0)
                        {
                            request = incoming.Dequeue();
                        }
                    }

                    // Check for a request
                    if (request == null)
                    {
                        idleFrames++;

                        // Take a long sleep - no load
                        Thread.Sleep((int)targetTime);
                        continue;
                    }

                    // Reset the idle frames
                    idleFrames = 0;

                    // Begin timing
                    timer.Reset();
                    timer.Start();

                    // Lock the grid while we search
                    lock (request.Grid)
                    {
                        // Execute the request
                        request.Grid.FindPath(request.Start, request.End, request.Diagonal, request.Traversal2D, (Path path, PathRequestStatus status) =>
                        {
                            // Create a result
                            AsyncPathResult result = new AsyncPathResult(request, path, status);

                            // Push the result to the outgoing queue
                            lock (outgoing)
                            {
                                // Add result
                                outgoing.Enqueue(result);
                            }
                        });
                    }

                    // Stop timing and calculate the average time
                    timer.Stop();
                    CalculateAverageTime(timer.ElapsedMilliseconds);

                    // Calculate the amount of rest time based on the thread load
                    int sleepDuration = (int)((1 - threadLoad) * targetTime);

                    // Sleep based on the current thread load
                    Thread.Sleep(sleepDuration);
                } // End while
            }
            catch (System.Exception e)
            {
                UnityEngine.Debug.Log(e);
            }
        }