public static void CancelBeforeWait()
        {
            CountdownEvent countdownEvent = new CountdownEvent(2);
            CancellationTokenSource cs = new CancellationTokenSource();
            cs.Cancel();
            CancellationToken ct = cs.Token;

            const int millisec = 100;
            TimeSpan timeSpan = new TimeSpan(100);
            string message = "CancelBeforeWait:  > Cancellation token does not match.";
            EnsureOperationCanceledExceptionThrown(() => countdownEvent.Wait(ct), ct, message);
            EnsureOperationCanceledExceptionThrown(() => countdownEvent.Wait(millisec, ct), ct, message);
            EnsureOperationCanceledExceptionThrown(() => countdownEvent.Wait(timeSpan, ct), ct, message);

            countdownEvent.Dispose();
        }
        /// <summary>Initializes the IOCompletionPortTaskScheduler.</summary>
        /// <param name="maxConcurrencyLevel">The maximum number of threads in the scheduler to be executing concurrently.</param>
        /// <param name="numAvailableThreads">The number of threads to have available in the scheduler for executing tasks.</param>
        public IOCompletionPortTaskScheduler(int maxConcurrencyLevel, int numAvailableThreads)
        {
            // Validate arguments
            if (maxConcurrencyLevel < 1) throw new ArgumentNullException("maxConcurrencyLevel");
            if (numAvailableThreads < 1) throw new ArgumentNullException("numAvailableThreads");

            m_tasks = new ConcurrentQueue<Task>();
            m_iocp = new IOCompletionPort(maxConcurrencyLevel);
            m_schedulerThread = new ThreadLocal<bool>();
            m_remainingThreadsToShutdown = new CountdownEvent(numAvailableThreads);

            // Create and start the threads
            for (int i = 0; i < numAvailableThreads; i++)
            {
                new Thread(() =>
                {
                    try
                    {
                        // Note that this is a scheduler thread.  Used for inlining checks.
                        m_schedulerThread.Value = true;

                        // Continually wait on the I/O completion port until 
                        // there's a work item, then process it.
                        while (m_iocp.WaitOne())
                        {
                            Task next;
                            if (m_tasks.TryDequeue(out next)) TryExecuteTask(next);
                        }
                    }
                    finally { m_remainingThreadsToShutdown.Signal(); }
                }) { IsBackground = true }.Start();
            }
        }
Example #3
0
        // Validates init, set, reset state transitions.
        private static void RunCountdownEventTest0_StateTrans(int initCount, int increms, bool takeAllAtOnce)
        {
            CountdownEvent ev = new CountdownEvent(initCount);

            Assert.Equal(initCount, ev.InitialCount);

            // Increment (optionally).
            for (int i = 1; i < increms + 1; i++)
            {
                ev.AddCount();
                Assert.Equal(initCount + i, ev.CurrentCount);
            }

            // Decrement until it hits 0.
            if (takeAllAtOnce)
            {
                ev.Signal(initCount + increms);
            }
            else
            {
                for (int i = 0; i < initCount + increms; i++)
                {
                    Assert.False(ev.IsSet, string.Format("  > error: latch is set after {0} signals", i));
                    ev.Signal();
                }
            }

            Assert.True(ev.IsSet);
            Assert.Equal(0, ev.CurrentCount);

            // Now reset the event and check its count.
            ev.Reset();
            Assert.Equal(ev.InitialCount, ev.CurrentCount);
        }
Example #4
0
 // Tries some simple timeout cases.
 private static void RunCountdownEventTest1_SimpleTimeout(int ms)
 {
     // Wait on the event.
     CountdownEvent ev = new CountdownEvent(999);
     Assert.False(ev.Wait(ms));
     Assert.False(ev.IsSet);
     Assert.False(ev.WaitHandle.WaitOne(ms));
 }
Example #5
0
        public static void RunCountdownEventTest2_Exceptions()
        {
            CountdownEvent cde = null;
            Assert.Throws<ArgumentOutOfRangeException>(() => cde = new CountdownEvent(-1));
            // Failure Case: Constructor didn't throw AORE when -1 passed

            cde = new CountdownEvent(1);
            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Signal(0));
            // Failure Case: Signal didn't throw AORE when 0 passed

            cde = new CountdownEvent(0);
            Assert.Throws<InvalidOperationException>(() => cde.Signal());
            // Failure Case: Signal didn't throw IOE when the count is zero

            cde = new CountdownEvent(1);
            Assert.Throws<InvalidOperationException>(() => cde.Signal(2));
            // Failure Case: Signal didn't throw IOE when the signal count > current count

            Assert.Throws<ArgumentOutOfRangeException>(() => cde.AddCount(0));
            // Failure Case: AddCount didn't throw AORE when 0 passed

            cde = new CountdownEvent(0);
            Assert.Throws<InvalidOperationException>(() => cde.AddCount(1));
            // Failure Case: AddCount didn't throw IOE when the count is zero

            cde = new CountdownEvent(int.MaxValue - 10);
            Assert.Throws<InvalidOperationException>(() => cde.AddCount(20));
            // Failure Case: AddCount didn't throw IOE when the count > int.Max

            cde = new CountdownEvent(2);
            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Reset(-1));
            // Failure Case: Reset didn't throw AORE when the count is zero

            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(-2));
            // Failure Case: Wait(int) didn't throw AORE when the totalmilliseconds < -1

            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(TimeSpan.FromDays(-1)));
            // Failure Case:  FAILED.  Wait(TimeSpan) didn't throw AORE when the totalmilliseconds < -1

            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(TimeSpan.MaxValue));
            // Failure Case: Wait(TimeSpan, CancellationToken) didn't throw AORE when the totalmilliseconds > int.max

            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(TimeSpan.FromDays(-1), new CancellationToken()));
            // Failure Case: Wait(TimeSpan) didn't throw AORE when the totalmilliseconds < -1

            Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(TimeSpan.MaxValue, new CancellationToken()));
            // Failure Case: Wait(TimeSpan, CancellationToken) didn't throw AORE when the totalmilliseconds > int.max

            cde.Dispose();

            Assert.Throws<ObjectDisposedException>(() => cde.Wait());
            // Failure Case: Wait() didn't throw ODE after Dispose
        }
Example #6
0
        /// <summary>Initializes the ActionCountdownEvent.</summary>
        /// <param name="initialCount">The number of signals required to set the CountdownEvent.</param>
        /// <param name="action">The delegate to be invoked when the count reaches zero.</param>
        public ActionCountdownEvent(int initialCount, Action action)
        {
            // Validate arguments
            if (initialCount < 0) throw new ArgumentOutOfRangeException("initialCount");
            if (action == null) throw new ArgumentNullException("action");

            // Store the action and create the event from the initial count. If the initial count forces the
            // event to be set, run the action immediately. Otherwise, capture the current execution context
            // so we can run the action in the right context later on.
            _action = action;
            _event = new CountdownEvent(initialCount);
            if (initialCount == 0) action();
            else _context = ExecutionContext.Capture();
        }
Example #7
0
        public static void For(int minInclusive, int maxExlusive, Action<int> action)
        {
            var countdownEvent = new CountdownEvent(maxExlusive - minInclusive);

            WaitCallback worker = delegate(object o)
            {
                action((int)o);
                countdownEvent.ReleaseOne();
            };

            for (var i = minInclusive; i < maxExlusive; i++)
            {
                ThreadPool.QueueUserWorkItem(worker, i);
            }

            countdownEvent.Wait();
        }
        public static void CancelAfterWait()
        {
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            CancellationToken cancellationToken = cancellationTokenSource.Token;

            CountdownEvent countdownEvent = new CountdownEvent(2); ;  // countdownEvent that will block all waiters

            Task.Run(() =>
            {
                cancellationTokenSource.Cancel();
            });

            //Now wait.. the wait should abort and an exception should be thrown
            EnsureOperationCanceledExceptionThrown(() => countdownEvent.Wait(cancellationToken), cancellationToken,
               "CancelAfterWait:  An OCE(null) should have been thrown that references the cancellationToken.");

            // the token should not have any listeners.
            // currently we don't expose this.. but it was verified manually
        }
