diff --git a/src/main/java/net/minestom/server/lock/Acquisition.java b/src/main/java/net/minestom/server/lock/Acquisition.java index 9891abc78..a266bc3b4 100644 --- a/src/main/java/net/minestom/server/lock/Acquisition.java +++ b/src/main/java/net/minestom/server/lock/Acquisition.java @@ -90,18 +90,22 @@ public final class Acquisition { time = System.nanoTime(); } - BatchThread current = (BatchThread) currentThread; - ReentrantLock currentLock = current.lock; - final boolean currentAcquired = currentLock.isHeldByCurrentThread(); - if (currentAcquired) + ReentrantLock currentLock; + { + final BatchThread current = currentThread instanceof BatchThread ? + (BatchThread) currentThread : null; + currentLock = current != null && current.getLock().isHeldByCurrentThread() ? + current.getLock() : null; + } + if (currentLock != null) currentLock.unlock(); GLOBAL_LOCK.lock(); - if (currentAcquired) + if (currentLock != null) currentLock.lock(); - final var lock = elementThread != null ? elementThread.lock : null; + final var lock = elementThread != null ? elementThread.getLock() : null; final boolean acquired = lock == null || lock.isHeldByCurrentThread(); if (!acquired) { lock.lock(); diff --git a/src/main/java/net/minestom/server/thread/BatchThread.java b/src/main/java/net/minestom/server/thread/BatchThread.java index 533ffd6f7..488eb9ed5 100644 --- a/src/main/java/net/minestom/server/thread/BatchThread.java +++ b/src/main/java/net/minestom/server/thread/BatchThread.java @@ -14,8 +14,7 @@ import java.util.concurrent.locks.ReentrantLock; public class BatchThread extends Thread { private final BatchRunnable runnable; - - public volatile ReentrantLock lock = new ReentrantLock(); + private final ReentrantLock lock = new ReentrantLock(); public BatchThread(@NotNull BatchRunnable runnable, int number) { super(runnable, MinecraftServer.THREAD_NAME_TICK + "-" + number); @@ -24,11 +23,14 @@ public class BatchThread extends Thread { this.runnable.setLinkedThread(this); } - @NotNull - public BatchRunnable getMainRunnable() { + public @NotNull BatchRunnable getMainRunnable() { return runnable; } + public @NotNull ReentrantLock getLock() { + return lock; + } + public void shutdown() { this.runnable.stop = true; LockSupport.unpark(this); diff --git a/src/main/java/net/minestom/server/thread/ThreadProvider.java b/src/main/java/net/minestom/server/thread/ThreadProvider.java index 359bb2a0f..fbb7dbe58 100644 --- a/src/main/java/net/minestom/server/thread/ThreadProvider.java +++ b/src/main/java/net/minestom/server/thread/ThreadProvider.java @@ -146,7 +146,7 @@ public abstract class ThreadProvider { .collect(Collectors.toList()); Acquirable.refreshEntities(Collections.unmodifiableList(entities)); - final ReentrantLock lock = thread.lock; + final ReentrantLock lock = thread.getLock(); lock.lock(); chunkEntries.forEach(chunkEntry -> { Chunk chunk = chunkEntry.chunk;