-package edu.berkeley.fleet.loops;
-import java.util.*;
-import java.net.*;
-import edu.berkeley.fleet.two.*;
-import edu.berkeley.fleet.api.*;
-import edu.berkeley.fleet.api.Instruction.*;
-import edu.berkeley.fleet.api.Instruction.Set;
-import edu.berkeley.fleet.api.Instruction.Set.*;
-import static edu.berkeley.fleet.util.BitManipulations.*;
-
-// QUESTION: does each dock mentioned by a context have a linear chain
-// of loops, or can it branch?
-
-// - or should we use "sub-contexts" for that
-// - advantage: it lets us convey the fact that a bunch of loops are dispatched together
-// - should loops have an "invoke context" opcode?
-
-/**
- * A Context is a collection of Loops which obey these rules:
- *
- * - A Context has exclusive control of all docks mentioned by any of its Loops.
- * - A Context includes a "starting loop" for every dock it mentions.
- * - When a Context is spawned, it starts the "starting loop" on all of its docks.
- * - A loop may or may not have a successor.
- * - Control may transfer only two ways:
- * - If a loop has a successor, control will transfer to the successor when the loop finishes.
- * - A loop may explicitly transfer control to another loop via abortAndInvoke(). The
- * invoked loop must belong to the same Dock and the same Context.
- * - A Context finishes when all of its docks have finished executing a loop with no successor.
- *
- * Definitions:
- * - A Move instruction is a blocking instruction if either its tokenIn or dataIn bits are set.
- * - All other instructions are non-blocking.
- * - A dock is quiescent if its instruction ring is empty.
- * - A dock is pre-quiescent if
- * - all instructions which remain in the target dock's instruction
- * fifo are nonblocking instructions,
- * - either the docks' OLC=0 or all instructions which remain
- * in its instruction fifo are one-shot instructions
- * - after executing these remaining instructions, the dock's hatch is open
- */
-public class Context {
-
- public final Fleet fleet;
-
- HashMap<Dock,LoopFactory> startupLoopFactories = new HashMap<Dock,LoopFactory>();
- HashSet<LoopFactory> loopFactories = new HashSet<LoopFactory>();
-
- private final ShipPool pool;
-
- // FIXME: currently not used
- private boolean sealed = false;
- public boolean isSealed() { return sealed; }
-
- public Context(Fleet fleet) { this(fleet, new ShipPool(fleet)); }
- public Context(Fleet fleet, ShipPool pool) {
- this.fleet = fleet;
- this.pool = pool;
- }
- public Ship allocateShip(String type) { return pool.allocateShip(type); }
-
- boolean autoflush = false;
- public void setAutoflush(boolean a) { this.autoflush = a; }
-
- public void emit(ArrayList<Instruction> ic) {
- sealed = true;
- for(LoopFactory lf : startupLoopFactories.values())
- lf.emit(ic);
- }
-
- public Instruction[] emit() {
- ArrayList<Instruction> ic = new ArrayList<Instruction>();
- emit(ic);
- return (Instruction[])ic.toArray(new Instruction[0]);
- }
-
- public void dispatch(FleetProcess fp) { dispatch(fp, false); }
- public void dispatch(FleetProcess fp, boolean flushWhenDone) {
- ArrayList<Instruction> ai;
- emit(ai = new ArrayList<Instruction>());
- for(Instruction ins : ai)
- fp.sendInstruction(ins);
- if (flushWhenDone) fp.flush();
- }
-
-}