00bd35d36daaadda8cde9490dc0f464413355f9e
[fleet.git] / src / edu / berkeley / fleet / loops / Context.java
1 package edu.berkeley.fleet.loops;
2 import java.util.*;
3 import java.net.*;
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.*;
10
11 // QUESTION: does each dock mentioned by a context have a linear chain
12 // of loops, or can it branch?
13
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?
17
18 /**
19  *  A Context is a collection of Loops which obey these rules:
20  *
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.
30  *
31  *  Definitions:
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
41  */
42 public class Context {
43
44     public final Fleet fleet;
45
46     HashMap<Dock,LoopFactory> startupLoopFactories = new HashMap<Dock,LoopFactory>();
47     HashSet<LoopFactory> loopFactories = new HashSet<LoopFactory>();
48
49     private final ShipPool pool;
50     public Context(Fleet fleet) { this(fleet, new ShipPool(fleet)); }
51     public Context(Fleet fleet, ShipPool pool) {
52         this.fleet = fleet;
53         this.pool = pool;
54     }
55     public Ship allocateShip(String type) { return pool.allocateShip(type); }
56
57     boolean autoflush = false;
58     public void setAutoflush(boolean a) { this.autoflush = a; }
59
60     public void emit(ArrayList<Instruction> ic) {
61         for(LoopFactory lf : startupLoopFactories.values())
62             lf.emit(ic);
63     }
64
65     public void dispatch(FleetProcess fp) {
66         ArrayList<Instruction> ai;
67         emit(ai = new ArrayList<Instruction>());
68         for(Instruction ins : ai) {
69             fp.sendInstruction(ins);
70         }
71     }
72
73 }