diff --git a/src/main/java/me/cortex/voxy/client/VoxyClientInstance.java b/src/main/java/me/cortex/voxy/client/VoxyClientInstance.java index 2cfe7f5a..c0b6f7db 100644 --- a/src/main/java/me/cortex/voxy/client/VoxyClientInstance.java +++ b/src/main/java/me/cortex/voxy/client/VoxyClientInstance.java @@ -29,8 +29,6 @@ public class VoxyClientInstance extends VoxyInstance { public VoxyClientInstance() { super(); - this.threadPool.setNumThreads(VoxyConfig.CONFIG.serviceThreads); - var path = FlashbackCompat.getReplayStoragePath(); this.noIngestOverride = path != null; if (path == null) { diff --git a/src/main/java/me/cortex/voxy/client/compat/SemaphoreBlockImpersonator.java b/src/main/java/me/cortex/voxy/client/compat/SemaphoreBlockImpersonator.java new file mode 100644 index 00000000..21dacdba --- /dev/null +++ b/src/main/java/me/cortex/voxy/client/compat/SemaphoreBlockImpersonator.java @@ -0,0 +1,33 @@ +package me.cortex.voxy.client.compat; + +import me.cortex.voxy.common.thread3.MultiThreadPrioritySemaphore; + +import java.util.concurrent.Semaphore; + +public class SemaphoreBlockImpersonator extends Semaphore { + private final MultiThreadPrioritySemaphore.Block block; + public SemaphoreBlockImpersonator(MultiThreadPrioritySemaphore.Block block) { + super(0); + this.block = block; + } + + @Override + public void release(int permits) { + this.block.release(permits); + } + + @Override + public void acquire() throws InterruptedException { + this.block.acquire(); + } + + @Override + public boolean tryAcquire() { + return this.block.tryAcquire(); + } + + @Override + public int availablePermits() { + return this.block.availablePermits(); + } +} diff --git a/src/main/java/me/cortex/voxy/client/config/VoxyConfigScreenPages.java b/src/main/java/me/cortex/voxy/client/config/VoxyConfigScreenPages.java index 197ad12e..cb706341 100644 --- a/src/main/java/me/cortex/voxy/client/config/VoxyConfigScreenPages.java +++ b/src/main/java/me/cortex/voxy/client/config/VoxyConfigScreenPages.java @@ -57,23 +57,13 @@ public abstract class VoxyConfigScreenPages { //Runtime.getRuntime().availableProcessors(),//Note: this is threads not cores, the default value is half the core count, is fine as this should technically be the limit but CpuLayout.CORES.length is more realistic 1, v->Text.literal(Integer.toString(v)))) .setBinding((s, v)->{ - boolean wasEnabled = VoxyCommon.getInstance() != null; - var vrsh = (IGetVoxyRenderSystem) MinecraftClient.getInstance().worldRenderer; - if (wasEnabled) { - if (vrsh != null) { - vrsh.shutdownRenderer(); - } - VoxyCommon.shutdownInstance(); - } - s.serviceThreads = v; - - if (wasEnabled) { - VoxyCommon.createInstance(); + var instance = VoxyCommon.getInstance(); + if (instance != null) { + instance.setNumThreads(v); } }, s -> s.serviceThreads) .setImpact(OptionImpact.HIGH) - .setFlags(OptionFlag.REQUIRES_RENDERER_RELOAD) .build() ).add(OptionImpl.createBuilder(boolean.class, storage) .setName(Text.translatable("voxy.config.general.ingest")) diff --git a/src/main/java/me/cortex/voxy/common/thread3/MultiThreadPrioritySemaphore.java b/src/main/java/me/cortex/voxy/common/thread3/MultiThreadPrioritySemaphore.java index 30623247..949c90f2 100644 --- a/src/main/java/me/cortex/voxy/common/thread3/MultiThreadPrioritySemaphore.java +++ b/src/main/java/me/cortex/voxy/common/thread3/MultiThreadPrioritySemaphore.java @@ -42,6 +42,23 @@ public class MultiThreadPrioritySemaphore { this.man.freeBlock(this); this.free0(); } + + public int availablePermits() { + return this.localSemaphore.availablePermits(); + } + + public boolean tryAcquire() { + if (this.localSemaphore.availablePermits()==0) return false;//Quick exit + if (!this.blockSemaphore.tryAcquire()) return false;//There is definatly none + if (this.localSemaphore.tryAcquire()) { + //we acquired a proper permit + return true; + } else { + //We must release the other permit as we dont do processing here + this.blockSemaphore.release(1); + return false; + } + } } private final Semaphore pooledSemaphore = new Semaphore(0); diff --git a/src/main/java/me/cortex/voxy/common/thread3/UnifiedServiceThreadPool.java b/src/main/java/me/cortex/voxy/common/thread3/UnifiedServiceThreadPool.java index 0ff91d3e..80080498 100644 --- a/src/main/java/me/cortex/voxy/common/thread3/UnifiedServiceThreadPool.java +++ b/src/main/java/me/cortex/voxy/common/thread3/UnifiedServiceThreadPool.java @@ -9,7 +9,7 @@ import java.util.concurrent.atomic.AtomicInteger; public class UnifiedServiceThreadPool { public final ServiceManager serviceManager; - private final MultiThreadPrioritySemaphore groupSemaphore; + public final MultiThreadPrioritySemaphore groupSemaphore; private final MultiThreadPrioritySemaphore.Block selfBlock; private final ThreadGroup dedicatedPool; diff --git a/src/main/java/me/cortex/voxy/commonImpl/VoxyInstance.java b/src/main/java/me/cortex/voxy/commonImpl/VoxyInstance.java index bfc140d4..28606792 100644 --- a/src/main/java/me/cortex/voxy/commonImpl/VoxyInstance.java +++ b/src/main/java/me/cortex/voxy/commonImpl/VoxyInstance.java @@ -1,5 +1,6 @@ package me.cortex.voxy.commonImpl; +import me.cortex.voxy.client.config.VoxyConfig; import me.cortex.voxy.common.Logger; import me.cortex.voxy.common.config.section.SectionStorage; import me.cortex.voxy.common.thread.ServiceThreadPool; @@ -58,6 +59,10 @@ public abstract class VoxyInstance { this.worldCleaner.start(); } + public void setNumThreads(int threads) { + this.threadPool.setNumThreads(threads); + } + protected ImportManager createImportManager() { return new ImportManager(); } @@ -65,6 +70,9 @@ public abstract class VoxyInstance { public ServiceManager getServiceManager() { return this.threadPool.serviceManager; } + public UnifiedServiceThreadPool getThreadPool() { + return this.threadPool; + } public VoxelIngestService getIngestService() { return this.ingestService; }