1 package edu.berkeley.fleet.loops;
4 import edu.berkeley.fleet.two.*;
5 import edu.berkeley.fleet.api.*;
6 import edu.berkeley.fleet.api.Instruction.*;
7 import edu.berkeley.fleet.api.Instruction.Set;
8 import edu.berkeley.fleet.api.Instruction.Set.*;
9 import static edu.berkeley.fleet.util.BitManipulations.*;
11 // QUESTION: does each dock mentioned by a context have a linear chain
12 // of loops, or can it branch?
14 // - or should we use "sub-contexts" for that
15 // - advantage: it lets us convey the fact that a bunch of loops are dispatched together
16 // - should loops have an "invoke context" opcode?
19 * A Context is a collection of Loops which obey these rules:
21 * - A Context has exclusive control of all docks mentioned by any of its Loops.
22 * - A Context includes a "starting loop" for every dock it mentions.
23 * - When a Context is spawned, it starts the "starting loop" on all of its docks.
24 * - A loop may or may not have a successor.
25 * - Control may transfer only two ways:
26 * - If a loop has a successor, control will transfer to the successor when the loop finishes.
27 * - A loop may explicitly transfer control to another loop via abortAndInvoke(). The
28 * invoked loop must belong to the same Dock and the same Context.
29 * - A Context finishes when all of its docks have finished executing a loop with no successor.
32 * - A Move instruction is a blocking instruction if either its tokenIn or dataIn bits are set.
33 * - All other instructions are non-blocking.
34 * - A dock is quiescent if its instruction ring is empty.
35 * - A dock is pre-quiescent if
36 * - all instructions which remain in the target dock's instruction
37 * fifo are nonblocking instructions,
38 * - either the docks' OLC=0 or all instructions which remain
39 * in its instruction fifo are one-shot instructions
40 * - after executing these remaining instructions, the dock's hatch is open
42 public class Context {
44 public final Fleet fleet;
46 HashMap<Dock,LoopFactory> startupLoopFactories = new HashMap<Dock,LoopFactory>();
47 HashSet<LoopFactory> loopFactories = new HashSet<LoopFactory>();
49 private final ShipPool pool;
51 // FIXME: currently not used
52 private boolean sealed = false;
53 public boolean isSealed() { return sealed; }
55 public Context(Fleet fleet) { this(fleet, new ShipPool(fleet)); }
56 public Context(Fleet fleet, ShipPool pool) {
60 public Ship allocateShip(String type) { return pool.allocateShip(type); }
62 boolean autoflush = false;
63 public void setAutoflush(boolean a) { this.autoflush = a; }
65 public void emit(ArrayList<Instruction> ic) {
67 for(LoopFactory lf : startupLoopFactories.values())
71 public Instruction[] emit() {
72 ArrayList<Instruction> ic = new ArrayList<Instruction>();
74 return (Instruction[])ic.toArray(new Instruction[0]);
77 public void dispatch(FleetProcess fp) { dispatch(fp, false); }
78 public void dispatch(FleetProcess fp, boolean flushWhenDone) {
79 ArrayList<Instruction> ai;
80 emit(ai = new ArrayList<Instruction>());
81 for(Instruction ins : ai)
82 fp.sendInstruction(ins);
83 if (flushWhenDone) fp.flush();