private static void BuildProjects(int startIndex, int endIndex, int totalCount, int failureCount, List <ProcessInfo> processList, ReadyToRunJittedMethods jittedMethods, int degreeOfParallelism) { using (AutoResetEvent processExitEvent = new AutoResetEvent(initialState: false)) { ProcessSlot[] processSlots = new ProcessSlot[degreeOfParallelism]; for (int index = 0; index < degreeOfParallelism; index++) { processSlots[index] = new ProcessSlot(index, processExitEvent); } int progressIndex = startIndex; for (int index = startIndex; index < endIndex; index++) { ProcessInfo processInfo = processList[index]; // Allocate a process slot, potentially waiting on the exit event // when all slots are busy (running) ProcessSlot freeSlot = null; do { foreach (ProcessSlot slot in processSlots) { if (slot.IsAvailable(ref progressIndex, ref failureCount)) { freeSlot = slot; break; } } if (freeSlot == null) { // All slots are busy - wait for a process to finish processExitEvent.WaitOne(200); } }while (freeSlot == null); freeSlot.Launch(processInfo, jittedMethods, index + 1, totalCount, progressIndex, failureCount); } // We have launched all the commands, now wait for all processes to finish bool activeProcessesExist; do { activeProcessesExist = false; foreach (ProcessSlot slot in processSlots) { if (!slot.IsAvailable(ref progressIndex, ref failureCount)) { activeProcessesExist = true; } } if (activeProcessesExist) { processExitEvent.WaitOne(); } }while (activeProcessesExist); } }
private static void BuildProjects(int startIndex, IEnumerable <ProcessInfo> processesToRun, ReadyToRunJittedMethods jittedMethods, StreamWriter logWriter, int degreeOfParallelism) { using (AutoResetEvent processExitEvent = new AutoResetEvent(initialState: false)) { ProcessSlot[] processSlots = new ProcessSlot[degreeOfParallelism]; for (int index = 0; index < degreeOfParallelism; index++) { processSlots[index] = new ProcessSlot(index, processExitEvent, logWriter); } foreach (ProcessInfo processInfo in processesToRun) { // Allocate a process slot, potentially waiting on the exit event // when all slots are busy (running) ProcessSlot freeSlot = null; do { foreach (ProcessSlot slot in processSlots) { if (slot.IsAvailable()) { freeSlot = slot; break; } } if (freeSlot == null) { // All slots are busy - wait for a process to finish processExitEvent.WaitOne(); } }while (freeSlot == null); freeSlot.Launch(processInfo, jittedMethods, startIndex++); } // We have launched all the commands, now wait for all processes to finish bool activeProcessesExist; do { activeProcessesExist = false; foreach (ProcessSlot slot in processSlots) { if (!slot.IsAvailable()) { activeProcessesExist = true; } } if (activeProcessesExist) { processExitEvent.WaitOne(); } }while (activeProcessesExist); } }
/// <summary> /// Execute a given set of mutually independent build commands with given degree of /// parallelism. /// </summary> /// <param name="processesToRun">Processes to execute in parallel</param> /// <param name="degreeOfParallelism">Maximum number of processes to execute in parallel</param> public static void Run(IEnumerable <ProcessInfo> processesToRun, int degreeOfParallelism) { int processCount = processesToRun.Count(); if (processCount < degreeOfParallelism) { // We never need a higher DOP than the number of process to execute degreeOfParallelism = processCount; } using (AutoResetEvent processExitEvent = new AutoResetEvent(initialState: false)) { ProcessSlot[] processSlots = new ProcessSlot[degreeOfParallelism]; for (int index = 0; index < degreeOfParallelism; index++) { processSlots[index] = new ProcessSlot(index, processExitEvent); } int processIndex = 0; foreach (ProcessInfo processInfo in processesToRun) { // Allocate a process slot, potentially waiting on the exit event // when all slots are busy (running) ProcessSlot freeSlot = null; do { foreach (ProcessSlot slot in processSlots) { if (slot.IsAvailable()) { freeSlot = slot; break; } } if (freeSlot == null) { // All slots are busy - wait for a process to finish processExitEvent.WaitOne(); } }while (freeSlot == null); freeSlot.Launch(processInfo, ++processIndex); } // We have launched all the commands, now wait for all processes to finish bool activeProcessesExist; do { activeProcessesExist = false; foreach (ProcessSlot slot in processSlots) { if (!slot.IsAvailable()) { activeProcessesExist = true; } } if (activeProcessesExist) { processExitEvent.WaitOne(); } }while (activeProcessesExist); } }