public BackgroundInventory(int parallelTasksCount, Dictionary <string, object> plugins)
 {
     if (plugins == null)
     {
         throw new ArgumentNullException(nameof(plugins));
     }
     this.scheduler = new QueuedTaskScheduler <SolarWinds.Orion.Core.BusinessLayer.BackgroundInventory.BackgroundInventory.InventoryTask>(new QueuedTaskScheduler <SolarWinds.Orion.Core.BusinessLayer.BackgroundInventory.BackgroundInventory.InventoryTask> .TaskProcessingRoutine(this.DoInventory), parallelTasksCount);
     this.scheduler.TaskProcessingFinished += new EventHandler(this.scheduler_TaskProcessingFinished);
     this.snmpGlobals = new SnmpGlobalSettings();
     this.snmpGlobals.set_MaxReplies(SettingsDAL.GetCurrentInt("SWNetPerfMon-Settings-SNMP MaxReps", 5));
     this.snmpGlobals.set_RequestTimeout(SettingsDAL.GetCurrentInt("SWNetPerfMon-Settings-SNMP Timeout", 2500));
     this.snmpGlobals.set_RequestRetries(SettingsDAL.GetCurrentInt("SWNetPerfMon-Settings-SNMP Retries", 2));
     this.snmpGlobals.set_HsrpEnabled((bool)SettingsDAL.GetCurrent <bool>("SWNetPerfMon-Settings-SNMP HSRPEnabled", (M0)1));
     this.wmiGlobals = new WmiGlobalSettings();
     this.wmiGlobals.set_UserImpersonationLevel((ImpersonationLevel)SettingsDAL.GetCurrent <ImpersonationLevel>("SWNetPerfMon-Settings-Wmi UserImpersonationLevel", (M0)0));
     this.wmiGlobals.set_ConnectionRationMode((WmiConnectionRationMode)SettingsDAL.GetCurrent <WmiConnectionRationMode>("SWNetPerfMon-Settings-Wmi ConnectionRationMode", (M0)1));
     this.wmiGlobals.set_MaxRationedConnections(SettingsDAL.GetCurrentInt("SWNetPerfMon-Settings-Wmi MaxRationedConnections", 0));
     this.wmiGlobals.set_KillProcessExcessiveError((bool)SettingsDAL.GetCurrent <bool>("SWNetPerfMon-Settings-Wmi KillProcessExcessiveError", (M0)1));
     this.wmiGlobals.set_ExcessiveErrorThreshhold(SettingsDAL.GetCurrentInt("SWNetPerfMon-Settings-Wmi ExcessiveErrorThreshhold", 50));
     this.wmiGlobals.set_WmiRetries(SettingsDAL.GetCurrentInt("SWNetPerfMon-Settings-WMI Retries", 0));
     this.wmiGlobals.set_WmiRetryInterval(SettingsDAL.GetCurrentInt("SWNetPerfMon-Settings-WMI Retry Interval", 0));
     this.wmiGlobals.set_WmiAutoCorrectRDNSInconsistency(Convert.ToBoolean(SettingsDAL.GetCurrentInt("SWNetPerfMon-Settings-WMI Auto Correct Reverse DNS", 0)));
     this.wmiGlobals.set_WmiDefaultRootNamespaceOverrideIndex(SettingsDAL.GetCurrentInt("SWNetPerfMon-Settings-WMI Default Root Namespace Override Index", 0));
     this.inventories = plugins;
 }
Esempio n. 2
0
        static void _mainTaskSheduler_QueueIsEmpty(object sender, EventArgs e)
        {
            QueuedTaskScheduler deprecatedScheduler = (QueuedTaskScheduler)sender;

            deprecatedScheduler.QueueIsEmpty -= _mainTaskSheduler_QueueIsEmpty;
            deprecatedScheduler.Dispose();
        }
