Exemple #1
0
        /// <summary>
        /// Registers a step execution with the current job execution.
        /// </summary>
        /// <param name="stepName"></param>
        /// <returns>the name of the step the new execution is associated with</returns>
        public StepExecution CreateStepExecution(string stepName)
        {
            StepExecution stepExecution = new StepExecution(stepName, this);

            _stepExecutions.TryAdd(stepExecution);
            return(stepExecution);
        }
Exemple #2
0
        public void Add(T item)
        {
            while (true)
            {
                while (isFull())
                {
                    if (isComplete.Value)
                    {
                        throw new InvalidOperationException("The BlockingCollection<T>"
                                                            + " has been marked as complete with regards to additions.");
                    }
                    Block();
                }
                // Extra check. The status might have changed after Block() or if isFull() is always false
                if (isComplete.Value)
                {
                    throw new InvalidOperationException("The BlockingCollection<T> has"
                                                        + " been marked as complete with regards to additions.");
                }
                // Go back in main waiting loop
                if (isFull())
                {
                    continue;
                }

                if (underlyingColl.TryAdd(item))
                {
                    break;
                }
            }
        }
Exemple #3
0
        private void DoWorkAndReport(IProducerConsumerCollection <string> inputCollection, IProducerConsumerCollection <object[]> outputCollection, ManualResetEvent pauseEvent, IProgress <int> progress)
        {
            if (Delimiter == null)
            {
                var outputMessage = "Delimiter is not set for this Stringsplitter";
                LogService.Instance.Error(outputMessage);
                throw new InvalidOperationException(outputMessage);
            }
            string[] _Delimiter = new string[] { Delimiter };
            string   InputString;
            int      ProcessedCount = 0;

            if (Qualifier != null)
            {
                while (HasWork)
                {
                    pauseEvent.WaitOne();
                    if (inputCollection.TryTake(out InputString))
                    {
                        string[] OutputString = StringAndText.SplitRow(InputString, Delimiter, Qualifier, false);

                        while (!outputCollection.TryAdd(OutputString))
                        {
                            pauseEvent.WaitOne();
                        }
                        ProcessedCount++;
                    }
                    else
                    {
                        Thread.Sleep(10);
                    }
                    if (ProcessedCount % 1000 == 0)
                    {
                        progress.Report(ProcessedCount);
                    }
                }
            }
            else
            {
                while (HasWork)
                {
                    pauseEvent.WaitOne();
                    if (inputCollection.TryTake(out InputString))
                    {
                        string[] OutputString = InputString.Split(_Delimiter, StringSplitOptions.None);

                        while (!outputCollection.TryAdd(OutputString))
                        {
                            pauseEvent.WaitOne();
                        }
                        ProcessedCount++;
                    }
                    if (ProcessedCount % 1000 == 0)
                    {
                        progress.Report(ProcessedCount);
                    }
                }
            }
            progress.Report(ProcessedCount);
        }
Exemple #4
0
        public void GetEnumerator_EnumerationsAreSnapshots()
        {
            IProducerConsumerCollection <int> c = CreateProducerConsumerCollection();

            Assert.Empty(c);

            using (IEnumerator <int> e1 = c.GetEnumerator())
            {
                Assert.True(c.TryAdd(1));
                using (IEnumerator <int> e2 = c.GetEnumerator())
                {
                    Assert.True(c.TryAdd(2));
                    using (IEnumerator <int> e3 = c.GetEnumerator())
                    {
                        int item;
                        Assert.True(c.TryTake(out item));
                        using (IEnumerator <int> e4 = c.GetEnumerator())
                        {
                            Assert.False(e1.MoveNext());

                            Assert.True(e2.MoveNext());
                            Assert.False(e2.MoveNext());

                            Assert.True(e3.MoveNext());
                            Assert.True(e3.MoveNext());
                            Assert.False(e3.MoveNext());

                            Assert.True(e4.MoveNext());
                            Assert.False(e4.MoveNext());
                        }
                    }
                }
            }
        }
Exemple #5
0
        public void AddTakeWithAtLeastOneElementInCollection_IsEmpty_AlwaysFalse()
        {
            int items = 1000;

            IProducerConsumerCollection <int> c = CreateProducerConsumerCollection();

            Assert.True(c.TryAdd(0)); // make sure it's never empty
            var cts = new CancellationTokenSource();

            // Consumer repeatedly calls IsEmpty until it's told to stop
            Task consumer = Task.Run(() =>
            {
                while (!cts.IsCancellationRequested)
                {
                    Assert.False(IsEmpty(c));
                }
            });

            // Producer enqueues/dequeues a bunch of items, then tells the consumer to stop
            Task producer = Task.Run(() =>
            {
                int ignored;
                for (int i = 1; i <= items; i++)
                {
                    Assert.True(c.TryAdd(i));
                    Assert.True(c.TryTake(out ignored));
                }
                cts.Cancel();
            });

            Task.WaitAll(producer, consumer);
        }
        internal static void SplitQueue(IProducerConsumerCollection <int> q)
        {
            //Wrap tail back to 0
            for (int i = 0; i < 5; i++)
            {
                q.TryAdd(111);
            }

            //First half
            for (int i = 0; i < 5; i++)
            {
                q.TryAdd(i);
            }

            //Move head by 5
            for (int i = 0; i < 5; i++)
            {
                q.TryTake(out int _);
            }

            //Second half (head and tail are now both 5)
            for (int i = 5; i < 10; i++)
            {
                q.TryAdd(i);
            }

            //Circular buffer now "ends" in the middle of the underlying array

            if (q.Count < 10)
            {
                throw new ArgumentException("Queue needs to have a capacity of at least 10.");
            }
        }