Example #9
0
        /// <summary>
        /// Creates the reader and processor threads, waits for them to finish
        /// the work and then prints the sum of the results from the result queue.
        /// </summary>
        static void Main()
        {
            _allThreadsDone = new CountdownEvent(DegreeOfParallelism);

            new Thread(Reader).Start();
            for (int i = 0; i < DegreeOfParallelism; ++i)
            {
                new Thread(Processor).Start();
            }

            _allThreadsDone.Wait();

            int sum = 0;
            //Here we can use UnsafeItems to access the queue because we know
            //for sure that all concurrent work on it has finished.
            foreach (int wc in _wordCounts.UnsafeItems)
            {
                sum += wc;
            }
            Console.WriteLine("Total words: " + sum);
        }
        public static void RunPartitionerStaticTest_SingleChunking()
        {
            CountdownEvent cde = new CountdownEvent(2);
            Action[] actions = new Action[256];

            // The thinking here is that we'll put enough "filler" into this array to
            // insure that "natural" chunk size is greater than 2.  Without the
            // NoBuffering option, the Parallel.ForEach below is certain to deadlock.
            // Somewhere a Signal() is going to be after a Wait() in the same chunk, and
            // the loop will deadlock.
            for (int i = 0; i < 252; i++) actions[i] = () => { };
            actions[252] = () => { cde.Wait(); };
            actions[253] = () => { cde.Signal(); };
            actions[254] = () => { cde.Wait(); };
            actions[255] = () => { cde.Signal(); };

            Debug.WriteLine("    * We'll hang here if EnumerablePartitionerOptions.NoBuffering is not working properly");
            Parallel.ForEach(Partitioner.Create(actions, EnumerablePartitionerOptions.NoBuffering), item =>
            {
                item();
            });
        }
        public object Clone()
        {
            var filePaths = new List<string>();
            var fileRelativePaths = new List<string>();

            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
            try
            {
                var cssSpriteOutput = string.Empty;
                var jsOutput = string.Empty;
                var cssOutput = string.Empty;
                
                var countdownEvents = new CountdownEvent(3);

                ThreadPool.QueueUserWorkItem(data =>
                    {
                        var countdownEvent = (CountdownEvent) data;
                        try
                        {
                            if (_crusherConfiguration != null)
                            {
                                var cssSpriteGroups = _crusherConfiguration.CssSpriteGroups;
                                var cssSpriteCreator = new CssSpriteCreator(_cacheManager, _retryableFileOpener, _pathProvider, _retryableFileWriter, _fileMetaData);
                                var cssSpriteGroupsProcessor = new CssSpriteGroupsProcessor();

                                cssSpriteOutput = cssSpriteGroupsProcessor.ProcessGroups(_pathProvider, cssSpriteCreator, cssSpriteGroups).ToString();

                                var cssFilePaths = new List<string>();
                                var cssFileRelativePaths = new List<string>();
                                foreach (CssSpriteGroupElement cssSpriteGroup in cssSpriteGroups)
                                {
                                    cssFilePaths.Add(new Uri(_pathProvider.MapPath(cssSpriteGroup.CssOutputFilePath)).LocalPath);
                                    cssFilePaths.Add(new Uri(_pathProvider.MapPath(cssSpriteGroup.ImageOutputFilePath)).LocalPath);

                                    cssFileRelativePaths.Add(MakeRelative(cssSpriteGroup.CssOutputFilePath));
                                    cssFileRelativePaths.Add(MakeRelative(cssSpriteGroup.ImageOutputFilePath));
                                }

                                filePaths.AddRange(cssFilePaths);
                                fileRelativePaths.AddRange(cssFileRelativePaths);

                                _logMessage(cssSpriteOutput);
                            }
                        }
                        catch (Exception exception)
                        {
                            _logError(exception.ToString());
                        }
                        countdownEvent.Signal();
                    }, countdownEvents);

                ThreadPool.QueueUserWorkItem(data =>
                    {
                        var countdownEvent = (CountdownEvent) data;

                        try
                        {
                            if (_crusherConfiguration != null)
                            {
                                var jsCrusher = new JsCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, _fileMetaData);
                                var jsGroups = _crusherConfiguration.JsGroups;
                                var jsGroupsProcessor = new JsGroupsProcessor();
                                jsOutput = jsGroupsProcessor.ProcessGroups(_pathProvider, jsCrusher, jsGroups).ToString();

                                var jsFilePaths = new List<string>();
                                var jsFileRelativePaths = new List<string>();
                                foreach (JsGroupElement jsGroup in jsGroups)
                                {
                                    jsFilePaths.Add(new Uri(_pathProvider.MapPath(jsGroup.OutputFilePath)).LocalPath);
                                    jsFileRelativePaths.Add(MakeRelative(jsGroup.OutputFilePath));
                                }

                                filePaths.AddRange(jsFilePaths);
                                fileRelativePaths.AddRange(jsFileRelativePaths);

                                _logMessage(jsOutput);
                            }
                        }
                        catch (Exception exception)
                        {
                            _logError(exception.ToString());
                        }
                        countdownEvent.Signal();
                    }, countdownEvents);

                ThreadPool.QueueUserWorkItem(data =>
                    {
                        var countdownEvent = (CountdownEvent) data;

                        try
                        {
                            if (_crusherConfiguration != null)
                            {
                                var hashQueryStringKeyName = _crusherConfiguration.QuerystringKeyName;
                                var cssAssetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, _hasher, _pathProvider);
                                var cssPathRewriter = new CssPathRewriter(cssAssetsFileHasher, _pathProvider);
                                var cssCrusher = new CssCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, cssPathRewriter, _fileMetaData, _crusherConfiguration.WatchAssets);
                                var cssGroups = _crusherConfiguration.CssGroups;
                                var cssGroupsCrusher = new CssGroupsProcessor();
                                cssOutput = cssGroupsCrusher.ProcessGroups(_pathProvider, cssCrusher, cssGroups).ToString();

                                var cssFilePaths = new List<string>();
                                var cssFileRelativePaths = new List<string>();

                                foreach (CssGroupElement cssGroup in cssGroups)
                                {
                                    cssFilePaths.Add(new Uri(_pathProvider.MapPath(cssGroup.OutputFilePath)).LocalPath);
                                    cssFileRelativePaths.Add(MakeRelative(cssGroup.OutputFilePath));
                                }

                                filePaths.AddRange(cssFilePaths);
                                fileRelativePaths.AddRange(cssFileRelativePaths);

                                _logMessage(cssOutput);
                            }
                        }
                        catch (Exception exception)
                        {
                            _logError(exception.ToString());
                        }
                        countdownEvent.Signal();
                    }, countdownEvents);

                countdownEvents.Wait();
            }
            catch (Exception exception)
            {
                _logError(exception.ToString());
            }
            finally
            {
                AppDomain.CurrentDomain.UnhandledException -= OnUnhandledException;
            }

            _setOutputFilePaths(filePaths.ToArray());
            _setOutputFileRelativePaths(fileRelativePaths.ToArray());

            return null;
        }
Example #12
0
        private void InitManager()
        {
            AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;

            var jsExceptions = new List<JsException>(); 
            var cssExceptions = new List<CssException>();
            var countdownEvents = new CountdownEvent(2);

            ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        var jsGroups = _crusherConfiguration.JsGroups;
                        var jsCrusher = new JsCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, _fileMetaData);
                        var groupsProcessor = new JsGroupsProcessor();
                        groupsProcessor.ProcessGroups(_pathProvider, jsCrusher, jsGroups);
                    }
                    catch (Exception exception)
                    {
                        jsExceptions.Add(new JsException(exception));
                    }
                    manualResetEvent.Signal();

                }, countdownEvents);

            ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        var hashQueryStringKeyName = _crusherConfiguration.QuerystringKeyName;
                        var cssGroups = _crusherConfiguration.CssGroups;
                        var cssAssetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, _hasher, _pathProvider);
                        var cssPathRewriter = new CssPathRewriter(cssAssetsFileHasher, _pathProvider);
                        var cssCrusher = new CssCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, cssPathRewriter, _fileMetaData, _crusherConfiguration.WatchAssets);
                        var groupsProcessor = new CssGroupsProcessor();
                        groupsProcessor.ProcessGroups(_pathProvider, cssCrusher, cssGroups);
                    }
                    catch (Exception exception)
                    {
                        cssExceptions.Add(new CssException(exception));
                    }
                    manualResetEvent.Signal();
                }, countdownEvents);

            countdownEvents.Wait();

            var exceptions = cssExceptions.Cast<Exception>().Concat(jsExceptions.Cast<Exception>()).ToList();

            if (exceptions.Any())
            {
                throw new AggregateException(exceptions);
            }
        }
Example #13
0
        private void InitManager()
        {
            AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;

            var jsExceptions = new List<JsException>();
            var cssExceptions = new List<CssException>();
            var countdownEvents = new CountdownEvent(2);

            ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        var groupsProcessor = new JsGroupsProcessor();
                        groupsProcessor.ProcessGroups(_pathProvider, _jsCrusher, _jsGroups);
                    }
                    catch (Exception exception)
                    {
                        jsExceptions.Add(new JsException(exception));
                    }
                    manualResetEvent.Signal();

                }, countdownEvents);

            ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        var groupsProcessor = new CssGroupsProcessor();
                        groupsProcessor.ProcessGroups(_pathProvider, _cssCrusher, _cssGroups);
                    }
                    catch (Exception exception)
                    {
                        cssExceptions.Add(new CssException(exception));
                    }
                    manualResetEvent.Signal();
                }, countdownEvents);

            countdownEvents.Wait();

            var exceptions = cssExceptions.Cast<Exception>().Concat(jsExceptions.Cast<Exception>()).ToList();

            if (exceptions.Any())
            {
                throw new AggregateException(exceptions);
            }
        }
Example #14
0
 public ThreadAnonymousInnerClassHelper(TestBagOfPositions outerInstance, int numTerms, int maxTermsPerDoc, ConcurrentQueue <string> postings, RandomIndexWriter iw, CountdownEvent startingGun, Random threadRandom, Document document, Field field)
 {
     this.OuterInstance  = outerInstance;
     this.NumTerms       = numTerms;
     this.MaxTermsPerDoc = maxTermsPerDoc;
     this.Postings       = postings;
     this.Iw             = iw;
     this.StartingGun    = startingGun;
     this.ThreadRandom   = threadRandom;
     this.Document       = document;
     this.Field          = field;
 }
        public async Task RunsTasksConcurrently(int concurrentTasks)
        {
            // Control + 3 experiments
            var totalTasks = 1 + 3;

            // Expected number of batches
            var expectedBatches = Math.Ceiling(1D * totalTasks / concurrentTasks);

            // Use CountdownEvents to ensure tasks don't finish before all tasks in that batch have started
            var startedSignal  = new CountdownEvent(concurrentTasks);
            var finishedSignal = new CountdownEvent(concurrentTasks);

            // Batch counter
            int batch = 1;

            // Our test task
            var task = new Func <Task <KeyValuePair <int, int> > >(() =>
            {
                return(Task.Run(() =>
                {
                    // Signal that we have started
                    var last = startedSignal.Signal();

                    var myBatch = batch;

                    // Wait till all tasks for this batch have started
                    startedSignal.Wait();

                    // Signal we have finished
                    finishedSignal.Signal();

                    // Last task to start needs to reset the events
                    if (last)
                    {
                        // Wait for all tasks in the batch to have finished
                        finishedSignal.Wait();

                        // Reset the countdown events
                        startedSignal.Reset();
                        finishedSignal.Reset();
                        batch++;
                    }

                    // Return threadId
                    return new KeyValuePair <int, int>(myBatch, Thread.CurrentThread.ManagedThreadId);
                }));
            });

            // Run the experiment
            string experimentName = nameof(RunsTasksConcurrently) + concurrentTasks;
            await Scientist.ScienceAsync <KeyValuePair <int, int> >(experimentName, concurrentTasks, experiment =>
            {
                // Add our control and experiments
                experiment.Use(task);
                for (int idx = 2; idx <= totalTasks; idx++)
                {
                    experiment.Try($"experiment{idx}", task);
                }
            });

            // Get the test result
            var result = TestHelper.Results <KeyValuePair <int, int> >(experimentName).First();

            // Consolidate the returned values from the tasks
            var results = result.Observations.Select(x => x.Value);

            // Assert correct number of batches
            Assert.Equal(expectedBatches, results.Select(x => x.Key).Distinct().Count());

            // Now check each batch
            for (int batchNo = 1; batchNo <= expectedBatches; batchNo++)
            {
                // Get the threadIds used by each task in the batch
                var batchThreadIds = results.Where(x => x.Key == batchNo).Select(x => x.Value);

                // Assert expected number of concurrent tasks in batch
                Assert.Equal(concurrentTasks, batchThreadIds.Count());

                // Assert unique threadIds in batch
                Assert.Equal(batchThreadIds.Count(), batchThreadIds.Distinct().Count());
            }
        }