Esempio n. 3
0
 public RPCUserInfo(SocketAsyncEventArgs asyn) : base(asyn, 1024 * 64)
 {
     QueueScheduler              = new QueuedTaskScheduler();
     RPC_Call                    = new RPC();
     RPC_Call.CallBufferOutSend += RPC_OBJ_CallBufferOutSend;
     Stream = new ZYNetRingBufferPool(1024 * 64);//64K
 }
        public void Sched_Task_NewTask_ContinueWith_Wrapped()
        {
            TaskScheduler scheduler = new QueuedTaskScheduler();

            Task wrapped = new Task(() =>
            {
                output.WriteLine("#0 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1}",
                    SynchronizationContext.Current, TaskScheduler.Current);

                Task t0 = new Task(() =>
                {
                    output.WriteLine("#1 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1}",
                        SynchronizationContext.Current, TaskScheduler.Current);
                    Assert.AreEqual(scheduler, TaskScheduler.Current, "TaskScheduler.Current #1");
                });
                Task t1 = t0.ContinueWith(task =>
                {
                    Assert.IsFalse(task.IsFaulted, "Task #1 Faulted=" + task.Exception);

                    output.WriteLine("#2 - new Task - SynchronizationContext.Current={0} TaskScheduler.Current={1}",
                        SynchronizationContext.Current, TaskScheduler.Current);
                    Assert.AreEqual(scheduler, TaskScheduler.Current, "TaskScheduler.Current #2");
                });
                t0.Start(scheduler);
                bool ok = t1.Wait(TimeSpan.FromSeconds(15));
                if (!ok) throw new TimeoutException();
            });
            wrapped.Start(scheduler);
            bool finished = wrapped.Wait(TimeSpan.FromSeconds(30));
            if (!finished) throw new TimeoutException();
        }
Esempio n. 5
0
        protected void Dispose(bool disposing)
        {
            if (!disposing)
            {
                return;
            }

            if (_cancel != null)
            {
                _cancel.Cancel();
                _cancel.Token.WaitHandle.WaitOne();
                _cancel.Dispose();
                _cancel = null;
            }
            _factories.Clear();
            _schedulers.Clear();

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

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

            if (Maintenance != null)
            {
                Maintenance.Dispose();
                Maintenance = null;
            }
        }
Esempio n. 6
0
        protected void Dispose(bool disposing)
        {
            if (!disposing)
            {
                return;
            }

            if (_cancel != null)
            {
                _cancel.Cancel();
                _cancel.Token.WaitHandle.WaitOne();
                _cancel.Dispose();
                _cancel = null;
            }

            _factories?.Clear();
            _schedulers?.Clear();

            _scheduler?.Dispose();
            _scheduler = null;

            _background?.Dispose();
            _background = null;

            _maintenance?.Dispose();
            _maintenance = null;
        }
Esempio n. 7
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);
        }
        public async Task Initialize()
        {
            await Manage().ConfigureAwait(false);
            string threadName = $"QuartzScheduler_{jobStore.InstanceName}-{jobStore.InstanceId}_ClusterManager";

            taskScheduler = new QueuedTaskScheduler(threadCount: 1, threadPriority: ThreadPriority.AboveNormal, threadName: threadName, useForegroundThreads: !jobStore.MakeThreadsDaemons);
            task = Task.Factory.StartNew(() => Run(cancellationTokenSource.Token), cancellationTokenSource.Token, TaskCreationOptions.HideScheduler, taskScheduler).Unwrap();
        }
Esempio n. 9
0
 public RPCUserInfo(SocketAsyncEventArgs asyn, int maxSize, bool isCallReturn)
 {
     Scheduler = new QueuedTaskScheduler(4);
     RPC_Call  = new RPC(isCallReturn);
     RPC_Call.CallBufferOutSend += SendData;
     Stream    = new ZYNetRingBufferPool(maxSize);
     this.Asyn = asyn;
 }