Exemple #7
0
        public static async Task <(ProofRecord holderProofRecord, ProofRecord RequestorProofRecord)> ProofProtocolAsync(
            IProofService proofService,
            IProducerConsumerCollection <AgentMessage> messages,
            ConnectionRecord holderConnection, ConnectionRecord requestorConnection,
            IAgentContext holderContext,
            IAgentContext requestorContext, ProofRequest proofRequestObject)
        {
            //Requestor sends a proof request
            var(message, requestorProofRecord) = await proofService.CreateRequestAsync(requestorContext, proofRequestObject, requestorConnection.Id);

            messages.TryAdd(message);

            // Holder accepts the proof requests and builds a proof
            var proofRequest = FindContentMessage <RequestPresentationMessage>(messages);

            Assert.NotNull(proofRequest);

            //Holder stores the proof request
            var holderProofRequestRecord = await proofService.ProcessRequestAsync(holderContext, proofRequest, holderConnection);

            var holderProofRecord = await proofService.GetAsync(holderContext, holderProofRequestRecord.Id);

            var holderProofRequest = JsonConvert.DeserializeObject <ProofRequest>(holderProofRecord.RequestJson);

            // Auto satify the proof with which ever credentials in the wallet are capable
            var requestedCredentials =
                await ProofServiceUtils.GetAutoRequestedCredentialsForProofCredentials(holderContext, proofService,
                                                                                       holderProofRequest);

            //Holder accepts the proof request and sends a proof
            (var proofMessage, _) = await proofService.CreatePresentationAsync(
                holderContext,
                holderProofRequestRecord.Id,
                requestedCredentials);

            messages.TryAdd(proofMessage);

            //Requestor retrives proof message from their cloud agent
            var proof = FindContentMessage <PresentationMessage>(messages);

            Assert.NotNull(proof);

            //Requestor stores proof
            requestorProofRecord = await proofService.ProcessPresentationAsync(requestorContext, proof);

            //Requestor verifies proof
            var requestorVerifyResult = await proofService.VerifyProofAsync(requestorContext, requestorProofRecord.Id);

            //Verify the proof is valid
            Assert.True(requestorVerifyResult);

            var requestorProofRecordResult = await proofService.GetAsync(requestorContext, requestorProofRecord.Id);

            var holderProofRecordResult = await proofService.GetAsync(holderContext, holderProofRecord.Id);

            return(holderProofRecordResult, requestorProofRecordResult);
        }
        /// <summary>
        /// Registers a step execution with the current job execution.
        /// </summary>
        /// <param name="stepName"></param>
        /// <returns>the name of the step the new execution is associated with</returns>
        public StepExecution CreateStepExecution(string stepName)
        {
            StepExecution stepExecution = new StepExecution(stepName, this);

            if (!_stepExecutions.Contains(stepExecution))
            {
                _stepExecutions.TryAdd(stepExecution);
            }
            return(stepExecution);
        }
Exemple #9
0
        public bool TryAdd(GameRequest gameRequest)
        {
            if (!_usersInQueue.ContainsKey(gameRequest.UserId) && _queue.TryAdd(gameRequest))
            {
                _usersInQueue.Add(gameRequest.UserId, 0);
                return(true);
            }

            return(false);
        }
