check that a task is valid before executing incase it was cancelled elsewhere

also set runners in the short circuit path so we know of the pending task incase its long running
This commit is contained in:
Aikar 2018-03-17 12:58:27 -04:00
parent fb8222874d
commit 7241626ffd

View File

@ -38,7 +38,7 @@ queue if a plugin schedules lots of asynchronous tasks.
diff --git a/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncScheduler.java b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncScheduler.java
new file mode 100644
index 000000000..b1efbc3e7
index 000000000..cf5aada2f
--- /dev/null
+++ b/src/main/java/org/bukkit/craftbukkit/scheduler/CraftAsyncScheduler.java
@@ -0,0 +0,0 @@
@ -82,6 +82,7 @@ index 000000000..b1efbc3e7
+
+ private final Executor management = Executors.newFixedThreadPool(1, new ThreadFactoryBuilder()
+ .setNameFormat("Craft Scheduler Management Thread").build());
+ private final List<CraftTask> temp = new ArrayList<>();
+ CraftAsyncScheduler() {
+ super(true);
+ }
@ -108,31 +109,40 @@ index 000000000..b1efbc3e7
+ }
+
+ private synchronized void runTasks(int currentTick) {
+ final List<CraftTask> temp = new ArrayList<>();
+ while (!this.pending.isEmpty() && this.pending.peek().getNextRun() <= currentTick) {
+ CraftTask task = this.pending.remove();
+ this.runners.put(task.getTaskId(), task);
+ this.executor.execute(new ServerSchedulerReportingWrapper(task));
+ final long period = task.getPeriod();
+ if (period > 0) {
+ task.setNextRun(currentTick + period);
+ temp.add(task);
+ if (executeTask(task)) {
+ final long period = task.getPeriod();
+ if (period > 0) {
+ task.setNextRun(currentTick + period);
+ temp.add(task);
+ }
+ }
+ }
+ this.pending.addAll(temp);
+ temp.clear();
+ }
+
+ @Override
+ protected CraftTask handle(CraftTask task, final long delay) {
+ if (task.getPeriod() == -1L && delay == 0L) {
+ this.executor.execute(task);
+ return task;
+ executeTask(task);
+ } else {
+ task.setNextRun(this.currentTick + delay);
+ this.management.execute(() -> this.addTask(task));
+ }
+ task.setNextRun(this.currentTick + delay);
+ this.management.execute(() -> this.addTask(task));
+ return task;
+ }
+
+ private boolean executeTask(CraftTask task) {
+ if (isValid(task)) {
+ this.runners.put(task.getTaskId(), task);
+ this.executor.execute(new ServerSchedulerReportingWrapper(task));
+ return true;
+ }
+ return false;
+ }
+
+ private synchronized void addTask(CraftTask task) {
+ this.pending.add(task);
+ }
@ -140,9 +150,9 @@ index 000000000..b1efbc3e7
+ @Override
+ public synchronized void cancelTasks(Plugin plugin) {
+ for (Iterator<CraftTask> iterator = this.pending.iterator(); iterator.hasNext(); ) {
+ CraftTask taskPending = iterator.next();
+ if (taskPending.getTaskId() != -1 && (plugin == null || taskPending.getOwner().equals(plugin))) {
+ taskPending.cancel0();
+ CraftTask task = iterator.next();
+ if (task.getTaskId() != -1 && (plugin == null || task.getOwner().equals(plugin))) {
+ task.cancel0();
+ iterator.remove();
+ }
+ }