Esempio n. 10
0
        /*
         * public void CallAsyn<Mode>(Expression<Action<Mode>> action)
         * {
         *  RPC_Call.CallAsyn<Mode>(action);
         * }
         * public void CallAsyn<Mode, Result>(Expression<Func<Mode, Result>> action, Action<AsynReturn> Callback)
         * {
         *   RPC_Call.CallAsyn<Mode, Result>(action, Callback);
         * }
         *
         * public void CallAsyn<Mode>(Expression<Action<Mode>> action, Action<AsynReturn> Callback)
         * {
         *  RPC_Call.CallAsyn<Mode>(action, Callback);
         * }
         *
         * public Result Call<Mode, Result>(Expression<Func<Mode, Result>> action)
         * {
         *  return RPC_Call.Call<Mode, Result>(action);
         * }
         *
         * public void Call<Mode>(Expression<Action<Mode>> action)
         * {
         *  RPC_Call.Call<Mode>(action);
         * }
         */
        #endregion


        public RPCUserInfo(SocketAsyncEventArgs asyn)
        {
            Scheduler = new QueuedTaskScheduler(4);
            RPC_Call  = new RPC();
            RPC_Call.CallBufferOutSend += RPC_OBJ_CallBufferOutSend;
            this.Asyn = asyn;
            Stream    = new ZYNetRingBufferPool(1024 * 1024 * 2);//2MB
        }
Esempio n. 11
0
 public RPCClient()
 {
     OrderSchedulerSend          = new QueuedTaskScheduler();
     OrderSchedulerRead          = new QueuedTaskScheduler();
     RPC_Call                    = new RPC();
     RPC_Call.CallBufferOutSend += RPC_Call_CallBufferOutSend;
     RPC_Call.ErrMsgOut         += RPC_Call_ErrMsgOut;
 }
Esempio n. 12
0
        public ProcessingGroupWrapper(string transportId, string name, ProcessingGroupInfo processingGroupInfo)
        {
            TransportId = transportId;
            Name        = name;
            var threadCount = Math.Max(processingGroupInfo.ConcurrencyLevel, 1);

            m_TaskScheduler = new QueuedTaskScheduler(threadCount);
            m_TaskFactories = new Dictionary <int, TaskFactory>();
        }
Esempio n. 13
0
        internal MisfireHandler(JobStoreSupport jobStoreSupport)
        {
            this.jobStoreSupport = jobStoreSupport;

            string threadName = $"QuartzScheduler_{jobStoreSupport.InstanceName}-{jobStoreSupport.InstanceId}_MisfireHandler";

            taskScheduler           = new QueuedTaskScheduler(threadCount: 1, threadName: threadName, useForegroundThreads: !jobStoreSupport.MakeThreadsDaemons);
            cancellationTokenSource = new CancellationTokenSource();
        }
Esempio n. 14
0
        public void Start()
        {
            if(_queue == null)
            {
                _queue = new QueuedTaskScheduler(_threads);
            }

            // This probaby uses a worker pool thread, so should be replaced eventually
            _background = new Timer(state => SeedJobsFromQueue(), null, 0, Linger.SleepDelay * 1000);
        }
Esempio n. 15
0
        public InMemoryTransport(Uri inputAddress, int concurrencyLimit)
        {
            _inputAddress = inputAddress;

            _sendObservable     = new SendObservable();
            _receiveObservable  = new ReceiveObservable();
            _endpointObservable = new ReceiveEndpointObservable();

            _scheduler = new QueuedTaskScheduler(TaskScheduler.Default, concurrencyLimit);
        }
Esempio n. 16
0
        public void Start()
        {
            if (_queue == null)
            {
                _queue = new QueuedTaskScheduler(_threads);
            }

            // This probaby uses a worker pool thread, so should be replaced eventually
            _background = new Timer(state => SeedJobsFromQueue(), null, 0, Linger.SleepDelay * 1000);
        }
        public QueuedTaskSchedulerFactory()
        {
            // TODO: hook up the number of threads to settings and figure out the optimal value.
            // We expect node splitting to have a lot of blocking when the work is moved to the GPU
            // so more than the number of cores is probably desired.

            // TODO: we might be able to build our own prioritized task scheduler that takes more things
            // into account than just the level of the node.  For instance, we could prioritize
            // currently-visible nodes over non-visible nodes.  To do this we'd probably drop
            // QueuedTaskScheduler in favor of a custom scheduler that uses Task.AsyncState to get
            // information about each queued node and then sorts them accordingly.
            _scheduler = new QueuedTaskScheduler(0, "", false, ThreadPriority.BelowNormal);
        }