Exemple #10
0
        private void DoReportingWork(PipelineContext context, IProducerConsumerCollection <string[]> output, ManualResetEvent pause, IProgress <int> progress)
        {
            string   filepath       = context.SourceFilePath;
            Encoding encodeingToUse = m_encoding;
            char     delim;

            if (context.Delimiter.Length == 1)
            {
                delim = context.Delimiter.ToCharArray()[0];
            }
            else
            {
                throw new InvalidCastException("MmfExtractor only supports single char delimiters");
            }
            int columncount = context.ColumnNames.Count();

            MemoryMappedFileSecurity security = new MemoryMappedFileSecurity();

            security.AddAccessRule(new System.Security.AccessControl.AccessRule <MemoryMappedFileRights>("everyone", MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));

            using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
            {
                m_Buffer = m_Buffer > fs.Length ? fs.Length : m_Buffer;
                using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fs, "D2SMMF", m_Buffer, MemoryMappedFileAccess.Read, security, HandleInheritability.Inheritable, true))
                {
                    using (MemoryMappedViewStream view = mmf.CreateViewStream(0, 0, MemoryMappedFileAccess.Read))
                    {
                        byte[] charArray       = new byte[byteArraySize]; //initialize a bytearray to read bytes into from the stream
                        long   length          = view.Length;             //determine how long the stream is
                        long   currentPosition = 0;                       //determine where in the stream we are
                        //determine the offset when reading a new chunk of bytes, we need to do this because we cannot guarentee that the last byte is the end of a row
                        //if it isn't then the remainder will be placed at the beginning of the array and the offset will be updated so it is not overwritten when the next batch of bytes is read from the stream
                        int offset = 0;
                        while (currentPosition < length)
                        {
                            currentPosition +=
                                view.Read(charArray, offset, byteArraySize - offset);
                            foreach (string[] line in GetStrings(ref charArray, encodeingToUse, delim, columncount))
                            {
                                output.TryAdd(line);
                            }
                            offset = m_LatestOffset;
                        }
                        //when we break out of this loop we will be left with a char array holding the last line (which wont have an end of line character so getstrings() did not append it to the result list
                        //we know that the length of this line is m_latestoffset so we can simply read it and add it before closing all resources and completing the task
                        char[] rawlastRow = encodeingToUse.GetChars(charArray, 0, m_LatestOffset);
                        //we're gonna have a bunch of null chars /0 in here so were just gonna trim those out rly quick
                        char[]   lastRow         = TrimNulls(rawlastRow);
                        string   lastRowAsString = new string(lastRow);
                        string[] finalResult     = lastRowAsString.Split(delim);
                        output.TryAdd(finalResult);
                    }
                }
            }
        }
Exemple #11
0
 bool Pipes.Interfaces.IInput <T> .PushObject(object element)
 {
     if (Input != null && Input != this)
     {
         return(Input.PushObject(element));
     }
     else if (element.GetType().IsAssignableFrom(typeof(T)))
     {
         return(queue.TryAdd((T)element));
     }
     return(false);
 }
 private void RunAction(TRandom existingInstance, int bufferLength, IProducerConsumerCollection <byte[]> byteList)
 {
     Assert.True(
         byteList.TryAdd(
             existingInstance // We first try with already used getter
             .GetBytes(bufferLength)
             )
         );
     Assert.True(
         byteList.TryAdd(
             GetStaticInstance() // We then test with using the getter again
             .GetBytes(bufferLength)));
 }
Exemple #13
0
        public void UseIProducerComsumerCollection(IProducerConsumerCollection <string> producerConsumer)
        {
            producerConsumer.TryAdd("sun");
            producerConsumer.TryAdd("yong");
            producerConsumer.TryAdd("liang");

            string name;
            bool   isEmpty = producerConsumer.TryTake(out name);

            if (isEmpty)
            {
                Console.WriteLine($"name is {name}");
            }
        }
Exemple #14
0
 private void ReportableWorkItem(PipelineContext context, IProducerConsumerCollection <string> output, ManualResetEvent pauseEvent, IProgress <int> progress)
 {
     if (context == null)
     {
         var outputMessage = "PipelineContext is not initialized for this instance of FlatfileExtractor";
         LogService.Instance.Error(outputMessage);
         throw new InvalidOperationException(outputMessage);
     }
     using (StreamReader Reader = new StreamReader(context.SourceFilePath))
     {
         if (context.FirstLineContainsHeaders)
         {
             Reader.ReadLine();
         }
         if (context.SourceFileIsSourcedFromDial)
         {
             Reader.ReadLine();
         }
         string line;
         int    progressCounter = 0;
         if (pauseEvent == null)
         {
             while ((line = Reader.ReadLine()) != null)
             {
                 output.TryAdd(line);
                 progressCounter++;
                 if (progressCounter % 1000 == 0)
                 {
                     progress.Report(progressCounter);
                 }
             }
         }
         else
         {
             while ((line = Reader.ReadLine()) != null)
             {
                 pauseEvent.WaitOne();
                 output.TryAdd(line);
                 progressCounter++;
                 if (progressCounter % 1000 == 0)
                 {
                     progress.Report(progressCounter);
                 }
             }
         }
         progress.Report(progressCounter);
     }
 }
Exemple #15
0
 public static void EnqueueMany <T>(this IProducerConsumerCollection <T> collection, IEnumerable <T> items)
 {
     foreach (var item in items)
     {
         collection.TryAdd(item);
     }
 }
        public static void AddStressTest(IProducerConsumerCollection <int> coll)
        {
            ParallelTestHelper.Repeat(delegate
            {
                var amount        = -1;
                const int Count   = 10;
                const int Threads = 5;

                ParallelTestHelper.ParallelStressTest(coll, (q) =>
                {
                    var t = Interlocked.Increment(ref amount);
                    for (var i = 0; i < Count; i++)
                    {
                        coll.TryAdd(t);
                    }
                }, Threads);

                Assert.AreEqual(Threads * Count, coll.Count, "#-1");
                var values = new int[Threads];
                int temp;
                while (coll.TryTake(out temp))
                {
                    values[temp]++;
                }

                for (var i = 0; i < Threads; i++)
                {
                    Assert.AreEqual(Count, values[i], "#" + i.ToString());
                }
            });
        }
