internal static void ForEach <TSource>(string parallelizationPointName, IEnumerable <TSource> source, Action <TSource> body) { Verify.ArgumentNotNull(source, "source"); if (source is TSource[]) { int elementsCount = (source as TSource[]).Length; if (elementsCount == 0) { return; } if (elementsCount == 1) { body((source as TSource[])[0]); return; } } else if (source is ICollection <TSource> ) { int elementsCount = (source as ICollection <TSource>).Count; if (elementsCount == 0) { return; } if (elementsCount == 1) { body((source as ICollection <TSource>).First()); return; } } if (ParallelizationProviderRegistry.Enabled && (string.IsNullOrEmpty(parallelizationPointName) || ParralelizationPointEnabled(parallelizationPointName))) { EnsureHttpContextItemsCollectionIsThreadSafe(); using (Profiler.Measure(GetPerformanceMeasureTitle(parallelizationPointName))) { ThreadDataManagerData parentData = ThreadDataManager.Current; var threadWrapper = new ThreadWrapper <TSource>(body, parentData); PromoteThreadAbortException(() => { Parallel.ForEach(source, threadWrapper.WrapperAction); }); } } else { foreach (var s in source) { body(s); } } }
public ThreadWrapper(Action <TSource> body, ThreadDataManagerData parentData) { _body = body; _parentData = parentData; _parentThreadLocale = LocalizationScopeManager.CurrentLocalizationScope; _parentThreadDataScope = DataScopeManager.CurrentDataScope; _parentThreadHttpContext = HttpContext.Current; var currentThread = System.Threading.Thread.CurrentThread; _parentThreadCulture = currentThread.CurrentCulture; _parentThreadUiCulture = currentThread.CurrentUICulture; }
private Dictionary <Type, DataInterceptor> GetDataInterceptors(ThreadDataManagerData threadData) { Verify.ArgumentNotNull(threadData, nameof(threadData)); const string threadDataKey = "DataFacade:DataInterceptors"; var dataInterceptors = threadData.GetValue(threadDataKey) as Dictionary <Type, DataInterceptor>; if (dataInterceptors == null) { dataInterceptors = new Dictionary <Type, DataInterceptor>(); threadData.SetValue(threadDataKey, dataInterceptors); } return(dataInterceptors); }
// private static readonly IEnumerable<Measurement> EmptyReport = new Measurement[0]; /// <exclude /> public static void BeginProfiling() { if (Disabled) { return; } ThreadDataManagerData threadData = ThreadDataManager.Current; Verify.That(!threadData.HasValue(ProfilerKey), "Profiler has already been initialized"); var stack = new Stack <Measurement>(); stack.Push(new Measurement("Root") { MemoryUsage = GC.GetTotalMemory(true) }); threadData.SetValue(ProfilerKey, stack); }
internal static void For(string parallelizationPointName, int fromInclusive, int toExclusive, Action <int> body) { int count = toExclusive - fromInclusive; if (count <= 0) { return; } if (count == 1) { body(fromInclusive); return; } if (ParallelizationProviderRegistry.Enabled && (parallelizationPointName.IsNullOrEmpty() || ParralelizationPointEnabled(parallelizationPointName))) { EnsureHttpContextItemsCollectionIsThreadSafe(); using (Profiler.Measure(GetPerformanceMeasureTitle(parallelizationPointName))) { ThreadDataManagerData parentData = ThreadDataManager.Current; var threadWrapper = new ThreadWrapper <int>(body, parentData); PromoteThreadAbortException(() => { Parallel.For(fromInclusive, toExclusive, threadWrapper.WrapperAction); }); } } else { for (int i = fromInclusive; i < toExclusive; i++) { body(i); } } }
private static bool GetCurrentNode( out Measurement parentNode, out Stack <Measurement> stack, out bool isInParallel) { if (Disabled) { parentNode = null; stack = null; isInParallel = false; return(false); } ThreadDataManagerData currentThreadData = ThreadDataManager.Current; ThreadDataManagerData threadData = currentThreadData; isInParallel = false; while (threadData != null) { if (threadData.HasValue(ProfilerKey)) { stack = threadData[ProfilerKey] as Stack <Measurement>; if (stack.Count > 0) { parentNode = stack.Peek(); return(true); } } // Going to parent thread threadData = threadData.Parent; isInParallel = true; } stack = null; parentNode = null; return(false); }
/// <exclude /> public static IDisposable Measure(string name, Func <EntityToken> entityTokenFactory) { if (Disabled) { return(EmptyDisposable.Instance); } Measurement currentNode; Stack <Measurement> stack; bool isInParallel; if (!GetCurrentNode(out currentNode, out stack, out isInParallel)) { return(EmptyDisposable.Instance); } Stack <Measurement> newNodeStack; if (isInParallel) { ThreadDataManagerData currentThreadData = ThreadDataManager.Current; if (currentThreadData.HasValue(currentThreadData)) { newNodeStack = currentThreadData[ProfilerKey] as Stack <Measurement>; } else { newNodeStack = new Stack <Measurement>(); currentThreadData.SetValue(ProfilerKey, newNodeStack); } } else { newNodeStack = stack; } return(new InfoCollector(currentNode, name, isInParallel, newNodeStack, entityTokenFactory)); }
public ThreadDataServiceScopeDisposable(ThreadDataManagerData threadData) { _threadData = threadData; }