Fix tick thread not being properly terminated

This commit is contained in:
TheMode 2021-04-15 04:06:55 +02:00
parent b3a548287a
commit f4ee5505ca
4 changed files with 26 additions and 17 deletions

View File

@ -251,6 +251,7 @@ public final class UpdateManager {
* Stops the server loop. * Stops the server loop.
*/ */
public void stop() { public void stop() {
stopRequested = true; this.stopRequested = true;
this.threadProvider.shutdown();
} }
} }

View File

@ -105,7 +105,7 @@ public interface Acquirable<T> {
} }
@ApiStatus.Internal @ApiStatus.Internal
public void refreshBatchInfo(BatchThread batchThread, Chunk batchChunk) { public void refreshBatchInfo(@NotNull BatchThread batchThread, @NotNull Chunk batchChunk) {
this.batchThread = batchThread; this.batchThread = batchThread;
this.batchChunk = batchChunk; this.batchChunk = batchChunk;
} }

View File

@ -34,7 +34,10 @@ public class BatchThread extends Thread {
} }
public void shutdown() { public void shutdown() {
synchronized (runnable.tickLock) {
this.runnable.stop = true; this.runnable.stop = true;
this.runnable.tickLock.notifyAll();
}
} }
public static class BatchRunnable implements Runnable { public static class BatchRunnable implements Runnable {
@ -79,6 +82,10 @@ public class BatchThread extends Thread {
// Wait for the next notify (game tick) // Wait for the next notify (game tick)
try { try {
if (stop) {
break;
}
this.tickLock.wait(); this.tickLock.wait();
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
@ -91,7 +98,7 @@ public class BatchThread extends Thread {
this.countDownLatch = countDownLatch; this.countDownLatch = countDownLatch;
this.queue.add(runnable); this.queue.add(runnable);
synchronized (tickLock) { synchronized (tickLock) {
this.tickLock.notify(); this.tickLock.notifyAll();
} }
} }

View File

@ -92,6 +92,7 @@ public abstract class ThreadProvider {
for (BatchThread thread : threads) { for (BatchThread thread : threads) {
final var chunkEntries = threadChunkMap.get(thread); final var chunkEntries = threadChunkMap.get(thread);
if (chunkEntries == null) { if (chunkEntries == null) {
// The thread never had any task
countDownLatch.countDown(); countDownLatch.countDown();
continue; continue;
} }
@ -115,8 +116,8 @@ public abstract class ThreadProvider {
} }
public synchronized void refreshThreads() { public synchronized void refreshThreads() {
// Clear removed entities // Clear removed entities
{
for (Entity entity : removedEntities) { for (Entity entity : removedEntities) {
Acquirable<Entity> acquirable = entity.getAcquiredElement(); Acquirable<Entity> acquirable = entity.getAcquiredElement();
Chunk batchChunk = acquirable.getHandler().getBatchChunk(); Chunk batchChunk = acquirable.getHandler().getBatchChunk();
@ -130,6 +131,7 @@ public abstract class ThreadProvider {
} }
} }
this.removedEntities.clear(); this.removedEntities.clear();
}
// Update as many entities as possible // Update as many entities as possible
// TODO: incremental update instead of full // TODO: incremental update instead of full
@ -163,7 +165,7 @@ public abstract class ThreadProvider {
} }
} }
public void removeEntity(Entity entity) { public void removeEntity(@NotNull Entity entity) {
this.removedEntities.add(entity); this.removedEntities.add(entity);
} }
@ -171,8 +173,7 @@ public abstract class ThreadProvider {
this.threads.forEach(BatchThread::shutdown); this.threads.forEach(BatchThread::shutdown);
} }
@NotNull public @NotNull List<@NotNull BatchThread> getThreads() {
public List<BatchThread> getThreads() {
return threads; return threads;
} }