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());
return;
}
this.threadPool.addWeight(this);
this.jobCount2.incrementAndGet();
this.jobCount.release();
this.threadPool.execute(this);
this.threadPool.execute();
}
public void shutdown() {

View File

@@ -137,8 +137,11 @@ public class ServiceThreadPool {
this.serviceSlices = newArr;
}
void execute(ServiceSlice service) {
this.totalJobWeight.addAndGet(service.weightPerJob);
long addWeight(ServiceSlice service) {
return this.totalJobWeight.addAndGet(service.weightPerJob);
}
void execute() {
this.jobCounter.release(1);
}
@@ -268,7 +271,15 @@ public class ServiceThreadPool {
//Consumed a job from the service, decrease weight by the amount
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