Example #16
0
        static int Main(string[] args)
        {
            var showHelp = false;
            var configPath = string.Empty;
            var crusherSectionName = "Crusher";
			var cssSpriteSectionName = "CssSprite";
            var applicationPath = "/";

            var options = new OptionSet()
            {
                {
                    "c=|configPath=",
                    "the configuration path to the web.config or app.config file. E.g. ../../../Talifun.Web.Examples/Crusher.Demo/web.config",
                    c => configPath = c
                },
                {
                    "cs=|crusherSectionName=",
                    "the section name of the configuration element for the Talifun.Crusher configuration. Defaults to 'Crusher' if not specified.",
                    cs => crusherSectionName = cs 
                },
                {
                    "css=|cssSpriteSectionName=",
                    "the section name of the configuration element for the Talifun.CssSprite configuration. Defaults to 'CssSprite' if not specified.",
                    css => cssSpriteSectionName = css 
                },
                {
                    "a=|applicationPath=",
                    "the application path to be relative from. Defaults to  '/' if not specified.",
                    a => applicationPath = a
                },
                {
                    "?|h|help", 
                    "display help screen",
                    h => showHelp = h != null
                }
            };
                
            try
            {
                options.Parse(args);
            }
            catch(OptionException e)
            {
                Console.WriteLine(HeaderMessage);
                Console.WriteLine(e.Message);
                Console.WriteLine(UsageMessage);
                Console.WriteLine(HelpMessage);
				return DisplayHelpScreenExitCode;
            }

            if (showHelp)
            {
                DisplayHelp(options);
				return DisplayHelpScreenExitCode;
            }

            if (string.IsNullOrEmpty(configPath))
            {
                Console.WriteLine(HeaderMessage);
                Console.WriteLine(UsageMessage);
                Console.WriteLine(HelpMessage);
				return DisplayHelpScreenExitCode;
            }

            var crusherConfiguration = GetCrusherSection(configPath, crusherSectionName);

			if (crusherConfiguration == null)
            {
                Console.WriteLine(HeaderMessage);
				Console.WriteLine("\"{0}\" section name not found in {1} ", crusherSectionName, configPath);
                Console.WriteLine(HelpMessage);
				return DisplayHelpScreenExitCode;
            }

        	try
        	{
				Console.WriteLine();
				Console.WriteLine("Settings used:");
				Console.WriteLine("configPath = " + configPath);
				Console.WriteLine("crusherSectionName = " + crusherSectionName);
				Console.WriteLine("applicationPath = " + applicationPath);

        	    var configUri = new Uri(configPath, UriKind.RelativeOrAbsolute);
                if (!configUri.IsAbsoluteUri)
                {
                    configUri = new Uri(Path.Combine(Environment.CurrentDirectory, configUri.ToString()));
                }

                var physicalApplicationPath = new FileInfo(configUri.LocalPath).DirectoryName;

				var retryableFileOpener = new RetryableFileOpener();
				var hasher = new Md5Hasher(retryableFileOpener);
				var retryableFileWriter = new RetryableFileWriter(BufferSize, Encoding, retryableFileOpener, hasher);
				var pathProvider = new PathProvider(applicationPath, physicalApplicationPath);
				var cacheManager = new HttpCacheManager();

                var fileMetaData = new MultiFileMetaData(retryableFileOpener, retryableFileWriter);

        	    var jsOutput = string.Empty;
        	    var cssOutput = string.Empty;
                var cssSpriteOutput = string.Empty;
     
                //We want to be able to use output from css sprites in crushed content
                var countdownEvents = new CountdownEvent(3);

                var cssSpriteExceptions = new List<CssSpriteException>();
                ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        if (crusherConfiguration != null)
                        {
                            var cssSpriteGroups = crusherConfiguration.CssSpriteGroups;
                            var cssSpriteCreator = new CssSpriteCreator(cacheManager, retryableFileOpener, pathProvider, retryableFileWriter, fileMetaData);
                            var cssSpriteGroupsProcessor = new CssSpriteGroupsProcessor();

                            cssSpriteOutput = cssSpriteGroupsProcessor.ProcessGroups(pathProvider, cssSpriteCreator, cssSpriteGroups).ToString();
                        }
                    }
                    catch (Exception exception)
                    {
                        cssSpriteExceptions.Add(new CssSpriteException(exception));
                    }
                    manualResetEvent.Signal();
                }, countdownEvents);

                var jsExceptions = new List<JsException>();
                ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        if (crusherConfiguration != null)
                        {
                            var jsCrusher = new JsCrusher(cacheManager, pathProvider, retryableFileOpener, retryableFileWriter, fileMetaData);
                            var jsGroups = crusherConfiguration.JsGroups;
                            var jsGroupsProcessor = new JsGroupsProcessor();

                            jsOutput = jsGroupsProcessor.ProcessGroups(pathProvider, jsCrusher, jsGroups).ToString();
                        }
                    }
                    catch (Exception exception)
                    {
                        jsExceptions.Add(new JsException(exception));
                    }
                    manualResetEvent.Signal();
                }, countdownEvents);

                var cssExceptions = new List<CssException>();
                ThreadPool.QueueUserWorkItem(data =>
                {
                    var manualResetEvent = (CountdownEvent)data;
                    try
                    {
                        if (crusherConfiguration != null)
                        {
                            var hashQueryStringKeyName = crusherConfiguration.QuerystringKeyName;
                            var cssAssetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, hasher, pathProvider);
                            var cssPathRewriter = new CssPathRewriter(cssAssetsFileHasher, pathProvider);
                            var cssCrusher = new CssCrusher(cacheManager, pathProvider, retryableFileOpener, retryableFileWriter, cssPathRewriter, fileMetaData, crusherConfiguration.WatchAssets);
                            var cssGroups = crusherConfiguration.CssGroups;
                            var cssGroupsCrusher = new CssGroupsProcessor();
                            cssOutput = cssGroupsCrusher.ProcessGroups(pathProvider, cssCrusher, cssGroups).ToString();
                        }
                    }
                    catch (Exception exception)
                    {
                        cssExceptions.Add(new CssException(exception));
                    }
                    manualResetEvent.Signal();
                }, countdownEvents);

        	    countdownEvents.Wait();

                if (string.IsNullOrEmpty(cssSpriteOutput) && !cssSpriteExceptions.Any())
                {
                    Console.WriteLine();
                    Console.WriteLine("Skipping css sprite creation. \"{0}\" section name not found in \"{1}\"", cssSpriteSectionName, configPath);
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine(cssSpriteOutput);

                    if (cssSpriteExceptions.Any())
                    {
                        Console.WriteLine("Css sprite errors:");
                        Console.WriteLine(new AggregateException(cssSpriteExceptions.Cast<Exception>()));
                    }
                }

                if (string.IsNullOrEmpty(jsOutput) && string.IsNullOrEmpty(cssOutput) && !jsExceptions.Any() && !cssExceptions.Any())
                {
                    Console.WriteLine();
                    Console.WriteLine("Skipping css/js crushed content creation. \"{0}\" section name not found in \"{1}\"", crusherSectionName, configPath);
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine(cssOutput);
                    if (cssExceptions.Any())
                    {
                        Console.WriteLine("Css errors:");
                        Console.WriteLine(new AggregateException(cssExceptions.Cast<Exception>()));
                    }

                    Console.WriteLine();
                    Console.WriteLine(jsOutput);
                    if (jsExceptions.Any())
                    {
                        Console.WriteLine("Js errors:");
                        Console.WriteLine(new AggregateException(jsExceptions.Cast<Exception>()));
                    }
                }
			}
			catch (Exception exception)
			{
				Console.Write(exception);
				return ErrorExitCode;
			}

			return SuccessExitCode;
        }
        public static void StaticWorkerFor(ParallelTasksStaticWorkerForInput input)
        {
            if (input.TasksLow < 0 || input.TasksHigh < input.TasksLow) return;

            ModuleProc PROC = new ModuleProc("ParallelTasks", "StaticFor");
            if (input.WorkerCount <= 0)
            {
                if (input.Chunk > 0) { input.WorkerCount = (int)Math.Max(Math.Ceiling(Convert.ToDouble(input.TasksHigh) / Convert.ToDouble(input.Chunk)), 1); }
                else { input.WorkerCount = 1; }
            }
            if (input.Chunk <= 0)
            {
                input.Chunk = (input.TasksHigh / input.WorkerCount);
            }
            if (input.Chunk <= 0) input.Chunk = 1;
            if (input.TasksHigh < input.Chunk) input.Chunk = ((input.TasksHigh - input.TasksLow) / input.WorkerCount);

            CountdownEvent cde = new CountdownEvent(input.WorkerCount);
            Thread[] threads = new Thread[input.WorkerCount];
            int currentCount = 0;
            ParallelTaskOptions options = new ParallelTaskOptions();
            WorkerItem item = new WorkerItem()
            {
                ExecutorService = input.Executor,
                Result = new ParallelTaskResult(ParallelTaskResultStatus.Created),
                Completed = input.WorkCompleted,
                EventHandle = cde,
            };

            try
            {
                for (int i = 0; i < input.WorkerCount; i++)
                {
                    threads[i] = Extensions.CreateThreadAndStart((o) =>
                    {
                        int k = (int)o;
                        int start = input.TasksLow + (k * input.Chunk);
                        int end = ((k == (input.WorkerCount - 1)) ? input.TasksHigh : (start + input.Chunk));

                        // work input.Chunk started
                        if (input.WorkChunkStarted != null)
                        {
                            try
                            {
                                input.WorkChunkStarted(new ParallelTaskWorkChunkStartArgs()
                                {
                                    Options = options,
                                    ThreadIndex = i,
                                    ChunkStart = start,
                                    ChunkEnd = end,
                                    SeqStart = 0,
                                    SeqEnd = (end - 1 - start),
                                });
                            }
                            catch (Exception ex)
                            {
                                Log.Exception(PROC, ex);
                                item.Result.Exceptions.Add(ex);
                            }
                        }

                        // work
                        int chunkProgress = start;
                        for (int j = start, sj = 0; j < end; j++, sj++)
                        {
                            if ((input.Executor != null && input.Executor.IsShutdown)
                                || options.IsCancelled) break;
                            chunkProgress = j;

                            try
                            {
                                int proIdx = Interlocked.Increment(ref currentCount);
                                int proPerc = (int)(((float)proIdx / (float)input.TasksHigh) * 100.0);
                                string text = string.Format("{0:D} of {1:D} ({2:D} %)", proIdx, input.TasksHigh, proPerc);
                                input.Work(new ParallelTaskWorkArgs()
                                {
                                    Options = options,
                                    ThreadIndex = k,
                                    ChunkProgress = j,
                                    ChunkSeqProgress = sj,
                                    OverallProgress = proIdx,
                                    Total = input.TasksHigh,
                                    ProgressText = text,
                                });
                            }
                            catch (Exception ex)
                            {
                                Log.Exception(PROC, ex);
                                item.Result.Exceptions.Add(ex);
                            }
                            finally
                            {
                                Interlocked.Increment(ref currentCount);
                            }

                            Thread.Sleep(input.SleepInMilliseconds);
                        }

                        // work input.Chunk completed
                        if (input.WorkChunkCompleted != null)
                        {
                            try
                            {
                                input.WorkChunkCompleted(new ParallelTaskWorkChunkCompletedArgs()
                                {
                                    Options = options,
                                    ThreadIndex = i,
                                    ChunkProgress = chunkProgress,
                                    OverallProgress = currentCount,
                                });
                            }
                            catch (Exception ex)
                            {
                                Log.Exception(PROC, ex);
                                item.Result.Exceptions.Add(ex);
                            }
                        }

                        cde.Signal();
                    }, i, "StaticFor_" + i.ToString() + "_");
                    Thread.Sleep(10);
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
            finally
            {
                Extensions.CreateThreadAndStart((o) =>
                {
                    WorkerItem wi = o as WorkerItem;
                    wi.EventHandle.Wait();
                    wi.Result.Status = (((input.Executor != null && input.Executor.IsShutdown)
                                        || options.IsCancelled) ? ParallelTaskResultStatus.Canceled : ParallelTaskResultStatus.Completed);
                    if (wi.Completed != null)
                    {
                        wi.Completed(new ParallelTaskWorkCompletedArgs()
                        {
                            Result = wi.Result,
                        });
                    }
                }, item, "StaticFor_Wait_");
            }
        }
 public ThreadAnonymousInnerClassHelper(TestControlledRealTimeReopenThread outerInstance, CountdownEvent latch, CountdownEvent signal, TrackingIndexWriter writer, SearcherManager manager)
 {
     this.OuterInstance = outerInstance;
     this.Latch         = latch;
     this.Signal        = signal;
     this.Writer        = writer;
     this.Manager       = manager;
 }
Example #19
0
        private void WaitForBatchAllowingPartialBatchResumption(CountdownEvent completionEvent, BatchStatistics batch, List <ThreadTask> childTasks, DocumentDatabase database = null)
        {
            Interlocked.Increment(ref _hasPartialBatchResumption);
            var cancellationToken = database?.WorkContext.CancellationToken ?? _ct;

            try
            {
                var waitHandles            = new[] { completionEvent.WaitHandle, _threadHasNoWorkToDo };
                var sp                     = Stopwatch.StartNew();
                var batchLeftEarly         = false;
                var lastThreadIndexChecked = 0;
                while (cancellationToken.IsCancellationRequested == false)
                {
                    // First, try to find work to do among child tasks, instead of just waiting
                    ThreadTask busyWaitWorkToDo = null;
                    for (; lastThreadIndexChecked < childTasks.Count && completionEvent.IsSet == false && _ct.IsCancellationRequested == false; lastThreadIndexChecked++)
                    {
                        var task = childTasks[lastThreadIndexChecked];
                        if (Interlocked.CompareExchange(ref task.Proccessing, 1, 0) == 1)
                        {
                            continue;
                        }
                        busyWaitWorkToDo = task;
                        break;
                    }

                    int returnedWaitHandleIndex;

                    // Run found work or just wait for all work to finish (returnedWaitHandleIndex=0) or to be notified about free threads in the system (returnedWaitHandleIndex=1)
                    // After which we'll decide whether we should early exit, or to wait some more
                    if (busyWaitWorkToDo != null)
                    {
                        RunThreadTask(busyWaitWorkToDo);
                        returnedWaitHandleIndex = WaitHandle.WaitAny(waitHandles, 0);
                    }
                    else
                    {
                        returnedWaitHandleIndex = WaitHandle.WaitAny(waitHandles);
                    }

                    if (returnedWaitHandleIndex == 0)
                    {
                        break;
                    }

                    // we won't consider breaking early if we haven't completed at least half the work
                    if (Thread.VolatileRead(ref batch.Completed) < batch.Total / 2)
                    {
                        continue;
                    }

                    var currentFreeThreads = _freedThreadsValue.Values.Count(isFree => isFree);

                    // we will break early only if there are more then half free threads
                    if (currentFreeThreads > _currentWorkingThreadsAmount / 2)
                    {
                        break;
                    }
                }

                // after we've decided to quit early, we will wait some more time, allowing a normal wait.
                // we decide how much we will wait by choosing the biggest among the next:
                // 1) half the time we've waited on the current batch
                // 2) a waiting time factor that increase for every second left early batch and decreases for every leave early batch that completed before leaving early

                var elapsedMilliseconds = (int)sp.ElapsedMilliseconds;
                if (completionEvent.Wait(Math.Max(elapsedMilliseconds / 2, Thread.VolatileRead(ref _partialMaxWait)), cancellationToken))
                {
                    _concurrentEvents.Enqueue(completionEvent);
                }

                // updating the waiting time factor
                if (batch.Completed != batch.Total)
                {
                    batchLeftEarly = true;
                    if (_partialMaxWaitChangeFlag > 0)
                    {
                        Interlocked.Exchange(ref _partialMaxWait, Math.Min(2500, (int)(Thread.VolatileRead(ref _partialMaxWait) * 1.25)));
                    }
                }
                else if (_partialMaxWaitChangeFlag < 0)
                {
                    Interlocked.Exchange(ref _partialMaxWait, Math.Max(Thread.VolatileRead(ref _partialMaxWait) / 2, 10));
                }

                Interlocked.Exchange(ref _partialMaxWaitChangeFlag, Thread.VolatileRead(ref _partialMaxWaitChangeFlag) * -1);

                // completionEvent is explicitly left to the finalizer
                // we expect this to be rare, and it isn't worth the trouble of trying
                // to manage this

                if (logger.IsDebugEnabled)
                {
                    logger.Debug($"Raven Thread Pool ended batch for database {database?.Name}. Done {batch.Completed} items out of {batch.Total}. Batch {(batchLeftEarly ? "was" : "was not")} left early");
                }
            }
            finally
            {
                Interlocked.Decrement(ref _hasPartialBatchResumption);
            }
        }
Example #20
0
        public void ExecuteBatch <T>(IList <T> src, Action <T> action, DocumentDatabase database = null, string description = null,
                                     bool allowPartialBatchResumption = false)
        {
            //, int completedMultiplier = 2, int freeThreadsMultiplier = 2, int maxWaitMultiplier = 1
            switch (src.Count)
            {
            case 0:
                return;

            case 1:
                //if we have only one source to go through,
                //we should execute it in the current thread, without using RTP threads
                ExecuteSingleBatchSynchronously(src, action, description, database);
                return;
            }

            var            now = DateTime.UtcNow;
            CountdownEvent countdownEvent;
            var            itemsCount = 0;

            var batch = new BatchStatistics
            {
                Total     = src.Count,
                Completed = 0
            };

            if (_concurrentEvents.TryDequeue(out countdownEvent) == false)
            {
                countdownEvent = new CountdownEvent(src.Count);
            }
            else
            {
                countdownEvent.Reset(src.Count);
            }

            var exceptions        = new ConcurrentSet <Exception>();
            var currentBatchTasks = new List <ThreadTask>(src.Count);

            for (; itemsCount < src.Count && _ct.IsCancellationRequested == false; itemsCount++)
            {
                var copy       = itemsCount;
                var threadTask = new ThreadTask
                {
                    Action = () =>
                    {
                        try
                        {
                            database?.WorkContext.CancellationToken.ThrowIfCancellationRequested();
                            action(src[copy]);
                        }
                        catch (Exception e)
                        {
                            exceptions.Add(e);
                            throw;
                        }
                        finally
                        {
                            countdownEvent.Signal();
                        }
                    },
                    Description = new OperationDescription
                    {
                        Type      = OperationDescription.OperationType.Atomic,
                        PlainText = description,
                        From      = itemsCount + 1,
                        To        = itemsCount + 1,
                        Total     = src.Count
                    },
                    Database    = database,
                    BatchStats  = batch,
                    EarlyBreak  = allowPartialBatchResumption,
                    QueuedAt    = now,
                    DoneEvent   = countdownEvent,
                    Proccessing = 0
                };
                _tasks.Add(threadTask);
                currentBatchTasks.Add(threadTask);
            }

            if (allowPartialBatchResumption == false)
            {
                WaitForBatchToCompletion(countdownEvent, currentBatchTasks, database?.WorkContext.CancellationToken);
            }
            else
            {
                WaitForBatchAllowingPartialBatchResumption(countdownEvent, batch, currentBatchTasks, database);
            }

            switch (exceptions.Count)
            {
            case 0:
                return;

            case 1:
                ExceptionDispatchInfo.Capture(exceptions.First()).Throw();
                break;

            default:
                throw new AggregateException(exceptions);
            }
        }
Example #21
0
        /// <summary>
        /// Executes given function in batches on received collection
        /// </summary>
        public void ExecuteBatch <T>(IList <T> src, Action <IEnumerator <T> > action, DocumentDatabase database = null,
                                     int pageSize = defaultPageSize, string description = null)
        {
            if (src.Count == 0)
            {
                return;
            }

            if (src.Count <= pageSize)
            {
                //if we have only none or less than pageSize,
                //we should execute it in the current thread, without using RTP threads
                ExecuteSingleBatchSynchronously(src, action, description, database);
                return;
            }

            var ranges       = new ConcurrentQueue <Tuple <int, int> >();
            var now          = DateTime.UtcNow;
            var numOfBatches = (src.Count / pageSize) + (src.Count % pageSize == 0 ? 0 : 1);

            CountdownEvent batchesCountdown;

            if (_concurrentEvents.TryDequeue(out batchesCountdown) == false)
            {
                batchesCountdown = new CountdownEvent(numOfBatches);
            }
            else
            {
                batchesCountdown.Reset(numOfBatches);
            }

            var localTasks = new List <ThreadTask>();
            var exceptions = new ConcurrentSet <Exception>();

            for (var i = 0; i < src.Count; i += pageSize)
            {
                var rangeStart = i;
                var rangeEnd   = i + pageSize - 1 < src.Count ? i + pageSize - 1 : src.Count - 1;
                ranges.Enqueue(Tuple.Create(rangeStart, rangeEnd));

                var threadTask = new ThreadTask
                {
                    Action = () =>
                    {
                        var numOfBatchesUsed = new Reference <int>();
                        try
                        {
                            database?.WorkContext.CancellationToken.ThrowIfCancellationRequested();
                            Tuple <int, int> range;
                            if (ranges.TryDequeue(out range) == false)
                            {
                                return;
                            }

                            action(YieldFromRange(ranges, range, src, numOfBatchesUsed, database?.WorkContext.CancellationToken));
                        }
                        catch (Exception e)
                        {
                            exceptions.Add(e);
                            throw;
                        }
                        finally
                        {
                            // we will try to release the countdown event if we got here because cancellation token was requested, else wi will signal the countdown event according to the number of batches we've proccessed
                            if (database?.WorkContext.CancellationToken.IsCancellationRequested ?? false)
                            {
                                try
                                {
                                    while (batchesCountdown.CurrentCount != 0)
                                    {
                                        batchesCountdown.Signal(1);
                                    }
                                }
                                catch
                                {
                                }
                            }
                            else
                            {
                                var numOfBatchesUsedValue = numOfBatchesUsed.Value;
                                //handle the case when we are out of ranges
                                if (numOfBatchesUsedValue > 0)
                                {
                                    try
                                    {
                                        batchesCountdown.Signal(numOfBatchesUsedValue);
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                            }
                        }
                    },
                    Description = new OperationDescription
                    {
                        Type      = OperationDescription.OperationType.Range,
                        PlainText = description,
                        From      = rangeStart,
                        To        = rangeEnd,
                        Total     = src.Count
                    },
                    Database   = database,
                    BatchStats = new BatchStatistics
                    {
                        Total     = rangeEnd - rangeStart,
                        Completed = 0
                    },
                    QueuedAt  = now,
                    DoneEvent = batchesCountdown,
                };
                localTasks.Add(threadTask);
            }

            // we must add the tasks to the global tasks after we added all the ranges
            // to prevent the tasks from completing the range fast enough that it won't
            // see the next range, see: http://issues.hibernatingrhinos.com/issue/RavenDB-4829
            foreach (var threadTask in localTasks)
            {
                _tasks.Add(threadTask, _ct);
            }

            WaitForBatchToCompletion(batchesCountdown, localTasks, database?.WorkContext.CancellationToken);

            switch (exceptions.Count)
            {
            case 0:
                return;

            case 1:
                ExceptionDispatchInfo.Capture(exceptions.First()).Throw();
                break;

            default:
                throw new AggregateException(exceptions);
            }
        }
Example #22
0
        public static void Main()
        {
            var cfg = new IgniteConfiguration
            {
                SpringConfigUrl = @"platforms\dotnet\examples\config\examples-config.xml",
                JvmOptions      = new List <string> {
                    "-Xms512m", "-Xmx512m"
                }
            };

            using (var ignite = Ignition.Start(cfg))
            {
                var remotes = ignite.GetCluster().ForRemotes();

                if (remotes.GetNodes().Count == 0)
                {
                    Console.WriteLine(">>> This example requires remote nodes to be started.");
                    Console.WriteLine(">>> Please start at least 1 remote node.");
                    Console.WriteLine(">>> Refer to example's documentation for details on configuration.");
                }
                else
                {
                    Console.WriteLine(">>> Messaging example started.");
                    Console.WriteLine();

                    // Set up local listeners
                    var localMessaging = ignite.GetCluster().ForLocal().GetMessaging();

                    var msgCount = remotes.GetNodes().Count * 10;

                    var orderedCounter   = new CountdownEvent(msgCount);
                    var unorderedCounter = new CountdownEvent(msgCount);

                    localMessaging.LocalListen(new LocalListener(unorderedCounter), Topic.Unordered);

                    localMessaging.LocalListen(new LocalListener(orderedCounter), Topic.Ordered);

                    // Set up remote listeners
                    var remoteMessaging = remotes.GetMessaging();

                    var idUnordered = remoteMessaging.RemoteListen(new RemoteUnorderedListener(), Topic.Unordered);
                    var idOrdered   = remoteMessaging.RemoteListen(new RemoteOrderedListener(), Topic.Ordered);

                    // Send unordered
                    Console.WriteLine(">>> Sending unordered messages...");

                    for (var i = 0; i < 10; i++)
                    {
                        remoteMessaging.Send(i, Topic.Unordered);
                    }

                    Console.WriteLine(">>> Finished sending unordered messages.");

                    // Send ordered
                    Console.WriteLine(">>> Sending ordered messages...");

                    for (var i = 0; i < 10; i++)
                    {
                        remoteMessaging.SendOrdered(i, Topic.Ordered);
                    }

                    Console.WriteLine(">>> Finished sending ordered messages.");

                    Console.WriteLine(">>> Check output on all nodes for message printouts.");
                    Console.WriteLine(">>> Waiting for messages acknowledgements from all remote nodes...");

                    unorderedCounter.Wait();
                    orderedCounter.Wait();

                    // Unsubscribe
                    remoteMessaging.StopRemoteListen(idUnordered);
                    remoteMessaging.StopRemoteListen(idOrdered);
                }
            }

            Console.WriteLine();
            Console.WriteLine(">>> Example finished, press any key to exit ...");
            Console.ReadKey();
        }
Example #23
0
 public SearcherFactoryAnonymousInnerClassHelper2(TestSearcherManager outerInstance, CountdownEvent awaitEnterWarm, CountdownEvent awaitClose, AtomicBoolean triedReopen, TaskScheduler es)
 {
     this.outerInstance  = outerInstance;
     this.awaitEnterWarm = awaitEnterWarm;
     this.awaitClose     = awaitClose;
     this.triedReopen    = triedReopen;
     this.es             = es;
 }
Example #24
0
 public ThreadAnonymousInnerClassHelper(TestMixedDocValuesUpdates outerInstance, string str, IndexWriter writer, int numDocs, CountdownEvent done, AtomicInt32 numUpdates, string f, string cf)
     : base(str)
 {
     this.OuterInstance = outerInstance;
     this.Writer        = writer;
     this.NumDocs       = numDocs;
     this.Done          = done;
     this.NumUpdates    = numUpdates;
     this.f             = f;
     this.Cf            = cf;
 }
Example #25
0
        private void RunChildren()
        {
            var children = new List<WorkItem>();

            foreach (Test test in _suite.Tests)
                if (_childFilter.Pass(test))
                    children.Add(WorkItem.CreateWorkItem(test, new TestExecutionContext(this.Context), _childFilter));

            if (children.Count > 0)
            {
                _childTestCountdown = new CountdownEvent(children.Count);

                foreach (WorkItem child in children)
                {
                    child.Completed += new EventHandler(OnChildCompleted);
#if NUNITLITE
                    child.Execute();
#else
                    if (Context.Dispatcher == null)
                        child.Execute();
                    // For now, run all test cases on the same thread as the fixture
                    // in order to handle ApartmentState preferences set on the fixture.
                    else if (child is SimpleWorkItem || child.Test is ParameterizedMethodSuite)
                    {
                        log.Debug("Executing WorkItem for {0}", child.Test.FullName);
                        child.Execute();
                    }
                    else
                        Context.Dispatcher.Dispatch(child);
#endif
                }
            }
        }
 public LatchedIndexWriter(Directory d, IndexWriterConfig conf, CountdownEvent latch, CountdownEvent signal)
     : base(d, conf)
 {
     this.Latch  = latch;
     this.Signal = signal;
 }
        public static ParallelTaskResult StaticFor(int low, int high,
            Action<ParallelTaskOptions, int, int> work, int workerCount, int chunk)
        {
            if (low < 0 || high < low) return new ParallelTaskResult();

            ModuleProc PROC = new ModuleProc("ParallelTasks", "StaticFor");
            ParallelTaskResult result = new ParallelTaskResult(ParallelTaskResultStatus.Created);
            if (workerCount <= 0) workerCount = 1;
            if (chunk <= 0) chunk = 1;
            if (high < chunk) chunk = ((high - low) / workerCount);

            CountdownEvent cde = new CountdownEvent(workerCount);
            Thread[] threads = new Thread[workerCount];
            int currentCount = 0;
            ParallelTaskOptions options = new ParallelTaskOptions();

            try
            {
                for (int i = 0; i < workerCount; i++)
                {
                    threads[i] = Extensions.CreateThreadAndStart((o) =>
                    {
                        int k = (int)o;
                        int start = low + (k * chunk);
                        int end = ((k == (workerCount - 1)) ? high : (start + chunk));

                        for (int j = start; j < end; j++)
                        {
                            if (options.IsCancelled) break;

                            try
                            {
                                work(options, j, currentCount);
                            }
                            catch (Exception ex)
                            {
                                Log.Exception(PROC, ex);
                                result.Exceptions.Add(ex);
                            }
                            finally
                            {
                                Interlocked.Increment(ref currentCount);
                            }
                        }

                        cde.Signal();
                    }, i, "StaticFor_" + i.ToString());
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
            finally
            {
                cde.Wait();
                result.Status = (options.IsCancelled ? ParallelTaskResultStatus.Canceled : ParallelTaskResultStatus.Completed);
            }

            return result;
        }
Example #28
0
 private void refreshData(CountdownEvent latch)
 {
     latch.Signal();
 }
Example #29
0
        public virtual void TestIntermediateClose()
        {
            Directory dir = NewDirectory();
            // Test can deadlock if we use SMS:
            IConcurrentMergeScheduler scheduler;

#if !FEATURE_CONCURRENTMERGESCHEDULER
            scheduler = new TaskMergeScheduler();
#else
            scheduler = new ConcurrentMergeScheduler();
#endif
            IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random)).SetMergeScheduler(scheduler));
            writer.AddDocument(new Document());
            writer.Commit();
            CountdownEvent awaitEnterWarm = new CountdownEvent(1);
            CountdownEvent awaitClose     = new CountdownEvent(1);
            AtomicBoolean  triedReopen    = new AtomicBoolean(false);
            //TaskScheduler es = Random().NextBoolean() ? null : Executors.newCachedThreadPool(new NamedThreadFactory("testIntermediateClose"));
            TaskScheduler   es              = Random.NextBoolean() ? null : TaskScheduler.Default;
            SearcherFactory factory         = new SearcherFactoryAnonymousInnerClassHelper2(this, awaitEnterWarm, awaitClose, triedReopen, es);
            SearcherManager searcherManager = Random.NextBoolean() ? new SearcherManager(dir, factory) : new SearcherManager(writer, Random.NextBoolean(), factory);
            if (VERBOSE)
            {
                Console.WriteLine("sm created");
            }
            IndexSearcher searcher = searcherManager.Acquire();
            try
            {
                assertEquals(1, searcher.IndexReader.NumDocs);
            }
            finally
            {
                searcherManager.Release(searcher);
            }
            writer.AddDocument(new Document());
            writer.Commit();
            AtomicBoolean success = new AtomicBoolean(false);
            Exception[]   exc     = new Exception[1];
            ThreadJob     thread  = new ThreadJob(() => new RunnableAnonymousInnerClassHelper(this, triedReopen, searcherManager, success, exc).Run());
            thread.Start();
            if (VERBOSE)
            {
                Console.WriteLine("THREAD started");
            }
            awaitEnterWarm.Wait();
            if (VERBOSE)
            {
                Console.WriteLine("NOW call close");
            }
            searcherManager.Dispose();
            awaitClose.Signal();
            thread.Join();
            try
            {
                searcherManager.Acquire();
                fail("already closed");
            }
#pragma warning disable 168
            catch (ObjectDisposedException ex)
#pragma warning restore 168
            {
                // expected
            }
            assertFalse(success);
            assertTrue(triedReopen);
            assertNull("" + exc[0], exc[0]);
            writer.Dispose();
            dir.Dispose();
            //if (es != null)
            //{
            //    es.shutdown();
            //    es.awaitTermination(1, TimeUnit.SECONDS);
            //}
        }
Example #30
0
 public AwaitLatch(CountdownEvent latch)
 {
     Latch = latch;
 }
Example #31
0
        public Horizon(Switch Device)
        {
            this.Device = Device;

            State = new SystemStateMgr();

            ResourceLimit = new KResourceLimit(this);

            KernelInit.InitializeResourceLimit(ResourceLimit);

            MemoryRegions = KernelInit.GetMemoryRegions();

            LargeMemoryBlockAllocator = new KMemoryBlockAllocator(MemoryBlockAllocatorSize * 2);
            SmallMemoryBlockAllocator = new KMemoryBlockAllocator(MemoryBlockAllocatorSize);

            UserSlabHeapPages = new KSlabHeap(
                UserSlabHeapBase,
                UserSlabHeapItemSize,
                UserSlabHeapSize);

            CriticalSection = new KCriticalSection(this);

            Scheduler = new KScheduler(this);

            TimeManager = new KTimeManager();

            Synchronization = new KSynchronization(this);

            ContextIdManager = new KContextIdManager();

            KipId     = InitialKipId;
            ProcessId = InitialProcessId;

            Scheduler.StartAutoPreemptionThread();

            KernelInitialized = true;

            ThreadCounter = new CountdownEvent(1);

            Processes = new SortedDictionary <long, KProcess>();

            AutoObjectNames = new ConcurrentDictionary <string, KAutoObject>();

            //Note: This is not really correct, but with HLE of services, the only memory
            //region used that is used is Application, so we can use the other ones for anything.
            KMemoryRegionManager Region = MemoryRegions[(int)MemoryRegion.NvServices];

            ulong HidPa  = Region.Address;
            ulong FontPa = Region.Address + HidSize;

            HidBaseAddress = (long)(HidPa - DramMemoryMap.DramBase);

            KPageList HidPageList  = new KPageList();
            KPageList FontPageList = new KPageList();

            HidPageList.AddRange(HidPa, HidSize / KMemoryManager.PageSize);
            FontPageList.AddRange(FontPa, FontSize / KMemoryManager.PageSize);

            HidSharedMem  = new KSharedMemory(HidPageList, 0, 0, MemoryPermission.Read);
            FontSharedMem = new KSharedMemory(FontPageList, 0, 0, MemoryPermission.Read);

            AppletState = new AppletStateMgr(this);

            AppletState.SetFocus(true);

            Font = new SharedFontManager(Device, (long)(FontPa - DramMemoryMap.DramBase));

            VsyncEvent = new KEvent(this);

            LoadKeySet();

            ContentManager = new ContentManager(Device);
        }
Example #32
0
 public Meet(CountdownEvent acknowledge, CountdownEvent waitFor)
 {
     Acknowledge = acknowledge;
     WaitFor     = waitFor;
 }
Example #33
0
        public virtual void Test()
        {
            IList <string> postingsList   = new List <string>();
            int            numTerms       = AtLeast(300);
            int            maxTermsPerDoc = TestUtil.NextInt32(Random, 10, 20);
            bool           isSimpleText   = "SimpleText".Equals(TestUtil.GetPostingsFormat("field"), StringComparison.Ordinal);

            IndexWriterConfig iwc = NewIndexWriterConfig(Random, TEST_VERSION_CURRENT, new MockAnalyzer(Random));

            if ((isSimpleText || iwc.MergePolicy is MockRandomMergePolicy) && (TEST_NIGHTLY || RANDOM_MULTIPLIER > 1))
            {
                // Otherwise test can take way too long (> 2 hours)
                numTerms /= 2;
            }
            if (VERBOSE)
            {
                Console.WriteLine("maxTermsPerDoc=" + maxTermsPerDoc);
                Console.WriteLine("numTerms=" + numTerms);
            }
            for (int i = 0; i < numTerms; i++)
            {
                string term = Convert.ToString(i, CultureInfo.InvariantCulture);
                for (int j = 0; j < i; j++)
                {
                    postingsList.Add(term);
                }
            }

            postingsList.Shuffle(Random);

            ConcurrentQueue <string> postings = new ConcurrentQueue <string>(postingsList);

            Directory dir = NewFSDirectory(CreateTempDir(GetFullMethodName()));

            RandomIndexWriter iw = new RandomIndexWriter(Random, dir, iwc);

            int threadCount = TestUtil.NextInt32(Random, 1, 5);

            if (VERBOSE)
            {
                Console.WriteLine("config: " + iw.IndexWriter.Config);
                Console.WriteLine("threadCount=" + threadCount);
            }

            Field     prototype = NewTextField("field", "", Field.Store.NO);
            FieldType fieldType = new FieldType(prototype.FieldType);

            if (Random.NextBoolean())
            {
                fieldType.OmitNorms = true;
            }
            int options = Random.Next(3);

            if (options == 0)
            {
                fieldType.IndexOptions     = IndexOptions.DOCS_AND_FREQS; // we dont actually need positions
                fieldType.StoreTermVectors = true;                        // but enforce term vectors when we do this so we check SOMETHING
            }
            else if (options == 1 && !DoesntSupportOffsets.Contains(TestUtil.GetPostingsFormat("field")))
            {
                fieldType.IndexOptions = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS;
            }
            // else just positions

            ThreadJob[]    threads     = new ThreadJob[threadCount];
            CountdownEvent startingGun = new CountdownEvent(1);

            for (int threadID = 0; threadID < threadCount; threadID++)
            {
                Random   threadRandom = new Random(Random.Next());
                Document document     = new Document();
                Field    field        = new Field("field", "", fieldType);
                document.Add(field);
                threads[threadID] = new ThreadAnonymousInnerClassHelper(this, numTerms, maxTermsPerDoc, postings, iw, startingGun, threadRandom, document, field);
                threads[threadID].Start();
            }
            startingGun.Signal();
            foreach (ThreadJob t in threads)
            {
                t.Join();
            }

            iw.ForceMerge(1);
            DirectoryReader ir = iw.GetReader();

            Assert.AreEqual(1, ir.Leaves.Count);
            AtomicReader air   = (AtomicReader)ir.Leaves[0].Reader;
            Terms        terms = air.GetTerms("field");

            // numTerms-1 because there cannot be a term 0 with 0 postings:
            Assert.AreEqual(numTerms - 1, terms.Count);
            TermsEnum termsEnum = terms.GetIterator(null);
            BytesRef  termBR;

            while ((termBR = termsEnum.Next()) != null)
            {
                int value = Convert.ToInt32(termBR.Utf8ToString(), CultureInfo.InvariantCulture);
                Assert.AreEqual(value, termsEnum.TotalTermFreq);
                // don't really need to check more than this, as CheckIndex
                // will verify that totalTermFreq == total number of positions seen
                // from a docsAndPositionsEnum.
            }
            ir.Dispose();
            iw.Dispose();
            dir.Dispose();
        }
Example #34
0
 public WaitAck(long time, CountdownEvent latch)
 {
     Time  = time;
     Latch = latch;
 }
Example #35
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (_isDisposed)
                return;

            base.Dispose(disposing);

            if (disposing)
            {
                var session = Session;
                if (session != null)
                {
                    Session = null;
                    session.RequestSuccessReceived -= Session_RequestSuccess;
                    session.RequestFailureReceived -= Session_RequestFailure;
                    session.ChannelOpenReceived -= Session_ChannelOpening;
                }

                var globalRequestResponse = _globalRequestResponse;
                if (globalRequestResponse != null)
                {
                    _globalRequestResponse = null;
                    globalRequestResponse.Dispose();
                }

                var pendingRequestsCountdown = _pendingChannelCountdown;
                if (pendingRequestsCountdown != null)
                {
                    _pendingChannelCountdown = null;
                    pendingRequestsCountdown.Dispose();
                }
            }

            _isDisposed = true;
        }
Example #36
0
 protected void AssertCountdown(CountdownEvent latch, int wait, string hint)
 {
     Assert.True(latch.Wait(wait), $"Failed to count down within {wait} milliseconds." + hint);
 }
Example #37
0
            /// <param name="outerInstance">
            /// LUCENENET specific
            /// Passed in because this class acceses non-static methods,
            /// NewTextField and NewIndexWriterConfig
            /// </param>
            public DelayedIndexAndCloseRunnable(LuceneTestCase outerInstance, Directory dir, CountdownEvent iwConstructed)
            {
#if FEATURE_INSTANCE_TESTDATA_INITIALIZATION
                this.outerInstance = outerInstance;
#endif
                this.dir           = dir;
                this.iwConstructed = iwConstructed;
            }
Example #38
0
 protected void AssertNoCountdown(CountdownEvent latch, int wait, string hint)
 {
     Assert.False(latch.Wait(wait), $"Expected count down to fail after {wait} milliseconds." + hint);
 }
Example #39
0
        public void Dispatch <T>(DispatcherSettings settings, Func <List <T> > sourceItemsProvider, Action <T> resultProcessor)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            if (sourceItemsProvider == null)
            {
                throw new ArgumentNullException(nameof(sourceItemsProvider));
            }
            if (resultProcessor == null)
            {
                throw new ArgumentNullException(nameof(resultProcessor));
            }

            using (_exitEvent)
            {
                while (!_exitEvent.IsSet)
                {
                    // Get the items from the provider
                    var items = new ConcurrentQueue <T>(sourceItemsProvider());
                    if (!items.Any())
                    {
                        // We don't have items. Wait for items
                        if (_exitEvent.Wait(settings.QueryItemsInterval))
                        {
                            break;
                        }
                    }

                    // Process items in parallel
                    using (var ce = new CountdownEvent(Math.Min(settings.Workers, items.Count)))
                    {
                        for (var i = 0; i < ce.InitialCount; i++)
                        {
                            ThreadPool.QueueUserWorkItem(_ =>
                            {
                                var parameters  = _ as object[];
                                var source      = parameters[0] as ConcurrentQueue <T>;
                                var finishEvent = parameters[1] as CountdownEvent;
                                var exitEvent   = parameters[2] as ManualResetEventSlim;

                                try
                                {
                                    T item;
                                    while (source.TryDequeue(out item) && !exitEvent.IsSet)
                                    {
                                        try
                                        {
                                            resultProcessor(item);
                                        }
                                        catch
                                        {
                                            // TODO : Unable to process item item. Log exception
                                        }
                                    }
                                }
                                finally
                                {
                                    finishEvent.Signal();
                                }
                            }, new object[] { items, ce, _exitEvent });
                        }

                        ce.Wait();
                    }
                }
            }
        }
Example #40
0
        public void A_dispatcher_must_handle_waves_of_actors()
        {
            var dispatcher = InterceptedDispatcher();
            var props      = Props.Create(() => new DispatcherActor()).WithDispatcher(dispatcher.Id);

            Action <int> flood = num =>
            {
                var cachedMessage            = new CountDownNStop(new CountdownEvent(num));
                var stopLatch                = new CountdownEvent(num);
                var keepAliveLatch           = new CountdownEvent(1);
                var waitTime                 = (int)Dilated(TimeSpan.FromSeconds(20)).TotalMilliseconds;
                Action <IActorDsl> bossActor = c =>
                {
                    c.Receive <string>(str => str.Equals("run"), (s, context) =>
                    {
                        for (var i = 1; i <= num; i++)
                        {
                            context.Watch(context.ActorOf(props)).Tell(cachedMessage);
                        }
                    });

                    c.Receive <Terminated>((terminated, context) =>
                    {
                        stopLatch.Signal();
                    });
                };
                var boss = Sys.ActorOf(Props.Create(() => new Act(bossActor)).WithDispatcher("boss"));

                try
                {
                    // this future is meant to keep the dispatcher alive until the end of the test run even if
                    // the boss doesn't create children fast enough to keep the dispatcher from becoming empty
                    // and it needs to be on a separate thread to not deadlock the calling thread dispatcher
                    dispatcher.Schedule(() =>
                    {
                        keepAliveLatch.Wait(waitTime);
                    });
                    boss.Tell("run");
                    try
                    {
                        AssertCountdown(cachedMessage.Latch, waitTime, "Counting down from " + num);
                    }
                    catch (Exception ex)
                    {
                        // TODO balancing dispatcher
                        throw;
                    }
                    AssertCountdown(stopLatch, waitTime, "Expected all children to stop.");
                }
                finally
                {
                    keepAliveLatch.Signal();
                    Sys.Stop(boss);
                }
            };

            for (var i = 1; i <= 3; i++)
            {
                flood(50000);
                AssertDispatcher(dispatcher, i);
            }
        }
Example #41
0
 public Publisher(RingBuffer <TestEvent> ringBuffer, int iterations, CountdownEvent start, CountdownEvent end)
 {
     _ringBuffer = ringBuffer;
     _end        = end;
     _start      = start;
     _iterations = iterations;
 }
Example #42
0
		public CountdownEventSlot (CountdownEvent evt)
		{
			this.evt = evt;
		}
        partial void InternalDispose(bool disposing)
        {
            if (disposing)
            {
                var listener = _listener;
                if (listener != null)
                {
                    _listener = null;
                    listener.Dispose();
                }

                var pendingRequestsCountdown = _pendingChannelCountdown;
                if (pendingRequestsCountdown != null)
                {
                    _pendingChannelCountdown = null;
                    pendingRequestsCountdown.Dispose();
                }
            }
        }
        private void RunChildren()
        {
            var children = new List<WorkItem>();

            foreach (Test test in _suite.Tests)
                if (_childFilter.Pass(test))
                    children.Add(WorkItem.CreateWorkItem(test, new TestExecutionContext(this.Context), _childFilter));

            if (children.Count > 0)
            {
                _childTestCountdown = new CountdownEvent(children.Count);

                foreach (WorkItem child in children)
                {
                    child.Completed += new EventHandler(OnChildCompleted);
#if NUNITLITE
                    child.Execute();
#else
                    // We run child items on the same thread as the parent...
                    // 1. If there is no dispatcher (NUnitLite or LevelOfParallelism = 0).
                    // 2. If there is no fixture, and so nothing to do but dispatch grandchildren.
                    // 3. For now, if this represents a test case. This avoids issues of
                    // tests that access the fixture state and allows handling ApartmentState
                    // preferences set on the fixture.
                    if (Context.Dispatcher == null ||child is SimpleWorkItem || child.Test.FixtureType == null)
                    {
                        log.Debug("Directly executing {0}", child.Test.Name);
                        child.Execute();
                    }
                    else
                        Context.Dispatcher.Dispatch(child);
#endif
                }
            }
        }
        public static ParallelTaskResult DynamicFor(int low, int high,
            Action<ParallelTaskOptions, int, int> work, int workerCount)
        {
            if (low < 0 || high < low) return new ParallelTaskResult();

            ModuleProc PROC = new ModuleProc("ParallelTasks", "For");
            ParallelTaskResult result = new ParallelTaskResult(ParallelTaskResultStatus.Created);
            if (workerCount <= 0) workerCount = 1;
            const int chunk = 16;

            CountdownEvent cde = new CountdownEvent(workerCount);
            Thread[] threads = new Thread[workerCount];
            int currentCount = 0;
            int currentValue = low;
            ParallelTaskOptions options = new ParallelTaskOptions();

            try
            {
                for (int i = 0; i < workerCount; i++)
                {
                    threads[i] = Extensions.CreateThreadAndStart((o) =>
                    {
                        int j = 0;
                        int currentChunk = 1;

                        while (true)
                        {
                            if (options.IsCancelled) break;
                            if ((currentValue + currentChunk) > Int32.MaxValue) break;
                            j = Interlocked.Add(ref currentValue, currentChunk) - currentChunk;
                            if (j >= high) break;

                            for (int k = 0; (k < currentChunk) && ((j + k) < high); k++)
                            {
                                if (options.IsCancelled) break;

                                try
                                {
                                    work(options, j, currentCount);
                                }
                                catch (Exception ex)
                                {
                                    Log.Exception(PROC, ex);
                                }
                                finally
                                {
                                    Interlocked.Increment(ref currentCount);
                                }
                            }
                            if (currentChunk < chunk) currentChunk *= 2;
                        }

                        cde.Signal();
                    }, i);
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
            finally
            {
                cde.Wait();
                result.Status = (options.IsCancelled ? ParallelTaskResultStatus.Canceled : ParallelTaskResultStatus.Completed);
            }

            return result;
        }
Example #46
0
        private Publisher[] Initialise(Publisher[] publishers, RingBuffer <TestEvent> buffer, int messageCount, CountdownEvent start, CountdownEvent end)
        {
            for (var i = 0; i < publishers.Length; i++)
            {
                publishers[i] = new Publisher(buffer, messageCount, start, end);
            }

            return(publishers);
        }
Example #47
0
        /// <summary>
        /// Performs SSH_FXP_WRITE request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="data">The data to send.</param>
        /// <param name="wait">The wait event handle if needed.</param>
        public void RequestWrite(byte[] handle, UInt64 offset, byte[] data)
        {
            const int maximumDataSize = 1024 * 32 - 38;

            if (data.Length < maximumDataSize + 1)
            {
                using (var wait = new AutoResetEvent(false))
                {
                    var request = new SftpWriteRequest(this.NextRequestId, handle, offset, data,
                                                       (response) =>
                                                           {
                                                               if (response.StatusCode == StatusCodes.Ok)
                                                               {
                                                                   wait.Set();
                                                               }
                                                               else
                                                               {
                                                                   ThrowSftpException(response);
                                                               }
                                                           });

                    this.SendRequest(request);

                   
                    this.WaitHandle(wait, this._operationTimeout);
                }

            }
            else
            {
              
            int block = ((data.Length - 1)/maximumDataSize) + 1;
            using (var cnt = new CountdownEvent(block))
            {
                for (int i = 0; i < block; i++)
                {
                    var blockBufferSize = Math.Min(data.Length - maximumDataSize*i, maximumDataSize);
                    var blockBuffer = new byte[blockBufferSize];

                    Buffer.BlockCopy(data, i*maximumDataSize, blockBuffer, 0, blockBufferSize);

                    var request = new SftpWriteRequest(this.NextRequestId, handle, offset + (ulong) (i*maximumDataSize),
                                                       blockBuffer,
                                                       (response) =>
                                                           {
                                                               if (response.StatusCode == StatusCodes.Ok)
                                                               {
                                                                   // if (wait != null)
                                                                   //  wait.Set();
                                                                   cnt.Signal();
                                                               }
                                                               else
                                                               {
                                                                   ThrowSftpException(response);
                                                               }
                                                           });

                    this.SendRequest(request);
                }

                this.WaitHandle(cnt.WaitHandle, this._operationTimeout/*new TimeSpan(block*this._operationTimeout.Ticks)*/);
                

            }
            }
        }
        public void StopTest_MultiplyWaiters()
        {
            var target = CreateFixedThreadPool(2, false);
            var heavyTask = new TaskMock(Wait);
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.High));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.Medium));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.Low));
            Assert.AreEqual(true, target.Execute(heavyTask, Priority.Low));

            Exception deferedException = null;
            var waitersCounter = new CountdownEvent(4);

            for (var i = 0; i < 4; i++)
            {
                new Thread(() =>
                {
                    target.Stop();
                    try
                    { // each thread should see properly stopped pool.
                        AssertIsProperlyStopped(target);
                    }
                    catch (Exception e)
                    {
                        deferedException = e;
                    }
                    waitersCounter.Signal();
                }).Start();
            }

            target.Stop();
            AssertIsProperlyStopped(target);

            waitersCounter.Wait(); // if thread pool works properly, will never blocked here

            if (deferedException != null)
            {
                throw deferedException;
            }
        }
