private void removeCq_Action(int item, ParallelLoopState ls) { int r; cq.TryDequeue(out r); cqDestination.Enqueue(r); }
/// <summary> /// ParallelFor DoAction callback /// </summary> /// <param name="iterationItem">Instance of <see cref="object"/></param> /// <param name="parallelLoopState">Instance of <see cref="ParallelLoopState"/></param> /// <param name="objectState">Instance of <see cref="ObjectState"/></param> /// <returns>Instance of <see cref="ObjectState"/></returns> private static ObjectState DoActionCallback(StringWrapper iterationItem, ParallelLoopState parallelLoopState, ObjectState objectState) { if (iterationItem == null) { return(objectState); } var characterSets = objectState.PasswordGenerator.CharacterSets; var characterSetsForRandom = objectState.PasswordGenerator.CharacterSetsForRandom; var characterSetsCount = objectState.PasswordGenerator.CharacterSetsCount; var passwordLength = objectState.PasswordLength; var maxPasswordLength = objectState.MaxLength; var passwordStringBuilder = new StringBuilder(passwordLength, maxPasswordLength); for (var i = 0; i < passwordLength; i++) { passwordStringBuilder.Append(PasswordGenerator.RetrieveRandomCharacter(characterSets, characterSetsForRandom, characterSetsCount)); } iterationItem.Password = passwordStringBuilder.ToString(); return(objectState); }
private byte[] AdventCoin(long counter, ParallelLoopState loopState) { var sb = new StringBuilder(); using var md5 = MD5.Create(); return(md5.ComputeHash(Encoding.ASCII.GetBytes($"{s}{counter}"))); }
/// <summary> /// Initializes all components in this map. /// </summary> public override void Revalidate(ParallelLoopState loopState) { Thread.CurrentThread.SetName("Revalidate-{0}".FormatWith(Thread.CurrentThread.ManagedThreadId)); // If we got into a hard error state (not an aggregate error), we won't be able to recover if (MapElement.State == RuntimeState.Error) { MapElement.UpdateStatus(); return; } MapElement.UpdateStatus("Validating", RuntimeState.Validating); try { Parallel.ForEach <ResourceHost>(Hosts, (h, l) => h.Revalidate(l)); if (!SessionMapElement.AllElementsSetTo <ResourceHost>(Hosts, RuntimeState.Validated)) { MapElement.UpdateStatus(RuntimeState.AggregateError); loopState.Break(); return; } MapElement.UpdateStatus("Validated", RuntimeState.Validated); } catch (AggregateException ex) { // Log the exception at this element level, then throw again to catch it higher MapElement.UpdateStatus(RuntimeState.Error, "Validation error", ex); throw; } }
// Метод, служащий в качестве тела параллельно выполняемого цикла. // Операторы этого цикла просто расходуют время ЦП для целей демонстрации. static void MyTransform(int i, ParallelLoopState pls) { // Прервать цикл при обнаружении отрицательного значения. if (data[i] < 0) { pls.Break(); } data[i] = data[i] / 10; if (data[i] < 1000) { data[i] = 0; } if (data[i] > 1000 & data[i] < 2000) { data[i] = 100; } if (data[i] > 2000 & data[i] < 3000) { data[i] = 200; } if (data[i] > 3000) { data[i] = 300; } }
/// <summary> /// Turns on all resource hosts in this map. /// </summary> public override void PowerUp(ParallelLoopState loopState) { try { MapElement.UpdateStatus("Starting", RuntimeState.Starting); TraceFactory.Logger.Debug("Calling PowerUp() in parallel on each Host"); Parallel.ForEach <ResourceHost>(Hosts, (h, l) => h.PowerUp(l)); if (!SessionMapElement.AllElementsSetTo <ResourceHost>(Hosts, RuntimeState.Ready)) { MapElement.UpdateStatus(RuntimeState.Error); loopState.Break(); return; } MapElement.UpdateStatus("Ready", RuntimeState.Ready); } catch (AggregateException ex) { TraceFactory.Logger.Error(ex.Message); // Log the exception at this element level, then throw again to catch it higher MapElement.UpdateStatus(RuntimeState.Error, "Power up error", ex); throw; } }
/// <summary> /// Builds all components in this map. /// </summary> public override void Stage(ParallelLoopState loopState) { Thread.CurrentThread.SetName("Stage-{0}".FormatWith(Thread.CurrentThread.ManagedThreadId)); TraceFactory.Logger.Debug("Entering..."); // Use the index to create an initial unique hostname that will be // replaced during the validation stage with an actual hostname. foreach (var manifest in Configuration.ManifestSet) { ResourceHost host = new ResourceHost(manifest); host.OnResourcesComplete += new EventHandler(ResourceHostsCompleted); Hosts.Add(host); } try { Parallel.ForEach <ResourceHost>(Hosts, (h, l) => h.Stage(l)); if (!SessionMapElement.AllElementsSetTo <ResourceHost>(Hosts, RuntimeState.Available)) { loopState.Break(); return; } } catch (AggregateException ex) { // Log the exception at this element level, then throw again to catch it higher MapElement.UpdateStatus(RuntimeState.Error, "Staging error", ex); throw; } }
static Jenkins96 HandleLine(string line, ParallelLoopState state, Jenkins96 hasher) { if (string.IsNullOrWhiteSpace(line)) { return(hasher); } if (line.IndexOfAny(pathInvalidChars) != -1) { return(hasher); } line = line.Replace('/', '\\'); //string extLower = Path.GetExtension(line).ToLower(); //if (extLower == ".mdx" || extLower == ".mdl") //{ // line = Path.ChangeExtension(line, ".m2"); // extLower = ".m2"; //} ulong hash = hasher.ComputeHash(line); if (!CheckName(hash, line)) { return(hasher); } processed[hash] = true; return(hasher); }
/// <summary> /// Builds all components in this map. /// </summary> /// <param name="loopState">State of the loop.</param> public override void Stage(ParallelLoopState loopState) { var allQueues = Configuration.ManifestSet.SelectMany(n => n.ActivityPrintQueues.Values.SelectMany(m => m.OfType <RemotePrintQueueInfo>())); foreach (RemotePrintQueueInfo queue in allQueues.GroupBy(n => n.PrintQueueId).Select(n => n.First())) { RemotePrintQueueElements.Add(new RemotePrintQueueElement(queue.QueueName, queue.ServerHostName)); } try { Parallel.ForEach <RemotePrintQueueElement>(RemotePrintQueueElements, (h, l) => h.Stage(l)); if (!SessionMapElement.AllElementsSetTo <RemotePrintQueueElement>(RemotePrintQueueElements, RuntimeState.Available)) { loopState.Break(); return; } MapElement.UpdateStatus("Available", RuntimeState.Available); } catch (AggregateException ex) { MapElement.UpdateStatus(RuntimeState.Error, "Staging error", ex); throw; } }
private SpellingIssue IdentifyIssues(string word, IDocument document, SyntaxToken identifier, CommonSyntaxNode node, ParallelLoopState state, SpellingIssue dummy) { bool found = WordLookupService.SearchExact(word.ToLower()); if (found) return (SpellingIssue)null; var suggestions = WordLookupService.Search(word.ToLower()) .Select(m => char.IsUpper(identifier.ValueText[0]) ? (char.ToUpper(m[0]) + m.Substring(1)) : m) .ToList(); var actions = new List<ICodeAction>(); foreach (var suggestion in suggestions) { actions.Add(new FixSpellingCodeAction(document, node, identifier.ValueText, identifier.ValueText.Replace(word, suggestion))); } var spanStart = identifier.Span.Start + identifier.ValueText.IndexOf(word, StringComparison.InvariantCultureIgnoreCase); var span = new TextSpan(spanStart, word.Length); return new SpellingIssue(span, word, actions); }
/// <summary> /// Executes this asset host, which may mean different things. /// </summary> public virtual void Run(ParallelLoopState loopState) { if (Endpoint == null) { MapElement.UpdateStatus("The endpoint for '{0}' is null".FormatWith(Id), RuntimeState.Error); return; } Action action = () => { using (var client = VirtualResourceManagementConnection.Create(Endpoint)) { TraceFactory.Logger.Debug("{0}: starting activities".FormatWith(Id)); client.Channel.StartMainRun(); } }; try { Retry.WhileThrowing(action, 2, TimeSpan.FromSeconds(1), new List <Type>() { typeof(Exception) }); MapElement.UpdateStatus("Running", RuntimeState.Running); } catch (EndpointNotFoundException ex) { MapElement.UpdateStatus("ComError", RuntimeState.Error); TraceFactory.Logger.Error(ex); } }
private void PatternNext(StringBuilder sb, int position, int length, ParallelLoopState pastate) { for (int i = 0; i < CharSetAvaliable.Length; i++) { sb.Replace(sb[position], CharSetAvaliable[i], position, 1); if (IsFound) { if (pastate != null) { pastate.Break(); } return; } if (position == length - 1) { OnPattern(sb.ToString()); } else { PatternNext(sb, position + 1, length); } } }
private void Calculate(int index, ParallelLoopState pls) { float x = minX + deltaX * index; float y = (float)graph.GetResult(x); points[index] = new Vector2(x, y); }
private void PingService(string ip, ParallelLoopState state) { HttpWebRequest request = null; HttpWebResponse response = null; var reqUrl = "http://" + ip + "/ajax.xml"; request = (HttpWebRequest) WebRequest.Create(reqUrl); request.Timeout = 20000; request.AllowAutoRedirect = false; Debug.WriteLine(reqUrl); var flag = -1; try { response = (HttpWebResponse) request.GetResponse(); flag = 1; } catch { flag = -1; } if (flag == 1) { if (response != null && response.ContentType.Equals("text/xml")) { _gatewayIp = ip; if (state != null) { state.Stop(); } } } }
private void RenderToImage(int layerIndex, ParallelLoopState pls) { if (pls.ShouldExitCurrentIteration) { return; } var layer = _layers[layerIndex]; if (layer.Enabled) { if (layer.MaxVisible >= _map.Zoom && layer.MinVisible < _map.Zoom) { var image = _images[layerIndex] = new Bitmap(_map.Size.Width, _map.Size.Height, PixelFormat.Format32bppArgb); using (var g = Graphics.FromImage(image)) { g.PageUnit = GraphicsUnit.Pixel; ApplyTransform(_transform, g); g.Clear(Color.Transparent); layer.Render(g, _map); } } } }
private void DownloadFileAux(Uri arg1, ParallelLoopState arg2, long arg3) { using (WebClient client = new WebClient()) { client.DownloadFileAsync(arg1, path + arg1.Segments[2].ToString()); } }
private bool ProcessRuleNode(XmlNode productNode, DOMReader dom, ParallelLoopState loopState) { foreach (XmlNode andOrNode in productNode.SelectSingleNode("Rules").ChildNodes) { if (loopState.IsStopped) { return(false); } if (andOrNode.NodeType == XmlNodeType.Element) { if (andOrNode.Name != "Rule") { bool match = ProcessLogicNode(andOrNode, dom); if (match) { return(true); } } else { //just in case there is only 1 rule in rules. No AND/OR if (ValidateRule(new MarketShareRule(andOrNode), dom)) { return(true); } } } } return(false); }
//Разбиение строки на слова, выборка и подсчёт триплетов static void FrequencyAnalysis(int index, ParallelLoopState pls) { char[] delimeters = { ' ', '\t' }; string[] words = lines[index].Split(delimeters); if (isEnd == true) //Прерывание цикла при нажатии клавиши { pls.Break(); } for (int i = 0; i < words.Length; i++) { //Console.WriteLine("we here"); if (words[i].Length >= 3) { for (int j = 0; j <= words[i].Length - 3; j++) { //Console.WriteLine("we here2"); string strTriplet = words[i].Substring(j, 3); if (IsLetterWord(strTriplet)) { Triplet triplet = new Triplet(strTriplet); if (!IsListContains(triplets, triplet)) { triplets.Add(triplet); } } } } } }
private void ProcessMessage(Message message, ParallelLoopState loopState) { //Here be dragons! (or your code to process a message, your choice :-)) //Use if(_cts.Token.IsCancellationRequested || loopState.ShouldExitCurrentIteration) to test if // we should quit out of the function early for a graceful shutdown. }
public static IEnumerable <long> GetPrimesParAggregation(long[] vals, int size) { LinkedList <List <long> > primes = new LinkedList <List <long> >(); ParallelLoopResult res = Parallel.For( 0, size, () => new List <long>(size / 50), (i, s, l) => { ParallelLoopState state = s; WorkerThreadReport.RegisterWorker(); if (IsPrime(vals[i])) { l.Add(vals[i]); } return(l); }, (l) => { lock (primes) { primes.AddLast(l); } }); var count = primes.Count(); return(primes.SelectMany(s => s)); }
private void TransformInsertsAndUpdates(TableMapping tableMapping, ParallelLoopState loopState) { if (!loopState.IsExceptional) { var etl = new TransformationEtlProcess( tableMapping.PublicStagingTable, (PublicTable)tableMapping.PublicTable, _caches, _connectionString, _timeoutSecs, _configuration, TransformationType.Upsert, _srcTimetableId); etl.Execute(); var errors = etl.GetAllErrors().ToArray(); if (errors.Any()) { loopState.Stop(); string msg = $"Errors occurred during transform of inserts and updates in public schema (table={tableMapping.PublicTable})"; _log.Error(msg); // throw the first exception throw new ApplicationException(msg, errors[0]); } } }
private int ExecuteSecondPass(AnalyseVideo analyseVideo, ParallelLoopState loopState, int currentPass) { //pass2 (remove prefix / suffixes) if (analyseVideo.Candidates.Count == 0 || analyseVideo.MatchPercentage < Constants.GREAT_MATCH_FOUND_TRESHOLD) { string fileNameGuess = analyseVideo.GetMainFileNameGuess(); string folderNameGuess = analyseVideo.GetMainFolderNameGuess(); string fileName = Path.GetFileNameWithoutExtension(analyseVideo.Video.Files[0].Path); if (fileName != null) { List <string> fileNameGuesses = VideoTitleExtractor.GetTitleGuessesFromString(fileName.ToLower(), true); analyseVideo.AddTitleGuesses(fileNameGuesses); } var directoryName = Path.GetDirectoryName(analyseVideo.Video.Files[0].Path); if (directoryName != null) { string folderName = directoryName.Split(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar).Last().ToLower(); List <string> folderNameGuesses = VideoTitleExtractor.GetTitleGuessesFromString(folderName, true); analyseVideo.AddTitleGuesses(folderNameGuesses); } FillCandidates(analyseVideo, fileNameGuess, folderNameGuess); } analyseVideo.HandledTitleGuesses(); ExecuteAfterPass(currentPass); //if (CancellationPending) //{ // e.Cancel = true; // return; //} return(currentPass); }
private ThreadData Extract(string relativeFilePath, ParallelLoopState state, ThreadData data) { var extension = Path.GetExtension(relativeFilePath); var extractor = data.GetExtractor(extension); if (extractor == null) { return(data); } string content; LocalizableTextInfo[] texts; try { content = File.ReadAllText(Path.Combine(_basePath !, relativeFilePath)); texts = extractor.Extract(content).ToArray(); } catch (Exception ex) { data.Results.Add(relativeFilePath, new ExtractResult { Error = ex.Message.Replace(Environment.NewLine, " ") }); return(data); } if (texts.Length > 0) { data.Results.Add(relativeFilePath, new ExtractResult { Texts = texts }); } return(data); }
private void Crawl2()//优化函数,嵌套循环就对外层循环进行并行处理,下载时候进行多线程 { var stopWatch = new Stopwatch(); stopWatch.Start(); //Console.WriteLine("开始爬行了。。。。"); Parallel.For(0, 11, (Ncount, ParallelLoopState) => { string current = null; foreach (string url in urls.Keys) //找到一个还没有下载过的链接 { if ((bool)urls[url]) { continue; //已经下载过的,不再下载 } current = url; } if (current == null || Ncount > 10) { ParallelLoopState.Break(); //这个线程达到停止要求了 return; } //Console.WriteLine("爬行" + current + "页面!"); string html = Download(current);//下载 urls[current] = true; Parse(html); } ); // Console.WriteLine("爬行结束"); stopWatch.Stop(); Console.WriteLine("ParallelFor run " + stopWatch.ElapsedMilliseconds + " ms.");//输出执行时间而已 }
// workload for Foreach overload that takes a range partitioner private void WorkWithIndexAndStop(Tuple <int, int> tuple, ParallelLoopState state, long index) { for (int i = tuple.Item1; i < tuple.Item2; i++) { WorkWithIndexAndStop(i, state, index); } }
/// <summary> /// Extracted method to make the code clearer. /// </summary> /// <param name="j"></param> /// <param name="options"></param> /// <param name="window"></param> /// <param name="xStep"></param> /// <param name="yStep"></param> /// <param name="xEnd"></param> /// <param name="bag"></param> private void ProcessWindows(int j, ParallelLoopState options, Rectangle window, int xStep, int yStep, int xEnd, ConcurrentBag <Rectangle> bag) { int y = j * yStep; // Create a local window reference Rectangle localWindow = window; localWindow.Y = y; // For each pixel in the window row for (int x = 0; x < xEnd; x += xStep) { if (options.ShouldExitCurrentIteration) { return; } localWindow.X = x; // Try to detect and object inside the window if (classifier.Compute(integralImage, localWindow)) { // an object has been detected bag.Add(localWindow); if (searchMode == ObjectDetectorSearchMode.Single) { options.Stop(); } } } }
// workload for Foreach overload that takes a range partitioner private void WorkWithStop(Tuple <int, int> tuple, ParallelLoopState state) { for (int i = tuple.Item1; i < tuple.Item2; i++) { WorkWithStop(i, state); } }
protected IList <T> WrapDoingWork(int index, ParallelLoopState state, IList <T> tasks) { try { if (IsStop()) { _log.Warn("Stopping spider work..."); state.Stop(); } else if (state.IsStopped) { _log.Warn("Stopped..."); } else { DoingWork(_availableTask[index]); tasks.Add(_availableTask[index]); } } catch (Exception ex) { _log.Error(ex, "Task Error: ", _availableTask[index]); } return(tasks); }
// workload for Foreach overload that takes a range partitioner private void WorkWithStop(Tuple <long, long> tuple, ParallelLoopState state) { for (long i = tuple.Item1; i < tuple.Item2; i++) { WorkWithStop(i, state); } }
private static long ParallelTask(TimeSpan timeSpan, ParallelLoopState parallelLoopState, long index) { Log.InfoFormat("Starting parallel branch {0} for {1}", index, timeSpan); var stopWatch = Stopwatch.StartNew(); long lastLog = 0; long nowTicks; while ((nowTicks = stopWatch.ElapsedTicks) < timeSpan.Ticks) { // Like Thread.Sleep but does not give thread control to others // approx 0.54 seconds at ~2.7Ghz Thread.SpinWait(100_000_000); if (nowTicks - lastLog > LogEvery.Ticks) { var percentage = nowTicks / (double)timeSpan.Ticks; Log.InfoFormat("Branch {0}, {1:##0.00%} completed", index, percentage); lastLog = nowTicks; } } stopWatch.Stop(); Log.Info("Completed branch " + index); return(stopWatch.ElapsedTicks); }
protected loopData RunPicker(LoadBits load, ParallelLoopState state, loopData data) { if (!isRunning || state.IsStopped || state.ShouldExitCurrentIteration || state.IsExceptional) { state.Stop(); return(data); } foreach (var rune in load.A.Runes) { data.Mon.ApplyRune(rune, 7); } foreach (var rune in load.B.Runes) { data.Mon.ApplyRune(rune, 7); } if (load.C != null) { foreach (var rune in load.C.Runes) { data.Mon.ApplyRune(rune, 7); } } bake(data, true); return(data); }
private void RenderToImage(int layerIndex, ParallelLoopState pls) { if (pls.ShouldExitCurrentIteration) { return; } var layer = _layers[layerIndex]; if (layer.Enabled) { double compare = layer.VisibilityUnits == VisibilityUnits.ZoomLevel ? _mapViewPort.Zoom : _mapScale; if (layer.MaxVisible >= compare && layer.MinVisible < compare) { var image = _images[layerIndex] = new Bitmap(_mapViewPort.Size.Width, _mapViewPort.Size.Height, PixelFormat.Format32bppArgb); using (var g = Graphics.FromImage(image)) { g.PageUnit = GraphicsUnit.Pixel; ApplyTransform(_mapViewPort.MapTransform, g); g.Clear(Color.Transparent); RenderLayer(layer, g, _mapViewPort); } } } }
public override void ParallelLoopBody(string str, ParallelLoopState state, long i) { var index = (int)i; var result = default(string); var node = this.IncreaseBuffer(false, i); if (node != null) { result = this.Replacer.Replace(str); node.Array[i % this.bufferSize] = result; return; } result = this.Replacer.Replace(str); node = this.IncreaseBuffer(true, i); if (node != null) node.Array[i % this.bufferSize] = result; else { // Determine if we search node from first or last one. if (this.lastNode.TotalCapacity - i > i) { node = this.firstNode.Next; while (node.TotalCapacity <= i) node = node.Next; } else { node = this.lastNode; while (node.TotalCapacity - this.bufferSize > i) node = node.Previous; } #if DEBUG Debug.WriteLineIf(i - (node.TotalCapacity - this.bufferSize) > node.Array.Length, string.Format("{0} - ({1} - {2})", i, node.TotalCapacity, this.bufferSize)); #endif node.Array[i - (node.TotalCapacity - this.bufferSize)] = result; } }
// Workload for Parallel.For with long range private void WorkWithNoLocalState(long i, ParallelLoopState state) { Logger.LogInformation("WorkWithNoLocalState(long) on index {0}, StartIndex: {1}, real index {2}", i, _startIndex, i - _startIndex); Work(i); _actions[(int)(i - _startIndex)].Invoke(i, state); }
/// <summary> /// This is the thread safe version of an action that throws exceptions and called be called concurrently /// /// This action throws an exception for the first parameters.Count/2 iterations /// /// For the rest of the actions, it performs the following checks /// 1) If an exception was already thrown then check that ParallelLoopState->IsException is true /// 2) If an exception was already thrown then check that ParallelLoopState->ShouldExitCurrentIteration is true /// </summary> /// <param name="i"></param> /// <param name="state"></param> private void MultipleExceptionAction(long i, ParallelLoopState state) { Debug.WriteLine("Calling ExceptionalAction2 on index {0}, StartIndex: {1}, real index {2}", i, _startIndex, i - _startIndex); if (Interlocked.Increment(ref _iterCount) < _parameters.Count / 2) { try { throw new System.InvalidOperationException("Throws test exception to verify it got handled properly"); } finally { _isExceptional = true; } } else { Assert.False(state.IsExceptional && !_isExceptional, String.Format("IsExceptional is expected to be {0} while getting {1}", _isExceptional, state.IsExceptional)); // Previous iteration throws exception, the Parallel should stop it's work Assert.False(state.IsExceptional && !state.ShouldExitCurrentIteration, String.Format("Expecting ShouldExitCurrentIteration to be true, since Exception was thrown on previous iterations")); } }
// Workload for Parallel.For with long range private void WorkWithNoLocalState(long i, ParallelLoopState state) { Debug.WriteLine("WorkWithNoLocalState(long) on index {0}, StartIndex: {1}, real index {2}", i, _startIndex, i - _startIndex); Work(i); _actions[(int)(i - _startIndex)].Invoke(i, state); }
/// <summary> /// This actions tests multiple Break calls from different iteration loops /// /// Helper function that calls Break for the first parameters.Count/2 iterations /// /// If Break was already called then check that /// 1) If this iteration is greater than the lowest break iteration, then shouldExitCurrentIteration should be true /// 2) if this iteration is lower than the lowest break iteration then shouldExitCurrentIteration should be false /// </summary> /// <param name="i"></param> /// <param name="state"></param> /// <param name="catchException"></param> private void MultipleBreakAction(long i, ParallelLoopState state) { if (Interlocked.Increment(ref _iterCount) < _parameters.Count / 2) { state.Break(); lock (_lock) { // Save the lowest beak iteration //m_lowestBreakIter = !m_lowestBreakIter.HasValue ? i : Math.Min(m_lowestBreakIter.Value, i); if (!_lowestBreakIter.HasValue) _lowestBreakIter = i; if (_lowestBreakIter.Value > i) _lowestBreakIter = i; } } else { // If we previously called Break() on the parallel state, // we expect all higher iterations see the state's ShouldExitCurrentIteration to be true if (state.LowestBreakIteration.HasValue) { Assert.False(i > state.LowestBreakIteration.Value && !state.ShouldExitCurrentIteration, String.Format("Expecting ShouldExitCurrentIteration to be true for iteration {0}, LowestBreakIteration is {1}", i, state.LowestBreakIteration.Value)); } if (state.LowestBreakIteration.HasValue && i < state.LowestBreakIteration.Value && state.ShouldExitCurrentIteration) { long lbi = state.LowestBreakIteration.Value; // If we previously called Break() on the parallel state, // we expect all lower iterations see the state's ShouldExitCurrentIteration to be false. // There is however a race condition during the check here, another Break could've happen // in between retrieving LowestBreakIteration value and ShouldExitCurrentIteration // which changes the value of ShouldExitCurrentIteration. // We do another sample instead of LowestBreakIteration before failing the test Assert.False(i < lbi, String.Format("Expecting ShouldExitCurrentIteration to be false for iteration {0}, LowestBreakIteration is {1}", i, lbi)); } } }
/// <summary> /// Note!! This function is not thread safe and care must be taken so it is not called concurrently /// /// This helper throws an exception from the current iteration if an exception is not already thrown /// /// 1) If an exception was previously thrown (m_isExceptional = true), then it checks if /// ParallelLoopState-->IsExceptional is set /// 2) If an exception was previously thrown then it checks if ParallelLoopState-->ShouldExitCurrentIteration is true /// /// If an exception was not thrown before this, then throw an exception and set test flag m_isExceptional to true /// </summary> /// <param name="i"></param> /// <param name="state"></param> private void ExceptionalAction(long i, ParallelLoopState state) { Debug.WriteLine("Calling ExceptionalAction on index {0}, StartIndex: {1}, real index {2}", i, _startIndex, i - _startIndex); Assert.False(_isExceptional != state.IsExceptional, String.Format("IsExceptional is expected to be {0} while getting {1}", _isExceptional, state.IsExceptional)); // Previous iteration throws exception, the Parallel should stop it's work Assert.False(_isExceptional && !state.ShouldExitCurrentIteration, String.Format("Expecting ShouldExitCurrentIteration to be true, since Exception was thrown on previous iterations")); try { throw new InvalidOperationException("Throws test exception to verify it got handled properly"); } finally { _isExceptional = true; } }
/// <summary> /// Thread safe version of Stop Action. This can safely be invoked concurrently /// /// Stops the loop for first parameters.Count/2 iterations and sets the test flag (m_iterCount) to indicate this /// /// 1) If Stop was previously called, then check that ParallelLoopState-->IsStopped is set to true /// 2) If Stop was previously called, then check that ParallelLoopState-->ShouldExitCurrentIteration is true /// </summary> /// <param name="i"></param> /// <param name="state"></param> /// <param name="catchException"></param> private void MultipleStopAction(long i, ParallelLoopState state) { if (Interlocked.Increment(ref _iterCount) < _parameters.Count / 2) { state.Stop(); _isStopped = true; } else { // We already called Stop() on the Parallel state Assert.False(_isStopped && !state.IsStopped, String.Format("Expecting IsStopped to be true for iteration {0}", i)); // If we previously called Stop() on the parallel state, // we expect all iterations see the state's ShouldExitCurrentIteration to be true Assert.False(_isStopped && !state.ShouldExitCurrentIteration, String.Format("Expecting ShouldExitCurrentIteration to be true for iteration {0}", i)); } }
/// <summary> /// NOTE!!! that this function is not thread safe and cannot be called concurrently /// /// Helper function that calls Break for the current iteration if /// 1) Break has never been called so far /// 2) if the current iteration is smaller than the iteration for which Break was previously called /// /// If Break was already called then check that /// 1) The lowest break iteration stored by us is the same as the one passed in State /// 2) If this iteration is greater than the lowest break iteration, then shouldExitCurrentIteration should be true /// 3) if this iteration is lower than the lowest break iteration then shouldExitCurrentIteration should be false /// </summary> /// <param name="i">current iteration </param> /// <param name="state">the parallel loop state</param> /// <param name="catchException">whether calling Break will throw an InvalidOperationException</param> private void BreakActionHelper(long i, ParallelLoopState state, bool catchException) { Debug.WriteLine("Calling BreakAction on index {0}, StartIndex: {1}, real index {2}", i, _startIndex, i - _startIndex); // If we previously called Break() on the parallel state, // we expect all iterations to have the same LowestBreakIteration value if (_lowestBreakIter.HasValue) { Assert.False(state.LowestBreakIteration.Value != _lowestBreakIter.Value, String.Format("Expecting LowestBreakIteration value to be {0} for iteration {1}, while getting {2}", _lowestBreakIter, i, state.LowestBreakIteration.Value)); // If we previously called Break() on the parallel state, // we expect all higher iterations see the state's ShouldExitCurrentIteration to be true Assert.False(i > _lowestBreakIter.Value && !state.ShouldExitCurrentIteration, String.Format("Expecting ShouldExitCurrentIteration to be true for iteration {0}, LowestBreakIteration is {1}", i, _lowestBreakIter)); } if (_lowestBreakIter.HasValue && i < _lowestBreakIter.Value && state.ShouldExitCurrentIteration) { long lbi = _lowestBreakIter.Value; // If we previously called Break() on the parallel state, // we expect all lower iterations see the state's ShouldExitCurrentIteration to be false. // There is however a race condition during the check here, another Break could've happen // in between retrieving LowestBreakIteration value and ShouldExitCurrentIteration // which changes the value of ShouldExitCurrentIteration. // We do another sample instead of LowestBreakIteration before failing the test Assert.False(i < lbi, String.Format("Expecting ShouldExitCurrentIteration to be false for iteration {0}, LowestBreakIteration is {1}", i, lbi)); } if (!_lowestBreakIter.HasValue || (_lowestBreakIter.HasValue && i < _lowestBreakIter.Value)) { // If calls Break for the first time or if current iteration less than LowestBreakIteration, // call Break() again, and make sure LowestBreakIteration value gets updated try { state.Break(); _lowestBreakIter = state.LowestBreakIteration; // Save the lowest beak iteration // If the test is checking the scenario where break is called after stop then // we expect an InvalidOperationException Assert.False(catchException, "Not getting InvalidOperationException from Break() when expecting one"); } // If the test is checking the scenario where break is called after stop then // we expect an InvalidOperationException catch (InvalidOperationException) when (catchException) { } } }
/// <summary> /// Calls Break for the current Iteration. Note that this is called by only one iteration in the loop /// </summary> /// <param name="i"></param> /// <param name="state"></param> private void BreakAction(long i, ParallelLoopState state) { BreakActionHelper(i, state, false); }
/// <summary> /// Note!! This function is not threadsafe and care must be taken so that it is not called concurrently /// /// Helper function that calls Stop for the current iteration and sets test flag(m_isStopped) to true /// /// 1) If stop was already called, check if ParallelLoopState-->IsStopped is true /// 2) If stop was already called, check if ParallelLoopState-->ShouldExitCurrentIteration is true /// </summary> /// <param name="i"></param> /// <param name="state"></param> /// <param name="catchException"></param> private void StopActionHelper(long i, ParallelLoopState state, bool catchException) { Debug.WriteLine("Calling StopAction on index: {0}, StartIndex: {1}, real index {2}", i, _startIndex, i - _startIndex); // We already called Stop() on the Parallel state Assert.False(_isStopped && _isStopped != state.IsStopped, String.Format("Expecting IsStopped to be true for iteration {0}", i)); // If we previously called Stop() on the parallel state, // we expect all iterations see the state's ShouldExitCurrentIteration to be true Assert.False(_isStopped && !state.ShouldExitCurrentIteration, String.Format("Expecting ShouldExitCurrentIteration to be true for iteration {0}", i)); try { state.Stop(); _isStopped = true; // If Stop is called after a Break was called then an InvalidOperationException is expected Assert.False(catchException, "Not getting InvalidOperationException from Stop() when expecting one"); } // If Stop is called after a Break was called then an InvalidOperationException is expected catch (InvalidOperationException) when (catchException) { } }
// Workload for Parallel.For / Foreach with index, parallel loop state and thread local state private List<int> WorkWithLocalState(int i, int index, ParallelLoopState state, List<int> threadLocalValue) { Debug.WriteLine("WorkWithLocalState(int, index) on index {0}, StartIndex: {1}, real index {2}", i, _startIndex, i - _startIndex); Work(i); threadLocalValue.Add(index + (int)_startIndex); _actions[index].Invoke(index, state); return threadLocalValue; }
// Workload for Parallel.For with long range private List<long> WorkWithLocalState(long i, ParallelLoopState state, List<long> threadLocalValue) { Debug.WriteLine("WorkWithLocalState(long) on index {0}, StartIndex: {1}, real index {2}", i, _startIndex, i - _startIndex); Work(i); threadLocalValue.Add(i + _startIndex); _actions[(int)(i - _startIndex)].Invoke(i, state); return threadLocalValue; }
/// <summary> /// This action waits for the other iteration to call Break and /// set the MRE when its done. Once the MRE is set, this function /// calls Stop which results in an InvalidOperationException /// </summary> /// <param name="i"></param> /// <param name="state"></param> private void SyncWaitStopCatchExp(long i, ParallelLoopState state) { _mreSlim.Wait(); StopActionHelper(i, state, true); }
/// <summary> /// This function waits for another iteration to call Stop /// and then set the shared MRE to notify when it is done /// </summary> /// <param name="i"></param> /// <param name="state"></param> private void SyncWaitStop(long i, ParallelLoopState state) { //Logger.LogInformation("Calling SyncWaitStopAction on index {0}, StartIndex: {1}, real index {2}", i, StartIndex, i - StartIndex); _mreSlim.Wait(); StopAction(i, state); }
/// <summary> /// This action calls Stop and notifies the other iteration by setting the MRE /// </summary> /// <param name="i"></param> /// <param name="state"></param> private void SyncSetStop(long i, ParallelLoopState state) { //Logger.LogInformation("Calling SyncSetStopAction on index {0}, StartIndex: {1}, real index {2}", i, StartIndex, i - StartIndex); // Do some sleep to reduce race condition with next action Task delay = Task.Delay(10); delay.Wait(); StopAction(i, state); _mreSlim.Set(); }
/// <summary> /// This actions calls Stop on the current iteration. Note that this is called by only one iteration in the loop /// </summary> /// <param name="i"></param> /// <param name="state"></param> private void StopAction(long i, ParallelLoopState state) { StopActionHelper(i, state, false); }
private void CheckAndAddAssambly([NotNull] string filename, ParallelLoopState state) { string fileName = Path.GetFileName(filename); if (fileName != null && (!string.IsNullOrEmpty(PriorityKey) && !Regex.IsMatch(fileName, PriorityKey, RegexOptions.CultureInvariant))) { LowPriorityList.Enqueue(fileName); return; } CheckAndAddAssambly(filename, state.Stop); }
/// <summary> /// This action waits for another iteration to throw an exception and notify /// when it is done by setting the MRE /// </summary> /// <param name="i"></param> /// <param name="state"></param> private void SyncWaitExceptional(long i, ParallelLoopState state) { _mreSlim.Wait(); ExceptionalAction(i, state); }
// Workload for Parallel.For / Foreach with parallel loopstate and thread local state private List<int> WorkWithLocalState(int i, ParallelLoopState state, List<int> threadLocalValue) { Logger.LogInformation("WorkWithLocalState(int) on index {0}, StartIndex: {1}, real index {2}", i, _startIndex, i - _startIndex); Work(i); threadLocalValue.Add(i + (int)_startIndex); _actions[i].Invoke(i, state); return threadLocalValue; }
/// <summary> /// This action throws an exception and notifies the rest of the iterations /// by setting a shared MRE /// </summary> /// <param name="i"></param> /// <param name="state"></param> private void SyncSetExceptional(long i, ParallelLoopState state) { // Do some sleep to reduce race condition with next action Task delay = Task.Delay(10); delay.Wait(); ExceptionalAction(i, state); _mreSlim.Set(); }
/// <summary> /// This action is a NOP - does nothing /// </summary> /// <param name="i"></param> /// <param name="state"></param> private void DummyAction(long i, ParallelLoopState state) { }
private StringBuilder ParallelRunDataObjects_Callback(int stepLabel, ParallelLoopState loopOption, StringBuilder info) { if (bwkRun.CancellationPending == true) { loopOption.Stop(); } else { int threadID = Thread.CurrentThread.ManagedThreadId; string strInfo = RunDataObjectsOnPool(stepLabel, threadID); info.Append(strInfo); } return info; }
private void DeliveryCycle(ParallelOptions options, EmailMessage message, ParallelLoopState state) { if (state.ShouldExitCurrentIteration) { _backlog.Enqueue(message); } else { if(message.DeliveryTime.HasValue) { if(message.DeliveryTime.Value.ToUniversalTime() > DateTime.UtcNow) { if(_outgoing.IsAddingCompleted) { _backlog.Enqueue(message); } else { _outgoing.Add(message); } return; } } if (_provider.Send(message)) { Interlocked.Increment(ref _delivered); } else { // Back seat in the queue if(_config.RetryPolicy != null) { var decision = _config.RetryPolicy.DecideOn(message); switch(decision) { case DeliveryRetryDecision.RetryImmediately: DeliveryCycle(options, message, state); break; case DeliveryRetryDecision.SendToBackOfQueue: _outgoing.Add(message); break; case DeliveryRetryDecision.SendToBacklog: _backlog.Enqueue(message); break; case DeliveryRetryDecision.SendToUndeliverableFolder: Undeliverable(message); break; case DeliveryRetryDecision.Destroy: break; default: throw new ArgumentOutOfRangeException(); } } else { _outgoing.Add(message); } } } options.CancellationToken.ThrowIfCancellationRequested(); }