Esempio n. 18
0
 public void Stop()
 {
     if(_queue != null)
     {
         _queue.Dispose();
         _queue = null;
     }
     if(_background != null)
     {
         _background.Dispose();
         _background = null;
     }
 }
 private void Dispose(bool disposing)
 {
     if (this.disposed)
     {
         return;
     }
     if (disposing && this.scheduler != null)
     {
         this.scheduler.Dispose();
         this.scheduler = (QueuedTaskScheduler <SolarWinds.Orion.Core.BusinessLayer.BackgroundInventory.BackgroundInventory.InventoryTask>)null;
     }
     this.disposed = true;
 }
Esempio n. 20
0
 public void Stop()
 {
     if (_queue != null)
     {
         _queue.Dispose();
         _queue = null;
     }
     if (_background != null)
     {
         _background.Dispose();
         _background = null;
     }
 }
Esempio n. 21
0
 public CommandDispatcher(string boundedContext, int threadCount = 1, long failedCommandRetryDelay = 60000)
 {
     m_FailedCommandRetryDelay = failedCommandRetryDelay;
     m_QueuedTaskScheduler     = new QueuedTaskScheduler(threadCount);
     foreach (var value in Enum.GetValues(typeof(CommandPriority)))
     {
         m_TaskFactories[(CommandPriority)value] = new TaskFactory(
             ((CommandPriority)value) == CommandPriority.Normal
                 ? new CurrentThreadTaskScheduler()
                 : m_QueuedTaskScheduler.ActivateNewQueue((int)value));
     }
     m_BoundedContext = boundedContext;
 }
        public QueuedTaskSchedulerFactory()
        {
            // TODO: hook up the number of threads to settings and figure out the optimal value.
            // We expect node splitting to have a lot of blocking when the work is moved to the GPU
            // so more than the number of cores is probably desired.

            // TODO: we might be able to build our own prioritized task scheduler that takes more things
            // into account than just the level of the node.  For instance, we could prioritize
            // currently-visible nodes over non-visible nodes.  To do this we'd probably drop
            // QueuedTaskScheduler in favor of a custom scheduler that uses Task.AsyncState to get
            // information about each queued node and then sorts them accordingly.
            _scheduler = new QueuedTaskScheduler(Environment.ProcessorCount, "QueuedTaskScheduler", false, ThreadPriority.BelowNormal);
        }
        public InMemoryTransport(Uri inputAddress, int concurrencyLimit)
        {
            _inputAddress = inputAddress;

            _sendObservable     = new SendObservable();
            _receiveObservable  = new ReceiveObservable();
            _endpointObservable = new ReceiveEndpointObservable();

            _supervisor  = new TaskSupervisor();
            _participant = _supervisor.CreateParticipant();

            _scheduler = new QueuedTaskScheduler(TaskScheduler.Default, concurrencyLimit);
        }
Esempio n. 24
0
        public void Start(bool immediate = false)
        {
            if (_scheduler == null)
            {
                _scheduler = new QueuedTaskScheduler(_threads);
            }

            Background.Produce(SeedTasksFromQueue, _settings.SleepInterval);
            Background.Start(immediate);

            Maintenance.Produce(HangingTasks, TimeSpan.FromMinutes(5));
            Maintenance.Start(immediate);
        }
Esempio n. 25
0
        public RabbitMqConnectionContext(IConnection connection, RabbitMqHostSettings hostSettings, CancellationToken cancellationToken)
        {
            _connection   = connection;
            _hostSettings = hostSettings;
            _payloadCache = new PayloadCache();

            _completed     = new TaskCompletionSource <bool>();
            _tokenSource   = new CancellationTokenSource();
            _registration  = cancellationToken.Register(OnCancellationRequested);
            _taskScheduler = new QueuedTaskScheduler(TaskScheduler.Default, 1);

            connection.ConnectionShutdown += OnConnectionShutdown;
        }
Esempio n. 26
0
        public void Start(bool immediate = false)
        {
            _logger.Info(() => "Starting background task host");

            if (_scheduler == null)
            {
                _scheduler = new QueuedTaskScheduler(ResolveConcurrency());
            }

            _background.Produce(EnqueueTasks, TimeSpan.FromSeconds(_options.CurrentValue.SleepIntervalSeconds));
            _background.Start(immediate);

            _maintenance.Produce(HangingTasks, TimeSpan.FromMinutes(1));
            _maintenance.Start(immediate);
        }
