1 package edu.berkeley.fleet.interpreter;
2 import edu.berkeley.sbp.util.ANSI;
3 import edu.berkeley.fleet.api.*;
4 import edu.berkeley.fleet.api.Instruction;
8 /** anything that has a source (instruction horn) address on the switch fabric */
9 abstract class InstructionBenkoBox extends InterpreterBenkoBox {
11 /** the currently executing instruction */
12 private Instruction.Executable executing = null;
14 /** all instructions waiting to be executed (excludes executing) */
15 private Queue<Instruction.Executable> instructions = new LinkedList<Instruction.Executable>();
17 /** count of how many "standing instruction only" kills remain to be executed */
18 private int killNextStandingInstruction = 0;
20 InstructionBenkoBox(InterpreterShip ship, String name, String[] ports) {
21 super(ship, name, ports);
24 public void kill(int count, boolean killOnlyStandingInstructions) {
26 if (!killOnlyStandingInstructions) {
27 if (executing != null) { executing = null; count--; }
28 for(;count > 0;count--) {
29 if (instructions.size()==0)
30 throw new RuntimeException("deadlocked " + this + " by killing too many instructions");
31 instructions.remove();
34 if (count != 1) throw new RuntimeException("this should never happen");
35 this.killNextStandingInstruction++;
39 /** an instruction arrives from the instruction horn */
40 void addInstruction(Instruction.Executable instr) {
41 if (killNextStandingInstruction > 0) { /* FIXME technically we should refuse to take the next instruction here */ }
42 instructions.add(instr);
45 protected void shutdown(boolean leaveAsInbox) {
46 if (!(executing != null || instructions.size() > 0)) return;
47 Log.println(ANSI.red(" WARNING: you left instructions on the instruction queue of port " +
48 this + "; they are:"));
49 if (executing != null)
50 Log.println(" " + executing);
51 for(Instruction.Executable i : instructions)
55 // interface to subclass ///////////////////////////////////////////////////////////////////////
57 /** this will be invoked periodically; should return true to "consume" an instruction, false to leave it executing */
58 protected abstract boolean service(Instruction.Executable instr);
60 protected final void service() {
61 if (executing == null)
62 if (instructions.size() > 0)
63 executing = instructions.remove();
67 if (executing != null && killNextStandingInstruction>0 && executing.isStanding()) {
69 killNextStandingInstruction--;
73 boolean ret = service(executing);
75 if (executing.requeue) {
76 executing = executing.decrementCount();
77 if (executing != null)
78 addInstruction(executing);
82 if (executing != null && !executing.isStanding())
83 executing = executing.decrementCount();