X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2FLogicExecuter.java;h=52adeba0b44968fced83828086fcb2681171de1a;hb=13577856cd85c46f2cd4ad956332697bc820f425;hp=a9fde7a142de5e3599b4fcf8daa8040417feddf1;hpb=58babf45ae7d259a296656451d796dbe601377a4;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..52adeba0 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,7 +3,9 @@ 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; +import net.mograsim.logic.core.timeline.Timeline.ExecutionResult; //TODO maybe move to logic core? public class LogicExecuter @@ -13,16 +15,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(() -> { @@ -35,19 +42,29 @@ public class LogicExecuter { while (shouldBeRunningLive.get()) { - // always execute to keep timeline from "hanging behind" for too long - long current = System.currentTimeMillis(); - timeline.executeUntil(timeline.laterThan(current), current + 10); + long current = tf.getTime(); + // The tf.isPaused() condition is justified, because timeline.getSimulationTime() returns the timestamp of the last + // processed event during executeUntil() + if (timeline.executeUntil(() -> timeline.laterThan(current).getAsBoolean() || tf.isPaused(), + System.currentTimeMillis() + 10) == ExecutionResult.EXEC_OUT_OF_TIME) + timeline.synchTime(); // TODO: should this also be called if tf.isPaused() condition is met? + long nextEventTime = timeline.nextEventTime(); long sleepTime; if (timeline.hasNext()) - sleepTime = timeline.nextEventTime() - current; + sleepTime = (long) ((nextEventTime - current) * tf.getSimulTimeToRealTimeFactor()); else sleepTime = 10000; try { - nextExecSimulTime.set(current + sleepTime); + nextExecSimulTime.set(nextEventTime); 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 @@ -69,6 +86,8 @@ public class LogicExecuter if (Timeline.timeCmp(event.getTiming(), nextExecSimulTime.get()) < 0) simulationThread.interrupt(); }); + // not optimal; but we don't expect this to happen very often + tf.addSimulTimeToRealTimeFactorChangedListener(d -> simulationThread.interrupt()); } public void executeNextStep() @@ -94,6 +113,40 @@ 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 double getSpeedFactor() + { + return tf.getSpeedFactor(); + } + + public void setSpeedFactor(double factor) + { + tf.setSpeedFactor(factor); + } + private void waitForIsRunning(boolean expectedState) { while (isRunningLive.get() ^ expectedState)