Remove MinestomThreadPool, use the common pool for generation

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-01-12 22:37:35 +01:00
parent 0366027c5d
commit 2ef1902664
7 changed files with 5 additions and 166 deletions

View File

@ -58,10 +58,6 @@ public final class MinecraftServer {
public static final String THREAD_NAME_TICK_SCHEDULER = "Ms-TickScheduler";
public static final String THREAD_NAME_TICK = "Ms-Tick";
public static final String THREAD_NAME_BLOCK_BATCH = "Ms-BlockBatchPool";
public static final int THREAD_COUNT_BLOCK_BATCH = getThreadCount("minestom.block-thread-count",
Runtime.getRuntime().availableProcessors() / 2);
// Config
// Can be modified at performance cost when increased
public static final int TICK_PER_SECOND = Integer.getInteger("minestom.tps", 20);

View File

@ -27,7 +27,6 @@ import net.minestom.server.scoreboard.TeamManager;
import net.minestom.server.storage.StorageLocation;
import net.minestom.server.storage.StorageManager;
import net.minestom.server.terminal.MinestomTerminal;
import net.minestom.server.thread.MinestomThreadPool;
import net.minestom.server.thread.ThreadDispatcher;
import net.minestom.server.timer.SchedulerManager;
import net.minestom.server.utils.PacketUtils;
@ -264,7 +263,6 @@ final class ServerProcessImpl implements ServerProcess {
LOGGER.info("Shutting down all thread pools.");
benchmark.disable();
MinestomTerminal.stop();
MinestomThreadPool.shutdownAll();
dispatcher.shutdown();
LOGGER.info("Minestom server stopped successfully.");
}

View File

@ -1,13 +1,12 @@
package net.minestom.server.instance.batch;
import net.minestom.server.MinecraftServer;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.thread.MinestomThreadPool;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
/**
* A Batch is a tool used to cache a list of block changes, and apply the changes whenever you want.
@ -29,9 +28,7 @@ import java.util.concurrent.ExecutorService;
*/
public interface Batch<C> extends Block.Setter {
ExecutorService BLOCK_BATCH_POOL = new MinestomThreadPool(
MinecraftServer.THREAD_COUNT_BLOCK_BATCH,
MinecraftServer.THREAD_NAME_BLOCK_BATCH);
ExecutorService BLOCK_BATCH_POOL = ForkJoinPool.commonPool();
/**
* Gets if the batch is ready to be applied to an instance.

View File

@ -1,10 +1,8 @@
package net.minestom.server.map;
import net.minestom.server.MinecraftServer;
import net.minestom.server.thread.MinestomThreadPool;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
public enum MapColors {
@ -187,16 +185,8 @@ public enum MapColors {
private static void fillRGBArray() {
rgbArray = new PreciseMapColor[0xFFFFFF + 1];
MinestomThreadPool threads = new MinestomThreadPool(Runtime.getRuntime().availableProcessors(), "RGBMapping", true);
for (int rgb = 0; rgb <= 0xFFFFFF; rgb++) {
int finalRgb = rgb;
threads.execute(() -> rgbArray[finalRgb] = mapColor(finalRgb));
}
try {
threads.shutdown();
threads.awaitTermination(100, TimeUnit.MINUTES);
} catch (Throwable t) {
MinecraftServer.getExceptionManager().handleException(t);
rgbArray[rgb] = mapColor(rgb);
}
}

View File

@ -20,7 +20,8 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static net.minestom.server.MinecraftServer.*;
import static net.minestom.server.MinecraftServer.THREAD_NAME_TICK;
import static net.minestom.server.MinecraftServer.THREAD_NAME_TICK_SCHEDULER;
/**
* Small monitoring tools that can be used to check the current memory usage and Minestom threads CPU usage.
@ -35,7 +36,6 @@ public final class BenchmarkManager {
private static final List<String> THREADS = new ArrayList<>();
static {
THREADS.add(THREAD_NAME_BLOCK_BATCH);
THREADS.add(THREAD_NAME_TICK_SCHEDULER);
THREADS.add(THREAD_NAME_TICK);
}

View File

@ -1,49 +0,0 @@
package net.minestom.server.thread;
import org.jetbrains.annotations.ApiStatus;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@ApiStatus.Internal
public class MinestomThreadPool extends ThreadPoolExecutor {
private static final Set<MinestomThreadPool> executors = new CopyOnWriteArraySet<>();
/**
* Creates a non-local thread pool executor
*
* @param nThreads the number of threads
* @param name the name of the thread pool
*/
public MinestomThreadPool(int nThreads, String name) {
this(nThreads, name, false);
}
/**
* @param nThreads the number of threads
* @param name the name of the thread pool
* @param local set to true if this executor is only used inside a method and should *not* be kept in the internal list of executors
*/
public MinestomThreadPool(int nThreads, String name, boolean local) {
super(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), r -> {
Thread thread = new MinestomThread(r);
thread.setDaemon(true);
thread.setName(thread.getName().replace("Thread", name));
return thread;
});
if (!local) {
MinestomThreadPool.executors.add(this);
}
}
/**
* Shutdown all the thread pools
*/
public static void shutdownAll() {
executors.forEach(MinestomThreadPool::shutdownNow);
}
}

View File

@ -1,93 +0,0 @@
package net.minestom.server.utils.thread;
import net.minestom.server.thread.MinestomThreadPool;
import org.jetbrains.annotations.NotNull;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Executor service which will always give the same thread to a given Runnable.
* Uses <pre>Runnable#hashCode()</pre> to determine the thread to assign.
*/
public class ThreadBindingExecutor extends AbstractExecutorService {
private MinestomThreadPool[] threadExecutors;
/**
* Creates a non-local thread-binding executor
*
* @param nThreads the number of threads
* @param name the name of the thread pool
*/
public ThreadBindingExecutor(int nThreads, String name) {
this(nThreads, name, false);
}
/**
* @param nThreads the number of threads
* @param name the name of the thread pool
* @param local set to true if this executor is only used inside a method and should *not* be kept in the internal list of executors
*/
public ThreadBindingExecutor(int nThreads, String name, boolean local) {
threadExecutors = new MinestomThreadPool[nThreads];
for (int i = 0; i < nThreads; i++) {
threadExecutors[i] = new MinestomThreadPool(1, name, local);
}
}
@Override
public void shutdown() {
for (MinestomThreadPool t : threadExecutors) {
t.shutdown();
}
}
@NotNull
@Override
public List<Runnable> shutdownNow() {
List<Runnable> allTasks = new LinkedList<>();
for (MinestomThreadPool t : threadExecutors) {
allTasks.addAll(t.shutdownNow());
}
return allTasks;
}
@Override
public boolean isShutdown() {
for (MinestomThreadPool t : threadExecutors) {
if(!t.isShutdown())
return false;
}
return true;
}
@Override
public boolean isTerminated() {
for (MinestomThreadPool t : threadExecutors) {
if(!t.isShutdown())
return false;
}
return true;
}
@Override
public boolean awaitTermination(long timeout, @NotNull TimeUnit unit) throws InterruptedException {
boolean terminated = true;
for (MinestomThreadPool t : threadExecutors) {
terminated &= t.awaitTermination(timeout, unit);
}
return terminated;
}
@Override
public void execute(@NotNull Runnable command) {
int hash = command.hashCode();
if(hash < 0) hash = -hash;
int bucket = hash % threadExecutors.length;
threadExecutors[bucket].execute(command);
}
}