Exemple #17
0
        public void start()
        {
            bool addWord;

            char[] currentWordArray;

            for (int index = minIndex; index < maxIndex; index++)
            {
                string currentWord = dictionary.ElementAt(index);
                currentWordArray = currentWord.ToCharArray();
                List <char> currentLetters = letters.ToList();

                addWord = true;

                foreach (char currentLetter in currentWordArray)
                {
                    if (currentLetters.Contains(currentLetter))
                    {
                        currentLetters.Remove(currentLetter);
                    }
                    else
                    {
                        addWord = false;
                        break;
                    }
                }

                if (addWord)
                {
                    wordsFound.TryAdd(currentWord);
                }
            }
        }
 void PushFieldCardTransaction(FieldCardTransaction transaction)
 {
     if (!FieldCardTransactionStream.TryAdd(transaction))
     {
         throw new Exception();
     }
 }
Exemple #19
0
 protected internal override void QueueTask(Task t)
 {
     // Add to the shared work pool
     workQueue.TryAdd(t);
     // Wake up some worker if they were asleep
     PulseAll();
 }
 public static void AddRange <T>(this IProducerConsumerCollection <T> col, IEnumerable <T> items)
 {
     foreach (var item in items)
     {
         col.TryAdd(item);
     }
 }
        public static void Test6_Interfaces()
        {
            ConcurrentQueue <int> queue = new ConcurrentQueue <int>();
            int item;

            IProducerConsumerCollection <int> ipcc = queue;

            Assert.Equal(0, ipcc.Count);
            Assert.False(ipcc.TryTake(out item), "TestIPCC:  IPCC.TryTake returned true when the collection is empty");
            Assert.True(ipcc.TryAdd(1));

            ICollection collection = queue;

            Assert.False(collection.IsSynchronized);

            queue.Enqueue(1);
            int         count      = queue.Count;
            IEnumerable enumerable = queue;

            foreach (object o in enumerable)
            {
                count--;
            }

            Assert.Equal(0, count);
        }
        private void DoPausableReportableWork(PipelineContext context, IProducerConsumerCollection <object[]> output, ManualResetEvent pause, IProgress <int> progress)
        {
            string filepath        = context.SourceFilePath;
            int    progressCounter = 0;

            using (var stream = File.Open(filepath, FileMode.Open, FileAccess.Read))
            {
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    if (context.FirstLineContainsHeaders)
                    {
                        reader.Read();
                    }
                    while (reader.Read())
                    {
                        object[] currentRow = new object[reader.FieldCount];
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            currentRow[i] = reader.GetValue(i);
                        }
                        output.TryAdd(currentRow);
                        if (++progressCounter % 1000 == 0)
                        {
                            progress.Report(progressCounter);
                        }
                    }
                    progress.Report(progressCounter);
                }
            }
        }
