From 321d185ffe59fdef96d8f8eca5b743a454884c16 Mon Sep 17 00:00:00 2001 From: TheMode Date: Mon, 19 Apr 2021 11:57:42 +0200 Subject: [PATCH] use a ReentrantLock instead of guava monitor --- .../net/minestom/server/lock/Acquisition.java | 24 +++++++++---------- .../minestom/server/thread/BatchThread.java | 4 ++-- .../server/thread/ThreadProvider.java | 14 +++++------ 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/minestom/server/lock/Acquisition.java b/src/main/java/net/minestom/server/lock/Acquisition.java index 31c6471f0..9891abc78 100644 --- a/src/main/java/net/minestom/server/lock/Acquisition.java +++ b/src/main/java/net/minestom/server/lock/Acquisition.java @@ -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(); } } diff --git a/src/main/java/net/minestom/server/thread/BatchThread.java b/src/main/java/net/minestom/server/thread/BatchThread.java index 568d22c7a..533ffd6f7 100644 --- a/src/main/java/net/minestom/server/thread/BatchThread.java +++ b/src/main/java/net/minestom/server/thread/BatchThread.java @@ -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); diff --git a/src/main/java/net/minestom/server/thread/ThreadProvider.java b/src/main/java/net/minestom/server/thread/ThreadProvider.java index 77c51abbd..359bb2a0f 100644 --- a/src/main/java/net/minestom/server/thread/ThreadProvider.java +++ b/src/main/java/net/minestom/server/thread/ThreadProvider.java @@ -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;