Remove magic values in scheduler package

By: Senmori <thesenmori@gmail.com>
This commit is contained in:
CraftBukkit/Spigot 2018-03-07 13:41:24 -05:00
parent 26e73eb61e
commit 39a4128d9d
4 changed files with 55 additions and 50 deletions

View file

@ -27,7 +27,7 @@ class CraftAsyncTask extends CraftTask {
public void run() { public void run() {
final Thread thread = Thread.currentThread(); final Thread thread = Thread.currentThread();
synchronized(workers) { synchronized(workers) {
if (getPeriod() == -2) { if (getPeriod() == CraftTask.CANCEL) {
// Never continue running after cancelled. // Never continue running after cancelled.
// Checking this with the lock is important! // Checking this with the lock is important!
return; return;
@ -99,7 +99,7 @@ class CraftAsyncTask extends CraftTask {
boolean cancel0() { boolean cancel0() {
synchronized (workers) { synchronized (workers) {
// Synchronizing here prevents race condition for a completing task // Synchronizing here prevents race condition for a completing task
setPeriod(-2l); setPeriod(CraftTask.CANCEL);
if (workers.isEmpty()) { if (workers.isEmpty()) {
runners.remove(getTaskId()); runners.remove(getTaskId());
} }

View file

@ -16,21 +16,21 @@ class CraftFuture<T> extends CraftTask implements Future<T> {
private Exception exception = null; private Exception exception = null;
CraftFuture(final Callable<T> callable, final Plugin plugin, final int id) { CraftFuture(final Callable<T> callable, final Plugin plugin, final int id) {
super(plugin, null, id, -1l); super(plugin, null, id, CraftTask.NO_REPEATING);
this.callable = callable; this.callable = callable;
} }
public synchronized boolean cancel(final boolean mayInterruptIfRunning) { public synchronized boolean cancel(final boolean mayInterruptIfRunning) {
if (getPeriod() != -1l) { if (getPeriod() != CraftTask.NO_REPEATING) {
return false; return false;
} }
setPeriod(-2l); setPeriod(CraftTask.CANCEL);
return true; return true;
} }
public boolean isDone() { public boolean isDone() {
final long period = this.getPeriod(); final long period = this.getPeriod();
return period != -1l && period != -3l; return period != CraftTask.NO_REPEATING && period != CraftTask.PROCESS_FOR_FUTURE;
} }
public T get() throws CancellationException, InterruptedException, ExecutionException { public T get() throws CancellationException, InterruptedException, ExecutionException {
@ -44,13 +44,13 @@ class CraftFuture<T> extends CraftTask implements Future<T> {
public synchronized T get(long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { public synchronized T get(long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
timeout = unit.toMillis(timeout); timeout = unit.toMillis(timeout);
long period = this.getPeriod(); long period = this.getPeriod();
long timestamp = timeout > 0 ? System.currentTimeMillis() : 0l; long timestamp = timeout > 0 ? System.currentTimeMillis() : 0L;
while (true) { while (true) {
if (period == -1l || period == -3l) { if (period == CraftTask.NO_REPEATING || period == CraftTask.PROCESS_FOR_FUTURE) {
this.wait(timeout); this.wait(timeout);
period = this.getPeriod(); period = this.getPeriod();
if (period == -1l || period == -3l) { if (period == CraftTask.NO_REPEATING || period == CraftTask.PROCESS_FOR_FUTURE) {
if (timeout == 0l) { if (timeout == 0L) {
continue; continue;
} }
timeout += timestamp - (timestamp = System.currentTimeMillis()); timeout += timestamp - (timestamp = System.currentTimeMillis());
@ -60,26 +60,26 @@ class CraftFuture<T> extends CraftTask implements Future<T> {
throw new TimeoutException(); throw new TimeoutException();
} }
} }
if (period == -2l) { if (period == CraftTask.CANCEL) {
throw new CancellationException(); throw new CancellationException();
} }
if (period == -4l) { if (period == CraftTask.DONE_FOR_FUTURE) {
if (exception == null) { if (exception == null) {
return value; return value;
} }
throw new ExecutionException(exception); throw new ExecutionException(exception);
} }
throw new IllegalStateException("Expected " + -1l + " to " + -4l + ", got " + period); throw new IllegalStateException("Expected " + CraftTask.NO_REPEATING + " to " + CraftTask.DONE_FOR_FUTURE + ", got " + period);
} }
} }
@Override @Override
public void run() { public void run() {
synchronized (this) { synchronized (this) {
if (getPeriod() == -2l) { if (getPeriod() == CraftTask.CANCEL) {
return; return;
} }
setPeriod(-3l); setPeriod(CraftTask.PROCESS_FOR_FUTURE);
} }
try { try {
value = callable.call(); value = callable.call();
@ -87,17 +87,17 @@ class CraftFuture<T> extends CraftTask implements Future<T> {
exception = e; exception = e;
} finally { } finally {
synchronized (this) { synchronized (this) {
setPeriod(-4l); setPeriod(CraftTask.DONE_FOR_FUTURE);
this.notifyAll(); this.notifyAll();
} }
} }
} }
synchronized boolean cancel0() { synchronized boolean cancel0() {
if (getPeriod() != -1l) { if (getPeriod() != CraftTask.NO_REPEATING) {
return false; return false;
} }
setPeriod(-2l); setPeriod(CraftTask.CANCEL);
notifyAll(); notifyAll();
return true; return true;
} }

View file

@ -85,37 +85,37 @@ public class CraftScheduler implements BukkitScheduler {
} }
public int scheduleSyncDelayedTask(final Plugin plugin, final Runnable task) { public int scheduleSyncDelayedTask(final Plugin plugin, final Runnable task) {
return this.scheduleSyncDelayedTask(plugin, task, 0l); return this.scheduleSyncDelayedTask(plugin, task, 0L);
} }
public BukkitTask runTask(Plugin plugin, Runnable runnable) { public BukkitTask runTask(Plugin plugin, Runnable runnable) {
return runTaskLater(plugin, runnable, 0l); return runTaskLater(plugin, runnable, 0L);
} }
@Deprecated @Deprecated
public int scheduleAsyncDelayedTask(final Plugin plugin, final Runnable task) { public int scheduleAsyncDelayedTask(final Plugin plugin, final Runnable task) {
return this.scheduleAsyncDelayedTask(plugin, task, 0l); return this.scheduleAsyncDelayedTask(plugin, task, 0L);
} }
public BukkitTask runTaskAsynchronously(Plugin plugin, Runnable runnable) { public BukkitTask runTaskAsynchronously(Plugin plugin, Runnable runnable) {
return runTaskLaterAsynchronously(plugin, runnable, 0l); return runTaskLaterAsynchronously(plugin, runnable, 0L);
} }
public int scheduleSyncDelayedTask(final Plugin plugin, final Runnable task, final long delay) { public int scheduleSyncDelayedTask(final Plugin plugin, final Runnable task, final long delay) {
return this.scheduleSyncRepeatingTask(plugin, task, delay, -1l); return this.scheduleSyncRepeatingTask(plugin, task, delay, CraftTask.NO_REPEATING);
} }
public BukkitTask runTaskLater(Plugin plugin, Runnable runnable, long delay) { public BukkitTask runTaskLater(Plugin plugin, Runnable runnable, long delay) {
return runTaskTimer(plugin, runnable, delay, -1l); return runTaskTimer(plugin, runnable, delay, CraftTask.NO_REPEATING);
} }
@Deprecated @Deprecated
public int scheduleAsyncDelayedTask(final Plugin plugin, final Runnable task, final long delay) { public int scheduleAsyncDelayedTask(final Plugin plugin, final Runnable task, final long delay) {
return this.scheduleAsyncRepeatingTask(plugin, task, delay, -1l); return this.scheduleAsyncRepeatingTask(plugin, task, delay, CraftTask.NO_REPEATING);
} }
public BukkitTask runTaskLaterAsynchronously(Plugin plugin, Runnable runnable, long delay) { public BukkitTask runTaskLaterAsynchronously(Plugin plugin, Runnable runnable, long delay) {
return runTaskTimerAsynchronously(plugin, runnable, delay, -1l); return runTaskTimerAsynchronously(plugin, runnable, delay, CraftTask.NO_REPEATING);
} }
public int scheduleSyncRepeatingTask(final Plugin plugin, final Runnable runnable, long delay, long period) { public int scheduleSyncRepeatingTask(final Plugin plugin, final Runnable runnable, long delay, long period) {
@ -124,13 +124,13 @@ public class CraftScheduler implements BukkitScheduler {
public BukkitTask runTaskTimer(Plugin plugin, Runnable runnable, long delay, long period) { public BukkitTask runTaskTimer(Plugin plugin, Runnable runnable, long delay, long period) {
validate(plugin, runnable); validate(plugin, runnable);
if (delay < 0l) { if (delay < 0L) {
delay = 0; delay = 0;
} }
if (period == 0l) { if (period == CraftTask.ERROR) {
period = 1l; period = 1L;
} else if (period < -1l) { } else if (period < CraftTask.NO_REPEATING) {
period = -1l; period = CraftTask.NO_REPEATING;
} }
return handle(new CraftTask(plugin, runnable, nextId(), period), delay); return handle(new CraftTask(plugin, runnable, nextId(), period), delay);
} }
@ -142,13 +142,13 @@ public class CraftScheduler implements BukkitScheduler {
public BukkitTask runTaskTimerAsynchronously(Plugin plugin, Runnable runnable, long delay, long period) { public BukkitTask runTaskTimerAsynchronously(Plugin plugin, Runnable runnable, long delay, long period) {
validate(plugin, runnable); validate(plugin, runnable);
if (delay < 0l) { if (delay < 0L) {
delay = 0; delay = 0;
} }
if (period == 0l) { if (period == CraftTask.ERROR) {
period = 1l; period = 1L;
} else if (period < -1l) { } else if (period < CraftTask.NO_REPEATING) {
period = -1l; period = CraftTask.NO_REPEATING;
} }
return handle(new CraftAsyncTask(runners, plugin, runnable, nextId(), period), delay); return handle(new CraftAsyncTask(runners, plugin, runnable, nextId(), period), delay);
} }
@ -156,7 +156,7 @@ public class CraftScheduler implements BukkitScheduler {
public <T> Future<T> callSyncMethod(final Plugin plugin, final Callable<T> task) { public <T> Future<T> callSyncMethod(final Plugin plugin, final Callable<T> task) {
validate(plugin, task); validate(plugin, task);
final CraftFuture<T> future = new CraftFuture<T>(task, plugin, nextId()); final CraftFuture<T> future = new CraftFuture<T>(task, plugin, nextId());
handle(future, 0l); handle(future, 0L);
return future; return future;
} }
@ -190,7 +190,7 @@ public class CraftScheduler implements BukkitScheduler {
} }
return false; return false;
}}); }});
handle(task, 0l); handle(task, 0L);
for (CraftTask taskPending = head.getNext(); taskPending != null; taskPending = taskPending.getNext()) { for (CraftTask taskPending = head.getNext(); taskPending != null; taskPending = taskPending.getNext()) {
if (taskPending == task) { if (taskPending == task) {
return; return;
@ -223,7 +223,7 @@ public class CraftScheduler implements BukkitScheduler {
} }
} }
}); });
handle(task, 0l); handle(task, 0L);
for (CraftTask taskPending = head.getNext(); taskPending != null; taskPending = taskPending.getNext()) { for (CraftTask taskPending = head.getNext(); taskPending != null; taskPending = taskPending.getNext()) {
if (taskPending == task) { if (taskPending == task) {
break; break;
@ -255,7 +255,7 @@ public class CraftScheduler implements BukkitScheduler {
CraftScheduler.this.temp.clear(); CraftScheduler.this.temp.clear();
} }
}); });
handle(task, 0l); handle(task, 0L);
for (CraftTask taskPending = head.getNext(); taskPending != null; taskPending = taskPending.getNext()) { for (CraftTask taskPending = head.getNext(); taskPending != null; taskPending = taskPending.getNext()) {
if (taskPending == task) { if (taskPending == task) {
break; break;
@ -284,11 +284,11 @@ public class CraftScheduler implements BukkitScheduler {
} }
for (CraftTask task = head.getNext(); task != null; task = task.getNext()) { for (CraftTask task = head.getNext(); task != null; task = task.getNext()) {
if (task.getTaskId() == taskId) { if (task.getTaskId() == taskId) {
return task.getPeriod() >= -1l; // The task will run return task.getPeriod() >= CraftTask.NO_REPEATING; // The task will run
} }
} }
CraftTask task = runners.get(taskId); CraftTask task = runners.get(taskId);
return task != null && task.getPeriod() >= -1l; return task != null && task.getPeriod() >= CraftTask.NO_REPEATING;
} }
public List<BukkitWorker> getActiveWorkers() { public List<BukkitWorker> getActiveWorkers() {
@ -318,13 +318,13 @@ public class CraftScheduler implements BukkitScheduler {
final ArrayList<BukkitTask> pending = new ArrayList<BukkitTask>(); final ArrayList<BukkitTask> pending = new ArrayList<BukkitTask>();
for (CraftTask task : runners.values()) { for (CraftTask task : runners.values()) {
if (task.getPeriod() >= -1l) { if (task.getPeriod() >= CraftTask.NO_REPEATING) {
pending.add(task); pending.add(task);
} }
} }
for (final CraftTask task : truePending) { for (final CraftTask task : truePending) {
if (task.getPeriod() >= -1l && !pending.contains(task)) { if (task.getPeriod() >= CraftTask.NO_REPEATING && !pending.contains(task)) {
pending.add(task); pending.add(task);
} }
} }
@ -340,7 +340,7 @@ public class CraftScheduler implements BukkitScheduler {
parsePending(); parsePending();
while (isReady(currentTick)) { while (isReady(currentTick)) {
final CraftTask task = pending.remove(); final CraftTask task = pending.remove();
if (task.getPeriod() < -1l) { if (task.getPeriod() < CraftTask.NO_REPEATING) {
if (task.isSync()) { if (task.isSync()) {
runners.remove(task.getTaskId(), task); runners.remove(task.getTaskId(), task);
} }
@ -413,7 +413,7 @@ public class CraftScheduler implements BukkitScheduler {
for (; task != null; task = (lastTask = task).getNext()) { for (; task != null; task = (lastTask = task).getNext()) {
if (task.getTaskId() == -1) { if (task.getTaskId() == -1) {
task.run(); task.run();
} else if (task.getPeriod() >= -1l) { } else if (task.getPeriod() >= CraftTask.NO_REPEATING) {
pending.add(task); pending.add(task);
runners.put(task.getTaskId(), task); runners.put(task.getTaskId(), task);
} }

View file

@ -8,6 +8,11 @@ import org.bukkit.scheduler.BukkitTask;
class CraftTask implements BukkitTask, Runnable { class CraftTask implements BukkitTask, Runnable {
private volatile CraftTask next = null; private volatile CraftTask next = null;
public static final int ERROR = 0;
public static final int NO_REPEATING = -1;
public static final int CANCEL = -2;
public static final int PROCESS_FOR_FUTURE = -3;
public static final int DONE_FOR_FUTURE = -4;
/** /**
* -1 means no repeating <br> * -1 means no repeating <br>
* -2 means cancel <br> * -2 means cancel <br>
@ -23,11 +28,11 @@ class CraftTask implements BukkitTask, Runnable {
private final int id; private final int id;
CraftTask() { CraftTask() {
this(null, null, -1, -1); this(null, null, CraftTask.NO_REPEATING, CraftTask.NO_REPEATING);
} }
CraftTask(final Runnable task) { CraftTask(final Runnable task) {
this(null, task, -1, -1); this(null, task, CraftTask.NO_REPEATING, CraftTask.NO_REPEATING);
} }
CraftTask(final Plugin plugin, final Runnable task, final int id, final long period) { CraftTask(final Plugin plugin, final Runnable task, final int id, final long period) {
@ -83,7 +88,7 @@ class CraftTask implements BukkitTask, Runnable {
@Override @Override
public boolean isCancelled() { public boolean isCancelled() {
return (period == -2l); return (period == CraftTask.CANCEL);
} }
public void cancel() { public void cancel() {
@ -96,7 +101,7 @@ class CraftTask implements BukkitTask, Runnable {
* @return false if it is a craft future task that has already begun execution, true otherwise * @return false if it is a craft future task that has already begun execution, true otherwise
*/ */
boolean cancel0() { boolean cancel0() {
setPeriod(-2l); setPeriod(CraftTask.CANCEL);
return true; return true;
} }
} }