Improve performance

This commit is contained in:
TheMode 2021-04-17 03:00:34 +02:00
parent 3b7353300d
commit a5df2376cf
3 changed files with 11 additions and 27 deletions

View File

@ -325,7 +325,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
packet.process(this);
}
if (username.equals("TheMode911"))
//if (username.equals("TheMode911"))
for (Player p1 : MinecraftServer.getConnectionManager().getOnlinePlayers()) {
p1.getAcquiredElement().acquire(o -> {
//for (Player p2 : MinecraftServer.getConnectionManager().getOnlinePlayers())

View File

@ -38,17 +38,11 @@ public interface Acquirable<T> {
* and execute {@code consumer} as a callback with the acquired object.
*
* @param consumer the consumer of the acquired object
* @return true if the acquisition happened without synchronization, false otherwise
*/
default boolean acquire(@NotNull Consumer<T> consumer) {
default void acquire(@NotNull Consumer<@NotNull T> consumer) {
final Thread currentThread = Thread.currentThread();
final Handler handler = getHandler();
final BatchThread elementThread = handler.getBatchThread();
final BatchThread elementThread = getHandler().getBatchThread();
Acquisition.acquire(currentThread, elementThread, () -> consumer.accept(unwrap()));
return true;
}
/**

View File

@ -68,7 +68,8 @@ public final class Acquisition {
if (elementThread == null || elementThread == currentThread) {
callback.run();
} else {
final Monitor currentMonitor = currentThread instanceof BatchThread ? ((BatchThread) currentThread).monitor : null;
final BatchThread currentBatch = currentThread instanceof BatchThread ? ((BatchThread) currentThread) : null;
final Monitor currentMonitor = currentBatch != null ? currentBatch.monitor : null;
boolean enter = false;
if (currentMonitor != null && currentMonitor.isOccupiedByCurrentThread()) {
@ -79,30 +80,20 @@ public final class Acquisition {
Monitor monitor = elementThread.monitor;
//System.out.println("acq " + System.currentTimeMillis() + " " + currentThread);
if (monitor.isOccupiedByCurrentThread()) {
//System.out.println("already");
callback.run();
process(elementThread);
} else if (GLOBAL_MONITOR.isOccupiedByCurrentThread()) {
if (monitor.isOccupiedByCurrentThread() || GLOBAL_MONITOR.isOccupiedByCurrentThread()) {
// We already have access to the thread
callback.run();
} else if (monitor.tryEnter()) {
//System.out.println("enter");
// Acquire the thread
callback.run();
process(elementThread);
monitor.leave();
} else {
// Thread is not available, forward request
final BatchThread currentBatch = (BatchThread) currentThread;
// Thread is not available, forward request to be executed later
while (!GLOBAL_MONITOR.tryEnter())
processMonitored(currentBatch);
//System.out.println("yes " + elementThread + " " + elementThread.getMainRunnable().isInTick());
var requests = getRequests(elementThread);
var requests = getRequests(elementThread);
Acquirable.Request request = new Acquirable.Request();
request.localLatch = new CountDownLatch(1);
request.processLatch = new CountDownLatch(1);
@ -110,13 +101,11 @@ public final class Acquisition {
try {
currentBatch.waitingOn = elementThread;
processMonitored(currentBatch);
request.localLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
currentBatch.waitingOn = null;
//System.out.println("end wait");
callback.run();
request.processLatch.countDown();
@ -132,6 +121,7 @@ public final class Acquisition {
public static void process(@NotNull BatchThread thread) {
var requests = getRequests(thread);
requests.forEach(request -> {
requests.remove(request);
request.localLatch.countDown();
try {
request.processLatch.await();