Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
ishland committed Jun 20, 2021
1 parent 8248fea commit 99c4b51
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 332 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,12 @@

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class GlobalExecutors {

public static final ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(
1,
new ThreadFactoryBuilder().setNameFormat("C2ME scheduler").setDaemon(true).setPriority(Thread.NORM_PRIORITY - 1).setThreadFactory(r -> {
final Thread thread = new Thread(r);
GlobalExecutors.schedulerThread.set(thread);
return thread;
}).build()
);
private static final AtomicReference<Thread> schedulerThread = new AtomicReference<>();
public static final ExecutorService scheduler = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("C2ME scheduler").setDaemon(true).setPriority(Thread.NORM_PRIORITY).build());

public static void ensureSchedulerThread() {
if (Thread.currentThread() != schedulerThread.get())
throw new IllegalStateException("Not on scheduler thread");
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,79 +1,19 @@
package com.ishland.c2me.mixin.threading.chunkio;

import com.ishland.c2me.common.threading.chunkio.C2MECachedRegionStorage;
import com.ishland.c2me.common.threading.chunkio.ChunkIoThreadingExecutorUtils;
import com.ishland.c2me.common.threading.chunkio.IAsyncChunkStorage;
import com.mojang.datafixers.util.Either;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.thread.TaskExecutor;
import net.minecraft.util.thread.TaskQueue;
import net.minecraft.world.storage.RegionBasedStorage;
import net.minecraft.world.storage.StorageIoWorker;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

@Mixin(StorageIoWorker.class)
public abstract class MixinStorageIoWorker implements IAsyncChunkStorage {

@Mutable
@Shadow @Final private RegionBasedStorage storage;

@Mutable
@Shadow @Final private Map<ChunkPos, StorageIoWorker.Result> results;

@Shadow @Final private AtomicBoolean closed;

@Mutable
@Shadow @Final private TaskExecutor<TaskQueue.PrioritizedTask> executor;

@Shadow @Final private static Logger LOGGER;

@Shadow protected abstract <T> CompletableFuture<T> run(Supplier<Either<T, Exception>> supplier);

@Shadow protected abstract CompletableFuture<NbtCompound> readChunkData(ChunkPos pos);

@Inject(method = "<init>", at = @At("RETURN"))
private void onPostInit(CallbackInfo info) {
//noinspection ConstantConditions
if (((Object) this) instanceof C2MECachedRegionStorage) {
this.storage = null;
this.results = null;
this.executor = null;
this.closed.set(true);
}
}

private final AtomicReference<ExecutorService> executorService = new AtomicReference<>();

@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/Util;getIoWorkerExecutor()Ljava/util/concurrent/Executor;"))
private Executor onGetStorageIoWorker() {
executorService.set(Executors.newSingleThreadExecutor(ChunkIoThreadingExecutorUtils.ioWorkerFactory));
return executorService.get();
}

@Inject(method = "close", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/thread/TaskExecutor;close()V", shift = At.Shift.AFTER))
private void onClose(CallbackInfo ci) {
final ExecutorService executorService = this.executorService.get();
if (executorService != null) executorService.shutdown();
}

@Override
public CompletableFuture<NbtCompound> getNbtAtAsync(ChunkPos pos) {
return readChunkData(pos);
Expand Down

0 comments on commit 99c4b51

Please sign in to comment.