diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 478331bb3..682ee080e 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -325,7 +325,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, packet.process(this); } - if (username.equals("TheMode911")) + //if (username.equals("TheMode911")) for (Player p1 : MinecraftServer.getConnectionManager().getOnlinePlayers()) { p1.getAcquiredElement().acquire(o -> { //for (Player p2 : MinecraftServer.getConnectionManager().getOnlinePlayers()) diff --git a/src/main/java/net/minestom/server/lock/Acquirable.java b/src/main/java/net/minestom/server/lock/Acquirable.java index 9d3d39747..0e1bb9ea7 100644 --- a/src/main/java/net/minestom/server/lock/Acquirable.java +++ b/src/main/java/net/minestom/server/lock/Acquirable.java @@ -38,17 +38,11 @@ public interface Acquirable { * and execute {@code consumer} as a callback with the acquired object. * * @param consumer the consumer of the acquired object - * @return true if the acquisition happened without synchronization, false otherwise */ - default boolean acquire(@NotNull Consumer consumer) { + default void acquire(@NotNull Consumer<@NotNull T> consumer) { final Thread currentThread = Thread.currentThread(); - - final Handler handler = getHandler(); - final BatchThread elementThread = handler.getBatchThread(); - + final BatchThread elementThread = getHandler().getBatchThread(); Acquisition.acquire(currentThread, elementThread, () -> consumer.accept(unwrap())); - - return true; } /** diff --git a/src/main/java/net/minestom/server/lock/Acquisition.java b/src/main/java/net/minestom/server/lock/Acquisition.java index a519c05d8..a0e133e9f 100644 --- a/src/main/java/net/minestom/server/lock/Acquisition.java +++ b/src/main/java/net/minestom/server/lock/Acquisition.java @@ -68,7 +68,8 @@ public final class Acquisition { if (elementThread == null || elementThread == currentThread) { callback.run(); } else { - final Monitor currentMonitor = currentThread instanceof BatchThread ? ((BatchThread) currentThread).monitor : null; + final BatchThread currentBatch = currentThread instanceof BatchThread ? ((BatchThread) currentThread) : null; + final Monitor currentMonitor = currentBatch != null ? currentBatch.monitor : null; boolean enter = false; if (currentMonitor != null && currentMonitor.isOccupiedByCurrentThread()) { @@ -79,30 +80,20 @@ public final class Acquisition { Monitor monitor = elementThread.monitor; - //System.out.println("acq " + System.currentTimeMillis() + " " + currentThread); - if (monitor.isOccupiedByCurrentThread()) { - //System.out.println("already"); - callback.run(); - process(elementThread); - } else if (GLOBAL_MONITOR.isOccupiedByCurrentThread()) { + if (monitor.isOccupiedByCurrentThread() || GLOBAL_MONITOR.isOccupiedByCurrentThread()) { + // We already have access to the thread callback.run(); } else if (monitor.tryEnter()) { - //System.out.println("enter"); + // Acquire the thread callback.run(); - - process(elementThread); - monitor.leave(); } else { - // Thread is not available, forward request - - final BatchThread currentBatch = (BatchThread) currentThread; + // Thread is not available, forward request to be executed later while (!GLOBAL_MONITOR.tryEnter()) processMonitored(currentBatch); - //System.out.println("yes " + elementThread + " " + elementThread.getMainRunnable().isInTick()); - var requests = getRequests(elementThread); + var requests = getRequests(elementThread); Acquirable.Request request = new Acquirable.Request(); request.localLatch = new CountDownLatch(1); request.processLatch = new CountDownLatch(1); @@ -110,13 +101,11 @@ public final class Acquisition { try { currentBatch.waitingOn = elementThread; - processMonitored(currentBatch); request.localLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); } currentBatch.waitingOn = null; - //System.out.println("end wait"); callback.run(); request.processLatch.countDown(); @@ -132,6 +121,7 @@ public final class Acquisition { public static void process(@NotNull BatchThread thread) { var requests = getRequests(thread); requests.forEach(request -> { + requests.remove(request); request.localLatch.countDown(); try { request.processLatch.await();