X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2FLogicExecuter.java;fp=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2FLogicExecuter.java;h=0000000000000000000000000000000000000000;hb=b5d8c2d71e27350ea7c9314e40df5bb0584271cd;hp=eb8a72359c533eaf78ecaf3442ac8f71fbcf7311;hpb=69cb6725ef670328959d55649257ded6ac924b33;p=Mograsim.git diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/LogicExecuter.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/LogicExecuter.java deleted file mode 100644 index eb8a7235..00000000 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/LogicExecuter.java +++ /dev/null @@ -1,111 +0,0 @@ -package net.mograsim.logic.ui; - -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicLong; - -import net.mograsim.logic.core.timeline.Timeline; - -//TODO maybe move to logic core? -public class LogicExecuter -{ - // TODO replace with LogicModel when it exists - private final Timeline timeline; - - private final AtomicBoolean shouldBeRunningLive; - private final AtomicBoolean isRunningLive; - private final AtomicLong nextExecSimulTime; - private final Thread simulationThread; - - public LogicExecuter(Timeline timeline) - { - this.timeline = timeline; - - timeline.setTimeFunction(System::currentTimeMillis); - shouldBeRunningLive = new AtomicBoolean(); - isRunningLive = new AtomicBoolean(); - nextExecSimulTime = new AtomicLong(); - simulationThread = new Thread(() -> - { - isRunningLive.set(true); - synchronized (isRunningLive) - { - isRunningLive.notify(); - } - try - { - 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 sleepTime; - if (timeline.hasNext()) - sleepTime = timeline.nextEventTime() - current; - else - sleepTime = 10000; - try - { - nextExecSimulTime.set(current + sleepTime); - if (sleepTime > 0) - Thread.sleep(sleepTime); - } - catch (@SuppressWarnings("unused") InterruptedException e) - {// do nothing; it is normal execution flow to be interrupted - } - } - } - finally - { - isRunningLive.set(false); - synchronized (isRunningLive) - { - isRunningLive.notify(); - } - } - }); - timeline.addEventAddedListener(event -> - { - if (isRunningLive.get()) - if (Timeline.timeCmp(event.getTiming(), nextExecSimulTime.get()) < 0) - simulationThread.interrupt(); - }); - } - - public void executeNextStep() - { - timeline.executeNext(); - } - - public synchronized void startLiveExecution() - { - if (shouldBeRunningLive.get()) - return; - shouldBeRunningLive.set(true); - simulationThread.start(); - waitForIsRunning(true); - } - - public synchronized void stopLiveExecution() - { - if (!shouldBeRunningLive.get()) - return; - shouldBeRunningLive.set(false); - simulationThread.interrupt(); - waitForIsRunning(false); - } - - private void waitForIsRunning(boolean expectedState) - { - while (isRunningLive.get() ^ expectedState) - try - { - synchronized (isRunningLive) - { - isRunningLive.wait(); - } - } - catch (@SuppressWarnings("unused") InterruptedException e) - {// no need to do anything - } - } -} \ No newline at end of file