Skip to content

Commit

Permalink
final threading cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
FalsePattern committed May 6, 2024
1 parent 2adb5e9 commit b527fe2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
import java.util.concurrent.atomic.AtomicInteger;

public class CircularTaskQueue {
private WorldRenderer[] renderers;
private final WorldRenderer[] renderers;
private final AtomicInteger head = new AtomicInteger();
private final AtomicInteger tail = new AtomicInteger();
public void setCapacity(int capacity) {

public CircularTaskQueue(int capacity) {
head.set(0);
tail.set(0);
renderers = new WorldRenderer[capacity];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public class ThreadedChunkUpdateHelper implements IRenderGlobalListener {
* Used within the scope of WorldRenderer#updateRenderer (on the main thread).
*/
public static WorldRenderer lastWorldRenderer;
private final AtomicReference<PendingTaskUpdate> taskQueueUnsorted = new AtomicReference<>();
/**
* Finished tasks ready for consumption
*/
Expand Down Expand Up @@ -171,13 +170,11 @@ public Future<List<WorldRenderer>> cleanup() {
/**
* Tasks not yet started
*/
private final CircularTaskQueue taskQueue = new CircularTaskQueue();
private final int framesSinceLastSubmit = 0;
private AtomicReference<PendingTaskUpdate> taskQueueUnsorted = null;
private AtomicBoolean run = null;
private PoolWorker currentPoolWorker = null;
private WorkerThread[] currentThreads = null;
private int threadCount = 0;
private WorkSorterThread workSorter;
private WorkSorterThread workSorter = null;

private static boolean hasFlag(int flags, int bit) {
return (flags & bit) != 0;
Expand Down Expand Up @@ -288,49 +285,43 @@ public void spawnThreads(RenderGlobal rg) {
if (run != null) {
run.set(false);
}

try {
if (currentPoolWorker != null) {
FTWorker.removeTask(currentPoolWorker);
currentPoolWorker = null;
}
if (currentThreads != null) {
for (val thread : currentThreads) {
thread.interrupt();
}
}
if (workSorter != null) {
FTWorker.removeTask(workSorter);
workSorter = null;
}
if (currentThreads != null) {
for (val thread: currentThreads)
thread.join();
currentThreads = null;
if (currentPoolWorker != null) {
FTWorker.removeTask(currentPoolWorker);
currentPoolWorker = null;
}
if (currentThreads != null) {
for (val thread : currentThreads) {
thread.interrupt();
}
} catch (InterruptedException e) {
e.printStackTrace();
currentThreads = null;
}
if (workSorter != null) {
FTWorker.removeTask(workSorter);
workSorter = null;
}
if (taskQueueUnsorted != null) {
taskQueueUnsorted.set(null);
}
Share.log.info("Creating " + threads + " chunk builder" + (threads > 1 ? "s" : ""));
String nameBase = "Chunk Update Worker #";
if (Loader.isModLoaded("lumina")) {
nameBase = "$LUMI_NO_RELIGHT" + nameBase;
}
taskQueue.setCapacity(rg.worldRenderers.length);
CircularTaskQueue taskQueue = new CircularTaskQueue(rg.worldRenderers.length);
taskQueueUnsorted = new AtomicReference<>(null);
run = new AtomicBoolean(true);
workSorter = new WorkSorterThread();
workSorter = new WorkSorterThread(run, taskQueueUnsorted, taskQueue);
FTWorker.addTask(workSorter);
if (threads > 0) {
currentThreads = new WorkerThread[threads];
threadCount = threads;
for (int i = 0; i < threads; i++) {
val t = new WorkerThread(nameBase + i);
val t = new WorkerThread(nameBase + i, run, taskQueue);
currentThreads[i] = t;
t.setDaemon(true);
t.start();
}
} else {
currentPoolWorker = new PoolWorker();
currentPoolWorker = new PoolWorker(run, taskQueue);
FTWorker.addTask(currentPoolWorker);
}
}
Expand All @@ -344,14 +335,15 @@ public void onConfigChangedEvent(ConfigChangedEvent.OnConfigChangedEvent e) {
}

private void preRendererUpdates(List<WorldRenderer> toUpdateList, int updateLimit) {
if (taskQueueUnsorted == null)
return;
updateWorkQueue(toUpdateList, updateLimit);
removeCancelledResults();
}

private void updateWorkQueue(List<WorldRenderer> toUpdateList, int updateLimit) {
final int updateQueueSize = updateLimit;//threadCount * ThreadingConfig.UPDATE_QUEUE_SIZE_PER_THREAD;

taskQueueUnsorted.getAndSet(new PendingTaskUpdate(new ArrayList<>(toUpdateList), updateQueueSize));
taskQueueUnsorted.getAndSet(new PendingTaskUpdate(new ArrayList<>(toUpdateList), updateLimit));
}

private void removeCancelledResults() {
Expand Down Expand Up @@ -556,9 +548,17 @@ public void renderedQuads(TesselatorVertexState quads) {
}

private class WorkSorterThread implements ThreadedTask {
private final AtomicBoolean myRun = run;
private final AtomicBoolean myRun;
private final WRComparator comp = new WRComparator(true);
private final InterruptableSorter<WorldRenderer> sorter = new InterruptableSorter<>(comp);
private final AtomicReference<PendingTaskUpdate> taskQueueUnsorted;
private final CircularTaskQueue taskQueue;

public WorkSorterThread(AtomicBoolean run, AtomicReference<PendingTaskUpdate> taskQueueUnsorted, CircularTaskQueue taskQueue) {
myRun = run;
this.taskQueueUnsorted = taskQueueUnsorted;
this.taskQueue = taskQueue;
}

@Override
public boolean alive() {
Expand Down Expand Up @@ -640,7 +640,13 @@ private void runTask(WorldRenderer wr) {
}

private class PoolWorker implements ThreadedTask {
private final AtomicBoolean myRun = run;
private final AtomicBoolean myRun;
private final CircularTaskQueue taskQueue;

public PoolWorker(AtomicBoolean run, CircularTaskQueue taskQueue) {
myRun = run;
this.taskQueue = taskQueue;
}

@Override
public boolean alive() {
Expand Down Expand Up @@ -670,19 +676,23 @@ public boolean doWork() {
}

private class WorkerThread extends Thread {
public WorkerThread(String name) {
private final AtomicBoolean myRun;
private final CircularTaskQueue taskQueue;
public WorkerThread(String name, AtomicBoolean run, CircularTaskQueue taskQueue) {
super(name);
this.myRun = run;
this.taskQueue = taskQueue;
}

@Override
public void run() {
val myRun = run;
while (myRun.get()) {
if (Debug.ENABLED && !Debug.chunkRebaking) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
if (!myRun.get())
return;
}
continue;
}
Expand All @@ -691,7 +701,8 @@ public void run() {
try {
Thread.sleep(1);
} catch (InterruptedException ignored) {

if (!myRun.get())
return;
}
continue;
}
Expand Down

0 comments on commit b527fe2

Please sign in to comment.