From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: wangxyper Date: Mon, 16 Jan 2023 12:09:59 +0800 Subject: [PATCH] Hearse: Refactor some codes Original license: MIT Original project: https://github.com/Era4FunMC/Hearse diff --git a/src/main/java/co/earthme/hearse/concurrent/WorkerThreadFactory.java b/src/main/java/co/earthme/hearse/concurrent/WorkerThreadFactory.java index e65b1eba68003a9f7ce5080d07a521817831ff48..5ffca8dac85fcbe50a4445ebc375b33d8228d690 100644 --- a/src/main/java/co/earthme/hearse/concurrent/WorkerThreadFactory.java +++ b/src/main/java/co/earthme/hearse/concurrent/WorkerThreadFactory.java @@ -1,5 +1,7 @@ package co.earthme.hearse.concurrent; +import co.earthme.hearse.concurrent.thread.WorkerThread; + public interface WorkerThreadFactory { WorkerThread getNewThread(Runnable task); } diff --git a/src/main/java/co/earthme/hearse/concurrent/thread/Worker.java b/src/main/java/co/earthme/hearse/concurrent/thread/Worker.java new file mode 100644 index 0000000000000000000000000000000000000000..e7a944bd515af644bad37a23e012a5a1997e110d --- /dev/null +++ b/src/main/java/co/earthme/hearse/concurrent/thread/Worker.java @@ -0,0 +1,7 @@ +package co.earthme.hearse.concurrent.thread; + +public interface Worker { + static boolean isWorker(){ + return Thread.currentThread() instanceof Worker; + } +} diff --git a/src/main/java/co/earthme/hearse/concurrent/WorkerThread.java b/src/main/java/co/earthme/hearse/concurrent/thread/WorkerThread.java similarity index 50% rename from src/main/java/co/earthme/hearse/concurrent/WorkerThread.java rename to src/main/java/co/earthme/hearse/concurrent/thread/WorkerThread.java index 421d4926ac674b5eb12d9613ceb6d20185ea557d..f27bfd7ab3ce10e6c318de0c6376a80fa9014d2a 100644 --- a/src/main/java/co/earthme/hearse/concurrent/WorkerThread.java +++ b/src/main/java/co/earthme/hearse/concurrent/thread/WorkerThread.java @@ -1,8 +1,8 @@ -package co.earthme.hearse.concurrent; +package co.earthme.hearse.concurrent.thread; import io.papermc.paper.util.TickThread; -public class WorkerThread extends TickThread { +public class WorkerThread extends TickThread implements Worker{ public WorkerThread(String name) { super(name); @@ -11,8 +11,4 @@ public class WorkerThread extends TickThread { public WorkerThread(Runnable run, String name) { super(run, name); } - - public static boolean isWorker(){ - return Thread.currentThread() instanceof WorkerThread; - } } diff --git a/src/main/java/co/earthme/hearse/concurrent/threadfactory/DefaultWorkerFactory.java b/src/main/java/co/earthme/hearse/concurrent/threadfactory/DefaultWorkerFactory.java index 03a29509821a17faac2dc8ab810a2693b03bfbc6..b88b4f2d3df7303252a3c02824be3514c2618673 100644 --- a/src/main/java/co/earthme/hearse/concurrent/threadfactory/DefaultWorkerFactory.java +++ b/src/main/java/co/earthme/hearse/concurrent/threadfactory/DefaultWorkerFactory.java @@ -1,11 +1,10 @@ package co.earthme.hearse.concurrent.threadfactory; -import co.earthme.hearse.concurrent.WorkerThread; +import co.earthme.hearse.concurrent.thread.WorkerThread; import co.earthme.hearse.concurrent.WorkerThreadFactory; import it.unimi.dsi.fastutil.objects.ObjectArrayList; import it.unimi.dsi.fastutil.objects.ObjectLists; import net.minecraft.server.MinecraftServer; - import java.util.List; import java.util.concurrent.atomic.AtomicInteger; diff --git a/src/main/java/co/earthme/hearse/server/ServerLevelTickHook.java b/src/main/java/co/earthme/hearse/server/ServerLevelTickHook.java index 987c98ea108d49c1335238bc529f782d3ec5b5e6..13ab82cf7d5b17768d9a83e6f92511c6d5fa60f3 100644 --- a/src/main/java/co/earthme/hearse/server/ServerLevelTickHook.java +++ b/src/main/java/co/earthme/hearse/server/ServerLevelTickHook.java @@ -2,19 +2,15 @@ package co.earthme.hearse.server; import co.earthme.hearse.Hearse; import co.earthme.hearse.HearseConfig; -import co.earthme.hearse.concurrent.WorkerThread; import co.earthme.hearse.concurrent.WorkerThreadPoolExecutor; import co.earthme.hearse.concurrent.threadfactory.DefaultWorkerFactory; import co.earthme.hearse.util.ArrayListBlockingQueue; -import net.minecraft.CrashReport; -import net.minecraft.ReportedException; +import io.netty.handler.codec.serialization.ObjectEncoder; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerLevel; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.LockSupport; diff --git a/src/main/java/co/earthme/hearse/util/ArrayListBlockingQueue.java b/src/main/java/co/earthme/hearse/util/ArrayListBlockingQueue.java index b2cea65ecfb8a41248e7ee74357b4127106f1d0a..ec63191dca67c51f387ede9796a039210c8c3f99 100644 --- a/src/main/java/co/earthme/hearse/util/ArrayListBlockingQueue.java +++ b/src/main/java/co/earthme/hearse/util/ArrayListBlockingQueue.java @@ -1,5 +1,7 @@ package co.earthme.hearse.util; +import org.jetbrains.annotations.NotNull; + import java.util.*; import java.util.concurrent.*; import java.util.concurrent.locks.*; @@ -9,7 +11,7 @@ public class ArrayListBlockingQueue implements BlockingQueue { private final StampedLock editLock = new StampedLock(); @Override - public boolean add(T t) { + public boolean add(@NotNull T t) { final long id = this.editLock.writeLock(); try { return this.internalList.add(t); @@ -19,7 +21,7 @@ public class ArrayListBlockingQueue implements BlockingQueue { } @Override - public boolean offer(T t) { + public boolean offer(@NotNull T t) { final long id = this.editLock.writeLock(); try { return this.internalList.add(t); @@ -75,7 +77,7 @@ public class ArrayListBlockingQueue implements BlockingQueue { } @Override - public void put(T t) { + public void put(@NotNull T t) { final long id = this.editLock.writeLock(); try { this.internalList.add(t); @@ -85,7 +87,7 @@ public class ArrayListBlockingQueue implements BlockingQueue { } @Override - public boolean offer(T t, long timeout, TimeUnit unit) { + public boolean offer(T t, long timeout, @NotNull TimeUnit unit) { final long id = this.editLock.writeLock(); try { return this.internalList.add(t); @@ -99,23 +101,24 @@ public class ArrayListBlockingQueue implements BlockingQueue { T t; while ((t = this.poll()) == null){ synchronized (this){ - this.wait(1); + this.wait(0,1); } } return t; } @Override - public T poll(long timeout, TimeUnit unit) throws InterruptedException { + public T poll(long timeout, @NotNull TimeUnit unit) throws InterruptedException { T t; + long countTime = unit.toNanos(timeout); while ((t = this.poll()) == null){ - if (timeout == 0){ + if (countTime == 0){ break; } synchronized (this){ - unit.sleep(1); + this.wait(0,1); } - timeout--; + countTime--; } return t; } @@ -136,7 +139,7 @@ public class ArrayListBlockingQueue implements BlockingQueue { } @Override - public boolean containsAll(Collection c) { + public boolean containsAll(@NotNull Collection c) { final long id = this.editLock.writeLock(); try { return new HashSet<>(this.internalList).containsAll(c); @@ -146,7 +149,7 @@ public class ArrayListBlockingQueue implements BlockingQueue { } @Override - public boolean addAll(Collection c) { + public boolean addAll(@NotNull Collection c) { final long id = this.editLock.writeLock(); try { return this.internalList.addAll(c); @@ -156,7 +159,7 @@ public class ArrayListBlockingQueue implements BlockingQueue { } @Override - public boolean removeAll(Collection c) { + public boolean removeAll(@NotNull Collection c) { final long id = this.editLock.writeLock(); try { return this.internalList.removeAll(c); @@ -166,7 +169,7 @@ public class ArrayListBlockingQueue implements BlockingQueue { } @Override - public boolean retainAll(Collection c) { + public boolean retainAll(@NotNull Collection c) { final long id = this.editLock.writeLock(); try { return this.internalList.retainAll(c); @@ -231,7 +234,7 @@ public class ArrayListBlockingQueue implements BlockingQueue { } @Override - public T1[] toArray(T1[] a) { + public T1[] toArray(T1 @NotNull [] a) { long id = this.editLock.readLock(); try { return this.internalList.toArray(a); @@ -241,12 +244,12 @@ public class ArrayListBlockingQueue implements BlockingQueue { } @Override - public int drainTo(Collection c) { + public int drainTo(@NotNull Collection c) { throw new UnsupportedOperationException("drainTo"); } @Override - public int drainTo(Collection c, int maxElements) { + public int drainTo(@NotNull Collection c, int maxElements) { throw new UnsupportedOperationException("drainTo"); } } diff --git a/src/main/java/co/earthme/hearse/workers/WorkerThreadPoolManager.java b/src/main/java/co/earthme/hearse/workers/WorkerThreadPoolManager.java index 527dba288e1988773fd5a89f076f92084034f421..8cb0d00fb3cd4282873c8b8db88c87e59f8ef9de 100644 --- a/src/main/java/co/earthme/hearse/workers/WorkerThreadPoolManager.java +++ b/src/main/java/co/earthme/hearse/workers/WorkerThreadPoolManager.java @@ -4,6 +4,7 @@ import co.earthme.hearse.concurrent.WorkerThreadPoolExecutor; import com.google.common.collect.Maps; import java.util.List; import java.util.Map; +import java.util.concurrent.ForkJoinPool; import java.util.concurrent.TimeUnit; public class WorkerThreadPoolManager { @@ -18,7 +19,7 @@ public class WorkerThreadPoolManager { if (!worker.isShutdown()){ worker.getQueue().clear(); //Clear the tasks.We don't need wait them worker.shutdown(); - while (worker.awaitTermination(100, TimeUnit.MILLISECONDS)) {} + while (worker.awaitTermination(100, TimeUnit.MILLISECONDS)); {} } } } diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java index 39c3aaf91514bd8a2f9f04496e25a6253442939f..20a4fbbc25b670ba89b428dd4b14725469c9bc96 100644 --- a/src/main/java/net/minecraft/server/level/ServerLevel.java +++ b/src/main/java/net/minecraft/server/level/ServerLevel.java @@ -1,7 +1,7 @@ package net.minecraft.server.level; import co.aikar.timings.TimingHistory; -import co.earthme.hearse.concurrent.WorkerThread; +import co.earthme.hearse.concurrent.thread.Worker; import co.earthme.hearse.server.ServerEntityTickHook; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Lists; @@ -223,7 +223,7 @@ public class ServerLevel extends Level implements WorldGenLevel { public final void loadChunksForMoveAsync(AABB axisalignedbb, ca.spottedleaf.concurrentutil.executor.standard.PrioritisedExecutor.Priority priority, java.util.function.Consumer> onLoad) { - if (Thread.currentThread() != this.thread && !WorkerThread.isWorker()) { + if (Thread.currentThread() != this.thread && !Worker.isWorker()) { this.getChunkSource().mainThreadProcessor.execute(() -> { this.loadChunksForMoveAsync(axisalignedbb, priority, onLoad); }); diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java index 4a75c2895f4f5e3229be3c7b177445611a1e70b6..2f06b3782c4a21ed244b66dc146ebc71de8dac34 100644 --- a/src/main/java/net/minecraft/world/level/Level.java +++ b/src/main/java/net/minecraft/world/level/Level.java @@ -1,11 +1,8 @@ package net.minecraft.world.level; -import co.aikar.timings.Timing; -import co.aikar.timings.Timings; -import co.earthme.hearse.concurrent.WorkerThread; +import co.earthme.hearse.concurrent.thread.Worker; import com.destroystokyo.paper.event.server.ServerExceptionEvent; import com.destroystokyo.paper.exception.ServerInternalException; -import com.google.common.base.MoreObjects; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.mojang.serialization.Codec; @@ -19,7 +16,6 @@ import java.util.function.Supplier; import javax.annotation.Nullable; import net.minecraft.CrashReport; import net.minecraft.CrashReportCategory; -import net.minecraft.ReportedException; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.Holder; @@ -37,15 +33,12 @@ import net.minecraft.server.level.ChunkHolder; import net.minecraft.server.level.ServerLevel; import net.minecraft.sounds.SoundEvent; import net.minecraft.sounds.SoundSource; -import net.minecraft.util.AbortableIterationConsumer; import net.minecraft.util.Mth; import net.minecraft.util.RandomSource; import net.minecraft.util.profiling.ProfilerFiller; import net.minecraft.world.DifficultyInstance; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.Entity; -import net.minecraft.world.entity.boss.EnderDragonPart; -import net.minecraft.world.entity.boss.enderdragon.EnderDragon; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; @@ -83,7 +76,6 @@ import net.minecraft.world.phys.Vec3; import net.minecraft.world.scores.Scoreboard; // CraftBukkit start -import java.util.HashMap; import java.util.Map; import net.minecraft.network.protocol.game.ClientboundSetBorderCenterPacket; import net.minecraft.network.protocol.game.ClientboundSetBorderLerpSizePacket; @@ -91,17 +83,14 @@ import net.minecraft.network.protocol.game.ClientboundSetBorderSizePacket; import net.minecraft.network.protocol.game.ClientboundSetBorderWarningDelayPacket; import net.minecraft.network.protocol.game.ClientboundSetBorderWarningDistancePacket; import org.bukkit.Bukkit; -import org.bukkit.Location; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.block.CapturedBlockState; import org.bukkit.craftbukkit.block.CraftBlockState; import org.bukkit.craftbukkit.block.data.CraftBlockData; import org.bukkit.craftbukkit.util.CraftSpawnCategory; -import org.bukkit.craftbukkit.util.CraftNamespacedKey; import org.bukkit.entity.SpawnCategory; import org.bukkit.event.block.BlockPhysicsEvent; -import org.bukkit.event.world.GenericGameEvent; // CraftBukkit end public abstract class Level implements LevelAccessor, AutoCloseable { @@ -1118,7 +1107,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable { } // Paper end // CraftBukkit end - return this.isOutsideBuildHeight(blockposition) ? null : (!this.isClientSide && !io.papermc.paper.util.TickThread.isTickThread() && !WorkerThread.isWorker() ? null : this.getChunkAt(blockposition).getBlockEntity(blockposition, LevelChunk.EntityCreationType.IMMEDIATE)); // Paper - rewrite chunk system + return this.isOutsideBuildHeight(blockposition) ? null : (!this.isClientSide && !io.papermc.paper.util.TickThread.isTickThread() && !Worker.isWorker() ? null : this.getChunkAt(blockposition).getBlockEntity(blockposition, LevelChunk.EntityCreationType.IMMEDIATE)); // Paper - rewrite chunk system } public void setBlockEntity(BlockEntity blockEntity) {