From 9d0677a3d7a280659b66d62ebfbdd35f34dd2363 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Sat, 7 Jan 2023 13:06:53 -0500 Subject: [PATCH] Make OrderedParallelModDispatcher more reliable --- .../modernfix/util/OrderedParallelModDispatcher.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/embeddedt/modernfix/util/OrderedParallelModDispatcher.java b/src/main/java/org/embeddedt/modernfix/util/OrderedParallelModDispatcher.java index 746ee543..a08754fe 100644 --- a/src/main/java/org/embeddedt/modernfix/util/OrderedParallelModDispatcher.java +++ b/src/main/java/org/embeddedt/modernfix/util/OrderedParallelModDispatcher.java @@ -35,10 +35,9 @@ public class OrderedParallelModDispatcher { public static void dispatchBlocking(Executor executor, Consumer task, Collection modIDsToFilter) { Set finishedMods = Collections.synchronizedSet(new HashSet<>(modIDsToFilter)); HashMap> submittedFutures = new HashMap<>(); - int numMods = ModList.get().getMods().size(); Semaphore jobWaitingSemaphore = new Semaphore(0); ArrayList remainingModList = new ArrayList<>(ModList.get().getMods()); - while(finishedMods.size() < numMods) { + while(remainingModList.size() > 0) { remainingModList.removeIf(modInfo -> { if(finishedMods.contains(modInfo.getModId())) return true; @@ -48,14 +47,14 @@ public class OrderedParallelModDispatcher { .filter(modId -> !finishedMods.contains(modId)) .collect(Collectors.toList()); if(missingDependencies.size() > 0) { - //ModernFix.LOGGER.debug(DISPATCHER, "Cannot process " + modInfo.getModId() + ", as it is waiting on mods: [" + String.join(", ", missingDependencies) + "]"); + ModernFix.LOGGER.debug(DISPATCHER, "Cannot process " + modInfo.getModId() + ", as it is waiting on mods: [" + String.join(", ", missingDependencies) + "]"); return false; } Optional modContainerOpt = ModList.get().getModContainerById(modInfo.getModId()); if(!modContainerOpt.isPresent()) throw new IllegalStateException("Can't find mod container"); ModContainer container = modContainerOpt.get(); - //ModernFix.LOGGER.debug(DISPATCHER, "Submitting job for " + modInfo.getModId()); + ModernFix.LOGGER.debug(DISPATCHER, "Submitting job for " + modInfo.getModId()); submittedFutures.put(modInfo.getModId(), CompletableFuture.runAsync(() -> { Supplier contextExtension = ObfuscationReflectionHelper.getPrivateValue(ModContainer.class, container, "contextExtension"); ModLoadingContext.get().setActiveContainer(container, contextExtension.get()); @@ -75,7 +74,7 @@ public class OrderedParallelModDispatcher { return true; }); Preconditions.checkState(submittedFutures.size() > 0, "The semaphore will block forever!"); - //ModernFix.LOGGER.debug(DISPATCHER, "Waiting for one of [" + String.join(", ", submittedFutures.keySet()) + "] to finish..."); + ModernFix.LOGGER.debug(DISPATCHER, "Waiting for one of [" + String.join(", ", submittedFutures.keySet()) + "] to finish..."); try { jobWaitingSemaphore.acquire(); } catch(InterruptedException e) { @@ -83,12 +82,13 @@ public class OrderedParallelModDispatcher { } submittedFutures.entrySet().removeIf(entry -> { if(entry.getValue().isDone()) { - //ModernFix.LOGGER.debug(DISPATCHER, "Job finished for " + entry.getKey()); + ModernFix.LOGGER.debug(DISPATCHER, "Job finished for " + entry.getKey()); return true; } return false; }); } + submittedFutures.values().forEach(CompletableFuture::join); } public static void dispatchBlocking(Executor executor, Consumer task) {