mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-15 01:08:35 +00:00
Lambda's and References and Cleanups! Oh My!
This commit is contained in:
@ -73,120 +73,117 @@ public class SetQueue {
|
||||
|
||||
public SetQueue() {
|
||||
tasks = new ConcurrentLinkedDeque<>();
|
||||
activeQueues = new ConcurrentLinkedDeque();
|
||||
activeQueues = new ConcurrentLinkedDeque<>();
|
||||
inactiveQueues = new ConcurrentLinkedDeque<>();
|
||||
if (TaskManager.IMP == null) return;
|
||||
TaskManager.IMP.repeat(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
boolean empty = (inactiveQueues.isEmpty() && activeQueues.isEmpty());
|
||||
boolean emptyTasks = tasks.isEmpty();
|
||||
if (emptyTasks && empty) {
|
||||
last = now;
|
||||
runEmptyTasks();
|
||||
return;
|
||||
}
|
||||
TaskManager.IMP.repeat(() -> {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
boolean empty = (inactiveQueues.isEmpty() && activeQueues.isEmpty());
|
||||
boolean emptyTasks = tasks.isEmpty();
|
||||
if (emptyTasks && empty) {
|
||||
last = now;
|
||||
runEmptyTasks();
|
||||
return;
|
||||
}
|
||||
|
||||
targetTPS = 18 - Math.max(Settings.IMP.QUEUE.EXTRA_TIME_MS * 0.05, 0);
|
||||
targetTPS = 18 - Math.max(Settings.IMP.QUEUE.EXTRA_TIME_MS * 0.05, 0);
|
||||
|
||||
long diff = (50 + SetQueue.this.last) - (SetQueue.this.last = now);
|
||||
long absDiff = Math.abs(diff);
|
||||
if (diff == 0) {
|
||||
allocate = Math.min(50, allocate + 1);
|
||||
} else if (diff < 0) {
|
||||
allocate = Math.max(5, allocate + diff);
|
||||
} else if (!Fawe.get().getTimer().isAbove(targetTPS)) {
|
||||
allocate = Math.max(5, allocate - 1);
|
||||
}
|
||||
long diff = (50 + SetQueue.this.last) - (SetQueue.this.last = now);
|
||||
long absDiff = Math.abs(diff);
|
||||
if (diff == 0) {
|
||||
allocate = Math.min(50, allocate + 1);
|
||||
} else if (diff < 0) {
|
||||
allocate = Math.max(5, allocate + diff);
|
||||
} else if (!Fawe.get().getTimer().isAbove(targetTPS)) {
|
||||
allocate = Math.max(5, allocate - 1);
|
||||
}
|
||||
|
||||
long currentAllocate = allocate - absDiff;
|
||||
long currentAllocate = allocate - absDiff;
|
||||
|
||||
if (!emptyTasks) {
|
||||
long taskAllocate = activeQueues.isEmpty() ? currentAllocate : 1 + (currentAllocate >> 1);
|
||||
long used = 0;
|
||||
boolean wait = false;
|
||||
do {
|
||||
Runnable task = tasks.poll();
|
||||
if (task == null) {
|
||||
if (wait) {
|
||||
synchronized (tasks) {
|
||||
tasks.wait(1);
|
||||
}
|
||||
task = tasks.poll();
|
||||
wait = false;
|
||||
} else {
|
||||
break;
|
||||
if (!emptyTasks) {
|
||||
long taskAllocate = activeQueues.isEmpty() ? currentAllocate : 1 + (currentAllocate >> 1);
|
||||
long used = 0;
|
||||
boolean wait = false;
|
||||
do {
|
||||
Runnable task = tasks.poll();
|
||||
if (task == null) {
|
||||
if (wait) {
|
||||
synchronized (tasks) {
|
||||
tasks.wait(1);
|
||||
}
|
||||
}
|
||||
if (task != null) {
|
||||
task.run();
|
||||
wait = true;
|
||||
}
|
||||
} while ((used = System.currentTimeMillis() - now) < taskAllocate);
|
||||
currentAllocate -= used;
|
||||
}
|
||||
|
||||
if (empty) {
|
||||
runEmptyTasks();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!MemUtil.isMemoryFree()) {
|
||||
final int mem = MemUtil.calculateMemory();
|
||||
if (mem != Integer.MAX_VALUE) {
|
||||
allocate = Math.max(5, allocate - 1);
|
||||
if ((mem <= 1) && Settings.IMP.PREVENT_CRASHES) {
|
||||
for (FaweQueue queue : getAllQueues()) {
|
||||
queue.saveMemory();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (SetQueue.this.forceChunkSet()) {
|
||||
System.gc();
|
||||
task = tasks.poll();
|
||||
wait = false;
|
||||
} else {
|
||||
SetQueue.this.runEmptyTasks();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (task != null) {
|
||||
task.run();
|
||||
wait = true;
|
||||
}
|
||||
} while ((used = System.currentTimeMillis() - now) < taskAllocate);
|
||||
currentAllocate -= used;
|
||||
}
|
||||
|
||||
if (empty) {
|
||||
runEmptyTasks();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!MemUtil.isMemoryFree()) {
|
||||
final int mem = MemUtil.calculateMemory();
|
||||
if (mem != Integer.MAX_VALUE) {
|
||||
allocate = Math.max(5, allocate - 1);
|
||||
if ((mem <= 1) && Settings.IMP.PREVENT_CRASHES) {
|
||||
for (FaweQueue queue : getAllQueues()) {
|
||||
queue.saveMemory();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
FaweQueue queue = getNextQueue();
|
||||
if (queue == null) {
|
||||
if (SetQueue.this.forceChunkSet()) {
|
||||
System.gc();
|
||||
} else {
|
||||
SetQueue.this.runEmptyTasks();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
long time = (long) Settings.IMP.QUEUE.EXTRA_TIME_MS + currentAllocate - System.currentTimeMillis() + now;
|
||||
// Disable the async catcher as it can't discern async vs parallel
|
||||
boolean parallel = Settings.IMP.QUEUE.PARALLEL_THREADS > 1;
|
||||
queue.startSet(parallel);
|
||||
try {
|
||||
if (!queue.next(Settings.IMP.QUEUE.PARALLEL_THREADS, time) && queue.getStage() == QueueStage.ACTIVE) {
|
||||
queue.setStage(QueueStage.NONE);
|
||||
queue.runTasks();
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
pool.awaitQuiescence(Settings.IMP.QUEUE.DISCARD_AFTER_MS, TimeUnit.MILLISECONDS);
|
||||
completer = new ExecutorCompletionService(pool);
|
||||
e.printStackTrace();
|
||||
FaweQueue queue = getNextQueue();
|
||||
if (queue == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
long time = (long) Settings.IMP.QUEUE.EXTRA_TIME_MS + currentAllocate - System.currentTimeMillis() + now;
|
||||
// Disable the async catcher as it can't discern async vs parallel
|
||||
boolean parallel = Settings.IMP.QUEUE.PARALLEL_THREADS > 1;
|
||||
queue.startSet(parallel);
|
||||
try {
|
||||
if (!queue.next(Settings.IMP.QUEUE.PARALLEL_THREADS, time) && queue.getStage() == QueueStage.ACTIVE) {
|
||||
queue.setStage(QueueStage.NONE);
|
||||
queue.runTasks();
|
||||
}
|
||||
if (pool.getQueuedSubmissionCount() != 0 || pool.getRunningThreadCount() != 0 || pool.getQueuedTaskCount() != 0) {
|
||||
} catch (Throwable e) {
|
||||
pool.awaitQuiescence(Settings.IMP.QUEUE.DISCARD_AFTER_MS, TimeUnit.MILLISECONDS);
|
||||
completer = new ExecutorCompletionService(pool);
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (pool.getQueuedSubmissionCount() != 0 || pool.getRunningThreadCount() != 0 || pool.getQueuedTaskCount() != 0) {
|
||||
// if (Fawe.get().isJava8())
|
||||
{
|
||||
pool.awaitQuiescence(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
{
|
||||
pool.awaitQuiescence(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
// else {
|
||||
// pool.shutdown();
|
||||
// pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
|
||||
// pool = new ForkJoinPool();
|
||||
// completer = new ExecutorCompletionService(pool);
|
||||
// }
|
||||
}
|
||||
queue.endSet(parallel);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
queue.endSet(parallel);
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, 1);
|
||||
}
|
||||
@ -264,7 +261,7 @@ public class SetQueue {
|
||||
|
||||
public void flush(FaweQueue queue) {
|
||||
int parallelThreads;
|
||||
if (Fawe.get().isMainThread()) {
|
||||
if (Fawe.isMainThread()) {
|
||||
parallelThreads = Settings.IMP.QUEUE.PARALLEL_THREADS;
|
||||
Settings.IMP.QUEUE.PARALLEL_THREADS = 1;
|
||||
} else {
|
||||
|
@ -124,14 +124,10 @@ public abstract class TaskManager {
|
||||
}
|
||||
for (i = 0; i < threads.length; i++) {
|
||||
final Runnable[] toRun = split[i];
|
||||
Thread thread = threads[i] = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (int j = 0; j < toRun.length; j++) {
|
||||
Runnable run = toRun[j];
|
||||
if (run != null) {
|
||||
run.run();
|
||||
}
|
||||
Thread thread = threads[i] = new Thread(() -> {
|
||||
for (Runnable run : toRun) {
|
||||
if (run != null) {
|
||||
run.run();
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -420,7 +416,7 @@ public abstract class TaskManager {
|
||||
} catch (InterruptedException e) {
|
||||
MainUtil.handleError(e);
|
||||
}
|
||||
if (run.value != null && run.value instanceof RuntimeException) {
|
||||
if (run.value instanceof RuntimeException) {
|
||||
throw (RuntimeException) run.value;
|
||||
}
|
||||
return (T) run.value;
|
||||
|
@ -124,7 +124,7 @@ public class WEManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!removed) return regions.toArray(new Region[regions.size()]);
|
||||
if (!removed) return regions.toArray(new Region[0]);
|
||||
masks.clear();
|
||||
}
|
||||
}
|
||||
@ -146,7 +146,7 @@ public class WEManager {
|
||||
}
|
||||
if (!tmpMasks.isEmpty()) {
|
||||
masks = tmpMasks;
|
||||
regions = masks.stream().map(mask -> mask.getRegion()).collect(Collectors.toSet());
|
||||
regions = masks.stream().map(FaweMask::getRegion).collect(Collectors.toSet());
|
||||
} else {
|
||||
regions.addAll(backupRegions);
|
||||
}
|
||||
@ -155,7 +155,7 @@ public class WEManager {
|
||||
} else {
|
||||
player.deleteMeta("lastMask");
|
||||
}
|
||||
return regions.toArray(new Region[regions.size()]);
|
||||
return regions.toArray(new Region[0]);
|
||||
}
|
||||
|
||||
|
||||
@ -179,36 +179,22 @@ public class WEManager {
|
||||
|
||||
public boolean delay(final FawePlayer<?> player, final String command) {
|
||||
final long start = System.currentTimeMillis();
|
||||
return this.delay(player, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
if ((System.currentTimeMillis() - start) > 1000) {
|
||||
BBC.WORLDEDIT_RUN.send(FawePlayer.wrap(player));
|
||||
}
|
||||
TaskManager.IMP.task(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final long start = System.currentTimeMillis();
|
||||
player.executeCommand(command.substring(1));
|
||||
TaskManager.IMP.later(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
SetQueue.IMP.addEmptyTask(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if ((System.currentTimeMillis() - start) > 1000) {
|
||||
BBC.WORLDEDIT_COMPLETE.send(FawePlayer.wrap(player));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 2);
|
||||
}
|
||||
});
|
||||
} catch (final Exception e) {
|
||||
MainUtil.handleError(e);
|
||||
return this.delay(player, () -> {
|
||||
try {
|
||||
if ((System.currentTimeMillis() - start) > 1000) {
|
||||
BBC.WORLDEDIT_RUN.send(FawePlayer.wrap(player));
|
||||
}
|
||||
TaskManager.IMP.task(() -> {
|
||||
final long start1 = System.currentTimeMillis();
|
||||
player.executeCommand(command.substring(1));
|
||||
TaskManager.IMP.later(() -> SetQueue.IMP.addEmptyTask(() -> {
|
||||
if ((System.currentTimeMillis() - start1) > 1000) {
|
||||
BBC.WORLDEDIT_COMPLETE.send(FawePlayer.wrap(player));
|
||||
}
|
||||
}), 2);
|
||||
});
|
||||
} catch (final Exception e) {
|
||||
MainUtil.handleError(e);
|
||||
}
|
||||
}, false, false);
|
||||
}
|
||||
|
@ -167,45 +167,33 @@ public class TaskBuilder extends Metadatable {
|
||||
}
|
||||
|
||||
public TaskBuilder abortIfTrue(final Runnable run) {
|
||||
tasks.add(RunnableTask.adapt(new Task<Boolean, Boolean>() {
|
||||
@Override
|
||||
public Boolean run(Boolean previous) {
|
||||
if (previous == Boolean.TRUE) run.run();
|
||||
return previous == Boolean.TRUE;
|
||||
}
|
||||
tasks.add(RunnableTask.adapt((Task<Boolean, Boolean>) previous -> {
|
||||
if (previous == Boolean.TRUE) run.run();
|
||||
return previous == Boolean.TRUE;
|
||||
}, TaskType.ABORT));
|
||||
return this;
|
||||
}
|
||||
|
||||
public TaskBuilder abortIfNull(final Runnable run) {
|
||||
tasks.add(RunnableTask.adapt(new Task<Boolean, Object>() {
|
||||
@Override
|
||||
public Boolean run(Object previous) {
|
||||
if (previous == null) run.run();
|
||||
return previous == null;
|
||||
}
|
||||
tasks.add(RunnableTask.adapt((Task<Boolean, Object>) previous -> {
|
||||
if (previous == null) run.run();
|
||||
return previous == null;
|
||||
}, TaskType.ABORT));
|
||||
return this;
|
||||
}
|
||||
|
||||
public TaskBuilder abortIfEqual(final Runnable run, final Object other) {
|
||||
tasks.add(RunnableTask.adapt(new Task<Boolean, Object>() {
|
||||
@Override
|
||||
public Boolean run(Object previous) {
|
||||
if (Objects.equals(previous, other)) run.run();
|
||||
return Objects.equals(previous, other);
|
||||
}
|
||||
tasks.add(RunnableTask.adapt((Task<Boolean, Object>) previous -> {
|
||||
if (Objects.equals(previous, other)) run.run();
|
||||
return Objects.equals(previous, other);
|
||||
}, TaskType.ABORT));
|
||||
return this;
|
||||
}
|
||||
|
||||
public TaskBuilder abortIfNotEqual(final Runnable run, final Object other) {
|
||||
tasks.add(RunnableTask.adapt(new Task<Boolean, Object>() {
|
||||
@Override
|
||||
public Boolean run(Object previous) {
|
||||
if (!Objects.equals(previous, other)) run.run();
|
||||
return !Objects.equals(previous, other);
|
||||
}
|
||||
tasks.add(RunnableTask.adapt((Task<Boolean, Object>) previous -> {
|
||||
if (!Objects.equals(previous, other)) run.run();
|
||||
return !Objects.equals(previous, other);
|
||||
}, TaskType.ABORT));
|
||||
return this;
|
||||
}
|
||||
@ -215,12 +203,7 @@ public class TaskBuilder extends Metadatable {
|
||||
* - As opposed to trying to using the current thread
|
||||
*/
|
||||
public void buildAsync() {
|
||||
TaskManager.IMP.async(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
build();
|
||||
}
|
||||
});
|
||||
TaskManager.IMP.async(this::build);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -240,20 +223,10 @@ public class TaskBuilder extends Metadatable {
|
||||
case SYNC:
|
||||
case ABORT:
|
||||
case SYNC_PARALLEL:
|
||||
TaskManager.IMP.later(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
build();
|
||||
}
|
||||
}, task.getDelay(result));
|
||||
TaskManager.IMP.later(this::build, task.getDelay(result));
|
||||
return;
|
||||
default:
|
||||
TaskManager.IMP.laterAsync(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
build();
|
||||
}
|
||||
}, task.getDelay(result));
|
||||
TaskManager.IMP.laterAsync(this::build, task.getDelay(result));
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -274,12 +247,7 @@ public class TaskBuilder extends Metadatable {
|
||||
case ASYNC:
|
||||
case ASYNC_PARALLEL:
|
||||
if (Fawe.isMainThread()) {
|
||||
TaskManager.IMP.async(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
build();
|
||||
}
|
||||
});
|
||||
TaskManager.IMP.async(this::build);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
@ -484,24 +452,21 @@ public class TaskBuilder extends Metadatable {
|
||||
|
||||
public Object execSplit(final Object previous) {
|
||||
this.value = previous;
|
||||
final Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
synchronized (asyncWaitLock) {
|
||||
asyncWaitLock.notifyAll();
|
||||
asyncWaitLock.wait(Long.MAX_VALUE);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
exec(previous);
|
||||
finished = true;
|
||||
waitingAsync = true;
|
||||
waitingSync = false;
|
||||
synchronized (syncWaitLock) {
|
||||
syncWaitLock.notifyAll();
|
||||
final Thread thread = new Thread(() -> {
|
||||
try {
|
||||
synchronized (asyncWaitLock) {
|
||||
asyncWaitLock.notifyAll();
|
||||
asyncWaitLock.wait(Long.MAX_VALUE);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
exec(previous);
|
||||
finished = true;
|
||||
waitingAsync = true;
|
||||
waitingSync = false;
|
||||
synchronized (syncWaitLock) {
|
||||
syncWaitLock.notifyAll();
|
||||
}
|
||||
});
|
||||
try {
|
||||
|
Reference in New Issue
Block a user