Example #49
0
 public CountDown(CountdownEvent latch)
 {
     Latch = latch;
 }
Example #50
0
 /// <summary>Initializes a new instance of the <see cref="VanillaListener"/> class.</summary>
 /// <param name="latch">The latch.</param>
 public VanillaListener(CountdownEvent latch)
 {
     this.latch = latch;
 }
Example #51
0
        public void TestEtw()
        {
            using (var listener = new TestEventListener(new Guid("16F53577-E41D-43D4-B47E-C17025BF4025"), EventLevel.Verbose))
            {
                ActionBlock<int> ab = null;
                BufferBlock<int> bb = null;
                int remaining = 0;
                CountdownEvent ce = new CountdownEvent(0);

                // Check that block creation events fire
                const int DataflowBlockCreatedId = 1;
                remaining = 2;
                listener.RunWithCallback(ev => {
                        Assert.Equal(expected: DataflowBlockCreatedId, actual: ev.EventId);
                        remaining--;
                    },
                    () => {
                        ab = new ActionBlock<int>(i => { });
                        bb = new BufferBlock<int>(); // trigger block creation event
                        Assert.Equal(expected: 0, actual: remaining);
                    });

                // Check that linking events fire
                const int BlockLinkedId = 4;
                remaining = 1;
                IDisposable link = null;
                listener.RunWithCallback(ev => {
                        Assert.Equal(expected: BlockLinkedId, actual: ev.EventId);
                        remaining--;
                    },
                    () => {
                        link = bb.LinkTo(ab);
                        Assert.Equal(expected: 0, actual: remaining);
                    });

                // Check that unlinking events fire
                const int BlockUnlinkedId = 5;
                remaining = 1;
                listener.RunWithCallback(ev => {
                        Assert.Equal(expected: BlockUnlinkedId, actual: ev.EventId);
                        remaining--;
                    },
                    () => {
                        link.Dispose();
                        Assert.Equal(expected: 0, actual: remaining);
                    });

                // Bug: It appears that private reflection problems are causing events with enum arguments
                //      to fail to fire on .NET Core.  Needs further investigation.  The following
                //      two tests are disabled as a result.

                //// Check that task launched events fire
                //const int TaskLaunchedId = 2;
                //ce.Reset(1);
                //listener.RunWithCallback(ev => {
                //        Assert.Equal(expected: TaskLaunchedId, actual: ev.EventId);
                //        ce.Signal();
                //    },
                //    () => {
                //        ab.Post(42);
                //        ce.Wait();
                //        Assert.Equal(expected: 0, actual: ce.CurrentCount);
                //    });

                //// Check that completion events fire
                //const int BlockCompletedId = 3;
                //ce.Reset(2);
                //listener.RunWithCallback(ev => {
                //        Assert.Equal(expected: BlockCompletedId, actual: ev.EventId);
                //        ce.Signal();
                //    },
                //    () => {
                //        ab.Complete();
                //        bb.Complete();
                //        ce.Wait();
                //        Assert.Equal(expected: 0, actual: ce.CurrentCount);
                //    });

            }
        }
