Added more options inside ThreadProvider

This commit is contained in:
TheMode 2021-04-22 19:33:07 +02:00
parent ef6f2ac5c9
commit 370f4c2f57
3 changed files with 69 additions and 6 deletions

View File

@ -9,6 +9,7 @@ import net.minestom.server.monitoring.TickMonitor;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.player.NettyPlayerConnection;
import net.minestom.server.thread.PerInstanceThreadProvider;
import net.minestom.server.thread.SingleThreadProvider;
import net.minestom.server.thread.ThreadProvider;
import net.minestom.server.utils.async.AsyncUtils;
import org.jetbrains.annotations.NotNull;
@ -41,6 +42,7 @@ public final class UpdateManager {
// DEFAULT THREAD PROVIDER
threadProvider = new PerInstanceThreadProvider();
//threadProvider = new PerChunkThreadProvider();
threadProvider = new SingleThreadProvider();
}
/**

View File

@ -16,4 +16,9 @@ public class SingleThreadProvider extends ThreadProvider {
public long findThread(@NotNull Chunk chunk) {
return 0;
}
@Override
public @NotNull RefreshType getChunkRefreshType() {
return RefreshType.NEVER;
}
}

View File

@ -35,9 +35,6 @@ public abstract class ThreadProvider {
// Represents the maximum percentage of tick time
// that can be spent refreshing chunks thread
protected double refreshPercentage = 0.3f;
// Minimum refresh time
private int min = 3;
private int max = (int) (MinecraftServer.TICK_MS * 0.3);
public ThreadProvider(int threadCount) {
this.threads = new ArrayList<>(threadCount);
@ -78,6 +75,42 @@ public abstract class ThreadProvider {
*/
public abstract long findThread(@NotNull Chunk chunk);
/**
* Defines how often chunks thread should be updated.
*
* @return the refresh type
*/
public @NotNull RefreshType getChunkRefreshType() {
return RefreshType.CONSTANT;
}
/**
* Defines how often entities thread should be updated.
*
* @return the refresh type
*/
public @NotNull RefreshType getEntityRefreshType() {
return RefreshType.CONSTANT;
}
/**
* Minimum time used to refresh chunks & entities thread.
*
* @return the minimum refresh time in milliseconds
*/
public int getMinimumRefreshTime() {
return 3;
}
/**
* Maximum time used to refresh chunks & entities thread.
*
* @return the maximum refresh time in milliseconds
*/
public int getMaximumRefreshTime() {
return (int) (MinecraftServer.TICK_MS * 0.3);
}
protected void addChunk(Chunk chunk) {
ChunkEntry chunkEntry = setChunkThread(chunk, (thread) -> new ChunkEntry(thread, chunk));
this.chunkEntryMap.put(chunk, chunkEntry);
@ -187,8 +220,13 @@ public abstract class ThreadProvider {
this.removedEntities.clear();
}
final boolean chunkRefresh = getChunkRefreshType() != RefreshType.NEVER;
final boolean entityRefresh = getEntityRefreshType() != RefreshType.NEVER;
if (!chunkRefresh && !entityRefresh)
return;
final int timeOffset = MathUtils.clamp((int) ((double) tickTime * refreshPercentage), min, max);
final int timeOffset = MathUtils.clamp((int) ((double) tickTime * refreshPercentage),
getMinimumRefreshTime(), getMaximumRefreshTime());
final long endTime = System.currentTimeMillis() + timeOffset;
final int size = chunks.size();
int counter = 0;
@ -200,12 +238,12 @@ public abstract class ThreadProvider {
}
// Update chunk threads
{
if (chunkRefresh) {
switchChunk(chunk);
}
// Update entities
{
if (entityRefresh) {
Instance instance = chunk.getInstance();
refreshEntitiesThread(instance, chunk);
if (instance instanceof InstanceContainer) {
@ -269,6 +307,24 @@ public abstract class ThreadProvider {
}
}
/**
* Defines how often chunks thread should be refreshed.
*/
public enum RefreshType {
/**
* Chunk thread is constant after being defined.
*/
NEVER,
/**
* Chunk thread should be recomputed as often as possible.
*/
CONSTANT,
/**
* Chunk thread should be recomputed, but not continuously.
*/
RARELY
}
public static class ChunkEntry {
private volatile BatchThread thread;
private final Chunk chunk;