From 8c32849672197006bb409246f70dc60ffcb97324 Mon Sep 17 00:00:00 2001
From: rtrimana <rtrimana@uci.edu>
Date: Fri, 2 Aug 2019 15:13:25 -0700
Subject: [PATCH] Adding a condition to not check for timeout when it is 0.

---
 .../jpf/listener/VariableConflictTracker.java | 46 +++++--------------
 1 file changed, 12 insertions(+), 34 deletions(-)

diff --git a/src/main/gov/nasa/jpf/listener/VariableConflictTracker.java b/src/main/gov/nasa/jpf/listener/VariableConflictTracker.java
index cee0d24..561a409 100644
--- a/src/main/gov/nasa/jpf/listener/VariableConflictTracker.java
+++ b/src/main/gov/nasa/jpf/listener/VariableConflictTracker.java
@@ -43,9 +43,8 @@ public class VariableConflictTracker extends ListenerAdapter {
   private final HashSet<String> conflictSet = new HashSet<>();
   private final HashSet<String> appSet = new HashSet<>();
   private boolean trackLocationVar;
-  private int timeout;
-  private Timer timeoutTimer;
-  private TimeoutTask timeoutTask;
+  private long timeout;
+  private long startTime;
 
   private final String SET_LOCATION_METHOD = "setLocationMode";
   private final String LOCATION_VAR = "location.mode";
@@ -66,42 +65,21 @@ public class VariableConflictTracker extends ListenerAdapter {
       }
     }
     trackLocationVar = config.getBoolean("track_location_var_conflict", false);
-    // Timeout is in minutes
-    timeout = config.getInt("timeout", 0);
-    timeoutTimer = null;
-    timeoutTask = null;
-  }
-
-  // Create a task for timer to do a timeout
-  private class TimeoutTask extends TimerTask {
-
-    VM vm;
-    Timer timer;
-
-    public TimeoutTask(VM vm, Timer timer) {
-      this.vm = vm;
-      this.timer = timer;
-    }
-
-    @Override
-    public void run() {
-      StringBuilder sb = new StringBuilder();
-      sb.append("Execution timeout!\n");
-      ThreadInfo ti = this.vm.getCurrentThread();
-      Instruction nextIns = ti.createAndThrowException("java.lang.RuntimeException", sb.toString());
-      ti.setNextPC(nextIns);
-      this.cancel();
-      this.timer.cancel();
-    }
+    // Timeout input from config is in minutes, so we need to convert into millis
+    timeout = config.getInt("timeout", 0) * 60 * 1000;
+    startTime = System.currentTimeMillis();
   }
 
   @Override
   public void instructionExecuted(VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn) {
     // Instantiate timeoutTimer
-    if (timeout > 0 && timeoutTimer == null && timeoutTask == null) {
-      timeoutTimer = new Timer();
-      timeoutTask = new TimeoutTask(vm, timeoutTimer);
-      timeoutTimer.schedule(timeoutTask, timeout * 60 * 1000);
+    if (timeout > 0) {
+      if (System.currentTimeMillis() - startTime > timeout) {
+        StringBuilder sb = new StringBuilder();
+        sb.append("Execution timeout: " + (timeout / (60 * 1000)) + " minutes have passed!");
+        Instruction nextIns = ti.createAndThrowException("java.lang.RuntimeException", sb.toString());
+        ti.setNextPC(nextIns);
+      }
     }
 
     // CASE #1: Detecting variable write-after-write conflict
-- 
2.34.1