mirror of
https://github.com/PaperMC/Paper.git
synced 2024-11-29 03:32:46 +01:00
Remove magic values in scheduler package
This commit is contained in:
parent
aa255689c1
commit
9f49ad60e7
4 changed files with 55 additions and 50 deletions
|
@ -27,7 +27,7 @@ class CraftAsyncTask extends CraftTask {
|
|||
public void run() {
|
||||
final Thread thread = Thread.currentThread();
|
||||
synchronized(workers) {
|
||||
if (getPeriod() == -2) {
|
||||
if (getPeriod() == CraftTask.CANCEL) {
|
||||
// Never continue running after cancelled.
|
||||
// Checking this with the lock is important!
|
||||
return;
|
||||
|
@ -99,7 +99,7 @@ class CraftAsyncTask extends CraftTask {
|
|||
boolean cancel0() {
|
||||
synchronized (workers) {
|
||||
// Synchronizing here prevents race condition for a completing task
|
||||
setPeriod(-2l);
|
||||
setPeriod(CraftTask.CANCEL);
|
||||
if (workers.isEmpty()) {
|
||||
runners.remove(getTaskId());
|
||||
}
|
||||
|
|
|
@ -16,21 +16,21 @@ class CraftFuture<T> extends CraftTask implements Future<T> {
|
|||
private Exception exception = null;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public synchronized boolean cancel(final boolean mayInterruptIfRunning) {
|
||||
if (getPeriod() != -1l) {
|
||||
if (getPeriod() != CraftTask.NO_REPEATING) {
|
||||
return false;
|
||||
}
|
||||
setPeriod(-2l);
|
||||
setPeriod(CraftTask.CANCEL);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isDone() {
|
||||
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 {
|
||||
|
@ -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 {
|
||||
timeout = unit.toMillis(timeout);
|
||||
long period = this.getPeriod();
|
||||
long timestamp = timeout > 0 ? System.currentTimeMillis() : 0l;
|
||||
long timestamp = timeout > 0 ? System.currentTimeMillis() : 0L;
|
||||
while (true) {
|
||||
if (period == -1l || period == -3l) {
|
||||
if (period == CraftTask.NO_REPEATING || period == CraftTask.PROCESS_FOR_FUTURE) {
|
||||
this.wait(timeout);
|
||||
period = this.getPeriod();
|
||||
if (period == -1l || period == -3l) {
|
||||
if (timeout == 0l) {
|
||||
if (period == CraftTask.NO_REPEATING || period == CraftTask.PROCESS_FOR_FUTURE) {
|
||||
if (timeout == 0L) {
|
||||
continue;
|
||||
}
|
||||
timeout += timestamp - (timestamp = System.currentTimeMillis());
|
||||
|
@ -60,26 +60,26 @@ class CraftFuture<T> extends CraftTask implements Future<T> {
|
|||
throw new TimeoutException();
|
||||
}
|
||||
}
|
||||
if (period == -2l) {
|
||||
if (period == CraftTask.CANCEL) {
|
||||
throw new CancellationException();
|
||||
}
|
||||
if (period == -4l) {
|
||||
if (period == CraftTask.DONE_FOR_FUTURE) {
|
||||
if (exception == null) {
|
||||
return value;
|
||||
}
|
||||
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
|
||||
public void run() {
|
||||
synchronized (this) {
|
||||
if (getPeriod() == -2l) {
|
||||
if (getPeriod() == CraftTask.CANCEL) {
|
||||
return;
|
||||
}
|
||||
setPeriod(-3l);
|
||||
setPeriod(CraftTask.PROCESS_FOR_FUTURE);
|
||||
}
|
||||
try {
|
||||
value = callable.call();
|
||||
|
@ -87,17 +87,17 @@ class CraftFuture<T> extends CraftTask implements Future<T> {
|
|||
exception = e;
|
||||
} finally {
|
||||
synchronized (this) {
|
||||
setPeriod(-4l);
|
||||
setPeriod(CraftTask.DONE_FOR_FUTURE);
|
||||
this.notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
synchronized boolean cancel0() {
|
||||
if (getPeriod() != -1l) {
|
||||
if (getPeriod() != CraftTask.NO_REPEATING) {
|
||||
return false;
|
||||
}
|
||||
setPeriod(-2l);
|
||||
setPeriod(CraftTask.CANCEL);
|
||||
notifyAll();
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -85,37 +85,37 @@ public class CraftScheduler implements BukkitScheduler {
|
|||
}
|
||||
|
||||
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) {
|
||||
return runTaskLater(plugin, runnable, 0l);
|
||||
return runTaskLater(plugin, runnable, 0L);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
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) {
|
||||
return runTaskLaterAsynchronously(plugin, runnable, 0l);
|
||||
return runTaskLaterAsynchronously(plugin, runnable, 0L);
|
||||
}
|
||||
|
||||
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) {
|
||||
return runTaskTimer(plugin, runnable, delay, -1l);
|
||||
return runTaskTimer(plugin, runnable, delay, CraftTask.NO_REPEATING);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
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) {
|
||||
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) {
|
||||
|
@ -124,13 +124,13 @@ public class CraftScheduler implements BukkitScheduler {
|
|||
|
||||
public BukkitTask runTaskTimer(Plugin plugin, Runnable runnable, long delay, long period) {
|
||||
validate(plugin, runnable);
|
||||
if (delay < 0l) {
|
||||
if (delay < 0L) {
|
||||
delay = 0;
|
||||
}
|
||||
if (period == 0l) {
|
||||
period = 1l;
|
||||
} else if (period < -1l) {
|
||||
period = -1l;
|
||||
if (period == CraftTask.ERROR) {
|
||||
period = 1L;
|
||||
} else if (period < CraftTask.NO_REPEATING) {
|
||||
period = CraftTask.NO_REPEATING;
|
||||
}
|
||||
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) {
|
||||
validate(plugin, runnable);
|
||||
if (delay < 0l) {
|
||||
if (delay < 0L) {
|
||||
delay = 0;
|
||||
}
|
||||
if (period == 0l) {
|
||||
period = 1l;
|
||||
} else if (period < -1l) {
|
||||
period = -1l;
|
||||
if (period == CraftTask.ERROR) {
|
||||
period = 1L;
|
||||
} else if (period < CraftTask.NO_REPEATING) {
|
||||
period = CraftTask.NO_REPEATING;
|
||||
}
|
||||
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) {
|
||||
validate(plugin, task);
|
||||
final CraftFuture<T> future = new CraftFuture<T>(task, plugin, nextId());
|
||||
handle(future, 0l);
|
||||
handle(future, 0L);
|
||||
return future;
|
||||
}
|
||||
|
||||
|
@ -190,7 +190,7 @@ public class CraftScheduler implements BukkitScheduler {
|
|||
}
|
||||
return false;
|
||||
}});
|
||||
handle(task, 0l);
|
||||
handle(task, 0L);
|
||||
for (CraftTask taskPending = head.getNext(); taskPending != null; taskPending = taskPending.getNext()) {
|
||||
if (taskPending == task) {
|
||||
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()) {
|
||||
if (taskPending == task) {
|
||||
break;
|
||||
|
@ -255,7 +255,7 @@ public class CraftScheduler implements BukkitScheduler {
|
|||
CraftScheduler.this.temp.clear();
|
||||
}
|
||||
});
|
||||
handle(task, 0l);
|
||||
handle(task, 0L);
|
||||
for (CraftTask taskPending = head.getNext(); taskPending != null; taskPending = taskPending.getNext()) {
|
||||
if (taskPending == task) {
|
||||
break;
|
||||
|
@ -284,11 +284,11 @@ public class CraftScheduler implements BukkitScheduler {
|
|||
}
|
||||
for (CraftTask task = head.getNext(); task != null; task = task.getNext()) {
|
||||
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);
|
||||
return task != null && task.getPeriod() >= -1l;
|
||||
return task != null && task.getPeriod() >= CraftTask.NO_REPEATING;
|
||||
}
|
||||
|
||||
public List<BukkitWorker> getActiveWorkers() {
|
||||
|
@ -318,13 +318,13 @@ public class CraftScheduler implements BukkitScheduler {
|
|||
|
||||
final ArrayList<BukkitTask> pending = new ArrayList<BukkitTask>();
|
||||
for (CraftTask task : runners.values()) {
|
||||
if (task.getPeriod() >= -1l) {
|
||||
if (task.getPeriod() >= CraftTask.NO_REPEATING) {
|
||||
pending.add(task);
|
||||
}
|
||||
}
|
||||
|
||||
for (final CraftTask task : truePending) {
|
||||
if (task.getPeriod() >= -1l && !pending.contains(task)) {
|
||||
if (task.getPeriod() >= CraftTask.NO_REPEATING && !pending.contains(task)) {
|
||||
pending.add(task);
|
||||
}
|
||||
}
|
||||
|
@ -340,7 +340,7 @@ public class CraftScheduler implements BukkitScheduler {
|
|||
parsePending();
|
||||
while (isReady(currentTick)) {
|
||||
final CraftTask task = pending.remove();
|
||||
if (task.getPeriod() < -1l) {
|
||||
if (task.getPeriod() < CraftTask.NO_REPEATING) {
|
||||
if (task.isSync()) {
|
||||
runners.remove(task.getTaskId(), task);
|
||||
}
|
||||
|
@ -413,7 +413,7 @@ public class CraftScheduler implements BukkitScheduler {
|
|||
for (; task != null; task = (lastTask = task).getNext()) {
|
||||
if (task.getTaskId() == -1) {
|
||||
task.run();
|
||||
} else if (task.getPeriod() >= -1l) {
|
||||
} else if (task.getPeriod() >= CraftTask.NO_REPEATING) {
|
||||
pending.add(task);
|
||||
runners.put(task.getTaskId(), task);
|
||||
}
|
||||
|
|
|
@ -8,6 +8,11 @@ import org.bukkit.scheduler.BukkitTask;
|
|||
class CraftTask implements BukkitTask, Runnable {
|
||||
|
||||
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>
|
||||
* -2 means cancel <br>
|
||||
|
@ -23,11 +28,11 @@ class CraftTask implements BukkitTask, Runnable {
|
|||
private final int id;
|
||||
|
||||
CraftTask() {
|
||||
this(null, null, -1, -1);
|
||||
this(null, null, CraftTask.NO_REPEATING, CraftTask.NO_REPEATING);
|
||||
}
|
||||
|
||||
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) {
|
||||
|
@ -83,7 +88,7 @@ class CraftTask implements BukkitTask, Runnable {
|
|||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return (period == -2l);
|
||||
return (period == CraftTask.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
|
||||
*/
|
||||
boolean cancel0() {
|
||||
setPeriod(-2l);
|
||||
setPeriod(CraftTask.CANCEL);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue