No longer expose Thread-binding to Task API

This commit is contained in:
jglrxavpok 2021-08-28 14:20:47 +02:00
parent 42938111d5
commit 85ca2a5302
4 changed files with 26 additions and 53 deletions

View File

@ -4,6 +4,7 @@ import net.minestom.server.MinecraftServer;
import net.minestom.server.map.Framebuffer; import net.minestom.server.map.Framebuffer;
import net.minestom.server.map.MapColors; import net.minestom.server.map.MapColors;
import net.minestom.server.timer.Task; import net.minestom.server.timer.Task;
import net.minestom.server.utils.thread.ThreadBindingExecutor;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import org.lwjgl.PointerBuffer; import org.lwjgl.PointerBuffer;
import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.GLFWErrorCallback;
@ -27,6 +28,8 @@ public abstract class GLFWCapableBuffer {
private final ByteBuffer colorsBuffer; private final ByteBuffer colorsBuffer;
private boolean onlyMapColors; private boolean onlyMapColors;
private static ThreadBindingExecutor threadBindingPool;
protected GLFWCapableBuffer(int width, int height) { protected GLFWCapableBuffer(int width, int height) {
this(width, height, GLFW_NATIVE_CONTEXT_API, GLFW_OPENGL_API); this(width, height, GLFW_NATIVE_CONTEXT_API, GLFW_OPENGL_API);
} }
@ -60,6 +63,12 @@ public abstract class GLFWCapableBuffer {
throw new RuntimeException("("+errcode+") Failed to create GLFW Window."); throw new RuntimeException("("+errcode+") Failed to create GLFW Window.");
} }
} }
synchronized(GLFWCapableBuffer.class) {
if(threadBindingPool == null) {
threadBindingPool = new ThreadBindingExecutor(MinecraftServer.THREAD_COUNT_SCHEDULER, MinecraftServer.THREAD_NAME_SCHEDULER);
}
}
} }
public GLFWCapableBuffer unbindContextFromThread() { public GLFWCapableBuffer unbindContextFromThread() {
@ -81,17 +90,19 @@ public abstract class GLFWCapableBuffer {
return MinecraftServer.getSchedulerManager() return MinecraftServer.getSchedulerManager()
.buildTask(new Runnable() { .buildTask(new Runnable() {
private boolean first = true; private boolean first = true;
private final Runnable subAction = () -> {
@Override
public void run() {
if(first) { if(first) {
changeRenderingThreadToCurrent(); changeRenderingThreadToCurrent();
first = false; first = false;
} }
render(rendering); render(rendering);
};
@Override
public void run() {
threadBindingPool.execute(subAction);
} }
}) })
.bindToSingleThread()
.repeat(period) .repeat(period)
.schedule(); .schedule();
} }

View File

@ -36,8 +36,6 @@ public final class SchedulerManager implements IExtensionObserver {
private final AtomicInteger shutdownCounter; private final AtomicInteger shutdownCounter;
//A threaded execution //A threaded execution
private final ExecutorService batchesPool; private final ExecutorService batchesPool;
//Thread execution, which always uses the same Thread for a given Task
private final ExecutorService threadBindingPool;
// A single threaded scheduled execution // A single threaded scheduled execution
private final ScheduledExecutorService timerExecutionService; private final ScheduledExecutorService timerExecutionService;
// All the registered tasks (task id = task) // All the registered tasks (task id = task)
@ -69,7 +67,6 @@ public final class SchedulerManager implements IExtensionObserver {
this.shutdownCounter = new AtomicInteger(); this.shutdownCounter = new AtomicInteger();
this.batchesPool = new MinestomThread(MinecraftServer.THREAD_COUNT_SCHEDULER, MinecraftServer.THREAD_NAME_SCHEDULER); this.batchesPool = new MinestomThread(MinecraftServer.THREAD_COUNT_SCHEDULER, MinecraftServer.THREAD_NAME_SCHEDULER);
this.threadBindingPool = new ThreadBindingExecutor(MinecraftServer.THREAD_COUNT_SCHEDULER, MinecraftServer.THREAD_NAME_SCHEDULER);
this.timerExecutionService = Executors.newSingleThreadScheduledExecutor(); this.timerExecutionService = Executors.newSingleThreadScheduledExecutor();
this.tasks = new Int2ObjectOpenHashMap<>(); this.tasks = new Int2ObjectOpenHashMap<>();
this.shutdownTasks = new Int2ObjectOpenHashMap<>(); this.shutdownTasks = new Int2ObjectOpenHashMap<>();
@ -196,17 +193,6 @@ public final class SchedulerManager implements IExtensionObserver {
return batchesPool; return batchesPool;
} }
/**
* Gets the execution service for all the registered {@link Task}, which are marked as thread-bound.
* The thread to which tasks are assigned depends on their runnable hashcode. Two (or more) tasks can be bound to the same Thread.
*
* @return the execution service for all the registered {@link Task}, which are marked as thread-bound
*/
@NotNull
public ExecutorService getThreadBindingPool() {
return threadBindingPool;
}
/** /**
* Gets the scheduled execution service for all the registered {@link Task}. * Gets the scheduled execution service for all the registered {@link Task}.
* *

View File

@ -45,10 +45,6 @@ public class Task implements Runnable {
private volatile Thread currentThreadTask; private volatile Thread currentThreadTask;
// The executor service used for this task // The executor service used for this task
private final ExecutorService executorService; private final ExecutorService executorService;
// Whether this task will always execute on the same thread
private final boolean boundToSingleThread;
// Action executed on the executor. Stored inside the Task to avoid changing the hashcode (which ThreadBindingExecutor relies on)
private final Runnable action;
/** /**
* Creates a task. * Creates a task.
@ -58,21 +54,25 @@ public class Task implements Runnable {
* @param shutdown Defines whether the task is a shutdown task * @param shutdown Defines whether the task is a shutdown task
* @param delay The time to delay * @param delay The time to delay
* @param repeat The time until the repetition * @param repeat The time until the repetition
* @param bindToSingleThread Whether to run the given task always on the same thread.
*/ */
public Task(@NotNull SchedulerManager schedulerManager, @NotNull Runnable runnable, boolean shutdown, long delay, long repeat, boolean isTransient, @Nullable String owningExtension, boolean bindToSingleThread) { public Task(@NotNull SchedulerManager schedulerManager, @NotNull Runnable runnable, boolean shutdown, long delay, long repeat, boolean isTransient, @Nullable String owningExtension) {
this.schedulerManager = schedulerManager; this.schedulerManager = schedulerManager;
this.runnable = runnable; this.runnable = runnable;
this.shutdown = shutdown; this.shutdown = shutdown;
this.id = shutdown ? this.schedulerManager.getShutdownCounterIdentifier() : this.schedulerManager.getCounterIdentifier(); this.id = shutdown ? this.schedulerManager.getShutdownCounterIdentifier() : this.schedulerManager.getCounterIdentifier();
this.executorService = bindToSingleThread ? this.schedulerManager.getThreadBindingPool() : this.schedulerManager.getBatchesPool(); this.executorService = this.schedulerManager.getBatchesPool();
this.delay = delay; this.delay = delay;
this.repeat = repeat; this.repeat = repeat;
this.isTransient = isTransient; this.isTransient = isTransient;
this.boundToSingleThread = bindToSingleThread;
this.owningExtension = owningExtension; this.owningExtension = owningExtension;
}
this.action = () -> { /**
* Executes the task.
*/
@Override
public void run() {
executorService.execute(() -> {
this.currentThreadTask = Thread.currentThread(); this.currentThreadTask = Thread.currentThread();
try { try {
this.runnable.run(); this.runnable.run();
@ -88,15 +88,7 @@ public class Task implements Runnable {
if (this.repeat == 0) this.finish(); if (this.repeat == 0) this.finish();
this.currentThreadTask = null; this.currentThreadTask = null;
} }
}; });
}
/**
* Executes the task.
*/
@Override
public void run() {
executorService.execute(action);
} }
/** /**

View File

@ -36,12 +36,6 @@ public class TaskBuilder {
*/ */
private boolean isTransient; private boolean isTransient;
/**
* Should this Task be run on the same thread for each run? Can be used for situations where context is bound to a
* single thread. (for instance OpenGL context)
*/
private boolean boundToSingleThread = false;
/** /**
* Creates a task builder. * Creates a task builder.
* <br> * <br>
@ -143,15 +137,6 @@ public class TaskBuilder {
return this; return this;
} }
/**
* Makes this Task be run on the same thread for each run. Can be used for situations where context is bound to a
* single thread. (for instance OpenGL context)
*/
public TaskBuilder bindToSingleThread() {
boundToSingleThread = true;
return this;
}
/** /**
* Builds a {@link Task}. * Builds a {@link Task}.
* *
@ -166,8 +151,7 @@ public class TaskBuilder {
this.delay, this.delay,
this.repeat, this.repeat,
this.isTransient, this.isTransient,
this.owningExtension, this.owningExtension);
this.boundToSingleThread);
} }
/** /**