Move async task to our executor
This commit is contained in:
@@ -1,41 +1,27 @@
|
||||
package io.akarin.server.core;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
|
||||
public class AkarinAsyncExecutor extends Thread {
|
||||
private final static Logger LOGGER = LogManager.getLogger("Akarin");
|
||||
private final static int STD_TICK_TIME = 50;
|
||||
public class AkarinAsyncExecutor {
|
||||
private static final Executor singleExecutor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("Akarin Single Async Executor Thread - %1$d").build());
|
||||
private static final Executor asyncExecutor = Executors.newFixedThreadPool(4, new ThreadFactoryBuilder().setNameFormat("Akarin Async Executor Thread - %1$d").build());
|
||||
|
||||
public static AkarinAsyncExecutor initalise() {
|
||||
return Singleton.instance;
|
||||
/**
|
||||
* Posts a task to be executed asynchronously in a single thread
|
||||
* @param run
|
||||
*/
|
||||
public static void scheduleSingleAsyncTask(Runnable run) {
|
||||
singleExecutor.execute(run);
|
||||
}
|
||||
|
||||
private static class Singleton {
|
||||
private static final AkarinAsyncExecutor instance;
|
||||
|
||||
static {
|
||||
instance = new AkarinAsyncExecutor();
|
||||
instance.setName("Akarin Slack Scheduler Thread");
|
||||
instance.setDaemon(true);
|
||||
instance.start();
|
||||
LOGGER.info("Async executor started");
|
||||
}
|
||||
/**
|
||||
* Posts a task to be executed asynchronously
|
||||
* @param run
|
||||
*/
|
||||
public static void scheduleAsyncTask(Runnable run) {
|
||||
asyncExecutor.execute(run);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
MinecraftServer server = MinecraftServer.getServer();
|
||||
|
||||
while (server.isRunning()) {
|
||||
try {
|
||||
Thread.sleep(STD_TICK_TIME);
|
||||
} catch (InterruptedException interrupted) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.akarin.server.core;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
|
||||
public class AkarinAsyncScheduler extends Thread {
|
||||
private final static Logger LOGGER = LogManager.getLogger("Akarin");
|
||||
private final static int STD_TICK_TIME = 50;
|
||||
|
||||
public static AkarinAsyncScheduler initalise() {
|
||||
return Singleton.instance;
|
||||
}
|
||||
|
||||
private static class Singleton {
|
||||
private static final AkarinAsyncScheduler instance;
|
||||
|
||||
static {
|
||||
instance = new AkarinAsyncScheduler();
|
||||
instance.setName("Akarin Async Scheduler Thread");
|
||||
instance.setDaemon(true);
|
||||
instance.start();
|
||||
LOGGER.info("Async executor started");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long currentLoop = System.currentTimeMillis();
|
||||
MinecraftServer server = MinecraftServer.getServer();
|
||||
|
||||
while (server.isRunning()) {
|
||||
try {
|
||||
Thread.sleep(STD_TICK_TIME - (System.currentTimeMillis() - currentLoop));
|
||||
} catch (InterruptedException interrupted) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,8 @@ import com.mojang.authlib.Agent;
|
||||
import com.mojang.authlib.GameProfile;
|
||||
import com.mojang.authlib.GameProfileRepository;
|
||||
import com.mojang.authlib.ProfileLookupCallback;
|
||||
|
||||
import io.akarin.server.core.AkarinAsyncExecutor;
|
||||
import io.akarin.server.core.AkarinGlobalConfig;
|
||||
import net.minecraft.server.UserCache.UserCacheEntry;
|
||||
|
||||
@@ -106,7 +108,7 @@ public class AkarinUserCache {
|
||||
|
||||
Runnable find = () -> profileRepo.findProfilesByNames(new String[] { keyUsername }, Agent.MINECRAFT, callbackHandler);
|
||||
if (async) {
|
||||
MCUtil.scheduleAsyncTask(find);
|
||||
AkarinAsyncExecutor.scheduleAsyncTask(find);
|
||||
return null;
|
||||
} else {
|
||||
find.run();
|
||||
@@ -265,7 +267,7 @@ public class AkarinUserCache {
|
||||
};
|
||||
|
||||
if (async)
|
||||
MCUtil.scheduleAsyncTask(save);
|
||||
AkarinAsyncExecutor.scheduleAsyncTask(save);
|
||||
else
|
||||
save.run();
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Queues;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import io.akarin.server.core.AkarinAsyncExecutor;
|
||||
import io.akarin.server.core.AkarinGlobalConfig;
|
||||
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
||||
import it.unimi.dsi.fastutil.longs.LongSet;
|
||||
@@ -297,7 +298,7 @@ public class Chunk implements IChunkAccess {
|
||||
// Akarin start
|
||||
};
|
||||
if (AkarinGlobalConfig.enableAsyncLighting)
|
||||
MCUtil.scheduleAsyncTask(runnable);
|
||||
AkarinAsyncExecutor.scheduleAsyncTask(runnable);
|
||||
else
|
||||
runnable.run();
|
||||
// Akarin end
|
||||
@@ -1534,7 +1535,7 @@ public class Chunk implements IChunkAccess {
|
||||
public void runOrQueueLightUpdate(Runnable runnable) {
|
||||
// Akarin start
|
||||
if (AkarinGlobalConfig.enableAsyncLighting) {
|
||||
MCUtil.scheduleAsyncTask(runnable);
|
||||
AkarinAsyncExecutor.scheduleAsyncTask(runnable);
|
||||
return;
|
||||
}
|
||||
// Akarin end
|
||||
|
||||
@@ -27,7 +27,7 @@ import java.util.concurrent.TimeoutException;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public final class MCUtil {
|
||||
private static final Executor asyncExecutor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("Akarin Async Task Handler Thread - %1$d").build()); // Akarin
|
||||
private static final Executor asyncExecutor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("Paper Async Task Handler Thread - %1$d").build());
|
||||
|
||||
private MCUtil() {}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package net.minecraft.server;
|
||||
|
||||
import co.aikar.timings.Timings;
|
||||
import io.akarin.server.core.AkarinAsyncExecutor;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
@@ -446,7 +448,7 @@ public class WorldServer extends World implements IAsyncTaskHandler {
|
||||
if (spigotConfig.randomLightUpdates && !this.players.isEmpty()) { // Spigot
|
||||
int i = this.random.nextInt(this.players.size());
|
||||
EntityHuman entityhuman = (EntityHuman) this.players.get(i);
|
||||
MCUtil.scheduleAsyncTask(() -> { // Akarin
|
||||
AkarinAsyncExecutor.scheduleAsyncTask(() -> { // Akarin
|
||||
int j = MathHelper.floor(entityhuman.locX) + this.random.nextInt(11) - 5;
|
||||
int k = MathHelper.floor(entityhuman.locY) + this.random.nextInt(11) - 5;
|
||||
int l = MathHelper.floor(entityhuman.locZ) + this.random.nextInt(11) - 5;
|
||||
@@ -481,7 +483,7 @@ public class WorldServer extends World implements IAsyncTaskHandler {
|
||||
int k = chunk.locZ * 16;
|
||||
|
||||
//this.methodProfiler.exitEnter("checkNextLight"); // Akarin - remove caller
|
||||
MCUtil.scheduleAsyncTask(chunk::x); // Akarin
|
||||
AkarinAsyncExecutor.scheduleAsyncTask(chunk::x); // Akarin
|
||||
//this.methodProfiler.exitEnter("tickChunk"); // Akarin - remove caller
|
||||
chunk.d(false);
|
||||
if ( !chunk.areNeighborsLoaded( 1 ) ) continue; // Spigot
|
||||
|
||||
Reference in New Issue
Block a user