Esempio n. 27
0
        public async Task TestRunWithTaskScheduler1()   // Aligned with the coroutine test TestExecuteRepeated1
        {
            QueuedTaskScheduler scheduler = new QueuedTaskScheduler(TaskScheduler.Default, maxConcurrencyLevel: 1);
            var cancel = new CancellationTokenSource();

            // Create both tasks at the same time:
            Task          t1 = TaskV2.Run(SomeAsyncTask1, cancel, scheduler);
            Task <string> t2 = TaskV2.Run(SomeAsyncTask2, cancel, scheduler);

            Assert.Equal(1, scheduler.GetRemainingScheduledTaskCount()); // 1 task started and 1 waiting
            Assert.Equal("Some string result", await t2);
            Assert.Equal(0, scheduler.GetRemainingScheduledTaskCount());

            // Since the scheduler allows only one task at a time, if t2 is done, t1 also must be completed:
            Assert.True(t1.IsCompleted);
        }
Esempio n. 28
0
        public void Task_GroupedMasterTaskScheduler()
        {
            const string testNameBase = "Task_GroupedMasterTaskScheduler";

            int[] testPoints = new[]
            {
                100,
                500,
                1000,
                2000,
                3000,
                4000,
                5000,
                10000,
                20000,
                30000,
                40000,
                50000,
                100000,
                500000
            };

            foreach (int numSchedulers in testPoints)
            {
                string testName = testNameBase + "-" + numSchedulers;

                int numTasks = numSchedulers * 10;

                Console.WriteLine(testName + " NumTasks=" + numTasks + " NumSchedulers=" + numSchedulers);

                // Run baseline test with single, Default scheduler
                var baseline = TimeRun(1, TimeSpan.Zero, testName + "-Baseline", () => RunTestLoop(numTasks, null));

                // Run test with many schedulers...

                // Pre-create schedulers
                var             masterScheduler = new QueuedTaskScheduler();
                TaskScheduler[] schedulers      = new TaskScheduler[numSchedulers];
                for (int i = 0; i < numSchedulers; i++)
                {
                    schedulers[i] = new TaskSchedulerWrapper(masterScheduler);
                }

                TimeRun(1, baseline, testName, () => RunTestLoop(numTasks, schedulers));
            }
        }
        public void Task_GroupedMasterTaskScheduler()
        {
            const string testNameBase = "Task_GroupedMasterTaskScheduler";

            int[] testPoints = new[]
            {
                100,
                500,
                1000,
                2000,
                3000,
                4000,
                5000,
                10000,
                20000,
                30000,
                40000,
                50000,
                100000,
                500000
            };

            foreach (int numSchedulers in testPoints)
            {
                string testName = testNameBase + "-" + numSchedulers;

                int numTasks = numSchedulers * 10;
                
                output.WriteLine(testName + " NumTasks=" + numTasks + " NumSchedulers=" + numSchedulers);

                // Run baseline test with single, Default scheduler
                var baseline = TimeRun(1, TimeSpan.Zero, testName + "-Baseline", output, () => RunTestLoop(numTasks, null));

                // Run test with many schedulers...

                // Pre-create schedulers
                var masterScheduler = new QueuedTaskScheduler();
                TaskScheduler[] schedulers = new TaskScheduler[numSchedulers];
                for (int i = 0; i < numSchedulers; i++)
                {
                    schedulers[i] = new TaskSchedulerWrapper(masterScheduler);
                }

                TimeRun(1, baseline, testName, output, () => RunTestLoop(numTasks, schedulers));
            }
        }
Esempio n. 30
0
        public RabbitMqModelContext(ConnectionContext connectionContext, IModel model, CancellationToken cancellationToken)
        {
            _connectionContext = connectionContext;
            _model             = model;

            _payloadCache  = new PayloadCache();
            _published     = new ConcurrentDictionary <ulong, PendingPublish>();
            _taskScheduler = new QueuedTaskScheduler(TaskScheduler.Default, 1);

            _tokenSource  = new CancellationTokenSource();
            _registration = cancellationToken.Register(OnCancellationRequested);

            _model.ModelShutdown += OnModelShutdown;
            _model.BasicAcks     += OnBasicAcks;
            _model.BasicNacks    += OnBasicNacks;
            _model.BasicReturn   += OnBasicReturn;
            _model.ConfirmSelect();
        }
