Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/rs117/hd/renderer/zone/ZoneRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ private void preSceneDrawTopLevel(
Scene scene,
float cameraX, float cameraY, float cameraZ, float cameraPitch, float cameraYaw
) {
jobSystem.processPendingClientCallbacks();
JobSystem.processPendingClientCallbacks();

scene.setDrawDistance(plugin.getDrawDistance());

Expand Down Expand Up @@ -601,7 +601,7 @@ private void preSceneDrawTopLevel(

@Override
public void postSceneDraw(Scene scene) {
jobSystem.processPendingClientCallbacks();
JobSystem.processPendingClientCallbacks();

WorldViewContext ctx = sceneManager.getContext(scene);
if (ctx == null || !sceneManager.isRoot(ctx) && ctx.isLoading)
Expand Down Expand Up @@ -1045,7 +1045,7 @@ public void draw(int overlayColor) {
plugin.drawUi(overlayColor);
frameTimer.end(Timer.DRAW_SUBMIT);

jobSystem.processPendingClientCallbacks();
JobSystem.processPendingClientCallbacks();

frameTimer.end(Timer.DRAW_FRAME);
frameTimer.end(Timer.RENDER_FRAME);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/rs117/hd/utils/collections/ConcurrentPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.annotation.Nonnull;
import rs117.hd.utils.Destructible;
import rs117.hd.utils.DestructibleHandler;
import rs117.hd.utils.jobs.JobSystem;

public final class ConcurrentPool<T> {
private final ConcurrentLinkedQueue<T> pool = new ConcurrentLinkedQueue<>();
Expand Down Expand Up @@ -42,6 +43,7 @@ public T acquireBlocking(int timeoutNanos) {
while ((obj = pool.poll()) == null) {
if (!parkedThreads.contains(currentThread))
parkedThreads.add(currentThread);
JobSystem.processPendingClientCallbacks();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While proof reading this, I realised that this call actually never does anything (currently). processPendingClientCallbacks exits early if it's not on the client thread, and acquireBlocking is currently only ever called from render threads in drawDynamic.

LockSupport.parkNanos(1000);
if (System.nanoTime() > deadline)
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/rs117/hd/utils/jobs/JobHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ boolean await(int timeoutNanos) throws InterruptedException {
long start = System.currentTimeMillis();
int seconds = 0;
while (!tryAcquireSharedNanos(0, TimeUnit.MILLISECONDS.toNanos(1))) {
JOB_SYSTEM.processPendingClientCallbacks();
JobSystem.processPendingClientCallbacks();
Thread.yield();
long elapsed = System.currentTimeMillis() - start;
int newSeconds = (int) (elapsed / 1000);
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/rs117/hd/utils/jobs/JobSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@Slf4j
@Singleton
public final class JobSystem {
private static JobSystem INSTANCE;
public static final boolean VALIDATE = false;

@Inject
Expand Down Expand Up @@ -53,6 +54,7 @@ public final class JobSystem {
Semaphore workerSemaphore;

public void startUp(CpuUsageLimit cpuUsageLimit) {
INSTANCE = this;
workerCount = max(1, ceil((PROCESSOR_COUNT - 1) * cpuUsageLimit.threadRatio));
workers = new Worker[workerCount];
workerSemaphore = new Semaphore(workerCount);
Expand Down Expand Up @@ -141,6 +143,7 @@ public void shutDown() {
log.debug("All workers shutdown successfully");

threadToWorker.clear();
clientCallbacks.clear();
workers = null;
}

Expand Down Expand Up @@ -239,13 +242,16 @@ void invokeClientCallback(Runnable callback) throws InterruptedException {
}
}

public void processPendingClientCallbacks() {
int size = clientCallbacks.size();
public static void processPendingClientCallbacks() {
if (INSTANCE == null || !INSTANCE.client.isClientThread())
return;

int size = INSTANCE.clientCallbacks.size();
if (size == 0)
return;

ClientCallbackJob pair;
while (size-- > 0 && (pair = clientCallbacks.poll()) != null) {
while (size-- > 0 && (pair = INSTANCE.clientCallbacks.poll()) != null) {
try {
pair.callback.run();
} catch (Throwable ex) {
Expand Down