Package kilim

Class Fiber

java.lang.Object
kilim.Fiber

public final class Fiber extends Object
This class serves as a context to manage and store the continuation stack. The actual capture of the closure is done in the Weaver-transformed code.
  • Field Details

    • curState

      public State curState
      The current frame's state (local vars and elements of the operand stack that will be needed when the Fiber is resumed. It is always kept equal to stateStack[iStack] if iStack is in the (0..stateStack.length-1) range, and null otherwise. This is used by the generated code to avoid having to manipulate stateStack in the generated code, and to isolate all stack manipulations to up() and down().
    • pc

      public int pc
      The "program counter", kept equal to stateStack[iStack].pc and is used to jump to the appropriate place in the code while rewinding the code, and also to inform the weaved code inside an exception handler which pausable method (if at all) was being invoked when that exception was thrown. The value 0 refers to normal operation; control transfers to the beginning of the original (pre-weaved) code. A value of n indicates a direct jump into the nth pausable method (after restoring the appropriate state). Accessed by generated code (hence public).
    • stateStack

      private State[] stateStack
    • iStack

      private int iStack
    • isPausing

      boolean isPausing
    • isDone

      boolean isDone
    • task

      public Task task
    • PAUSE_STATE

      private static final State PAUSE_STATE
    • NOT_PAUSING__NO_STATE

      public static final int NOT_PAUSING__NO_STATE
      See Also:
    • NOT_PAUSING__HAS_STATE

      public static final int NOT_PAUSING__HAS_STATE
      See Also:
    • PAUSING__NO_STATE

      public static final int PAUSING__NO_STATE
      See Also:
    • PAUSING__HAS_STATE

      public static final int PAUSING__HAS_STATE
      See Also:
  • Constructor Details

    • Fiber

      public Fiber(Task t)
  • Method Details

    • task

      public Task task()
    • isDone

      public boolean isDone()
    • pause

      public static void pause() throws Pausable
      Throws:
      Pausable
    • reset

      public void reset()
    • yield

      public static void yield() throws Pausable
      yield cooperatively to the next task waiting to use the thread
      Throws:
      Pausable
    • yield

      public static void yield(Fiber f)
      yield cooperatively to the next task waiting to use the thread
    • pause

      public static void pause(Fiber f)
    • up

      public int up()
    • begin

      public final Fiber begin()
    • end

      public final boolean end()
      end() is the last up(). returns true if the fiber is not pausing.
    • down

      public Fiber down()
    • ds

      static void ds()
    • upEx

      public int upEx()
      In the normal (non-exception) scheme of things, the iStack is incremented by down() on the way down and decremented by a corresponding up() when returning or pausing. If, however, an exception is thrown, we lose track of where we are in the hierarchy. We recalibrate iStack by creating a dummy exception and comparing it to the stack depth of an exception taken earlier. This is done in scheduler.getStackDepth(); A sample stack trace of the dummy exception looks as follows
         at kilim.Fiber.upEx(Fiber.java:250)
         at kilim.test.ex.ExCatch.normalCatch(ExCatch.java)
         at kilim.test.ex.ExCatch.test(ExCatch.java)
         at kilim.test.ex.ExCatch.execute(ExCatch.java)
         at kilim.Task.runExecute(Task.java)
         at kilim.WorkerThread.run(WorkerThread.java:11)
       
      We have to figure out the stack depth (iStack) of the method that caught the exception and called upEx ("normalCatch" here). The call stack below runExecute may be owned by the scheduler, which may permit more than one task to build up on the stack. For this reason, we let the scheduler tell us the depth of upEx below the task's execute().
      Returns:
      Fiber.pc (note: in contrast up() returns status)
    • getCallee

      public Object getCallee()
      Called by the weaved code while rewinding the stack. If we are about to call a virtual pausable method, we need an object reference on which to call that method. The next state has that information in state.self
    • setCallee

      public void setCallee(Object callee)
    • ensureSize

      private State[] ensureSize(int newsize)
    • setState

      public void setState(State state)
      Called by the generated code before pausing and unwinding its stack frame.
      Parameters:
      state -
    • getState

      public State getState()
    • togglePause

      void togglePause()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • wrongPC

      public void wrongPC()
    • stateToString

      private static void stateToString(StringBuilder sb, State s)
    • clearPausing

      void clearPausing()