Esempio n. 31
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. 32
0
        private static void CreateTaskScheduler()
        {
            if (_mainTaskSheduler != null)
            {
                _mainTaskSheduler.QueueIsEmpty += _mainTaskSheduler_QueueIsEmpty;
            }
            if (_monoConcurrencyTaskSheduler == null)
            {
                _monoConcurrencyTaskSheduler = new QueuedTaskScheduler(TaskScheduler.Default, 1);
            }
            //We will never start more thread than the number of Virutal processor on the computer !
            int nbrThreads = ThreadsManager._isBoostMode ? _totThread + 4 : _totThread;

            _mainTaskSheduler = new QueuedTaskScheduler(TaskScheduler.Default, nbrThreads);
            //Create the Priority queues
            _highPrioritySchedduler   = _mainTaskSheduler.ActivateNewQueue(0);
            _normalPrioritySchedduler = _mainTaskSheduler.ActivateNewQueue(1);
            _lowPrioritySchedduler    = _mainTaskSheduler.ActivateNewQueue(2);
        }
Esempio n. 33
0
        private void CreateEmails()
        {
            int  threadCount          = 0;
            bool isOperationCancelled = false;

            try
            {
                TaskScheduler taskScheduler = TaskScheduler.Current;
                List <Task>   taskList      = new List <Task>(dtRecords.Rows.Count);
                string        threadcnt     = txtThreads.Text;
                if (!int.TryParse(txtThreads.Text, out threadCount))
                {
                    MessageBox.Show("Please enter valid thread count.");
                }
                cancellationTokenSource = new CancellationTokenSource();

                QueuedTaskScheduler qts  = new QueuedTaskScheduler(TaskScheduler.Default, threadCount);
                TaskScheduler       pri0 = qts.ActivateNewQueue(priority: 0);

                int i = 0;
                List <Task <ThreadResult> > lst = new List <Task <ThreadResult> >();
                for (i = 0; i < dtRecords.Rows.Count; i = i + 9)
                {
                    CreateParallelRequests(pri0, i);
                }
            }
            catch (OperationCanceledException)
            {
                isOperationCancelled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Exception occured: {0}\nPlease try again", ex.Message));
            }
            finally
            {
                if (isOperationCancelled)
                {
                    //MessageBox.Show("Operation Cancelled.");
                }
            }
        }
Esempio n. 34
0
        public async Task TestRunWithTaskScheduler2()   // Aligned with the coroutine test TestExecuteRepeated1
        {
            var maxConcurrencyLevel       = 2;
            QueuedTaskScheduler scheduler = new QueuedTaskScheduler(TaskScheduler.Default, maxConcurrencyLevel);
            var cancel = new CancellationTokenSource();

            // Create both tasks at the same time:
            Task          t1 = TaskV2.Run(SomeAsyncTask1, cancel, scheduler);
            Task <string> t2 = TaskV2.Run(SomeAsyncTask2, cancel, scheduler);
            var           t3 = TaskV2.Run(SomeAsyncTask1, cancel, scheduler); // Add a 3rd task (will not be started)

            Assert.True(scheduler.GetRemainingScheduledTaskCount() >= 1);

            Assert.Equal("Some string result", await t2);

            // Check that now also task t3 was started:
            Assert.Equal(0, scheduler.GetRemainingScheduledTaskCount());

            // Since the scheduler allows 2 tasks at a time, t1 will not be complete when t2 is done:
            Assert.False(t1.IsCompleted);
            await t1;
        }
Esempio n. 35
0
 public void Dispose(bool disposing)
 {
     if (!disposing)
     {
         return;
     }
     if(_cancel != null)
     {
         _cancel.Cancel();
         _cancel.Token.WaitHandle.WaitOne();
         _cancel.Dispose();
         _cancel = null;
     }
     _factories.Clear();
     _schedulers.Clear();
     if (_queue == null)
     {
         return;
     }
     _queue.Dispose();
     _queue = null;
 }
Esempio n. 36
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]);
                            }
                        }
                    }
                }
            }
        }