This commit is contained in:
Anuken
2020-04-30 21:58:48 -04:00
parent 8de0ca7d6e
commit 273f4590df
9 changed files with 189 additions and 37 deletions

View File

@ -1,6 +1,8 @@
package mindustry.async;
import arc.*;
import arc.struct.*;
import mindustry.game.EventType.*;
import java.util.concurrent.*;
@ -12,15 +14,28 @@ public class AsyncLogic{
private Array<Future<?>> futures = new Array<>();
private ExecutorService executor = Executors.newFixedThreadPool(processes.size, r -> {
Thread thread = new Thread(r, "AsyncExecutor-Thread");
Thread thread = new Thread(r, "AsyncLogic-Thread");
thread.setDaemon(true);
thread.setUncaughtExceptionHandler((t, e) -> {
e.printStackTrace();
//TODO crash!
});
thread.setUncaughtExceptionHandler((t, e) -> Core.app.post(() -> { throw new RuntimeException(e); }));
return thread;
});
public AsyncLogic(){
Events.on(WorldLoadEvent.class, e -> {
complete();
for(AsyncProcess p : processes){
p.init();
}
});
Events.on(ResetEvent.class, e -> {
complete();
for(AsyncProcess p : processes){
p.reset();
}
});
}
public void begin(){
//sync begin
for(AsyncProcess p : processes){
@ -36,6 +51,15 @@ public class AsyncLogic{
}
public void end(){
complete();
//sync end (flush data)
for(AsyncProcess p : processes){
p.end();
}
}
private void complete(){
//wait for all threads to stop processing
for(Future future : futures){
try{
@ -45,11 +69,7 @@ public class AsyncLogic{
}
}
//clear processed futures
futures.clear();
//sync end (flush data)
for(AsyncProcess p : processes){
p.end();
}
}
}