X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2FLogicExecuter.java;h=eb67f8dc13d61b5e4f81ce70ad873a793356452f;hb=46d4c053db9a363185a9dce28fcefbc3bf9d6afd;hp=a9fde7a142de5e3599b4fcf8daa8040417feddf1;hpb=76c2b3eab6cec47490bb75713356152deb5d07ed;p=Mograsim.git diff --git a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicExecuter.java b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicExecuter.java index a9fde7a1..eb67f8dc 100644 --- a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicExecuter.java +++ b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicExecuter.java @@ -3,6 +3,7 @@ package net.mograsim.logic.model; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; +import net.mograsim.logic.core.timeline.PauseableTimeFunction; import net.mograsim.logic.core.timeline.Timeline; //TODO maybe move to logic core? @@ -13,16 +14,21 @@ public class LogicExecuter private final AtomicBoolean shouldBeRunningLive; private final AtomicBoolean isRunningLive; + private final AtomicBoolean isPaused; private final AtomicLong nextExecSimulTime; private final Thread simulationThread; + PauseableTimeFunction tf; + public LogicExecuter(Timeline timeline) { this.timeline = timeline; - timeline.setTimeFunction(System::currentTimeMillis); + tf = new PauseableTimeFunction(); + timeline.setTimeFunction(tf); shouldBeRunningLive = new AtomicBoolean(); isRunningLive = new AtomicBoolean(); + isPaused = new AtomicBoolean(); nextExecSimulTime = new AtomicLong(); simulationThread = new Thread(() -> { @@ -36,7 +42,7 @@ public class LogicExecuter while (shouldBeRunningLive.get()) { // always execute to keep timeline from "hanging behind" for too long - long current = System.currentTimeMillis(); + long current = tf.getAsLong(); timeline.executeUntil(timeline.laterThan(current), current + 10); long sleepTime; if (timeline.hasNext()) @@ -48,6 +54,12 @@ public class LogicExecuter nextExecSimulTime.set(current + sleepTime); if (sleepTime > 0) Thread.sleep(sleepTime); + + synchronized (isPaused) + { + while (isPaused.get()) + isPaused.wait(); + } } catch (@SuppressWarnings("unused") InterruptedException e) {// do nothing; it is normal execution flow to be interrupted @@ -94,6 +106,35 @@ public class LogicExecuter waitForIsRunning(false); } + public void unpauseLiveExecution() + { + synchronized (isPaused) + { + tf.unpause(); + isPaused.set(false); + isPaused.notify(); + } + } + + public void pauseLiveExecution() + { + synchronized (isPaused) + { + tf.pause(); + isPaused.set(true); + } + } + + public boolean isPaused() + { + return isPaused.get(); + } + + public void setSpeedPercentage(int percentage) + { + tf.setSpeedPercentage(percentage); + } + private void waitForIsRunning(boolean expectedState) { while (isRunningLive.get() ^ expectedState)