Removed multithreading
This commit is contained in:
@@ -172,7 +172,6 @@ public class Logic extends Module{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(){
|
public void update(){
|
||||||
if(threads.isEnabled() && !threads.isOnThread()) return;
|
|
||||||
|
|
||||||
if(Vars.control != null){
|
if(Vars.control != null){
|
||||||
control.runUpdateLogic();
|
control.runUpdateLogic();
|
||||||
@@ -238,9 +237,5 @@ public class Logic extends Module{
|
|||||||
checkGameOver();
|
checkGameOver();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(threads.isEnabled()){
|
|
||||||
netServer.update();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -416,7 +416,6 @@ public class NetServer extends Module{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void update(){
|
public void update(){
|
||||||
if(threads.isEnabled() && !threads.isOnThread()) return;
|
|
||||||
|
|
||||||
if(!headless && !closing && Net.server() && state.is(State.menu)){
|
if(!headless && !closing && Net.server() && state.is(State.menu)){
|
||||||
closing = true;
|
closing = true;
|
||||||
|
|||||||
@@ -1,80 +1,35 @@
|
|||||||
package io.anuke.mindustry.core;
|
package io.anuke.mindustry.core;
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.utils.Queue;
|
|
||||||
import com.badlogic.gdx.utils.TimeUtils;
|
import com.badlogic.gdx.utils.TimeUtils;
|
||||||
import io.anuke.ucore.core.Settings;
|
import io.anuke.ucore.core.Settings;
|
||||||
import io.anuke.ucore.core.Timers;
|
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 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{
|
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;
|
private long lastFrameTime;
|
||||||
|
|
||||||
public ThreadHandler(){
|
public ThreadHandler(){
|
||||||
Threads.setThreadInfoProvider(this);
|
|
||||||
graphicsThread = Thread.currentThread();
|
|
||||||
|
|
||||||
Timers.setDeltaProvider(() -> {
|
Timers.setDeltaProvider(() -> {
|
||||||
float result = isOnThread() ? delta : Gdx.graphics.getDeltaTime() * 60f;
|
float result = Gdx.graphics.getDeltaTime() * 60f;
|
||||||
return Math.min(Float.isNaN(result) ? 1f : result, 15f);
|
return Math.min(Float.isNaN(result) || Float.isInfinite(result) ? 1f : result, 15f);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run(Runnable r){
|
public void run(Runnable r){
|
||||||
if(enabled){
|
r.run();
|
||||||
synchronized(toRun){
|
|
||||||
toRun.addLast(r);
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
r.run();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void runGraphics(Runnable r){
|
public void runGraphics(Runnable r){
|
||||||
if(enabled){
|
r.run();
|
||||||
Gdx.app.postRunnable(r);
|
|
||||||
}else{
|
|
||||||
r.run();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void runDelay(Runnable r){
|
public void runDelay(Runnable r){
|
||||||
if(enabled){
|
Gdx.app.postRunnable(r);
|
||||||
synchronized(toRun){
|
|
||||||
toRun.addLast(r);
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
Gdx.app.postRunnable(r);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getTPS(){
|
|
||||||
if(smoothDelta == 0f){
|
|
||||||
return 60;
|
|
||||||
}
|
|
||||||
return (int) (60 / smoothDelta);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getFrameID(){
|
public long getFrameID(){
|
||||||
return enabled ? frame : Gdx.graphics.getFrameId();
|
return Gdx.graphics.getFrameId();
|
||||||
}
|
|
||||||
|
|
||||||
public float getFramesSinceUpdate(){
|
|
||||||
return framesSinceUpdate;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleBeginRender(){
|
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
|
@Override
|
||||||
public boolean isOnLogicThread() {
|
public boolean isOnLogicThread() {
|
||||||
return !enabled || Thread.currentThread() == thread;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isOnGraphicsThread() {
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.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){
|
if(!mobile){
|
||||||
graphics.checkPref("vsync", true, b -> Gdx.graphics.setVSync(b));
|
graphics.checkPref("vsync", true, b -> Gdx.graphics.setVSync(b));
|
||||||
|
|||||||
@@ -126,7 +126,6 @@ public class HudFragment extends Fragment{
|
|||||||
IntFormat tps = new IntFormat("text.tps");
|
IntFormat tps = new IntFormat("text.tps");
|
||||||
IntFormat ping = new IntFormat("text.ping");
|
IntFormat ping = new IntFormat("text.ping");
|
||||||
t.label(() -> fps.get(Gdx.graphics.getFramesPerSecond())).padRight(10);
|
t.label(() -> fps.get(Gdx.graphics.getFramesPerSecond())).padRight(10);
|
||||||
t.label(() -> tps.get(threads.getTPS())).visible(() -> threads.isEnabled());
|
|
||||||
t.row();
|
t.row();
|
||||||
if(Net.hasClient()){
|
if(Net.hasClient()){
|
||||||
t.label(() -> ping.get(Net.getPing())).visible(Net::client).colspan(2);
|
t.label(() -> ping.get(Net.getPing())).visible(Net::client).colspan(2);
|
||||||
|
|||||||
Reference in New Issue
Block a user