diff options
| author | Paul Buetow <paul@buetow.org> | 2008-05-31 03:41:18 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2008-05-31 03:41:18 +0000 |
| commit | f32bc24469d0e0d8e184c6a27fe04b13f59d6fb0 (patch) | |
| tree | 2bd245377baf56d2cccad26a9cc56f88b232140b | |
| parent | 28c8f68ecef4b4e30994ae1c1fd201a0cd871f0d (diff) | |
local timed tasks bugfix.
| -rw-r--r-- | sources/core/VSProcess.java | 22 | ||||
| -rw-r--r-- | sources/core/VSTask.java | 19 | ||||
| -rw-r--r-- | sources/core/VSTaskManager.java | 212 | ||||
| -rw-r--r-- | sources/simulator/VSSimulatorCanvas.java | 11 |
4 files changed, 167 insertions, 97 deletions
diff --git a/sources/core/VSProcess.java b/sources/core/VSProcess.java index 6d1b01d..c5a90d9 100644 --- a/sources/core/VSProcess.java +++ b/sources/core/VSProcess.java @@ -87,6 +87,9 @@ public class VSProcess extends VSPrefs { /** The vector time. */ private VSVectorTime vectorTime; + /** The tasks of the process. */ + private VSPriorityQueue<VSTask> tasks; + /** The process has crashed. But may be working again. */ private boolean hasCrashed; @@ -198,6 +201,7 @@ public class VSProcess extends VSPrefs { this.simulatorCanvas = simulatorCanvas; this.logging = logging; + tasks = new VSPriorityQueue<VSTask>(); processID = simulatorCanvas.processIDCount(); random = new VSRandom(processID*processNum+processID+processNum); @@ -944,6 +948,24 @@ public class VSProcess extends VSPrefs { } /** + * Gets the tasks of the process. + * + * @return The tasks + */ + public VSPriorityQueue<VSTask> getTasks() { + return tasks; + } + + /** + * Sets the tasks of the process. + * + * @tasks The tasks + */ + public void setTasks(VSPriorityQueue<VSTask> tasks) { + this.tasks = tasks; + } + + /** * Gets the protocol object. * * @param protocolClassname the protocol classname diff --git a/sources/core/VSTask.java b/sources/core/VSTask.java index 5039612..c7b378d 100644 --- a/sources/core/VSTask.java +++ b/sources/core/VSTask.java @@ -249,12 +249,14 @@ public class VSTask implements Comparable { */ public String toString() { StringBuffer buffer = new StringBuffer(); + buffer.append(prefs.getString("lang.task")); buffer.append(" "); buffer.append(getTaskTime()); buffer.append(event.toString()); buffer.append("; PID: "); buffer.append(process.getProcessID()); + return buffer.toString(); } @@ -271,7 +273,7 @@ public class VSTask implements Comparable { else if (taskTime > task.getTaskTime()) return 1; - /* If it's a ProtocolRecover, it should get handled very first */ + /* If it's a recovering, it should get handled very first */ boolean a = event instanceof VSProcessRecoverEvent; boolean b = task.getEvent() instanceof VSProcessRecoverEvent; @@ -284,7 +286,20 @@ public class VSTask implements Comparable { if (b) return 1; - /* If it's a VSProtocolEvent, it should get handled first */ + /* If it's a crash, it should get handled second first */ + a = event instanceof VSProcessCrashEvent; + b = task.getEvent() instanceof VSProcessCrashEvent; + + if (a && b) + return 0; + + if (a) + return -1; + + if (b) + return 1; + + /* If it's a VSProtocolEvent, it should get handled third */ a = event instanceof VSProtocolEvent; b = task.getEvent() instanceof VSProtocolEvent; diff --git a/sources/core/VSTaskManager.java b/sources/core/VSTaskManager.java index 66cfe75..b12b093 100644 --- a/sources/core/VSTaskManager.java +++ b/sources/core/VSTaskManager.java @@ -26,6 +26,7 @@ package core; import java.util.*; import prefs.*; +import simulator.*; import utils.*; /** @@ -40,8 +41,8 @@ public class VSTaskManager { /** The seriao version uid */ private static final long serialVersionUID = 1L; - /** The tasks. */ - private PriorityQueue<VSTask> tasks; + /** The simulator canvas. */ + private VSSimulatorCanvas simulatorCanvas; /** The global tasks. */ private PriorityQueue<VSTask> globalTasks; @@ -63,9 +64,9 @@ public class VSTaskManager { * * @param prefs the simulator's default prefs */ - public VSTaskManager(VSPrefs prefs) { + public VSTaskManager(VSPrefs prefs, VSSimulatorCanvas simulatorCanvas) { this.prefs = prefs; - this.tasks = new PriorityQueue<VSTask>(); + this.simulatorCanvas = simulatorCanvas; this.globalTasks = new PriorityQueue<VSTask>(); this.fullfilledProgrammedTasks = new LinkedList<VSTask>(); } @@ -81,13 +82,13 @@ public class VSTaskManager { public synchronized void runTasks(long step, long offset, long lastGlobalTime) { VSTask task = null; - VSProcess process = null; long localTime; long offsetTime; long taskTime; long globalTime; final long globalOffsetTime = lastGlobalTime + step; boolean redo; + Vector<VSProcess> processes = simulatorCanvas.getProcesses(); do { redo = false; @@ -95,7 +96,7 @@ public class VSTaskManager { /* Run tasks which have for its schedule the global time */ while (globalTasks.size() != 0) { task = globalTasks.peek(); - process = task.getProcess(); + VSProcess process = task.getProcess(); localTime = process.getTime(); offsetTime = localTime + step; taskTime = task.getTaskTime(); @@ -147,55 +148,65 @@ public class VSTaskManager { fullfilledProgrammedTasks.add(task); } - /* Run tasks which have for its schedule the local process times */ - while (tasks.size() != 0) { - task = tasks.peek(); - process = task.getProcess(); - localTime = process.getTime(); - offsetTime = localTime + step; - taskTime = task.getTaskTime(); - globalTime = process.getGlobalTime(); - - if (offsetTime < taskTime) - break; - - tasks.poll(); - redo = true; - - if (process.isCrashed() && !task.isVSProcessRecoverEvent()) { - if (task.isProgrammed()) - fullfilledProgrammedTasks.add(task); - continue; - } - - if (offsetTime == taskTime) { - process.setGlobalTime(globalOffsetTime); - process.setLocalTime(offsetTime); - process.timeModified(false); - task.run(); - process.setGlobalTime(globalTime); - if (process.timeModified()) - process.addClockOffset(process.getTime()-offsetTime); - process.setLocalTime(localTime); - - } else { /* if (offsetTime > taskTime) */ - final long diff = offsetTime - taskTime; - if (globalOffsetTime - diff < lastGlobalTime) - process.setGlobalTime(lastGlobalTime); - else - process.setGlobalTime(globalOffsetTime - diff); - process.setLocalTime(offsetTime - diff); - process.timeModified(false); - task.run(); - process.setGlobalTime(globalTime); - if (process.timeModified()) - process.addClockOffset(process.getTime()- - (offsetTime-diff)); - process.setLocalTime(localTime); + synchronized (processes) { + for (VSProcess process : processes) { + PriorityQueue<VSTask> tasks = process.getTasks(); + + /* Run tasks which have for its schedule the local + process times */ + while (tasks.size() != 0) { + task = tasks.peek(); + process = task.getProcess(); + localTime = process.getTime(); + offsetTime = localTime + step; + taskTime = task.getTaskTime(); + globalTime = process.getGlobalTime(); + + if (offsetTime < taskTime) + break; + + tasks.poll(); + redo = true; + + if (process.isCrashed() && + !task.isVSProcessRecoverEvent()) { + if (task.isProgrammed()) + fullfilledProgrammedTasks.add(task); + continue; + } + + if (offsetTime == taskTime) { + process.setGlobalTime(globalOffsetTime); + process.setLocalTime(offsetTime); + process.timeModified(false); + task.run(); + process.setGlobalTime(globalTime); + if (process.timeModified()) + process.addClockOffset(process.getTime()- + offsetTime); + process.setLocalTime(localTime); + + } else { /* if (offsetTime > taskTime) */ + final long diff = offsetTime - taskTime; + if (globalOffsetTime - diff < lastGlobalTime) + process.setGlobalTime(lastGlobalTime); + else + process.setGlobalTime(globalOffsetTime- + diff); + process.setLocalTime(offsetTime - diff); + process.timeModified(false); + task.run(); + process.setGlobalTime(globalTime); + if (process.timeModified()) + process.addClockOffset(process.getTime()- + (offsetTime-diff)); + process.setLocalTime(localTime); + } + + if (task.isProgrammed()) + fullfilledProgrammedTasks.add(task); + } } - - if (task.isProgrammed()) - fullfilledProgrammedTasks.add(task); } } while (redo); @@ -205,28 +216,32 @@ public class VSTaskManager { * Resets the task manager. */ public synchronized void reset() { - PriorityQueue<VSTask> tmp = tasks; - PriorityQueue<VSTask> tmp2 = globalTasks; - tasks = new PriorityQueue<VSTask>(); - globalTasks = new PriorityQueue<VSTask>(); + Vector<VSProcess> processes = simulatorCanvas.getProcesses(); + PriorityQueue<VSTask> tmp = null; - while (fullfilledProgrammedTasks.size() != 0) { - VSTask task = fullfilledProgrammedTasks.removeFirst(); - if (task.isProgrammed()) - insert(task); + synchronized (processes) { + for (VSProcess process : processes) { + tmp = process.getTasks(); + process.setTasks(new VSPriorityQueue<VSTask>()); + + for (VSTask task : tmp) { + if (task.isProgrammed()) + insert(task); + } + } } + tmp = globalTasks; + globalTasks = new PriorityQueue<VSTask>(); + + while (fullfilledProgrammedTasks.size() != 0) + insert(fullfilledProgrammedTasks.removeFirst()); + while (tmp.size() != 0) { VSTask task = tmp.poll(); if (task.isProgrammed()) insert(task); } - - while (tmp2.size() != 0) { - VSTask task = tmp2.poll(); - if (task.isProgrammed()) - insert(task); - } } /** @@ -237,14 +252,16 @@ public class VSTaskManager { * @param task the task to insert */ private void insert(VSTask task) { - if (task.timeOver()) - fullfilledProgrammedTasks.addLast(task); + if (task.timeOver()) { + if (task.isProgrammed()) + fullfilledProgrammedTasks.addLast(task); - else if (task.isGlobalTimed()) + } else if (task.isGlobalTimed()) { globalTasks.add(task); - else - tasks.add(task); + } else { + task.getProcess().getTasks().add(task); + } } /** @@ -275,14 +292,16 @@ public class VSTaskManager { * @return true, if the task has been removed with success */ public synchronized boolean removeTask(VSTask task) { - if (fullfilledProgrammedTasks.remove(task)) + if (fullfilledProgrammedTasks.remove(task)) { return true; - else if (task.isGlobalTimed() && globalTasks.remove(task)) + } else if (task.isGlobalTimed() && globalTasks.remove(task)) { return true; - else if (!task.isGlobalTimed() && tasks.remove(task)) - return true; + } else if (!task.isGlobalTimed()) { + if (task.getProcess().getTasks().remove(task)) + return true; + } return false; } @@ -321,14 +340,7 @@ public class VSTaskManager { for (VSTask task : removeThose) globalTasks.remove(task); - removeThose.clear(); - - for (VSTask task : tasks) - if (task.isProcess(process)) - removeThose.add(task); - - for (VSTask task : removeThose) - tasks.remove(task); + process.getTasks().clear(); } /** @@ -338,14 +350,19 @@ public class VSTaskManager { */ public synchronized VSPriorityQueue<VSTask> getLocalTasks() { VSPriorityQueue<VSTask> localTasks = new VSPriorityQueue<VSTask>(); + Vector<VSProcess> processes = simulatorCanvas.getProcesses(); for (VSTask task : fullfilledProgrammedTasks) - if (!task.isGlobalTimed() && task.isProgrammed()) + if (!task.isGlobalTimed()) localTasks.add(task); - for (VSTask task : tasks) - if (task.isProgrammed()) - localTasks.add(task); + synchronized (processes) { + for (VSProcess process : processes) { + VSPriorityQueue<VSTask> tasks = process.getTasks(); + for (VSTask task : tasks) + localTasks.add(task); + } + } return localTasks; } @@ -359,7 +376,7 @@ public class VSTaskManager { VSPriorityQueue<VSTask> globalTasks = new VSPriorityQueue<VSTask>(); for (VSTask task : fullfilledProgrammedTasks) - if (task.isGlobalTimed() && task.isProgrammed()) + if (task.isGlobalTimed()) globalTasks.add(task); for (VSTask task : globalTasks) @@ -379,6 +396,7 @@ public class VSTaskManager { public synchronized VSPriorityQueue<VSTask> getProcessLocalTasks( VSProcess process) { VSPriorityQueue<VSTask> processTasks = new VSPriorityQueue<VSTask>(); + VSPriorityQueue<VSTask> tasks = process.getTasks(); for (VSTask task : fullfilledProgrammedTasks) if (!task.isGlobalTimed() && task.isProcess(process) && @@ -386,7 +404,7 @@ public class VSTaskManager { processTasks.add(task); for (VSTask task : tasks) - if (task.isProcess(process) && task.isProgrammed()) + if (task.isProgrammed()) processTasks.add(task); return processTasks; @@ -440,9 +458,15 @@ public class VSTaskManager { buffer.append(prefs.getString("lang.tasks.local")); - for (VSTask task : tasks) { - buffer.append(task); - buffer.append("; "); + Vector<VSProcess> processes = simulatorCanvas.getProcesses(); + synchronized (processes) { + for (VSProcess process : processes) { + VSPriorityQueue<VSTask> tasks = process.getTasks(); + for (VSTask task : tasks) { + buffer.append(task); + buffer.append("; "); + } + } } String descr = buffer.toString(); diff --git a/sources/simulator/VSSimulatorCanvas.java b/sources/simulator/VSSimulatorCanvas.java index a40a88d..d8071bc 100644 --- a/sources/simulator/VSSimulatorCanvas.java +++ b/sources/simulator/VSSimulatorCanvas.java @@ -467,7 +467,7 @@ public class VSSimulatorCanvas extends Canvas implements Runnable { this.prefs = prefs; this.simulation = simulation; this.logging = logging; - this.taskManager = new VSTaskManager(prefs); + this.taskManager = new VSTaskManager(prefs, this); this.messageLines = new LinkedList<VSMessageLine>(); this.messageLinesToRemove = new LinkedList<VSMessageLine>(); this.processes = new Vector<VSProcess>(); @@ -1393,6 +1393,15 @@ public class VSSimulatorCanvas extends Canvas implements Runnable { } /** + * Gets the processes. + * + * @return the processes + */ + public Vector<VSProcess> getProcesses() { + return processes; + } + + /** * Updates from the prefs. Called by the VSSimulatorEditor if values * have been saved. */ |
