From 85ca2a5302b838d11118b4341d77fb0239be2df7 Mon Sep 17 00:00:00 2001 From: jglrxavpok Date: Sat, 28 Aug 2021 14:20:47 +0200 Subject: [PATCH] No longer expose Thread-binding to Task API --- .../map/framebuffers/GLFWCapableBuffer.java | 19 ++++++++++--- .../server/timer/SchedulerManager.java | 14 ---------- .../java/net/minestom/server/timer/Task.java | 28 +++++++------------ .../minestom/server/timer/TaskBuilder.java | 18 +----------- 4 files changed, 26 insertions(+), 53 deletions(-) diff --git a/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java b/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java index 18dfb866f..4b2fbc951 100644 --- a/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java +++ b/src/lwjgl/java/net/minestom/server/map/framebuffers/GLFWCapableBuffer.java @@ -4,6 +4,7 @@ import net.minestom.server.MinecraftServer; import net.minestom.server.map.Framebuffer; import net.minestom.server.map.MapColors; import net.minestom.server.timer.Task; +import net.minestom.server.utils.thread.ThreadBindingExecutor; import org.lwjgl.BufferUtils; import org.lwjgl.PointerBuffer; import org.lwjgl.glfw.GLFWErrorCallback; @@ -27,6 +28,8 @@ public abstract class GLFWCapableBuffer { private final ByteBuffer colorsBuffer; private boolean onlyMapColors; + private static ThreadBindingExecutor threadBindingPool; + protected GLFWCapableBuffer(int width, int height) { 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."); } } + + synchronized(GLFWCapableBuffer.class) { + if(threadBindingPool == null) { + threadBindingPool = new ThreadBindingExecutor(MinecraftServer.THREAD_COUNT_SCHEDULER, MinecraftServer.THREAD_NAME_SCHEDULER); + } + } } public GLFWCapableBuffer unbindContextFromThread() { @@ -81,17 +90,19 @@ public abstract class GLFWCapableBuffer { return MinecraftServer.getSchedulerManager() .buildTask(new Runnable() { private boolean first = true; - - @Override - public void run() { + private final Runnable subAction = () -> { if(first) { changeRenderingThreadToCurrent(); first = false; } render(rendering); + }; + + @Override + public void run() { + threadBindingPool.execute(subAction); } }) - .bindToSingleThread() .repeat(period) .schedule(); } diff --git a/src/main/java/net/minestom/server/timer/SchedulerManager.java b/src/main/java/net/minestom/server/timer/SchedulerManager.java index 698044727..a05ef7dc2 100644 --- a/src/main/java/net/minestom/server/timer/SchedulerManager.java +++ b/src/main/java/net/minestom/server/timer/SchedulerManager.java @@ -36,8 +36,6 @@ public final class SchedulerManager implements IExtensionObserver { private final AtomicInteger shutdownCounter; //A threaded execution 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 private final ScheduledExecutorService timerExecutionService; // All the registered tasks (task id = task) @@ -69,7 +67,6 @@ public final class SchedulerManager implements IExtensionObserver { this.shutdownCounter = new AtomicInteger(); 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.tasks = new Int2ObjectOpenHashMap<>(); this.shutdownTasks = new Int2ObjectOpenHashMap<>(); @@ -196,17 +193,6 @@ public final class SchedulerManager implements IExtensionObserver { 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}. * diff --git a/src/main/java/net/minestom/server/timer/Task.java b/src/main/java/net/minestom/server/timer/Task.java index 2e3d40e7d..397170bd1 100644 --- a/src/main/java/net/minestom/server/timer/Task.java +++ b/src/main/java/net/minestom/server/timer/Task.java @@ -45,10 +45,6 @@ public class Task implements Runnable { private volatile Thread currentThreadTask; // The executor service used for this task 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. @@ -58,21 +54,25 @@ public class Task implements Runnable { * @param shutdown Defines whether the task is a shutdown task * @param delay The time to delay * @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.runnable = runnable; this.shutdown = shutdown; 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.repeat = repeat; this.isTransient = isTransient; - this.boundToSingleThread = bindToSingleThread; this.owningExtension = owningExtension; + } - this.action = () -> { + /** + * Executes the task. + */ + @Override + public void run() { + executorService.execute(() -> { this.currentThreadTask = Thread.currentThread(); try { this.runnable.run(); @@ -88,15 +88,7 @@ public class Task implements Runnable { if (this.repeat == 0) this.finish(); this.currentThreadTask = null; } - }; - } - - /** - * Executes the task. - */ - @Override - public void run() { - executorService.execute(action); + }); } /** diff --git a/src/main/java/net/minestom/server/timer/TaskBuilder.java b/src/main/java/net/minestom/server/timer/TaskBuilder.java index 287e3d3b9..f1387f0c7 100644 --- a/src/main/java/net/minestom/server/timer/TaskBuilder.java +++ b/src/main/java/net/minestom/server/timer/TaskBuilder.java @@ -36,12 +36,6 @@ public class TaskBuilder { */ 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. *
@@ -143,15 +137,6 @@ public class TaskBuilder { 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}. * @@ -166,8 +151,7 @@ public class TaskBuilder { this.delay, this.repeat, this.isTransient, - this.owningExtension, - this.boundToSingleThread); + this.owningExtension); } /**