640f12c20fb30a94dbca8e2066ac870d78fe303f
[fleet.git] / src / edu / berkeley / fleet / interpreter / InstructionPump.java
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;
6
7 import java.util.*;
8
9 /** anything that has a source (instruction horn) address on the switch fabric */
10 abstract class InstructionPump extends InterpreterPump {
11
12     public int clogged = 0;
13
14     /** the currently executing instruction */
15     private Instruction.Executable executing = null;
16
17     /** all instructions waiting to be executed (excludes executing) */
18     private Queue<Instruction.Executable> instructions = new LinkedList<Instruction.Executable>();
19
20     /** count of how many "standing instruction only" kills remain to be executed */
21     private int killNextStandingInstruction = 0;
22
23     InstructionPump(InterpreterShip ship, String name, String[] ports) {
24         super(ship, name, ports);
25     }
26
27     public void kill(int count, boolean killOnlyStandingInstructions) {
28         if (count==0) return;
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();
35             }
36         } else {
37             if (count != 1) throw new RuntimeException("this should never happen");
38             this.killNextStandingInstruction++;
39         }
40     }
41
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);
46     }
47
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)
55             Log.println("   " + i);
56     }
57
58     // interface to subclass ///////////////////////////////////////////////////////////////////////
59
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);
62
63     protected final void service() {
64         if (executing == null)
65             if (instructions.size() > 0)
66                 executing = instructions.remove();
67             else
68                 return;
69
70         if (executing != null && killNextStandingInstruction>0 && executing.isStanding()) {
71             executing = null;
72             killNextStandingInstruction--;
73             return;
74         }
75
76         boolean ret = service(executing);
77         if (!ret) return;
78         if (executing.isRequeueing()) {
79             executing = executing.decrementCount();
80             if (executing != null)
81                 addInstruction(executing);
82             executing = null;
83
84         } else {
85             if (executing != null && !executing.isStanding())
86                 executing = executing.decrementCount();
87         }
88     }
89
90
91 }