]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.simulation.sequences/src/org/simantics/simulation/sequences/action/AbstractActionContext.java
Fix management of scheduledAt in AbstractActionContext
[simantics/platform.git] / bundles / org.simantics.simulation.sequences / src / org / simantics / simulation / sequences / action / AbstractActionContext.java
index 2adb9a045cf8a06fb525124b1a109b73f2163559..5b5917f6ab33d3b016bd685ad0d391dfcb58f8a4 100644 (file)
@@ -76,12 +76,15 @@ public abstract class AbstractActionContext implements ActionContext {
 
     public boolean isStopped() {
        synchronized (this) {
-               return stopped || (scheduledNextStep.isEmpty() && scheduledAt.isEmpty());
+               return stopped;
        }
     }
     
     public double handleStep(double currentTime) {
        synchronized (this) {
+               if (stopped)
+                       return Double.POSITIVE_INFINITY;
+               
                this.currentTime = currentTime;
                {
                    ArrayList<Function1<Tuple0, Object>> temp = scheduledNow;
@@ -93,7 +96,6 @@ public abstract class AbstractActionContext implements ActionContext {
                SCLContext context = SCLContext.getCurrent();
                Object oldActionContext = context.put("sequenceAction", this);
                try {
-                   Task firstTask = scheduledAt.peek();
                    while(true) {
                        while(!scheduledNow.isEmpty()) {
                                try {
@@ -106,16 +108,16 @@ public abstract class AbstractActionContext implements ActionContext {
                                        this.exceptions.add(new RuntimeException("Action failure at " + currentTime + ": " + e.getMessage(), e));
                                }
                        }
-                       if(firstTask == null)
+                       Task firstTask = scheduledAt.peek();
+                       if(firstTask == null) {
+                           if (scheduledNextStep.isEmpty())
+                               stopped = true;
                            return Double.POSITIVE_INFINITY;
-                       else if(firstTask.time > currentTime+TIME_TOLERANCE)
+                       } else if(firstTask.time > currentTime+TIME_TOLERANCE) {
                            return firstTask.time;
-                       else {
+                       } else {
+                           scheduledAt.remove();
                            firstTask.continuation.apply(Tuple0.INSTANCE);
-                               synchronized (this) {
-                                       scheduledAt.remove();
-                               }
-                           firstTask = scheduledAt.peek();
                        }
                    }
                } finally {
@@ -128,6 +130,9 @@ public abstract class AbstractActionContext implements ActionContext {
         synchronized (this) {
             List<Function1<StopReason, Object>> stopFunctions = new ArrayList<>(scheduledWhenStopped);
             scheduledWhenStopped.clear();
+            
+            scheduledNextStep.clear();
+            scheduledAt.clear();
 
             SCLContext context = SCLContext.getCurrent();
             Object oldActionContext = context.put("sequenceAction", this);