Exemplo n.º 1
0
        /// <summary>
        /// Runs a search thread for parallel DFS.
        /// (here for potential multi-threading support in the future)
        /// </summary>
        /// <param name="end">goal method</param>
        /// <param name="proctor">the stack "proctor" for all threads</param>
        /// <param name="threadId">the ID for this thread (for use with proctor)</param>
        /// <param name="cancellationToken">the cancellation token</param>
        private void RunSearchThread(Method end,
                                     StackProctor proctor,
                                     int threadId,
                                     CancellationToken cancellationToken)
        {
            var stack = proctor.GetStack(threadId);

            proctor.SetStackWaiting(threadId);

            var shouldTerminate = false;

            while (!shouldTerminate)
            {
                if (stack.Count == 0)
                {
                    foreach (var workItem in proctor.PullWork())
                    {
                        stack.Push(workItem);
                    }
                }

                while (stack.Count != 0)
                {
                    proctor.SetStackRunning(threadId);
                    DepthFirstSearch(end, stack, cancellationToken);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        AppLogger.Current.Debug(
                            "Thread with ID: {0} has had its operation cancelled. Terminating.",
                            threadId);

                        cancellationToken.ThrowIfCancellationRequested();

                        return;
                    }

                    proctor.SetStackWaiting(threadId);
                    foreach (var workItem in proctor.PullWork())
                    {
                        stack.Push(workItem);
                    }
                }

                shouldTerminate = proctor.IsCompleted;
                Thread.Sleep(50);
            }

            AppLogger.Current.Debug(
                "Terminating thread with ID: {0}",
                threadId);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructs the partial paths which forms the sub-graph of solution nodes
        /// </summary>
        /// <param name="start">starting method</param>
        /// <param name="end">ending method</param>
        /// <param name="cancellationToken">cancellation token</param>
        /// <returns>void Task</returns>
        public async Task ConstructPartialPaths(Method start,
                                                Method end,
                                                CancellationToken cancellationToken)
        {
            this.partialPaths     = new List <CodePath>();
            this.visited          = new HashSet <Method>();
            this.pathSolutionMap  = new Dictionary <Method, List <CodePath> >();
            this.solutionMethods  = new HashSet <Method>();
            this.completedMethods = new HashSet <Method>();

            // left for potential future multi-threading support
            var proctor = new StackProctor(1);
            var stack   = proctor.GetStack(0);

            stack.Push(CodePath.FromSingleMethod(start));

            await Task.Run(() => RunSearchThread(end, proctor, 0, cancellationToken));
        }