Example #52
0
        public void Add(T activeObject)
        {
            var latch = new CountdownEvent(1);

            _locks.TryAdd(activeObject, latch);
        }
Example #53
0
        public void AsyncReadPagesFromDevice(int numPages, long destinationStartPage, IDevice device, out CountdownEvent completed)
        {
            var asyncResult = new PageAsyncFlushResult();

            ISegmentedDevice objlogDevice = null;

            if (Key.HasObjectsToSerialize() || Value.HasObjectsToSerialize())
            {
                objlogDevice = CreateObjectLogDevice(device);
                throw new Exception("Reading pages with object log not yet supported");
            }
            completed                = new CountdownEvent(numPages);
            asyncResult.handle       = completed;
            asyncResult.objlogDevice = objlogDevice;

            for (long flushPage = destinationStartPage; flushPage < destinationStartPage + numPages; flushPage++)
            {
                long pageStartAddress = flushPage << LogPageSizeBits;
                long pageEndAddress   = (flushPage + 1) << LogPageSizeBits;

                device.ReadAsync(
                    (ulong)(AlignedPageSizeBytes * (flushPage - destinationStartPage)),
                    pointers[flushPage % BufferSize],
                    (uint)(PageSize * PrivateRecordSize),
                    AsyncReadPageFromDeviceCallback,
                    asyncResult);
            }
        }
