Package kilim

Class Fiber


  • public final class Fiber
    extends java.lang.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.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Fiber.MethodRef  
      static interface  Fiber.Worker  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      State curState
      The current frame's state (local vars and elements of the operand stack that will be needed when the Fiber is resumed.
      (package private) boolean isDone  
      (package private) boolean isPausing  
      private int iStack  
      static int NOT_PAUSING__HAS_STATE  
      static int NOT_PAUSING__NO_STATE  
      private static State PAUSE_STATE  
      static int PAUSING__HAS_STATE  
      static int PAUSING__NO_STATE  
      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.
      private State[] stateStack  
      Task task  
    • Constructor Summary

      Constructors 
      Constructor Description
      Fiber​(Task t)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Fiber begin()  
      (package private) void clearPausing()  
      Fiber down()  
      (package private) static void ds()  
      boolean end()
      end() is the last up().
      private State[] ensureSize​(int newsize)  
      java.lang.Object getCallee()
      Called by the weaved code while rewinding the stack.
      State getState()  
      boolean isDone()  
      static void pause()  
      static void pause​(Fiber f)  
      void reset()  
      void setCallee​(java.lang.Object callee)  
      void setState​(State state)
      Called by the generated code before pausing and unwinding its stack frame.
      private static void stateToString​(java.lang.StringBuilder sb, State s)  
      Task task()  
      (package private) void togglePause()  
      java.lang.String toString()  
      int up()  
      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.
      void wrongPC()  
      static void yield()
      yield cooperatively to the next task waiting to use the thread
      static void yield​(Fiber f)
      yield cooperatively to the next task waiting to use the thread
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • 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
    • Constructor Detail

      • Fiber

        public Fiber​(Task t)
    • Method Detail

      • task

        public Task task()
      • isDone

        public boolean isDone()
      • 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 java.lang.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​(java.lang.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 java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • wrongPC

        public void wrongPC()
      • stateToString

        private static void stateToString​(java.lang.StringBuilder sb,
                                          State s)
      • clearPausing

        void clearPausing()