Remove unnecessary volatile, fix acquisition from threads other than the tick ones

This commit is contained in:
TheMode 2021-04-19 15:25:19 +02:00
parent 321d185ffe
commit d8e28ad73a
3 changed files with 17 additions and 11 deletions

View File

@ -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();

View File

@ -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);

View File

@ -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;