From cd3b40399c4331fad20f52300f3563c5409f8c25 Mon Sep 17 00:00:00 2001 From: mcrcortex <18544518+MCRcortex@users.noreply.github.com> Date: Tue, 6 Aug 2024 22:09:37 +1000 Subject: [PATCH] Fix service pools --- .../common/world/thread/ServiceSlice.java | 1 + .../world/thread/ServiceThreadPool.java | 22 ++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main/java/me/cortex/voxy/common/world/thread/ServiceSlice.java b/src/main/java/me/cortex/voxy/common/world/thread/ServiceSlice.java index f040c366..ebaabc65 100644 --- a/src/main/java/me/cortex/voxy/common/world/thread/ServiceSlice.java +++ b/src/main/java/me/cortex/voxy/common/world/thread/ServiceSlice.java @@ -74,6 +74,7 @@ public class ServiceSlice extends TrackedObject { if (!this.alive) { throw new IllegalStateException("Tried to do work on a dead service"); } + this.jobCount.release(); this.threadPool.execute(this); } diff --git a/src/main/java/me/cortex/voxy/common/world/thread/ServiceThreadPool.java b/src/main/java/me/cortex/voxy/common/world/thread/ServiceThreadPool.java index f9578298..d1b150fc 100644 --- a/src/main/java/me/cortex/voxy/common/world/thread/ServiceThreadPool.java +++ b/src/main/java/me/cortex/voxy/common/world/thread/ServiceThreadPool.java @@ -42,6 +42,10 @@ public class ServiceThreadPool { synchronized void removeService(ServiceSlice service) { this.removeServiceFromArray(service); this.totalJobWeight.addAndGet(-((long) service.weightPerJob) * service.jobCount.availablePermits()); + //Need to acquire all the shut-down jobs + if (!this.jobCounter.tryAcquire(service.jobCount.availablePermits())) { + throw new IllegalStateException("Failed to acquire all the permits for the shut down jobs"); + } } private synchronized void removeServiceFromArray(ServiceSlice service) { @@ -80,16 +84,24 @@ public class ServiceThreadPool { private void worker(int threadId) { long seed = 1234342; while (true) { - seed = (seed ^ seed >>> 30) * -4658895280553007687L; - seed = (seed ^ seed >>> 27) * -7723592293110705685L; - long clamped = seed&((1L<<63)-1); this.jobCounter.acquireUninterruptibly(); if (!this.running) { break; } + int attempts = 50; while (true) { + if (attempts-- == 0) { + throw new IllegalStateException("All attempts at executing a job failed! something critically wrong has occurred"); + } + seed = (seed ^ seed >>> 30) * -4658895280553007687L; + seed = (seed ^ seed >>> 27) * -7723592293110705685L; + long clamped = seed&((1L<<63)-1); var ref = this.serviceSlices; + if (ref.length == 0) { + System.err.println("Service worker tried to run but had 0 slices"); + break; + } long chosenNumber = clamped % this.totalJobWeight.get(); ServiceSlice service = ref[(int) (clamped % ref.length)]; for (var slice : ref) { @@ -138,6 +150,10 @@ public class ServiceThreadPool { worker.join(); } } catch (InterruptedException e) {throw new RuntimeException(e);} + + if (this.totalJobWeight.get() != 0) { + throw new IllegalStateException("Service pool job weight not 0 after shutdown"); + } } public int getThreadCount() {