Esempio n. 1
0
        public void Stop(CancellationToken cancellationToken = default, bool immediate = false)
        {
            _logger.Info(() => "Stopping background task host");

            var options = new ParallelOptions
            {
                CancellationToken = cancellationToken, MaxDegreeOfParallelism = ResolveConcurrency()
            };

            var pendingTasks = _pending.Where(entry => entry.Value.OnHalt != null);

            Parallel.ForEach(pendingTasks, options, async e =>
            {
                // FIXME: halt operation won't have user data in this context
                using var context = ProvisionExecutionContext(cancellationToken, null);

                // ReSharper disable once AccessToDisposedClosure (this is safe: immediately invoked and blocking)
                await e.Value.OnHalt.HaltAsync(context, immediate);
            });

            _pending.Clear();

            _scheduler?.Dispose();
            _scheduler = null;

            _background.Stop(immediate);
            _maintenance.Stop(immediate);
        }
Esempio n. 2
0
        public void Stop(bool immediate, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            _logger.LogInformation(_localizer.GetString("Stopping operations host"));

            _scheduler?.Dispose();
            _scheduler = null;

            _background?.Stop(immediate);
            _maintenance?.Stop(immediate);
        }
Esempio n. 3
0
 public void Stop()
 {
     if (_queue != null)
     {
         _queue.Dispose();
         _queue = null;
     }
     if (_background != null)
     {
         _background.Dispose();
         _background = null;
     }
 }
Esempio n. 4
0
        static void _mainTaskSheduler_QueueIsEmpty(object sender, EventArgs e)
        {
            QueuedTaskScheduler deprecatedScheduler = (QueuedTaskScheduler)sender;

            deprecatedScheduler.QueueIsEmpty -= _mainTaskSheduler_QueueIsEmpty;
            deprecatedScheduler.Dispose();
        }
Esempio n. 5
0
        public void Stop(bool immediate = false)
        {
            Parallel.ForEach(_pending.Where(entry => entry.Value.OnHalt != null), e =>
            {
                e.Value.OnHalt.Halt(immediate);
            });

            _pending.Clear();

            if (_scheduler != null)
            {
                _scheduler.Dispose();
                _scheduler = null;
            }

            Background.Stop(immediate);
            Maintenance.Stop(immediate);
        }
Esempio n. 6
0
 public async Task Shutdown()
 {
     cancellationTokenSource.Cancel();
     try
     {
         taskScheduler.Dispose();
         await task.ConfigureAwait(false);
     }
     catch (OperationCanceledException)
     {
     }
 }
Esempio n. 7
0
 public static void Dispose()
 {
     //Before disposing, Have to stop working thread on them
     if (_mainTaskSheduler != null)
     {
         _mainTaskSheduler.Dispose();
     }
     if (_monoConcurrencyTaskSheduler != null)
     {
         _monoConcurrencyTaskSheduler.Dispose();
     }
 }
