public static void ForEach <TKey, TValue, TLocal>([NotNull] Dictionary <TKey, TValue> collection, [Pooled] Func <TLocal> initializeLocal, [Pooled] Action <KeyValuePair <TKey, TValue>, TLocal> action, [Pooled] Action <TLocal> finalizeLocal = null) { if (MaxDegreeOfParallelism <= 1 || collection.Count <= 1) { ExecuteBatch(collection, 0, collection.Count, initializeLocal, action, finalizeLocal); } else { var state = BatchState.Acquire(); try { var batchCount = Math.Min(MaxDegreeOfParallelism, collection.Count); var batchSize = (collection.Count + (batchCount - 1)) / batchCount; // Kick off a worker, then perform work synchronously state.AddReference(); Fork(collection, batchSize, MaxDegreeOfParallelism, initializeLocal, action, finalizeLocal, state); // Wait for all workers to finish if (state.ActiveWorkerCount != 0) { state.Finished.WaitOne(); } } finally { state.Release(); } } }
public static void ForEach <TKey, TValue, TLocal>([NotNull] Dictionary <TKey, TValue> collection, [Pooled] Func <TLocal> initializeLocal, [Pooled] Action <KeyValuePair <TKey, TValue>, TLocal> action, [Pooled] Action <TLocal> finalizeLocal = null) { if (MaxDegreeOfParallelism <= 1 || collection.Count <= 1) { ExecuteBatch(collection, 0, collection.Count, initializeLocal, action, finalizeLocal); } else { var state = BatchState.Acquire(); try { var batchCount = Math.Min(MaxDegreeOfParallelism, collection.Count); var batchSize = (collection.Count + (batchCount - 1)) / batchCount; // Kick off a worker, then perform work synchronously state.AddReference(); Fork(collection, batchSize, MaxDegreeOfParallelism, initializeLocal, action, finalizeLocal, state); // Wait for all workers to finish state.WaitCompletion(collection.Count); var ex = Interlocked.Exchange(ref state.ExceptionThrown, null); if (ex != null) { throw ex; } } finally { state.Release(); } } }
public static void ForEach <TKey, TValue>(Dictionary <TKey, TValue> collection, [Pooled] Action <KeyValuePair <TKey, TValue> > action) { if (MaxDregreeOfParallelism <= 1 || collection.Count <= 1) { ExecuteBatch(collection, 0, collection.Count, action); } else { var state = BatchState.Acquire(); try { int batchCount = Math.Min(MaxDregreeOfParallelism, collection.Count); int batchSize = (collection.Count + (batchCount - 1)) / batchCount; // Kick off a worker, then perform work synchronously state.AddReference(); Fork(collection, batchSize, MaxDregreeOfParallelism, action, state); // Wait for all workers to finish if (state.ActiveWorkerCount != 0) { state.Finished.WaitOne(); } } finally { state.Release(); } } }
public static void For <TLocal>(int fromInclusive, int toExclusive, [Pooled] Func <TLocal> initializeLocal, [Pooled] Action <int, TLocal> action, [Pooled] Action <TLocal> finalizeLocal = null) { using (Profile(action)) { if (fromInclusive > toExclusive) { var temp = fromInclusive; fromInclusive = toExclusive + 1; toExclusive = temp + 1; } var count = toExclusive - fromInclusive; if (count == 0) { return; } if (MaxDegreeOfParallelism <= 1 || count == 1) { ExecuteBatch(fromInclusive, toExclusive, initializeLocal, action, finalizeLocal); } else { var state = BatchState.Acquire(); state.WorkDone = state.StartInclusive = fromInclusive; try { var batchCount = Math.Min(MaxDegreeOfParallelism, count); var batchSize = (count + (batchCount - 1)) / batchCount; // Kick off a worker, then perform work synchronously state.AddReference(); Fork(toExclusive, batchSize, MaxDegreeOfParallelism, initializeLocal, action, finalizeLocal, state); // Wait for all workers to finish if (state.WorkDone < toExclusive) { state.Finished.WaitOne(); } var ex = Interlocked.Exchange(ref state.ExceptionThrown, null); if (ex != null) { throw ex; } } finally { state.Release(); } } } }
public static void For(int fromInclusive, int toExclusive, [Pooled] Action <int> action) { using (Profile(action)) { if (fromInclusive > toExclusive) { var temp = fromInclusive; fromInclusive = toExclusive + 1; toExclusive = temp + 1; } var count = toExclusive - fromInclusive; if (count == 0) { return; } if (MaxDegreeOfParallelism <= 1 || count == 1) { ExecuteBatch(fromInclusive, toExclusive, action); } else { var state = BatchState.Acquire(); state.StartInclusive = fromInclusive; try { var batchCount = Math.Min(MaxDegreeOfParallelism, count); var batchSize = (count + (batchCount - 1)) / batchCount; // Kick off a worker, then perform work synchronously state.AddReference(); Fork(toExclusive, batchSize, MaxDegreeOfParallelism, action, state); // Wait for all workers to finish if (state.ActiveWorkerCount != 0) { state.Finished.WaitOne(); } } finally { state.Release(); } } } }