Reorder operations in attempt to fix race conditions

This commit is contained in:
mcrcortex
2025-07-12 14:12:06 +10:00
parent f0e1f18379
commit 3199b77ae5
2 changed files with 16 additions and 4 deletions

View File

@@ -96,9 +96,10 @@ public class ServiceSlice extends TrackedObject {
Logger.error("Tried to do work on a dead service: " + this.name, new Throwable()); Logger.error("Tried to do work on a dead service: " + this.name, new Throwable());
return; return;
} }
this.threadPool.addWeight(this);
this.jobCount2.incrementAndGet(); this.jobCount2.incrementAndGet();
this.jobCount.release(); this.jobCount.release();
this.threadPool.execute(this); this.threadPool.execute();
} }
public void shutdown() { public void shutdown() {

View File

@@ -137,8 +137,11 @@ public class ServiceThreadPool {
this.serviceSlices = newArr; this.serviceSlices = newArr;
} }
void execute(ServiceSlice service) { long addWeight(ServiceSlice service) {
this.totalJobWeight.addAndGet(service.weightPerJob); return this.totalJobWeight.addAndGet(service.weightPerJob);
}
void execute() {
this.jobCounter.release(1); this.jobCounter.release(1);
} }
@@ -268,7 +271,15 @@ public class ServiceThreadPool {
//Consumed a job from the service, decrease weight by the amount //Consumed a job from the service, decrease weight by the amount
if (this.totalJobWeight.addAndGet(-service.weightPerJob)<0) { if (this.totalJobWeight.addAndGet(-service.weightPerJob)<0) {
throw new IllegalStateException("Total job weight is negative"); Logger.error("Total job weight is negative");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (this.totalJobWeight.get()<0) {
throw new IllegalStateException("Total job weight still negative");
}
} }
//Sleep for a bit after running a job, yeild the thread //Sleep for a bit after running a job, yeild the thread