mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 00:17:58 +01:00
use a ReentrantLock instead of guava monitor
This commit is contained in:
parent
562927ee44
commit
321d185ffe
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user