Fix rare case where the phaser gets terminated on ARM cpus

This commit is contained in:
themode 2022-02-13 15:52:01 +01:00
parent 1fe93ce653
commit 36c27cbe05
2 changed files with 20 additions and 14 deletions

View File

@ -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<P> {
// Requests consumed at the end of each tick
private final MessagePassingQueue<DispatchUpdate<P>> updates = new MpscUnboundedArrayQueue<>(1024);
private final Phaser phaser = new Phaser(1);
private ThreadDispatcher(ThreadProvider<P> 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<P> {
}
});
// 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);
}
}
/**

View File

@ -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<ThreadDispatcher.Partition> 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);