Esempio n. 8
0
        protected void parseSources(ParserFactory factory, IEnumerable <InputDescriptor> sources)
        {
            Stopwatch startTime = Stopwatch.StartNew();

            Thread.VolatileWrite(ref tokenCount, 0);
            int sourceCount = 0;
            int inputSize   = 0;

#if NET40PLUS
            BlockingCollection <int> threadIdentifiers = new BlockingCollection <int>();
            for (int i = 0; i < NUMBER_OF_THREADS; i++)
            {
                threadIdentifiers.Add(i);
            }

            ICollection <Task <int> > results             = new List <Task <int> >();
            QueuedTaskScheduler       executorServiceHost = new QueuedTaskScheduler(NUMBER_OF_THREADS);
            TaskScheduler             executorService     = executorServiceHost.ActivateNewQueue();
#else
            ICollection <Func <int> > results = new List <Func <int> >();
#endif
            foreach (InputDescriptor inputDescriptor in sources)
            {
                ICharStream input = inputDescriptor.GetInputStream();
                sourceCount++;
                input.Seek(0);
                inputSize += input.Size;
#if NET40PLUS
                Task <int> futureChecksum = Task.Factory.StartNew <int>(new Callable_1(input, factory, threadIdentifiers).call, CancellationToken.None, TaskCreationOptions.None, executorService);
#else
                Func <int> futureChecksum = new Callable_1(input, factory).call;
#endif
                results.Add(futureChecksum);
            }

            Checksum checksum = new CRC32();
            foreach (var future in results)
            {
#if NET40PLUS
                int value = future.Result;
#else
                int value = future();
#endif
                if (COMPUTE_CHECKSUM)
                {
                    updateChecksum(checksum, value);
                }
            }

#if NET40PLUS
            executorServiceHost.Dispose();
#endif

            Console.Out.WriteLine("Total parse time for {0} files ({1} KB, {2} tokens, checksum 0x{3:X8}): {4}ms",
                                  sourceCount,
                                  inputSize / 1024,
                                  Thread.VolatileRead(ref tokenCount),
                                  COMPUTE_CHECKSUM ? checksum.Value : 0,
                                  startTime.ElapsedMilliseconds);

            if (sharedLexers.Length > 0)
            {
                Lexer             lexer            = sharedLexers[0];
                LexerATNSimulator lexerInterpreter = lexer.Interpreter;
                DFA[]             modeToDFA        = lexerInterpreter.atn.modeToDFA;
                if (SHOW_DFA_STATE_STATS)
                {
                    int states  = 0;
                    int configs = 0;
                    HashSet <ATNConfig> uniqueConfigs = new HashSet <ATNConfig>();

                    for (int i = 0; i < modeToDFA.Length; i++)
                    {
                        DFA dfa = modeToDFA[i];
                        if (dfa == null || dfa.states == null)
                        {
                            continue;
                        }

                        states += dfa.states.Count;
                        foreach (DFAState state in dfa.states.Values)
                        {
                            configs += state.configs.Count;
                            uniqueConfigs.UnionWith(state.configs);
                        }
                    }

                    Console.Out.WriteLine("There are {0} lexer DFAState instances, {1} configs ({2} unique), {3} prediction contexts.", states, configs, uniqueConfigs.Count, lexerInterpreter.atn.ContextCacheSize);
                }
            }

            if (RUN_PARSER && sharedParsers.Length > 0)
            {
                Parser parser = sharedParsers[0];
                // make sure the individual DFAState objects actually have unique ATNConfig arrays
                ParserATNSimulator interpreter = parser.Interpreter;
                DFA[] decisionToDFA            = interpreter.atn.decisionToDFA;

                if (SHOW_DFA_STATE_STATS)
                {
                    int states  = 0;
                    int configs = 0;
                    HashSet <ATNConfig> uniqueConfigs = new HashSet <ATNConfig>();

                    for (int i = 0; i < decisionToDFA.Length; i++)
                    {
                        DFA dfa = decisionToDFA[i];
                        if (dfa == null || dfa.states == null)
                        {
                            continue;
                        }

                        states += dfa.states.Count;
                        foreach (DFAState state in dfa.states.Values)
                        {
                            configs += state.configs.Count;
                            uniqueConfigs.UnionWith(state.configs);
                        }
                    }

                    Console.Out.WriteLine("There are {0} parser DFAState instances, {1} configs ({2} unique), {3} prediction contexts.", states, configs, uniqueConfigs.Count, interpreter.atn.ContextCacheSize);
                }

                int   localDfaCount      = 0;
                int   globalDfaCount     = 0;
                int   localConfigCount   = 0;
                int   globalConfigCount  = 0;
                int[] contextsInDFAState = new int[0];

                for (int i = 0; i < decisionToDFA.Length; i++)
                {
                    DFA dfa = decisionToDFA[i];
                    if (dfa == null || dfa.states == null)
                    {
                        continue;
                    }

                    if (SHOW_CONFIG_STATS)
                    {
                        foreach (DFAState state in dfa.states.Keys)
                        {
                            if (state.configs.Count >= contextsInDFAState.Length)
                            {
                                Array.Resize(ref contextsInDFAState, state.configs.Count + 1);
                            }

                            if (state.IsAcceptState)
                            {
                                bool hasGlobal = false;
                                foreach (ATNConfig config in state.configs)
                                {
                                    if (config.ReachesIntoOuterContext)
                                    {
                                        globalConfigCount++;
                                        hasGlobal = true;
                                    }
                                    else
                                    {
                                        localConfigCount++;
                                    }
                                }

                                if (hasGlobal)
                                {
                                    globalDfaCount++;
                                }
                                else
                                {
                                    localDfaCount++;
                                }
                            }

                            contextsInDFAState[state.configs.Count]++;
                        }
                    }

                    if (EXPORT_LARGEST_CONFIG_CONTEXTS)
                    {
                        foreach (DFAState state in dfa.states.Keys)
                        {
                            foreach (ATNConfig config in state.configs)
                            {
                                string configOutput = config.ToDotString();
                                if (configOutput.Length <= configOutputSize)
                                {
                                    continue;
                                }

                                configOutputSize = configOutput.Length;
                                writeFile(tmpdir, "d" + dfa.decision + ".s" + state.stateNumber + ".a" + config.Alt + ".config.dot", configOutput);
                            }
                        }
                    }
                }

                if (SHOW_CONFIG_STATS && currentPass == 0)
                {
                    Console.Out.WriteLine("  DFA accept states: {0} total, {1} with only local context, {2} with a global context", localDfaCount + globalDfaCount, localDfaCount, globalDfaCount);
                    Console.Out.WriteLine("  Config stats: {0} total, {1} local, {2} global", localConfigCount + globalConfigCount, localConfigCount, globalConfigCount);
                    if (SHOW_DFA_STATE_STATS)
                    {
                        for (int i = 0; i < contextsInDFAState.Length; i++)
                        {
                            if (contextsInDFAState[i] != 0)
                            {
                                Console.Out.WriteLine("  {0} configs = {1}", i, contextsInDFAState[i]);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 9
0
 public void Dispose()
 {
     m_QueuedTaskScheduler.Dispose();
 }
Esempio n. 10
0
        protected void parseSources(ParserFactory factory, IEnumerable<ICharStream> sources)
        {
            Stopwatch startTime = Stopwatch.StartNew();
            Thread.VolatileWrite(ref tokenCount, 0);
            int sourceCount = 0;
            int inputSize = 0;

#if NET_4_0
            BlockingCollection<int> threadIdentifiers = new BlockingCollection<int>();
            for (int i = 0; i < NUMBER_OF_THREADS; i++)
                threadIdentifiers.Add(i);

            ICollection<Task<int>> results = new List<Task<int>>();
            QueuedTaskScheduler executorServiceHost = new QueuedTaskScheduler(NUMBER_OF_THREADS);
            TaskScheduler executorService = executorServiceHost.ActivateNewQueue();
#else
            ICollection<Func<int>> results = new List<Func<int>>();
#endif
            foreach (ICharStream input in sources)
            {
                sourceCount++;
                input.Seek(0);
                inputSize += input.Size;
#if NET_4_0
                Task<int> futureChecksum = Task.Factory.StartNew<int>(new Callable_1(input, factory, threadIdentifiers).call, CancellationToken.None, TaskCreationOptions.None, executorService);
#else
                Func<int> futureChecksum = new Callable_1(input, factory).call;
#endif
                results.Add(futureChecksum);
            }

            Checksum checksum = new CRC32();
            foreach (var future in results)
            {
#if NET_4_0
                int value = future.Result;
#else
                int value = future();
#endif
                if (COMPUTE_CHECKSUM)
                {
                    updateChecksum(checksum, value);
                }
            }

#if NET_4_0
            executorServiceHost.Dispose();
#endif

            Console.Out.WriteLine("Total parse time for {0} files ({1} KB, {2} tokens, checksum 0x{3:X8}): {4}ms",
                              sourceCount,
                              inputSize / 1024,
                              Thread.VolatileRead(ref tokenCount),
                              COMPUTE_CHECKSUM ? checksum.Value : 0,
                              startTime.ElapsedMilliseconds);

            if (sharedLexers.Length > 0)
            {
                Lexer lexer = sharedLexers[0];
                LexerATNSimulator lexerInterpreter = lexer.Interpreter;
                DFA[] modeToDFA = lexerInterpreter.atn.modeToDFA;
                if (SHOW_DFA_STATE_STATS)
                {
                    int states = 0;
                    int configs = 0;
                    HashSet<ATNConfig> uniqueConfigs = new HashSet<ATNConfig>();

                    for (int i = 0; i < modeToDFA.Length; i++)
                    {
                        DFA dfa = modeToDFA[i];
                        if (dfa == null || dfa.states == null)
                        {
                            continue;
                        }

                        states += dfa.states.Count;
                        foreach (DFAState state in dfa.states.Values)
                        {
                            configs += state.configs.Count;
                            uniqueConfigs.UnionWith(state.configs);
                        }
                    }

                    Console.Out.WriteLine("There are {0} lexer DFAState instances, {1} configs ({2} unique), {3} prediction contexts.", states, configs, uniqueConfigs.Count, lexerInterpreter.atn.GetContextCacheSize());
                }
            }

            if (RUN_PARSER && sharedParsers.Length > 0)
            {
                Parser parser = sharedParsers[0];
                // make sure the individual DFAState objects actually have unique ATNConfig arrays
                ParserATNSimulator interpreter = parser.Interpreter;
                DFA[] decisionToDFA = interpreter.atn.decisionToDFA;

                if (SHOW_DFA_STATE_STATS)
                {
                    int states = 0;
                    int configs = 0;
                    HashSet<ATNConfig> uniqueConfigs = new HashSet<ATNConfig>();

                    for (int i = 0; i < decisionToDFA.Length; i++)
                    {
                        DFA dfa = decisionToDFA[i];
                        if (dfa == null || dfa.states == null)
                        {
                            continue;
                        }

                        states += dfa.states.Count;
                        foreach (DFAState state in dfa.states.Values)
                        {
                            configs += state.configs.Count;
                            uniqueConfigs.UnionWith(state.configs);
                        }
                    }

                    Console.Out.WriteLine("There are {0} parser DFAState instances, {1} configs ({2} unique), {3} prediction contexts.", states, configs, uniqueConfigs.Count, interpreter.atn.GetContextCacheSize());
                }

                int localDfaCount = 0;
                int globalDfaCount = 0;
                int localConfigCount = 0;
                int globalConfigCount = 0;
                int[] contextsInDFAState = new int[0];

                for (int i = 0; i < decisionToDFA.Length; i++)
                {
                    DFA dfa = decisionToDFA[i];
                    if (dfa == null || dfa.states == null)
                    {
                        continue;
                    }

                    if (SHOW_CONFIG_STATS)
                    {
                        foreach (DFAState state in dfa.states.Keys)
                        {
                            if (state.configs.Count >= contextsInDFAState.Length)
                            {
                                Array.Resize(ref contextsInDFAState, state.configs.Count + 1);
                            }

                            if (state.isAcceptState)
                            {
                                bool hasGlobal = false;
                                foreach (ATNConfig config in state.configs)
                                {
                                    if (config.ReachesIntoOuterContext)
                                    {
                                        globalConfigCount++;
                                        hasGlobal = true;
                                    }
                                    else
                                    {
                                        localConfigCount++;
                                    }
                                }

                                if (hasGlobal)
                                {
                                    globalDfaCount++;
                                }
                                else
                                {
                                    localDfaCount++;
                                }
                            }

                            contextsInDFAState[state.configs.Count]++;
                        }
                    }

                    if (EXPORT_LARGEST_CONFIG_CONTEXTS)
                    {
                        foreach (DFAState state in dfa.states.Keys)
                        {
                            foreach (ATNConfig config in state.configs)
                            {
                                string configOutput = config.ToDotString();
                                if (configOutput.Length <= configOutputSize)
                                {
                                    continue;
                                }

                                configOutputSize = configOutput.Length;
                                writeFile(tmpdir, "d" + dfa.decision + ".s" + state.stateNumber + ".a" + config.Alt + ".config.dot", configOutput);
                            }
                        }
                    }
                }

                if (SHOW_CONFIG_STATS && currentPass == 0)
                {
                    Console.Out.WriteLine("  DFA accept states: {0} total, {1} with only local context, {2} with a global context", localDfaCount + globalDfaCount, localDfaCount, globalDfaCount);
                    Console.Out.WriteLine("  Config stats: {0} total, {1} local, {2} global", localConfigCount + globalConfigCount, localConfigCount, globalConfigCount);
                    if (SHOW_DFA_STATE_STATS)
                    {
                        for (int i = 0; i < contextsInDFAState.Length; i++)
                        {
                            if (contextsInDFAState[i] != 0)
                            {
                                Console.Out.WriteLine("  {0} configs = {1}", i, contextsInDFAState[i]);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 11
0
 public void Dispose()
 {
     m_TaskScheduler.Dispose();
 }