1 package edu.berkeley.fleet.interpreter;
2 import edu.berkeley.sbp.util.ANSI;
3 import edu.berkeley.fleet.api.*;
4 import edu.berkeley.fleet.doc.*;
5 import edu.berkeley.fleet.api.Instruction;
9 /** anything that has a source (instruction horn) address on the switch fabric */
10 abstract class InstructionPump extends InterpreterPump {
12 public int clogged = 0;
14 /** the currently executing instruction */
15 private Instruction.Executable executing = null;
17 /** all instructions waiting to be executed (excludes executing) */
18 private Queue<Instruction.Executable> instructions = new LinkedList<Instruction.Executable>();
20 /** count of how many "standing instruction only" kills remain to be executed */
21 private int killNextStandingInstruction = 0;
23 InstructionPump(InterpreterShip ship, String name, String[] ports) {
24 super(ship, name, ports);
27 public void kill(int count, boolean killOnlyStandingInstructions) {
29 if (!killOnlyStandingInstructions) {
30 if (executing != null) { executing = null; count--; }
31 for(;count > 0;count--) {
32 if (instructions.size()==0)
33 throw new RuntimeException("deadlocked " + this + " by killing too many instructions");
34 instructions.remove();
37 if (count != 1) throw new RuntimeException("this should never happen");
38 this.killNextStandingInstruction++;
42 /** an instruction arrives from the instruction horn */
43 void addInstruction(Instruction.Executable instr) {
44 if (killNextStandingInstruction > 0) { /* FIXME technically we should refuse to take the next instruction here */ }
45 instructions.add(instr);
48 protected void shutdown(boolean leaveAsInbox) {
49 if (!(executing != null || instructions.size() > 0)) return;
50 Log.println(ANSI.red(" WARNING: you left instructions on the instruction queue of port " +
51 this + "; they are:"));
52 if (executing != null)
53 Log.println(" " + executing);
54 for(Instruction.Executable i : instructions)
58 // interface to subclass ///////////////////////////////////////////////////////////////////////
60 /** this will be invoked periodically; should return true to "consume" an instruction, false to leave it executing */
61 protected abstract boolean service(Instruction.Executable instr);
63 protected final void service() {
64 if (executing == null)
65 if (instructions.size() > 0)
66 executing = instructions.remove();
70 if (executing != null && killNextStandingInstruction>0 && executing.isStanding()) {
72 killNextStandingInstruction--;
76 boolean ret = service(executing);
78 if (executing.isRequeueing()) {
79 executing = executing.decrementCount();
80 if (executing != null)
81 addInstruction(executing);
85 if (executing != null && !executing.isStanding())
86 executing = executing.decrementCount();