use a ReentrantLock instead of guava monitor

This commit is contained in:
TheMode 2021-04-19 11:57:42 +02:00
parent 562927ee44
commit 321d185ffe
3 changed files with 21 additions and 21 deletions

View File

@ -1,6 +1,5 @@
package net.minestom.server.lock;
import com.google.common.util.concurrent.Monitor;
import net.minestom.server.MinecraftServer;
import net.minestom.server.thread.BatchThread;
import org.jetbrains.annotations.NotNull;
@ -8,6 +7,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
public final class Acquisition {
@ -17,7 +17,7 @@ public final class Acquisition {
/**
* Global lock used for synchronization.
*/
private static final Monitor GLOBAL_MONITOR = new Monitor();
private static final ReentrantLock GLOBAL_LOCK = new ReentrantLock();
private static final AtomicLong WAIT_COUNTER_NANO = new AtomicLong();
@ -91,20 +91,20 @@ public final class Acquisition {
}
BatchThread current = (BatchThread) currentThread;
Monitor currentMonitor = current.monitor;
final boolean currentAcquired = currentMonitor.isOccupiedByCurrentThread();
ReentrantLock currentLock = current.lock;
final boolean currentAcquired = currentLock.isHeldByCurrentThread();
if (currentAcquired)
current.monitor.leave();
currentLock.unlock();
GLOBAL_MONITOR.enter();
GLOBAL_LOCK.lock();
if (currentAcquired)
current.monitor.enter();
currentLock.lock();
final var monitor = elementThread != null ? elementThread.monitor : null;
final boolean acquired = monitor == null || monitor.isOccupiedByCurrentThread();
final var lock = elementThread != null ? elementThread.lock : null;
final boolean acquired = lock == null || lock.isHeldByCurrentThread();
if (!acquired) {
monitor.enter();
lock.lock();
}
// Monitoring
@ -116,9 +116,9 @@ public final class Acquisition {
callback.run();
if (!acquired) {
monitor.leave();
lock.unlock();
}
GLOBAL_MONITOR.leave();
GLOBAL_LOCK.unlock();
}
}

View File

@ -1,6 +1,5 @@
package net.minestom.server.thread;
import com.google.common.util.concurrent.Monitor;
import net.minestom.server.MinecraftServer;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
@ -10,12 +9,13 @@ import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;
public class BatchThread extends Thread {
private final BatchRunnable runnable;
public volatile Monitor monitor = new Monitor();
public volatile ReentrantLock lock = new ReentrantLock();
public BatchThread(@NotNull BatchRunnable runnable, int number) {
super(runnable, MinecraftServer.THREAD_NAME_TICK + "-" + number);

View File

@ -1,6 +1,5 @@
package net.minestom.server.thread;
import com.google.common.util.concurrent.Monitor;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Entity;
import net.minestom.server.instance.Chunk;
@ -15,6 +14,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -146,24 +146,24 @@ public abstract class ThreadProvider {
.collect(Collectors.toList());
Acquirable.refreshEntities(Collections.unmodifiableList(entities));
final Monitor monitor = thread.monitor;
monitor.enter();
final ReentrantLock lock = thread.lock;
lock.lock();
chunkEntries.forEach(chunkEntry -> {
Chunk chunk = chunkEntry.chunk;
if (!ChunkUtils.isLoaded(chunk))
return;
chunk.tick(time);
chunkEntry.entities.forEach(entity -> {
final boolean hasQueue = monitor.hasQueuedThreads();
final boolean hasQueue = lock.hasQueuedThreads();
if (hasQueue) {
monitor.leave();
lock.unlock();
// #acquire callbacks should be called here
monitor.enter();
lock.lock();
}
entity.tick(time);
});
});
monitor.leave();
lock.unlock();
});
}
return countDownLatch;