mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-07 17:08:30 +01:00
Dynamic chunk thread change
This commit is contained in:
parent
e9f13d0a82
commit
2df5484e1a
@ -12,6 +12,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -64,15 +65,35 @@ public abstract class ThreadProvider {
|
|||||||
public abstract long findThread(@NotNull Chunk chunk);
|
public abstract long findThread(@NotNull Chunk chunk);
|
||||||
|
|
||||||
protected void addChunk(Chunk chunk) {
|
protected void addChunk(Chunk chunk) {
|
||||||
|
ChunkEntry chunkEntry = setChunkThread(chunk, (thread) -> new ChunkEntry(thread, chunk));
|
||||||
|
this.chunkEntryMap.put(chunk, chunkEntry);
|
||||||
|
this.chunks.add(chunk);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void switchChunk(@NotNull Chunk chunk) {
|
||||||
|
ChunkEntry chunkEntry = chunkEntryMap.get(chunk);
|
||||||
|
if (chunkEntry == null)
|
||||||
|
return;
|
||||||
|
var chunks = threadChunkMap.get(chunkEntry.thread);
|
||||||
|
if (chunks == null || chunks.isEmpty())
|
||||||
|
return;
|
||||||
|
chunks.remove(chunkEntry);
|
||||||
|
|
||||||
|
setChunkThread(chunk, batchThread -> {
|
||||||
|
chunkEntry.thread = batchThread;
|
||||||
|
return chunkEntry;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected @NotNull ChunkEntry setChunkThread(@NotNull Chunk chunk,
|
||||||
|
@NotNull Function<@NotNull BatchThread, @NotNull ChunkEntry> chunkEntrySupplier) {
|
||||||
final int threadId = (int) (Math.abs(findThread(chunk)) % threads.size());
|
final int threadId = (int) (Math.abs(findThread(chunk)) % threads.size());
|
||||||
BatchThread thread = threads.get(threadId);
|
BatchThread thread = threads.get(threadId);
|
||||||
var chunks = threadChunkMap.computeIfAbsent(thread, batchThread -> ConcurrentHashMap.newKeySet());
|
var chunks = threadChunkMap.computeIfAbsent(thread, batchThread -> ConcurrentHashMap.newKeySet());
|
||||||
|
|
||||||
ChunkEntry chunkEntry = new ChunkEntry(thread, chunk);
|
ChunkEntry chunkEntry = chunkEntrySupplier.apply(thread);
|
||||||
chunks.add(chunkEntry);
|
chunks.add(chunkEntry);
|
||||||
|
return chunkEntry;
|
||||||
this.chunkEntryMap.put(chunk, chunkEntry);
|
|
||||||
this.chunks.add(chunk);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void removeChunk(Chunk chunk) {
|
protected void removeChunk(Chunk chunk) {
|
||||||
@ -153,7 +174,7 @@ public abstract class ThreadProvider {
|
|||||||
|
|
||||||
// Update chunk threads
|
// Update chunk threads
|
||||||
{
|
{
|
||||||
// TODO
|
switchChunk(chunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update entities
|
// Update entities
|
||||||
@ -215,7 +236,7 @@ public abstract class ThreadProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static class ChunkEntry {
|
public static class ChunkEntry {
|
||||||
private final BatchThread thread;
|
private volatile BatchThread thread;
|
||||||
private final Chunk chunk;
|
private final Chunk chunk;
|
||||||
private final List<Entity> entities = new ArrayList<>();
|
private final List<Entity> entities = new ArrayList<>();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user