Exemple #23
0
        public static void AddStressTest(IProducerConsumerCollection <int> coll)
        {
            ParallelTestHelper.Repeat(delegate {
                int amount        = -1;
                const int count   = 10;
                const int threads = 5;

                ParallelTestHelper.ParallelStressTest(coll, (q) => {
                    int t = Interlocked.Increment(ref amount);
                    for (int i = 0; i < count; i++)
                    {
                        coll.TryAdd(t);
                    }
                }, threads);

                Assert.AreEqual(threads * count, coll.Count, "#-1");
                int[] values = new int[threads];
                int temp;
                while (coll.TryTake(out temp))
                {
                    values[temp]++;
                }

                for (int i = 0; i < threads; i++)
                {
                    Assert.AreEqual(count, values[i], "#" + i);
                }
            });
        }
        public static void AddStressTest(IProducerConsumerCollection<int> coll)
        {
            ParallelTestHelper.Repeat(delegate
            {
                int amount = -1;
                const int count = 10;
                const int threads = 5;

                ParallelTestHelper.ParallelStressTest(coll, (q) =>
                {
                    int t = Interlocked.Increment(ref amount);
                    for (int i = 0; i < count; i++)
                        coll.TryAdd(t);
                }, threads);

                Assert.AreEqual(threads * count, coll.Count, "#-1");
                int[] values = new int[threads];
                int temp;
                while (coll.TryTake(out temp))
                {
                    values[temp]++;
                }

                for (int i = 0; i < threads; i++)
                    Assert.AreEqual(count, values[i], "#" + i);
            });
        }
        /// <summary>
        /// Tries to add an item to the collection.
        /// May fail if an awaiter that's supposed to receive the item is cancelled. If this is the case, the TryAdd() method must be called again.
        /// </summary>
        /// <param name="item">The item to add to the collection.</param>
        /// <returns>True if the item was added to the collection; false if the awaiter was cancelled and the operation must be retried.</returns>
        private bool TryAdd(T item)
        {
            long     balanceAfterCurrentItem = Interlocked.Increment(ref _queueBalance);
            SpinWait spin = new SpinWait();

            if (balanceAfterCurrentItem > 0)
            {
                //	Items are dominating, so we can safely add a new item to the queue.
                while (!_itemQueue.TryAdd(item))
                {
                    spin.SpinOnce();
                }

                return(true);
            }
            else
            {
                //	There's at least one awaiter available or being added as we're speaking, so we're giving the item to it.

                IAwaiter <T> awaiter;

                while (!_awaiterQueue.TryDequeue(out awaiter))
                {
                    spin.SpinOnce();
                }

                //	Returns false if the cancellation occurred earlier.
                return(awaiter.TrySetResult(item));
            }
        }
        public static void Test9_Interfaces()
        {
            ConcurrentStack <int>             stack = new ConcurrentStack <int>();
            IProducerConsumerCollection <int> ipcc  = stack;

            Assert.Equal(0, ipcc.Count);
            int item;

            Assert.False(ipcc.TryTake(out item));
            Assert.True(ipcc.TryAdd(1));

            ICollection collection = stack;

            Assert.False(collection.IsSynchronized);

            stack.Push(1);
            int         count      = stack.Count;
            IEnumerable enumerable = stack;

            foreach (object o in enumerable)
            {
                count--;
            }

            Assert.Equal(0, count);
        }
        public static void RTest8_Interfaces()
        {
            ConcurrentBag <int> bag = new ConcurrentBag <int>();
            //IPCC
            IProducerConsumerCollection <int> ipcc = bag as IProducerConsumerCollection <int>;

            Assert.False(ipcc == null, "RTest8_Interfaces:  ConcurrentBag<T> doesn't implement IPCC<T>");
            Assert.True(ipcc.TryAdd(1), "RTest8_Interfaces:  IPCC<T>.TryAdd failed");
            Assert.Equal(1, bag.Count);

            int result = -1;

            Assert.True(ipcc.TryTake(out result), "RTest8_Interfaces:  IPCC<T>.TryTake failed");
            Assert.True(1 == result, "RTest8_Interfaces:  IPCC<T>.TryTake failed");
            Assert.Equal(0, bag.Count);

            //ICollection
            ICollection collection = bag as ICollection;

            Assert.False(collection == null, "RTest8_Interfaces:  ConcurrentBag<T> doesn't implement ICollection");
            Assert.False(collection.IsSynchronized, "RTest8_Interfaces:  IsSynchronized returned true");

            //IEnumerable
            IEnumerable enumerable = bag as IEnumerable;

            Assert.False(enumerable == null, "RTest8_Interfaces:  ConcurrentBag<T> doesn't implement IEnumerable");
            foreach (object o in enumerable)
            {
                Assert.True(false, "RTest8_Interfaces:  Enumerable returned items when the bag is empty");
            }
        }
        private async Task CreateFolderAsyncAndEnqueue(string fileSystemPath)
        {
            var repositoryPath = GetRepositoryPath(fileSystemPath);

            try
            {
                // TODO: implement multiple target servers

                // this will do the real job (create the folder)
                var folder = await Tools.EnsurePathAsync(repositoryPath, _options.ContainerTypeName).ConfigureAwait(false);

                // subfolders are ready to be processed later: add this path to the queue
                _folderPathCollection.TryAdd(fileSystemPath);

                _options.FolderImportCallback?.Invoke(fileSystemPath, repositoryPath, folder, null);
            }
            catch (ClientException cex)
            {
                //Trace.WriteLine("ERROR FOLDER: " + fileSystemPath + " *** EXCEPTION: " + cex.ErrorData.ExceptionType + " " + cex);
                _options.FolderImportCallback?.Invoke(fileSystemPath, repositoryPath, null, cex);
            }
            catch (Exception ex)
            {
                //Trace.WriteLine("ERROR FOLDER: " + fileSystemPath + " *** EXCEPTION: " + ex);
                _options.FolderImportCallback?.Invoke(fileSystemPath, repositoryPath, null, ex);
            }
            finally
            {
                ReleaseSemaphoreAndContinue();
            }
        }
Exemple #29
0
 public void AddWork(Task t)
 {
     // Add to the shared work pool
     workQueue.TryAdd(t);
     // Wake up some worker if they were asleep
     PulseAll();
 }
 /// <summary>Adds the contents of an enumerable to the collection.</summary>
 /// <typeparam name="T">Specifies the type of the elements in the collection.</typeparam>
 /// <param name="target">The target collection to be augmented.</param>
 /// <param name="source">The source enumerable containing the data to be added.</param>
 public static void AddFromEnumerable <T>(this IProducerConsumerCollection <T> target, IEnumerable <T> source)
 {
     foreach (var item in source)
     {
         target.TryAdd(item);
     }
 }
