mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-04-06 07:46:08 +00:00
Move a lot of utilities in engine to util because they are utilities
This commit is contained in:
104
src/main/java/com/volmit/iris/util/parallel/BurstExecutor.java
Normal file
104
src/main/java/com/volmit/iris/util/parallel/BurstExecutor.java
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.parallel;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
public class BurstExecutor {
|
||||
private final ExecutorService executor;
|
||||
private final KList<CompletableFuture<Void>> futures;
|
||||
|
||||
public BurstExecutor(ExecutorService executor, int burstSizeEstimate) {
|
||||
this.executor = executor;
|
||||
futures = new KList<CompletableFuture<Void>>(burstSizeEstimate);
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public CompletableFuture<Void> queue(Runnable r) {
|
||||
synchronized (futures) {
|
||||
CompletableFuture<Void> c = CompletableFuture.runAsync(r, executor);
|
||||
futures.add(c);
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
public BurstExecutor queue(KList<Runnable> r) {
|
||||
synchronized (futures) {
|
||||
for (Runnable i : r) {
|
||||
CompletableFuture<Void> c = CompletableFuture.runAsync(i, executor);
|
||||
futures.add(c);
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public BurstExecutor queue(Runnable[] r) {
|
||||
synchronized (futures) {
|
||||
for (Runnable i : r) {
|
||||
CompletableFuture<Void> c = CompletableFuture.runAsync(i, executor);
|
||||
futures.add(c);
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public void complete() {
|
||||
synchronized (futures) {
|
||||
if (futures.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get();
|
||||
futures.clear();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
Iris.reportError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean complete(long maxDur) {
|
||||
synchronized (futures) {
|
||||
if (futures.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
try {
|
||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(maxDur, TimeUnit.MILLISECONDS);
|
||||
} catch (TimeoutException e) {
|
||||
return false;
|
||||
}
|
||||
futures.clear();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
Iris.reportError(e);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
30
src/main/java/com/volmit/iris/util/parallel/BurstedHunk.java
Normal file
30
src/main/java/com/volmit/iris/util/parallel/BurstedHunk.java
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.parallel;
|
||||
|
||||
|
||||
import com.volmit.iris.util.hunk.Hunk;
|
||||
|
||||
public interface BurstedHunk<T> extends Hunk<T> {
|
||||
int getOffsetX();
|
||||
|
||||
int getOffsetY();
|
||||
|
||||
int getOffsetZ();
|
||||
}
|
||||
99
src/main/java/com/volmit/iris/util/parallel/GridLock.java
Normal file
99
src/main/java/com/volmit/iris/util/parallel/GridLock.java
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.parallel;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.util.hunk.Hunk;
|
||||
import com.volmit.iris.util.function.NastyRunnable;
|
||||
import com.volmit.iris.util.io.IORunnable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class GridLock {
|
||||
private final Hunk<ReentrantLock> locks;
|
||||
|
||||
public GridLock(int x, int z) {
|
||||
this.locks = Hunk.newAtomicHunk(x, 1, z);
|
||||
locks.iterateSync((a, b, c) -> locks.set(a, b, c, new ReentrantLock()));
|
||||
}
|
||||
|
||||
public void with(int x, int z, Runnable r) {
|
||||
lock(x, z);
|
||||
r.run();
|
||||
unlock(x, z);
|
||||
}
|
||||
|
||||
public void withNasty(int x, int z, NastyRunnable r) throws Throwable {
|
||||
lock(x, z);
|
||||
r.run();
|
||||
unlock(x, z);
|
||||
}
|
||||
|
||||
public void withIO(int x, int z, IORunnable r) throws IOException {
|
||||
lock(x, z);
|
||||
r.run();
|
||||
unlock(x, z);
|
||||
}
|
||||
|
||||
public <T> T withResult(int x, int z, Supplier<T> r) {
|
||||
lock(x, z);
|
||||
T t = r.get();
|
||||
unlock(x, z);
|
||||
return t;
|
||||
}
|
||||
|
||||
public void withAll(Runnable r) {
|
||||
locks.iterateSync((a, b, c, d) -> d.lock());
|
||||
r.run();
|
||||
locks.iterateSync((a, b, c, d) -> d.unlock());
|
||||
}
|
||||
|
||||
public <T> T withAllResult(Supplier<T> r) {
|
||||
locks.iterateSync((a, b, c, d) -> d.lock());
|
||||
T t = r.get();
|
||||
locks.iterateSync((a, b, c, d) -> d.unlock());
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
public boolean tryLock(int x, int z) {
|
||||
return locks.get(x, 0, z).tryLock();
|
||||
}
|
||||
|
||||
public boolean tryLock(int x, int z, long timeout) {
|
||||
try {
|
||||
return locks.get(x, 0, z).tryLock(timeout, TimeUnit.MILLISECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
Iris.reportError(e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void lock(int x, int z) {
|
||||
locks.get(x, 0, z).lock();
|
||||
}
|
||||
|
||||
public void unlock(int x, int z) {
|
||||
locks.get(x, 0, z).unlock();
|
||||
}
|
||||
}
|
||||
109
src/main/java/com/volmit/iris/util/parallel/HyperLock.java
Normal file
109
src/main/java/com/volmit/iris/util/parallel/HyperLock.java
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.parallel;
|
||||
|
||||
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.engine.cache.Cache;
|
||||
import com.volmit.iris.util.function.NastyRunnable;
|
||||
import com.volmit.iris.util.io.IORunnable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class HyperLock {
|
||||
private final ConcurrentLinkedHashMap<Long, ReentrantLock> locks;
|
||||
private final BiFunction<? super Long, ? super ReentrantLock, ? extends ReentrantLock> accessor;
|
||||
|
||||
public HyperLock() {
|
||||
this(1024, false);
|
||||
}
|
||||
|
||||
public HyperLock(int capacity) {
|
||||
this(capacity, false);
|
||||
}
|
||||
|
||||
public HyperLock(int capacity, boolean fair) {
|
||||
locks = new ConcurrentLinkedHashMap.Builder<Long, ReentrantLock>()
|
||||
.initialCapacity(capacity)
|
||||
.maximumWeightedCapacity(capacity)
|
||||
.listener((k, v) -> {
|
||||
if (v.isLocked() || v.isHeldByCurrentThread()) {
|
||||
Iris.warn("InfiniLock Eviction of " + k + " still has locks on it!");
|
||||
}
|
||||
})
|
||||
.concurrencyLevel(32)
|
||||
.build();
|
||||
accessor = (k, v) -> v == null ? new ReentrantLock(fair) : v;
|
||||
}
|
||||
|
||||
public void with(int x, int z, Runnable r) {
|
||||
lock(x, z);
|
||||
r.run();
|
||||
unlock(x, z);
|
||||
}
|
||||
|
||||
public void withNasty(int x, int z, NastyRunnable r) throws Throwable {
|
||||
lock(x, z);
|
||||
r.run();
|
||||
unlock(x, z);
|
||||
}
|
||||
|
||||
public void withIO(int x, int z, IORunnable r) throws IOException {
|
||||
lock(x, z);
|
||||
r.run();
|
||||
unlock(x, z);
|
||||
}
|
||||
|
||||
public <T> T withResult(int x, int z, Supplier<T> r) {
|
||||
lock(x, z);
|
||||
T t = r.get();
|
||||
unlock(x, z);
|
||||
return t;
|
||||
}
|
||||
|
||||
public boolean tryLock(int x, int z) {
|
||||
return getLock(x, z).tryLock();
|
||||
}
|
||||
|
||||
public boolean tryLock(int x, int z, long timeout) {
|
||||
try {
|
||||
return getLock(x, z).tryLock(timeout, TimeUnit.MILLISECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
Iris.reportError(e);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private ReentrantLock getLock(int x, int z) {
|
||||
return locks.compute(Cache.key(x, z), accessor);
|
||||
}
|
||||
|
||||
public void lock(int x, int z) {
|
||||
getLock(x, z).lock();
|
||||
}
|
||||
|
||||
public void unlock(int x, int z) {
|
||||
getLock(x, z).unlock();
|
||||
}
|
||||
}
|
||||
174
src/main/java/com/volmit/iris/util/parallel/MultiBurst.java
Normal file
174
src/main/java/com/volmit/iris/util/parallel/MultiBurst.java
Normal file
@@ -0,0 +1,174 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.parallel;
|
||||
|
||||
import com.volmit.iris.Iris;
|
||||
import com.volmit.iris.core.IrisSettings;
|
||||
import com.volmit.iris.util.collection.KList;
|
||||
import com.volmit.iris.util.math.M;
|
||||
import com.volmit.iris.util.scheduling.J;
|
||||
import com.volmit.iris.util.scheduling.Looper;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
public class MultiBurst {
|
||||
public static final MultiBurst burst = new MultiBurst("Iris", IrisSettings.get().getConcurrency().getMiscThreadPriority(), IrisSettings.getThreadCount(IrisSettings.get().getConcurrency().getMiscThreadCount()));
|
||||
private ExecutorService service;
|
||||
private final Looper heartbeat;
|
||||
private final AtomicLong last;
|
||||
private int tid;
|
||||
private final String name;
|
||||
private final int tc;
|
||||
private final int priority;
|
||||
|
||||
public MultiBurst(int tc) {
|
||||
this("Iris", 6, tc);
|
||||
}
|
||||
|
||||
public MultiBurst(String name, int priority, int tc) {
|
||||
this.name = name;
|
||||
this.priority = priority;
|
||||
this.tc = tc;
|
||||
last = new AtomicLong(M.ms());
|
||||
heartbeat = new Looper() {
|
||||
@Override
|
||||
protected long loop() {
|
||||
if (M.ms() - last.get() > TimeUnit.MINUTES.toMillis(1) && service != null) {
|
||||
service.shutdown();
|
||||
service = null;
|
||||
Iris.debug("Shutting down MultiBurst Pool " + getName() + " to conserve resource.");
|
||||
}
|
||||
|
||||
return 60000;
|
||||
}
|
||||
};
|
||||
heartbeat.setName(name);
|
||||
heartbeat.start();
|
||||
}
|
||||
|
||||
private synchronized ExecutorService getService() {
|
||||
last.set(M.ms());
|
||||
if (service == null || service.isShutdown()) {
|
||||
service = Executors.newFixedThreadPool(Math.max(tc, 1), r -> {
|
||||
tid++;
|
||||
Thread t = new Thread(r);
|
||||
t.setName(name + " " + tid);
|
||||
t.setPriority(priority);
|
||||
t.setUncaughtExceptionHandler((et, e) ->
|
||||
{
|
||||
Iris.info("Exception encountered in " + et.getName());
|
||||
e.printStackTrace();
|
||||
});
|
||||
|
||||
return t;
|
||||
});
|
||||
Iris.debug("Started MultiBurst Pool " + name + " with " + tc + " threads at " + priority + " priority.");
|
||||
}
|
||||
|
||||
return service;
|
||||
}
|
||||
|
||||
public void burst(Runnable... r) {
|
||||
burst(r.length).queue(r).complete();
|
||||
}
|
||||
|
||||
public void burst(KList<Runnable> r) {
|
||||
burst(r.size()).queue(r).complete();
|
||||
}
|
||||
|
||||
public void sync(Runnable... r) {
|
||||
for (Runnable i : r) {
|
||||
i.run();
|
||||
}
|
||||
}
|
||||
|
||||
public BurstExecutor burst(int estimate) {
|
||||
return new BurstExecutor(getService(), estimate);
|
||||
}
|
||||
|
||||
public BurstExecutor burst() {
|
||||
return burst(16);
|
||||
}
|
||||
|
||||
public <T> Future<T> lazySubmit(Callable<T> o) {
|
||||
return getService().submit(o);
|
||||
}
|
||||
|
||||
public void lazy(Runnable o) {
|
||||
getService().execute(o);
|
||||
}
|
||||
|
||||
public Future<?> future(Runnable o) {
|
||||
return getService().submit(o);
|
||||
}
|
||||
|
||||
public CompletableFuture<?> complete(Runnable o) {
|
||||
return CompletableFuture.runAsync(o, getService());
|
||||
}
|
||||
|
||||
public void shutdownNow() {
|
||||
Iris.debug("Shutting down MultiBurst Pool " + heartbeat.getName() + ".");
|
||||
heartbeat.interrupt();
|
||||
|
||||
if (service != null) {
|
||||
service.shutdownNow().forEach(Runnable::run);
|
||||
}
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
Iris.debug("Shutting down MultiBurst Pool " + heartbeat.getName() + ".");
|
||||
heartbeat.interrupt();
|
||||
|
||||
if (service != null) {
|
||||
service.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
public void shutdownLater() {
|
||||
if (service != null) {
|
||||
service.submit(() -> {
|
||||
J.sleep(3000);
|
||||
Iris.debug("Shutting down MultiBurst Pool " + heartbeat.getName() + ".");
|
||||
|
||||
if (service != null) {
|
||||
service.shutdown();
|
||||
}
|
||||
});
|
||||
|
||||
heartbeat.interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
public void shutdownAndAwait() {
|
||||
Iris.debug("Shutting down MultiBurst Pool " + heartbeat.getName() + ".");
|
||||
heartbeat.interrupt();
|
||||
if (service != null) {
|
||||
service.shutdown();
|
||||
try {
|
||||
while (!service.awaitTermination(10, TimeUnit.SECONDS)) {
|
||||
Iris.info("Still waiting to shutdown burster...");
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
Iris.reportError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2021 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.volmit.iris.util.parallel;
|
||||
|
||||
import com.volmit.iris.util.function.NastyRunnable;
|
||||
import com.volmit.iris.util.io.IORunnable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class NOOPGridLock extends GridLock {
|
||||
public NOOPGridLock(int x, int z) {
|
||||
super(x, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void with(int x, int z, Runnable r) {
|
||||
r.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void withNasty(int x, int z, NastyRunnable r) throws Throwable {
|
||||
r.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void withIO(int x, int z, IORunnable r) throws IOException {
|
||||
r.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T withResult(int x, int z, Supplier<T> r) {
|
||||
return r.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void withAll(Runnable r) {
|
||||
r.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T withAllResult(Supplier<T> r) {
|
||||
return r.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLock(int x, int z) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryLock(int x, int z, long timeout) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lock(int x, int z) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unlock(int x, int z) {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user