{
private PriorityQueue<InnerEvent> events;
private TimeFunction time;
- private long lastTimeUpdated = 0;
+ private long processedUntil = 0;
private long eventCounter = 0;
private final List<Consumer<TimelineEvent>> eventAddedListener;
@Override
public void setTime(long time)
{
- lastTimeUpdated = time;
+ processedUntil = time;
}
@Override
public long getTime()
{
- return lastTimeUpdated;
+ return processedUntil;
}
};
public final TimeFunction realTimeExec = new TimeFunction()
{
if (events.isEmpty())
{
- lastTimeUpdated = getSimulationTime();
+ processedUntil = getSimulationTime();
return ExecutionResult.NOTHING_DONE;
}
working();
{
event = events.remove();
}
- lastTimeUpdated = event.getTiming();
+ processedUntil = event.getTiming();
event.run();
// Don't check after every run
checkStop = (checkStop + 1) % 10;
if (checkStop == 0 && System.currentTimeMillis() >= stopMillis)
{
notWorking();
- lastTimeUpdated = getSimulationTime();
return ExecutionResult.EXEC_OUT_OF_TIME;
}
}
notWorking();
- lastTimeUpdated = getSimulationTime();
+ processedUntil = getSimulationTime();
return hasNext() ? ExecutionResult.EXEC_UNTIL_EMPTY : ExecutionResult.EXEC_UNTIL_CONDITION;
}
*/
public long getSimulationTime()
{
- return isWorking() ? lastTimeUpdated : time.getTime();
+ return isWorking() ? processedUntil : time.getTime();
}
/**
{
events.clear();
}
- lastTimeUpdated = time.getTime();
+ processedUntil = time.getTime();
}
/**
{
eventsString = events.toString();
}
- return String.format("Simulation time: %s, Last update: %d, Events: %s", getSimulationTime(), lastTimeUpdated, eventsString);
+ return String.format("Simulation time: %s, Last update: %d, Events: %s", getSimulationTime(), processedUntil, eventsString);
}
public static enum ExecutionResult
void setTime(long time);
}
+
+ /**
+ * Sets the time of the {@link TimeFunction} to the timestamp of the latest processed event
+ */
+ public void synchTime()
+ {
+ time.setTime(processedUntil);
+ }
}
\ No newline at end of file
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
while (shouldBeRunningLive.get())
{
long current = tf.getTime();
- timeline.executeUntil(timeline.laterThan(current), System.currentTimeMillis() + 10);
+ // 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())