Exemple #31
0
        private void DoWorkAndReport(IProducerConsumerCollection <string> inputCollection, IProducerConsumerCollection <object[]> outputCollection, ManualResetEvent pauseEvent, IProgress <int> progress)
        {
            string[] _Delimiter = this._Delimiter;
            string   InputString;
            int      ProcessedCount = 0;

            while (HasWork)
            {
                pauseEvent.WaitOne();
                if (inputCollection.TryTake(out InputString))
                {
                    string[] OutputString = InputString.Split(_Delimiter, StringSplitOptions.None);
                    while (!outputCollection.TryAdd(OutputString))
                    {
                        pauseEvent.WaitOne();
                    }
                    ProcessedCount++;
                }
                if (ProcessedCount % 1000 == 0)
                {
                    progress.Report(ProcessedCount);
                }
            }
            progress.Report(ProcessedCount);
        }
		public static void RemoveStressTest (IProducerConsumerCollection<int> coll, CheckOrderingType order)
		{
			ParallelTestHelper.Repeat (delegate {
				
				const int count = 10;
				const int threads = 5;
				const int delta = 5;
				
				for (int i = 0; i < (count + delta) * threads; i++)
					while (!coll.TryAdd (i));
				
				bool state = true;
				bool ran = false;
				
				Assert.AreEqual ((count + delta) * threads, coll.Count, "#0");
				
				ParallelTestHelper.ParallelStressTest (coll, (q) => {
					ran = true;
					bool s = true;
					int t;
					
					for (int i = 0; i < count; i++) {
						s &= coll.TryTake (out t);
						// try again in case it was a transient failure
						if (!s && coll.TryTake (out t))
							s = true;
					}
					
					if (!s)
						state = false;
				}, threads);

				Assert.IsTrue (ran, "#1-pre");
				Assert.IsTrue (state, "#1");
				Assert.AreEqual (delta * threads, coll.Count, "#2");
				
				string actual = string.Empty;
				int temp;
				while (coll.TryTake (out temp)) {
					actual += temp.ToString ();;
				}
				
				IEnumerable<int> range = Enumerable.Range (order == CheckOrderingType.Reversed ? 0 : count * threads, delta * threads);
				if (order == CheckOrderingType.Reversed)
					range = range.Reverse ();
				
				string expected = range.Aggregate (string.Empty, (acc, v) => acc + v);
				
				if (order == CheckOrderingType.DontCare) {
					Assert.That (actual, new CollectionEquivalentConstraint (expected), "#3, same");
				} else { 
					Assert.AreEqual (expected, actual, "#3");
					Assert.That (actual, new EqualConstraint (expected), "#3, in order");
				}
			}, 100);
		}
		public static void RemoveStressTest (IProducerConsumerCollection<int> coll, CheckOrderingType order)
		{
			ParallelTestHelper.Repeat (delegate {
				
				const int count = 10;
				const int threads = 5;
				const int delta = 5;
				
				for (int i = 0; i < (count + delta) * threads; i++)
					coll.TryAdd (i);
				
				bool state = true;
				
				Assert.AreEqual ((count + delta) * threads, coll.Count, "#0");
				
				ParallelTestHelper.ParallelStressTest (coll, (q) => {
					bool s = true;
					int t;
					
					for (int i = 0; i < count; i++)
						s &= coll.TryTake (out t);
					
					if (!s)
						state = false;
				}, threads);
				
				Assert.IsTrue (state, "#1");
				Assert.AreEqual (delta * threads, coll.Count, "#2");
				
				string actual = string.Empty;
				int temp;
				while (coll.TryTake (out temp)) {
					actual += temp.ToString ();;
				}
				
				IEnumerable<int> range = Enumerable.Range (order == CheckOrderingType.Reversed ? 0 : count * threads, delta * threads);
				if (order == CheckOrderingType.Reversed)
					range = range.Reverse ();
				
				string expected = range.Aggregate (string.Empty, (acc, v) => acc + v);
				
				if (order == CheckOrderingType.DontCare)
					CollectionAssert.AreEquivalent (expected, actual, "#3");
				else 
					Assert.AreEqual (expected, actual, "#3");
			}, 1000);
		}
