mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-28 14:37:31 +02:00
Remove threadChunkMap
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
1edab499f1
commit
860c6b21d1
@ -19,8 +19,6 @@ public final class ThreadDispatcher {
|
|||||||
private final ThreadProvider provider;
|
private final ThreadProvider provider;
|
||||||
private final List<TickThread> threads;
|
private final List<TickThread> threads;
|
||||||
|
|
||||||
// Contains all the chunks being ticked in a thread
|
|
||||||
private final Map<TickThread, Collection<ChunkEntry>> threadChunkMap = new HashMap<>();
|
|
||||||
// Chunk -> ChunkEntry mapping
|
// Chunk -> ChunkEntry mapping
|
||||||
private final Map<Chunk, ChunkEntry> chunkEntryMap = new HashMap<>();
|
private final Map<Chunk, ChunkEntry> chunkEntryMap = new HashMap<>();
|
||||||
// Queue to update chunks linked thread
|
// Queue to update chunks linked thread
|
||||||
@ -37,7 +35,6 @@ public final class ThreadDispatcher {
|
|||||||
private ThreadDispatcher(ThreadProvider provider, int threadCount) {
|
private ThreadDispatcher(ThreadProvider provider, int threadCount) {
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
this.threads = new ArrayList<>(threadCount);
|
this.threads = new ArrayList<>(threadCount);
|
||||||
|
|
||||||
for (int i = 0; i < threadCount; i++) {
|
for (int i = 0; i < threadCount; i++) {
|
||||||
final TickThread tickThread = new TickThread(phaser, i);
|
final TickThread tickThread = new TickThread(phaser, i);
|
||||||
this.threads.add(tickThread);
|
this.threads.add(tickThread);
|
||||||
@ -88,16 +85,8 @@ public final class ThreadDispatcher {
|
|||||||
* @param time the tick time in milliseconds
|
* @param time the tick time in milliseconds
|
||||||
*/
|
*/
|
||||||
public void updateAndAwait(long time) {
|
public void updateAndAwait(long time) {
|
||||||
for (var entry : threadChunkMap.entrySet()) {
|
for (TickThread thread : threads) {
|
||||||
final Collection<ChunkEntry> chunkEntries = entry.getValue();
|
thread.startTick(time);
|
||||||
if (chunkEntries == null || chunkEntries.isEmpty()) {
|
|
||||||
// Nothing to tick
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Execute tick
|
|
||||||
this.phaser.register();
|
|
||||||
TickThread thread = entry.getKey();
|
|
||||||
thread.startTick(chunkEntries, time);
|
|
||||||
}
|
}
|
||||||
this.phaser.arriveAndAwaitAdvance();
|
this.phaser.arriveAndAwaitAdvance();
|
||||||
}
|
}
|
||||||
@ -177,7 +166,7 @@ public final class ThreadDispatcher {
|
|||||||
while ((chunk = chunkLoadRequests.poll()) != null) {
|
while ((chunk = chunkLoadRequests.poll()) != null) {
|
||||||
final TickThread thread = retrieveThread(chunk);
|
final TickThread thread = retrieveThread(chunk);
|
||||||
final ChunkEntry chunkEntry = new ChunkEntry(thread, chunk);
|
final ChunkEntry chunkEntry = new ChunkEntry(thread, chunk);
|
||||||
this.threadChunkMap.computeIfAbsent(thread, t -> new ArrayList<>()).add(chunkEntry);
|
thread.entries().add(chunkEntry);
|
||||||
this.chunkEntryMap.put(chunk, chunkEntry);
|
this.chunkEntryMap.put(chunk, chunkEntry);
|
||||||
this.chunkUpdateQueue.add(chunk);
|
this.chunkUpdateQueue.add(chunk);
|
||||||
}
|
}
|
||||||
@ -188,9 +177,8 @@ public final class ThreadDispatcher {
|
|||||||
while ((chunk = chunkUnloadRequests.poll()) != null) {
|
while ((chunk = chunkUnloadRequests.poll()) != null) {
|
||||||
final ChunkEntry chunkEntry = chunkEntryMap.remove(chunk);
|
final ChunkEntry chunkEntry = chunkEntryMap.remove(chunk);
|
||||||
if (chunkEntry != null) {
|
if (chunkEntry != null) {
|
||||||
final TickThread thread = chunkEntry.thread;
|
TickThread thread = chunkEntry.thread;
|
||||||
Collection<ChunkEntry> chunks = threadChunkMap.get(thread);
|
thread.entries().remove(chunkEntry);
|
||||||
if (chunks != null) chunks.remove(chunkEntry);
|
|
||||||
}
|
}
|
||||||
this.chunkUpdateQueue.remove(chunk);
|
this.chunkUpdateQueue.remove(chunk);
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,8 @@ import net.minestom.server.instance.Chunk;
|
|||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Phaser;
|
import java.util.concurrent.Phaser;
|
||||||
import java.util.concurrent.locks.LockSupport;
|
import java.util.concurrent.locks.LockSupport;
|
||||||
@ -25,8 +25,7 @@ public final class TickThread extends Thread {
|
|||||||
private volatile boolean stop;
|
private volatile boolean stop;
|
||||||
|
|
||||||
private long tickTime;
|
private long tickTime;
|
||||||
private boolean isTicking;
|
private final List<ThreadDispatcher.ChunkEntry> entries = new ArrayList<>();
|
||||||
private Collection<ThreadDispatcher.ChunkEntry> entries;
|
|
||||||
|
|
||||||
public TickThread(Phaser phaser, int number) {
|
public TickThread(Phaser phaser, int number) {
|
||||||
super(MinecraftServer.THREAD_NAME_TICK + "-" + number);
|
super(MinecraftServer.THREAD_NAME_TICK + "-" + number);
|
||||||
@ -37,9 +36,7 @@ public final class TickThread extends Thread {
|
|||||||
public void run() {
|
public void run() {
|
||||||
LockSupport.park(this);
|
LockSupport.park(this);
|
||||||
while (!stop) {
|
while (!stop) {
|
||||||
this.isTicking = true;
|
|
||||||
tick();
|
tick();
|
||||||
this.isTicking = false;
|
|
||||||
this.phaser.arriveAndDeregister();
|
this.phaser.arriveAndDeregister();
|
||||||
LockSupport.park(this);
|
LockSupport.park(this);
|
||||||
}
|
}
|
||||||
@ -74,15 +71,17 @@ public final class TickThread extends Thread {
|
|||||||
// #acquire() callbacks
|
// #acquire() callbacks
|
||||||
}
|
}
|
||||||
|
|
||||||
void startTick(Collection<ThreadDispatcher.ChunkEntry> entries, long tickTime) {
|
void startTick(long tickTime) {
|
||||||
|
if (entries.isEmpty())
|
||||||
|
return; // Nothing to tick
|
||||||
|
this.phaser.register();
|
||||||
this.tickTime = tickTime;
|
this.tickTime = tickTime;
|
||||||
this.entries = entries;
|
|
||||||
this.stop = false;
|
this.stop = false;
|
||||||
LockSupport.unpark(this);
|
LockSupport.unpark(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<ThreadDispatcher.ChunkEntry> entries() {
|
public Collection<ThreadDispatcher.ChunkEntry> entries() {
|
||||||
return isTicking ? entries : Collections.emptyList();
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user