ChunkTaskDispatcher .java

net.minecraft.server.level.ChunkTaskDispatcher (class): members: DISPATCHER_PRIORITY_COUNT, scheduleForExecution, onLevelChange, popTasks, sleeping, queue.

net.minecraft.server.levelclass107 linesserver3.5 KB

Implements

API Surface (8)

boolean hasWork()
void onLevelChange(final ChunkPos pos, final IntSupplier oldLevel, final int newLevel, final IntConsumer setQueueLevel)
void release(final long pos, final Runnable whenReleased, final boolean clearQueue)
void submit(final Runnable task, final long pos, final IntSupplier level)
void pollTask()
void scheduleForExecution(final ChunkTaskPriorityQueue.TasksForChunk tasksForChunk)
void onRelease(final long key)
void close()

Fields & Constants (6)

DISPATCHER_PRIORITY_COUNT
LOGGER
queue
executor
dispatcher
sleeping

String literals (2)

"_queue"
"dispatcher"

Full Source

ChunkTaskDispatcher.java107 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.level;
2
3
import com.mojang.logging.LogUtils;
4
import java.util.concurrent.CompletableFuture;
5
import java.util.concurrent.Executor;
6
import java.util.function.IntConsumer;
7
import java.util.function.IntSupplier;
8
import net.minecraft.SharedConstants;
9
import net.minecraft.util.Unit;
10
import net.minecraft.util.thread.PriorityConsecutiveExecutor;
11
import net.minecraft.util.thread.StrictQueue;
12
import net.minecraft.util.thread.TaskScheduler;
13
import net.minecraft.world.level.ChunkPos;
14
import org.jspecify.annotations.Nullable;
15
import org.slf4j.Logger;
16
17
public class ChunkTaskDispatcher implements ChunkHolder.LevelChangeListener, AutoCloseable {
18
	public static final int DISPATCHER_PRIORITY_COUNT = 4;
19
	private static final Logger LOGGER = LogUtils.getLogger();
20
	private final ChunkTaskPriorityQueue queue;
21
	private final TaskScheduler<Runnable> executor;
22
	private final PriorityConsecutiveExecutor dispatcher;
23
	protected boolean sleeping;
24
25
	public ChunkTaskDispatcher(final TaskScheduler<Runnable> executor, final Executor dispatcherExecutor) {
26
		this.queue = new ChunkTaskPriorityQueue(executor.name() + "_queue");
27
		this.executor = executor;
28
		this.dispatcher = new PriorityConsecutiveExecutor(4, dispatcherExecutor, "dispatcher");
29
		this.sleeping = true;
30
	}
31
32
	public boolean hasWork() {
33
		return this.dispatcher.hasWork() || this.queue.hasWork();
34
	}
35
36
	@Override
37
	public void onLevelChange(final ChunkPos pos, final IntSupplier oldLevel, final int newLevel, final IntConsumer setQueueLevel) {
38
		this.dispatcher.schedule(new StrictQueue.RunnableWithPriority(0, () -> {
39
			int oldTicketLevel = oldLevel.getAsInt();
40
			if (SharedConstants.DEBUG_VERBOSE_SERVER_EVENTS) {
41
				LOGGER.debug("RES {} {} -> {}", pos, oldTicketLevel, newLevel);
42
			}
43
44
			this.queue.resortChunkTasks(oldTicketLevel, pos, newLevel);
45
			setQueueLevel.accept(newLevel);
46
		}));
47
	}
48
49
	public void release(final long pos, final Runnable whenReleased, final boolean clearQueue) {
50
		this.dispatcher.schedule(new StrictQueue.RunnableWithPriority(1, () -> {
51
			this.queue.release(pos, clearQueue);
52
			this.onRelease(pos);
53
			if (this.sleeping) {
54
				this.sleeping = false;
55
				this.pollTask();
56
			}
57
58
			whenReleased.run();
59
		}));
60
	}
61
62
	public void submit(final Runnable task, final long pos, final IntSupplier level) {
63
		this.dispatcher.schedule(new StrictQueue.RunnableWithPriority(2, () -> {
64
			int ticketLevel = level.getAsInt();
65
			if (SharedConstants.DEBUG_VERBOSE_SERVER_EVENTS) {
66
				LOGGER.debug("SUB {} {} {} {}", ChunkPos.unpack(pos), ticketLevel, this.executor, this.queue);
67
			}
68
69
			this.queue.submit(task, pos, ticketLevel);
70
			if (this.sleeping) {
71
				this.sleeping = false;
72
				this.pollTask();
73
			}
74
		}));
75
	}
76
77
	protected void pollTask() {
78
		this.dispatcher.schedule(new StrictQueue.RunnableWithPriority(3, () -> {
79
			ChunkTaskPriorityQueue.TasksForChunk tasksForChunk = this.popTasks();
80
			if (tasksForChunk == null) {
81
				this.sleeping = true;
82
			} else {
83
				this.scheduleForExecution(tasksForChunk);
84
			}
85
		}));
86
	}
87
88
	protected void scheduleForExecution(final ChunkTaskPriorityQueue.TasksForChunk tasksForChunk) {
89
		CompletableFuture.allOf(tasksForChunk.tasks().stream().map(message -> this.executor.scheduleWithResult(future -> {
90
			message.run();
91
			future.complete(Unit.INSTANCE);
92
		})).toArray(CompletableFuture[]::new)).thenAccept(r -> this.pollTask());
93
	}
94
95
	protected void onRelease(final long key) {
96
	}
97
98
	protected ChunkTaskPriorityQueue.@Nullable TasksForChunk popTasks() {
99
		return this.queue.pop();
100
	}
101
102
	@Override
103
	public void close() {
104
		this.executor.close();
105
	}
106
}
107

Decompiled Minecraft 26.3 source of ChunkTaskDispatcher (net/minecraft/server/level/ChunkTaskDispatcher.java) with Mojang official mappings, stored verbatim on the site.