Exemple #34
0
		// Almost same as above but with an added predicate and treating one item at a time. 
		// It's used by Scheduler Participate(...) method for special waiting case like
		// Task.WaitAll(someTasks) or Task.WaitAny(someTasks)
		// Predicate should be really fast and not blocking as it is called a good deal of time
		// Also, the method skip tasks that are LongRunning to avoid blocking (Task are not LongRunning by default)
		public static void ParticipativeWorkerMethod (Task self,
		                                              ManualResetEventSlim predicateEvt,
		                                              int millisecondsTimeout,
		                                              IProducerConsumerCollection<Task> sharedWorkQueue,
		                                              ThreadWorker[] others,
		                                              ManualResetEvent evt)
		{
			const int stage1 = 5, stage2 = 0;
			int tries = 8;
			WaitHandle[] handles = null;
			Watch watch = Watch.StartNew ();
			if (millisecondsTimeout == -1)
				millisecondsTimeout = int.MaxValue;

			while (!predicateEvt.IsSet && watch.ElapsedMilliseconds < millisecondsTimeout) {
				Task value;
				
				// If we are in fact a normal ThreadWorker, use our own deque
				if (autoReference != null) {
					while (autoReference.dDeque.PopBottom (out value) == PopResult.Succeed && value != null) {
						evt.Set ();
						if (CheckTaskFitness (self, value))
							value.Execute (autoReference.ChildWorkAdder);
						else {
							sharedWorkQueue.TryAdd (value);
							evt.Set ();
						}

						if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
							return;
					}
				}

				int count = sharedWorkQueue.Count;

				// Dequeue only one item as we have restriction
				while (--count >= 0 && sharedWorkQueue.TryTake (out value) && value != null) {
					evt.Set ();
					if (CheckTaskFitness (self, value))
						value.Execute (null);
					else {
						if (autoReference == null)
							sharedWorkQueue.TryAdd (value);
						else
							autoReference.dDeque.PushBottom (value);
						evt.Set ();
					}

					if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
						return;
				}

				// First check to see if we comply to predicate
				if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
					return;
				
				// Try to complete other work by stealing since our desired tasks may be in other worker
				ThreadWorker other;
				for (int i = 0; i < others.Length; i++) {
					if ((other = others [i]) == autoReference || other == null)
						continue;

					if (other.dDeque.PopTop (out value) == PopResult.Succeed && value != null) {
						evt.Set ();
						if (CheckTaskFitness (self, value))
							value.Execute (null);
						else {
							if (autoReference == null)
								sharedWorkQueue.TryAdd (value);
							else
								autoReference.dDeque.PushBottom (value);
							evt.Set ();
						}
					}

					if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
						return;
				}

				if (--tries > stage1)
					Thread.Yield ();
				else if (tries >= stage2)
					predicateEvt.Wait (ComputeTimeout (100, millisecondsTimeout, watch));
				else {
					if (tries == stage2 - 1)
						handles = new [] { predicateEvt.WaitHandle, evt };
					WaitHandle.WaitAny (handles, ComputeTimeout (1000, millisecondsTimeout, watch));
				}
			}
		}
		// Almost same as above but with an added predicate and treating one item at a time. 
		// It's used by Scheduler Participate(...) method for special waiting case like
		// Task.WaitAll(someTasks) or Task.WaitAny(someTasks)
		// Predicate should be really fast and not blocking as it is called a good deal of time
		// Also, the method skip tasks that are LongRunning to avoid blocking (Task are not LongRunning by default)
		public static void WorkerMethod (Func<bool> predicate, IProducerConsumerCollection<Task> sharedWorkQueue,
		                                 ThreadWorker[] others)
		{
			while (!predicate ()) {
				Task value;
				
				// Dequeue only one item as we have restriction
				if (sharedWorkQueue.TryTake (out value)) {
					if (value != null) {
						if (CheckTaskFitness (value))
							value.Execute (null);
						else
							sharedWorkQueue.TryAdd (value);
					}
				}
				
				// First check to see if we comply to predicate
				if (predicate ()) {
					return;
				}
				
				// Try to complete other work by stealing since our desired tasks may be in other worker
				ThreadWorker other;
				for (int i = 0; i < others.Length; i++) {
					if ((other = others [i]) == null)
						continue;
					
					if (other.dDeque.PopTop (out value) == PopResult.Succeed) {
						if (value != null) {
							if (CheckTaskFitness (value))
								value.Execute (null);
							else
								sharedWorkQueue.TryAdd (value);
						}
					}
					
					if (predicate ()) {
						return;
					}
				}
			}
		}
Exemple #36
0
		// Almost same as above but with an added predicate and treating one item at a time. 
		// It's used by Scheduler Participate(...) method for special waiting case like
		// Task.WaitAll(someTasks) or Task.WaitAny(someTasks)
		// Predicate should be really fast and not blocking as it is called a good deal of time
		// Also, the method skip tasks that are LongRunning to avoid blocking (Task are not LongRunning by default)
		public static void WorkerMethod (Func<bool> predicate, IProducerConsumerCollection<Task> sharedWorkQueue,
		                                 ThreadWorker[] others, ManualResetEvent evt)
		{
			while (!predicate ()) {
				Task value;
				
				// If we are in fact a normal ThreadWorker, use our own deque
				if (autoReference != null) {
					while (autoReference.dDeque.PopBottom (out value) == PopResult.Succeed && value != null) {
						evt.Set ();
						if (CheckTaskFitness (value))
							value.Execute (autoReference.ChildWorkAdder);
						else {
							autoReference.dDeque.PushBottom (value);
							evt.Set ();
						}

						if (predicate ())
							return;
					}
				}

				// Dequeue only one item as we have restriction
				while (sharedWorkQueue.TryTake (out value) && value != null) {
					evt.Set ();
					if (CheckTaskFitness (value))
						value.Execute (null);
					else {
						sharedWorkQueue.TryAdd (value);
						evt.Set ();
					}

					if (predicate ())
						return;
				}

				// First check to see if we comply to predicate
				if (predicate ())
					return;
				
				// Try to complete other work by stealing since our desired tasks may be in other worker
				ThreadWorker other;
				for (int i = 0; i < others.Length; i++) {
					if ((other = others [i]) == null)
						continue;
					
					if (other.dDeque.PopTop (out value) == PopResult.Succeed && value != null) {
						evt.Set ();
						if (CheckTaskFitness (value))
							value.Execute (null);
						else {
							sharedWorkQueue.TryAdd (value);
							evt.Set ();
						}
					}
					
					if (predicate ())
						return;
				}

				Thread.Yield ();
			}
		}
