From 36c27cbe0544697299d69a748aecfb23a66424fd Mon Sep 17 00:00:00 2001 From: themode Date: Sun, 13 Feb 2022 15:52:01 +0100 Subject: [PATCH] Fix rare case where the phaser gets terminated on ARM cpus --- .../server/thread/ThreadDispatcher.java | 14 ++++++++----- .../minestom/server/thread/TickThread.java | 20 ++++++++++--------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/minestom/server/thread/ThreadDispatcher.java b/src/main/java/net/minestom/server/thread/ThreadDispatcher.java index eb5153cea..308d073c4 100644 --- a/src/main/java/net/minestom/server/thread/ThreadDispatcher.java +++ b/src/main/java/net/minestom/server/thread/ThreadDispatcher.java @@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Unmodifiable; import java.util.*; -import java.util.concurrent.Phaser; +import java.util.concurrent.CountDownLatch; /** * Used to link chunks into multiple groups. @@ -29,12 +29,11 @@ public final class ThreadDispatcher

{ // Requests consumed at the end of each tick private final MessagePassingQueue> updates = new MpscUnboundedArrayQueue<>(1024); - private final Phaser phaser = new Phaser(1); private ThreadDispatcher(ThreadProvider

provider, int threadCount) { this.provider = provider; TickThread[] threads = new TickThread[threadCount]; - Arrays.setAll(threads, i -> new TickThread(phaser, i)); + Arrays.setAll(threads, i -> new TickThread(i)); this.threads = List.of(threads); this.threads.forEach(Thread::start); } @@ -73,8 +72,13 @@ public final class ThreadDispatcher

{ } }); // Tick all partitions - for (TickThread thread : threads) thread.startTick(time); - this.phaser.arriveAndAwaitAdvance(); + CountDownLatch latch = new CountDownLatch(threads.size()); + for (TickThread thread : threads) thread.startTick(latch, time); + try { + latch.await(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } } /** diff --git a/src/main/java/net/minestom/server/thread/TickThread.java b/src/main/java/net/minestom/server/thread/TickThread.java index 770b026a8..4b60891ee 100644 --- a/src/main/java/net/minestom/server/thread/TickThread.java +++ b/src/main/java/net/minestom/server/thread/TickThread.java @@ -10,7 +10,7 @@ import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.concurrent.Phaser; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.ReentrantLock; @@ -22,15 +22,14 @@ import java.util.concurrent.locks.ReentrantLock; @ApiStatus.Internal public final class TickThread extends MinestomThread { private final ReentrantLock lock = new ReentrantLock(); - private final Phaser phaser; private volatile boolean stop; + private CountDownLatch latch; private long tickTime; private final List entries = new ArrayList<>(); - public TickThread(Phaser phaser, int number) { + public TickThread(int number) { super(MinecraftServer.THREAD_NAME_TICK + "-" + number); - this.phaser = phaser; } @Override @@ -45,7 +44,7 @@ public final class TickThread extends MinestomThread { } this.lock.unlock(); // #acquire() callbacks - this.phaser.arriveAndDeregister(); + this.latch.countDown(); LockSupport.park(this); } } @@ -69,10 +68,13 @@ public final class TickThread extends MinestomThread { } } - void startTick(long tickTime) { - if (entries.isEmpty()) - return; // Nothing to tick - this.phaser.register(); + void startTick(CountDownLatch latch, long tickTime) { + if (entries.isEmpty()) { + // Nothing to tick + latch.countDown(); + return; + } + this.latch = latch; this.tickTime = tickTime; this.stop = false; LockSupport.unpark(this);