Example #54
0
        /// <summary>
        /// Flush pages from startPage (inclusive) to endPage (exclusive)
        /// to specified log device and obj device
        /// </summary>
        /// <param name="startPage"></param>
        /// <param name="endPage"></param>
        /// <param name="device"></param>
        public void AsyncFlushPagesToDevice(long startPage, long endPage, IDevice device, out CountdownEvent completed)
        {
            var asyncResult = new PageAsyncFlushResult();

            int numPages = (int)(endPage - startPage);

            ISegmentedDevice objlogDevice = null;

            if (Key.HasObjectsToSerialize() || Value.HasObjectsToSerialize())
            {
                numPages     = numPages * 2;
                objlogDevice = CreateObjectLogDevice(device);
            }

            completed          = new CountdownEvent(numPages);
            asyncResult.handle = completed;
            for (long flushPage = startPage; flushPage < endPage; flushPage++)
            {
                long pageStartAddress = flushPage << LogPageSizeBits;
                long pageEndAddress   = (flushPage + 1) << LogPageSizeBits;

                WriteAsync(pointers[flushPage % BufferSize],
                           (ulong)(AlignedPageSizeBytes * (flushPage - startPage)),
                           (uint)(PageSize * PrivateRecordSize),
                           AsyncFlushPageToDeviceCallback,
                           asyncResult, device, objlogDevice);
            }
        }