Exemple #37
0
		// Almost same as above but with an added predicate and treating one item at a time. 
		// It's used by Scheduler Participate(...) method for special waiting case like
		// Task.WaitAll(someTasks) or Task.WaitAny(someTasks)
		// Predicate should be really fast and not blocking as it is called a good deal of time
		// Also, the method skip tasks that are LongRunning to avoid blocking (Task are not LongRunning by default)
		public static void ParticipativeWorkerMethod (Task self,
		                                              ManualResetEventSlim predicateEvt,
		                                              int millisecondsTimeout,
		                                              IProducerConsumerCollection<Task> sharedWorkQueue,
		                                              ThreadWorker[] others,
		                                              ManualResetEvent evt,
		                                              Func<Task, Task, bool> checkTaskFitness)
		{
			const int stage1 = 5, stage2 = 0;
			int tries = 50;
			WaitHandle[] handles = null;
			Watch watch = Watch.StartNew ();
			if (millisecondsTimeout == -1)
				millisecondsTimeout = int.MaxValue;
			bool aggressive = false;
			bool hasAutoReference = autoReference != null;
			Action<Task> adder = null;

			while (!predicateEvt.IsSet && watch.ElapsedMilliseconds < millisecondsTimeout && !self.IsCompleted) {
				// We try to execute the self task as it may be the simplest way to unlock
				// the situation
				if (self.Status == TaskStatus.WaitingToRun) {
					self.Execute (hasAutoReference ? autoReference.adder : (Action<Task>)null);
					if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
						return;
				}

				Task value;
				
				// If we are in fact a normal ThreadWorker, use our own deque
				if (hasAutoReference) {
					var enumerable = autoReference.dDeque.GetEnumerable ();
					if (adder == null)
						adder = hasAutoReference ? autoReference.adder : (Action<Task>)null;

					if (enumerable != null) {
						foreach (var t in enumerable) {
							if (t == null)
								continue;

							if (checkTaskFitness (self, t))
								t.Execute (adder);

							if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
								return;
						}
					}
				}

				int count = sharedWorkQueue.Count;

				// Dequeue only one item as we have restriction
				while (--count >= 0 && sharedWorkQueue.TryTake (out value) && value != null) {
					evt.Set ();
					if (checkTaskFitness (self, value) || aggressive)
						value.Execute (null);
					else {
						if (autoReference == null)
							sharedWorkQueue.TryAdd (value);
						else
							autoReference.dDeque.PushBottom (value);
						evt.Set ();
					}

					if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
						return;
				}

				// First check to see if we comply to predicate
				if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
					return;
				
				// Try to complete other work by stealing since our desired tasks may be in other worker
				ThreadWorker other;
				for (int i = 0; i < others.Length; i++) {
					if ((other = others [i]) == autoReference || other == null)
						continue;

					if (other.dDeque.PopTop (out value) == PopResult.Succeed && value != null) {
						evt.Set ();
						if (checkTaskFitness (self, value) || aggressive)
							value.Execute (null);
						else {
							if (autoReference == null)
								sharedWorkQueue.TryAdd (value);
							else
								autoReference.dDeque.PushBottom (value);
							evt.Set ();
						}
					}

					if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
						return;
				}

				/* Waiting is split in 4 phases
				 *   - until stage 1 we simply yield the thread to let others add data
				 *   - between stage 1 and stage2 we use ManualResetEventSlim light waiting mechanism
				 *   - after stage2 we fall back to the heavier WaitHandle waiting mechanism
				 *   - if really the situation isn't evolving after a couple of sleep, we disable
				 *     task fitness check altogether
				 */
				if (--tries > stage1)
					Thread.Yield ();
				else if (tries >= stage2)
					predicateEvt.Wait (ComputeTimeout (5, millisecondsTimeout, watch));
				else {
					if (tries == stage2 - 1)
						handles = new [] { predicateEvt.WaitHandle, evt };
					System.Threading.WaitHandle.WaitAny (handles, ComputeTimeout (1000, millisecondsTimeout, watch));
					if (tries == stage2 - 10)
						aggressive = true;
				}
			}
		}