Skip to content

Commit 92a710f

Browse files
committed
new: fully threaded scheduling
1 parent 2324fe6 commit 92a710f

File tree

3 files changed

+11
-49
lines changed

3 files changed

+11
-49
lines changed

c2me-rewrites-chunk-system/src/main/java/com/ishland/c2me/rewrites/chunksystem/common/TheChunkSystem.java

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import com.ishland.c2me.base.common.scheduler.SchedulingManager;
66
import com.ishland.c2me.base.mixin.access.IThreadedAnvilChunkStorage;
77
import com.ishland.c2me.base.mixin.access.IVersionedChunkStorage;
8-
import com.ishland.flowsched.scheduler.DaemonizedStatusAdvancingScheduler;
98
import com.ishland.flowsched.scheduler.ExceptionHandlingAction;
109
import com.ishland.flowsched.scheduler.ItemHolder;
1110
import com.ishland.flowsched.scheduler.ItemStatus;
1211
import com.ishland.flowsched.scheduler.KeyStatusPair;
12+
import com.ishland.flowsched.scheduler.StatusAdvancingScheduler;
1313
import com.ishland.flowsched.util.Assertions;
1414
import io.netty.util.internal.PlatformDependent;
1515
import io.reactivex.rxjava3.core.Scheduler;
@@ -21,49 +21,35 @@
2121
import net.minecraft.server.WorldGenerationProgressListener;
2222
import net.minecraft.server.world.ChunkHolder;
2323
import net.minecraft.server.world.ServerChunkLoadingManager;
24-
import net.minecraft.util.Util;
2524
import net.minecraft.util.collection.BoundedRegionArray;
26-
import net.minecraft.util.crash.CrashException;
27-
import net.minecraft.util.crash.ReportType;
2825
import net.minecraft.util.math.ChunkPos;
2926
import org.slf4j.Logger;
3027
import org.slf4j.LoggerFactory;
3128

32-
import java.nio.file.Path;
3329
import java.util.Queue;
3430
import java.util.concurrent.Executor;
35-
import java.util.concurrent.ThreadFactory;
3631

37-
public class TheChunkSystem extends DaemonizedStatusAdvancingScheduler<ChunkPos, ChunkState, ChunkLoadingContext, NewChunkHolderVanillaInterface> {
32+
public class TheChunkSystem extends StatusAdvancingScheduler<ChunkPos, ChunkState, ChunkLoadingContext, NewChunkHolderVanillaInterface> {
3833

3934
private final Logger LOGGER;
4035

4136
private final Long2IntMap managedTickets = Long2IntMaps.synchronize(new Long2IntOpenHashMap());
4237
private final SchedulingManager schedulingManager;
4338
private final Executor backingBackgroundExecutor = GlobalExecutors.prioritizedScheduler.executor(15);
44-
private Queue<Runnable> backgroundTaskQueue = PlatformDependent.newSpscQueue();
45-
private final Executor backgroundExecutor = command -> {
46-
if (Thread.currentThread() != this.thread) {
47-
command.run();
48-
} else {
49-
backgroundTaskQueue.add(command);
50-
}
51-
};
52-
private final Scheduler backgroundScheduler = Schedulers.from(this.backgroundExecutor);
39+
private final Scheduler backgroundScheduler = Schedulers.from(this.backingBackgroundExecutor);
5340
private final ServerChunkLoadingManager tacs;
5441

55-
public TheChunkSystem(ThreadFactory threadFactory, ServerChunkLoadingManager tacs) {
56-
super(threadFactory, TheSpeedyObjectFactory.INSTANCE);
42+
public TheChunkSystem(ServerChunkLoadingManager tacs) {
43+
super(TheSpeedyObjectFactory.INSTANCE);
5744
this.tacs = tacs;
5845
this.schedulingManager = ((IVanillaChunkManager) tacs).c2me$getSchedulingManager();
5946
this.LOGGER = LoggerFactory.getLogger("Chunk System of %s".formatted(((IThreadedAnvilChunkStorage) tacs).getWorld().getRegistryKey().getValue()));
6047
managedTickets.defaultReturnValue(NewChunkStatus.vanillaLevelToStatus.length - 1);
61-
this.thread.start();
6248
}
6349

6450
@Override
6551
protected Executor getBackgroundExecutor() {
66-
return this.backgroundExecutor;
52+
return this.backingBackgroundExecutor;
6753
}
6854

6955
@Override
@@ -216,24 +202,4 @@ protected void onItemDowngrade(ItemHolder<ChunkPos, ChunkState, ChunkLoadingCont
216202
public int vanillaIf$getManagedLevel(long pos) {
217203
return this.managedTickets.get(pos);
218204
}
219-
220-
@Override
221-
public boolean tick() {
222-
boolean tick = super.tick();
223-
if (!this.backgroundTaskQueue.isEmpty()) {
224-
Queue<Runnable> queue = this.backgroundTaskQueue;
225-
this.backgroundTaskQueue = PlatformDependent.newSpscQueue();
226-
this.backingBackgroundExecutor.execute(() -> {
227-
Runnable runnable;
228-
while ((runnable = queue.poll()) != null) {
229-
try {
230-
runnable.run();
231-
} catch (Throwable t) {
232-
t.printStackTrace();
233-
}
234-
}
235-
});
236-
}
237-
return tick;
238-
}
239205
}

c2me-rewrites-chunk-system/src/main/java/com/ishland/c2me/rewrites/chunksystem/mixin/MixinThreadedAnvilChunkStorage.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.ishland.c2me.rewrites.chunksystem.mixin;
22

3-
import com.google.common.util.concurrent.ThreadFactoryBuilder;
43
import com.ishland.c2me.base.common.scheduler.IVanillaChunkManager;
54
import com.ishland.c2me.rewrites.chunksystem.common.ChunkLoadingContext;
65
import com.ishland.c2me.rewrites.chunksystem.common.ChunkState;
@@ -39,9 +38,6 @@ public class MixinThreadedAnvilChunkStorage implements IChunkSystemAccess {
3938
@Inject(method = "<init>", at = @At("RETURN"))
4039
private void onInit(CallbackInfo ci) {
4140
newSystem = new TheChunkSystem(
42-
new ThreadFactoryBuilder()
43-
.setNameFormat("chunksystem-" + this.world.getRegistryKey().getValue().toUnderscoreSeparatedString())
44-
.build(),
4541
(ServerChunkLoadingManager) (Object) this
4642
);
4743
}
@@ -92,10 +88,10 @@ public ChunkHolder getChunkHolder(long pos) {
9288
return this.getCurrentChunkHolder(pos);
9389
}
9490

95-
@Inject(method = "close", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/poi/PointOfInterestStorage;close()V", shift = At.Shift.AFTER))
96-
private void closeNewSystem(CallbackInfo ci) {
97-
this.newSystem.shutdown();
98-
}
91+
// @Inject(method = "close", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/poi/PointOfInterestStorage;close()V", shift = At.Shift.AFTER))
92+
// private void closeNewSystem(CallbackInfo ci) {
93+
// this.newSystem.shutdown();
94+
// }
9995

10096
@Redirect(method = "save(Lnet/minecraft/world/chunk/Chunk;)Z", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/chunk/ChunkStatus;getChunkType()Lnet/minecraft/world/chunk/ChunkType;"), require = 0)
10197
private ChunkType alwaysSaveChunk(ChunkStatus instance) {

0 commit comments

Comments
 (0)