Example #55
0
        private void RunChildren()
        {
            int childCount = _children.Count;
            if (childCount == 0)
                throw new InvalidOperationException("RunChildren called but item has no children");

            _childTestCountdown = new CountdownEvent(childCount);

            foreach (WorkItem child in _children)
            {
                if (CheckForCancellation())
                    break;

                child.Completed += new EventHandler(OnChildCompleted);
                child.InitializeContext(new TestExecutionContext(Context));

                Context.Dispatcher.Dispatch(child);
                childCount--;
            }

            if (childCount > 0)
            {
                while (childCount-- > 0)
                    CountDownChildTest();
            }
        }
Example #56
0
 /// <summary>
 /// The reset.
 /// </summary>
 public static void Reset()
 {
     WaitEvent  = new CountdownEvent(3);
     BuildCount = 0;
 }
Example #57
0
        public void TestEtw()
        {
            using (var listener = new TestEventListener(new Guid("16F53577-E41D-43D4-B47E-C17025BF4025"), EventLevel.Verbose))
            {
                ActionBlock<int> ab = null;
                BufferBlock<int> bb = null;
                int remaining = 0;
                CountdownEvent ce = new CountdownEvent(0);

                // Check that block creation events fire
                const int DataflowBlockCreatedId = 1;
                remaining = 2;
                listener.RunWithCallback(ev => {
                        Assert.Equal(expected: DataflowBlockCreatedId, actual: ev.EventId);
                        remaining--;
                    },
                    () => {
                        ab = new ActionBlock<int>(i => { });
                        bb = new BufferBlock<int>(); // trigger block creation event
                        Assert.Equal(expected: 0, actual: remaining);
                    });

                // Check that linking events fire
                const int BlockLinkedId = 4;
                remaining = 1;
                IDisposable link = null;
                listener.RunWithCallback(ev => {
                        Assert.Equal(expected: BlockLinkedId, actual: ev.EventId);
                        remaining--;
                    },
                    () => {
                        link = bb.LinkTo(ab);
                        Assert.Equal(expected: 0, actual: remaining);
                    });

                // Check that unlinking events fire
                const int BlockUnlinkedId = 5;
                remaining = 1;
                listener.RunWithCallback(ev => {
                        Assert.Equal(expected: BlockUnlinkedId, actual: ev.EventId);
                        remaining--;
                    },
                    () => {
                        link.Dispose();
                        Assert.Equal(expected: 0, actual: remaining);
                    });

                // Check that task launched events fire
                const int TaskLaunchedId = 2;
                ce.Reset(1);
                listener.RunWithCallback(ev => {
                        Assert.Equal(expected: TaskLaunchedId, actual: ev.EventId);
                        ce.Signal();
                    },
                    () => {
                        ab.Post(42);
                        ce.Wait();
                        Assert.Equal(expected: 0, actual: ce.CurrentCount);
                    });

                // Check that completion events fire
                const int BlockCompletedId = 3;
                ce.Reset(2);
                listener.RunWithCallback(ev => {
                        Assert.Equal(expected: BlockCompletedId, actual: ev.EventId);
                        ce.Signal();
                    },
                    () => {
                        ab.Complete();
                        bb.Complete();
                        ce.Wait();
                        Assert.Equal(expected: 0, actual: ce.CurrentCount);
                    });

            }
        }
        public object Clone()
        {
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
            try
            {
                var cssSpriteOutput = string.Empty;
                var jsOutput = string.Empty;
                var cssOutput = string.Empty;

                var countdownEvents = new CountdownEvent(1);

                ThreadPool.QueueUserWorkItem(data =>
                    {
                        var countdownEvent = (CountdownEvent) data;

                        try
                        {
                            if (_cssSpriteConfiguration != null)
                            {
                                var cssSpriteGroups = _cssSpriteConfiguration.CssSpriteGroups;
                                var cssSpriteCreator = new CssSpriteCreator(_cacheManager, _retryableFileOpener, _pathProvider, _retryableFileWriter, _fileMetaData);
                                var cssSpriteGroupsProcessor = new CssSpriteGroupsProcessor();

                                cssSpriteOutput = cssSpriteGroupsProcessor.ProcessGroups(_pathProvider, cssSpriteCreator, cssSpriteGroups).ToString();

                                _logMessage(cssSpriteOutput);
                            }
                        }
                        catch (Exception exception)
                        {
                            _logError(exception.ToString());
                        }
                        countdownEvent.Signal();
                    }, countdownEvents);

                countdownEvents.Wait();

                countdownEvents = new CountdownEvent(2);

                ThreadPool.QueueUserWorkItem(data =>
                    {
                        var countdownEvent = (CountdownEvent) data;

                        try
                        {
                            if (_crusherConfiguration != null)
                            {
                                var jsCrusher = new JsCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, _fileMetaData);
                                var jsGroups = _crusherConfiguration.JsGroups;
                                var jsGroupsProcessor = new JsGroupsProcessor();

                                jsOutput = jsGroupsProcessor.ProcessGroups(_pathProvider, jsCrusher, jsGroups).ToString();

                                _logMessage(jsOutput);
                            }
                        }
                        catch (Exception exception)
                        {
                            _logError(exception.ToString());
                        }
                        countdownEvent.Signal();
                    }, countdownEvents);

                ThreadPool.QueueUserWorkItem(data =>
                    {
                        var countdownEvent = (CountdownEvent) data;

                        try
                        {
                            if (_crusherConfiguration != null)
                            {
                                var hashQueryStringKeyName = _crusherConfiguration.QuerystringKeyName;
                                var cssAssetsFileHasher = new CssAssetsFileHasher(hashQueryStringKeyName, _hasher, _pathProvider);
                                var cssPathRewriter = new CssPathRewriter(cssAssetsFileHasher, _pathProvider);
                                var cssCrusher = new CssCrusher(_cacheManager, _pathProvider, _retryableFileOpener, _retryableFileWriter, cssPathRewriter, _fileMetaData, _crusherConfiguration.WatchAssets);
                                var cssGroups = _crusherConfiguration.CssGroups;
                                var cssGroupsCrusher = new CssGroupsProcessor();
                                cssOutput = cssGroupsCrusher.ProcessGroups(_pathProvider, cssCrusher, cssGroups).ToString();

                                _logMessage(cssOutput);
                            }
                        }
                        catch (Exception exception)
                        {
                            _logError(exception.ToString());
                        }
                        countdownEvent.Signal();
                    }, countdownEvents);

                countdownEvents.Wait();
            }
            catch (Exception exception)
            {
                _logError(exception.ToString());
            }
            finally
            {
                AppDomain.CurrentDomain.UnhandledException -= OnUnhandledException;
            }

            return null;
        }