Removed multithreading

This commit is contained in:
Anuken
2018-11-11 10:32:00 -05:00
parent 53bf622065
commit 65a48b324d
5 changed files with 8 additions and 168 deletions

View File

@@ -172,7 +172,6 @@ public class Logic extends Module{
@Override
public void update(){
if(threads.isEnabled() && !threads.isOnThread()) return;
if(Vars.control != null){
control.runUpdateLogic();
@@ -238,9 +237,5 @@ public class Logic extends Module{
checkGameOver();
}
}
if(threads.isEnabled()){
netServer.update();
}
}
}

View File

@@ -416,7 +416,6 @@ public class NetServer extends Module{
}
public void update(){
if(threads.isEnabled() && !threads.isOnThread()) return;
if(!headless && !closing && Net.server() && state.is(State.menu)){
closing = true;

View File

@@ -1,80 +1,35 @@
package io.anuke.mindustry.core;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Queue;
import com.badlogic.gdx.utils.TimeUtils;
import io.anuke.ucore.core.Settings;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.util.Log;
import io.anuke.ucore.util.Threads;
import io.anuke.ucore.util.Threads.ThreadInfoProvider;
import static io.anuke.mindustry.Vars.control;
import static io.anuke.mindustry.Vars.logic;
public class ThreadHandler implements ThreadInfoProvider{
private final Queue<Runnable> toRun = new Queue<>();
private Thread thread, graphicsThread;
private final Object updateLock = new Object();
private float delta = 1f;
private float smoothDelta = 1f;
private long frame = 0, lastDeltaUpdate;
private float framesSinceUpdate;
private boolean enabled;
private boolean rendered = true;
private long lastFrameTime;
public ThreadHandler(){
Threads.setThreadInfoProvider(this);
graphicsThread = Thread.currentThread();
Timers.setDeltaProvider(() -> {
float result = isOnThread() ? delta : Gdx.graphics.getDeltaTime() * 60f;
return Math.min(Float.isNaN(result) ? 1f : result, 15f);
float result = Gdx.graphics.getDeltaTime() * 60f;
return Math.min(Float.isNaN(result) || Float.isInfinite(result) ? 1f : result, 15f);
});
}
public void run(Runnable r){
if(enabled){
synchronized(toRun){
toRun.addLast(r);
}
}else{
r.run();
}
r.run();
}
public void runGraphics(Runnable r){
if(enabled){
Gdx.app.postRunnable(r);
}else{
r.run();
}
r.run();
}
public void runDelay(Runnable r){
if(enabled){
synchronized(toRun){
toRun.addLast(r);
}
}else{
Gdx.app.postRunnable(r);
}
}
public int getTPS(){
if(smoothDelta == 0f){
return 60;
}
return (int) (60 / smoothDelta);
Gdx.app.postRunnable(r);
}
public long getFrameID(){
return enabled ? frame : Gdx.graphics.getFrameId();
}
public float getFramesSinceUpdate(){
return framesSinceUpdate;
return Gdx.graphics.getFrameId();
}
public void handleBeginRender(){
@@ -95,119 +50,16 @@ public class ThreadHandler implements ThreadInfoProvider{
}
}
}
if(!enabled) return;
framesSinceUpdate += Timers.delta();
synchronized(updateLock){
rendered = true;
updateLock.notify();
}
}
public boolean isEnabled(){
return enabled;
}
public void setEnabled(boolean enabled){
if(enabled){
logic.doUpdate = false;
Timers.runTask(2f, () -> {
if(thread != null){
thread.interrupt();
thread = null;
}
thread = new Thread(this::runLogic);
thread.setDaemon(true);
thread.setName("Update Thread");
thread.start();
Log.info("Starting logic thread.");
this.enabled = true;
});
}else{
this.enabled = false;
if(thread != null){
thread.interrupt();
thread = null;
}
Timers.runTask(2f, () -> {
logic.doUpdate = true;
});
}
}
public boolean doInterpolate(){
return enabled && Gdx.graphics.getFramesPerSecond() - getTPS() > 20 && getTPS() < 30;
}
public boolean isOnThread(){
return Thread.currentThread() == thread;
}
@Override
public boolean isOnLogicThread() {
return !enabled || Thread.currentThread() == thread;
return true;
}
@Override
public boolean isOnGraphicsThread() {
return !enabled || Thread.currentThread() == graphicsThread;
return true;
}
private void runLogic(){
try{
while(true){
long time = TimeUtils.nanoTime();
while(true){
Runnable r;
synchronized(toRun){
if(toRun.size > 0){
r = toRun.removeFirst();
}else{
break;
}
}
r.run();
}
logic.doUpdate = true;
logic.update();
logic.doUpdate = false;
long elapsed = TimeUtils.nanosToMillis(TimeUtils.timeSinceNanos(time));
long target = (long) ((1000) / 60f);
if(elapsed < target){
Thread.sleep(target - elapsed);
}
synchronized(updateLock){
while(!rendered){
updateLock.wait();
}
rendered = false;
}
long actuallyElapsed = TimeUtils.nanosToMillis(TimeUtils.timeSinceNanos(time));
delta = Math.max(actuallyElapsed, target) / 1000f * 60f;
if(TimeUtils.timeSinceMillis(lastDeltaUpdate) > 1000){
lastDeltaUpdate = TimeUtils.millis();
smoothDelta = delta;
}
frame++;
framesSinceUpdate = 0;
}
}catch(InterruptedException ex){
Log.info("Stopping logic thread.");
}catch(Throwable ex){
control.setError(ex);
}
}
}

View File

@@ -190,11 +190,6 @@ public class SettingsMenuDialog extends SettingsDialog{
});
graphics.sliderPref("fpscap", 125, 5, 125, 5, s -> (s > 120 ? Bundles.get("setting.fpscap.none") : Bundles.format("setting.fpscap.text", s)));
graphics.checkPref("multithread", mobile, threads::setEnabled);
if(Settings.getBool("multithread")){
threads.setEnabled(true);
}
if(!mobile){
graphics.checkPref("vsync", true, b -> Gdx.graphics.setVSync(b));

View File

@@ -126,7 +126,6 @@ public class HudFragment extends Fragment{
IntFormat tps = new IntFormat("text.tps");
IntFormat ping = new IntFormat("text.ping");
t.label(() -> fps.get(Gdx.graphics.getFramesPerSecond())).padRight(10);
t.label(() -> tps.get(threads.getTPS())).visible(() -> threads.isEnabled());
t.row();
if(Net.hasClient()){
t.label(() -> ping.get(Net.getPing())).visible(Net::client).colspan(2);