Package kilim
Class Fiber
- java.lang.Object
-
- kilim.Fiber
-
public final class Fiber extends java.lang.ObjectThis 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 classFiber.MethodRefstatic interfaceFiber.Worker
-
Field Summary
Fields Modifier and Type Field Description StatecurStateThe current frame's state (local vars and elements of the operand stack that will be needed when the Fiber is resumed.(package private) booleanisDone(package private) booleanisPausingprivate intiStackstatic intNOT_PAUSING__HAS_STATEstatic intNOT_PAUSING__NO_STATEprivate static StatePAUSE_STATEstatic intPAUSING__HAS_STATEstatic intPAUSING__NO_STATEintpcThe "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[]stateStackTasktask
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Fiberbegin()(package private) voidclearPausing()Fiberdown()(package private) static voidds()booleanend()end() is the last up().private State[]ensureSize(int newsize)java.lang.ObjectgetCallee()Called by the weaved code while rewinding the stack.StategetState()booleanisDone()static voidpause()static voidpause(Fiber f)voidreset()voidsetCallee(java.lang.Object callee)voidsetState(State state)Called by the generated code before pausing and unwinding its stack frame.private static voidstateToString(java.lang.StringBuilder sb, State s)Tasktask()(package private) voidtogglePause()java.lang.StringtoString()intup()intupEx()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.voidwrongPC()static voidyield()yield cooperatively to the next task waiting to use the threadstatic voidyield(Fiber f)yield cooperatively to the next task waiting to use the thread
-
-
-
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
-
NOT_PAUSING__NO_STATE
public static final int NOT_PAUSING__NO_STATE
- See Also:
- Constant Field Values
-
NOT_PAUSING__HAS_STATE
public static final int NOT_PAUSING__HAS_STATE
- See Also:
- Constant Field Values
-
PAUSING__NO_STATE
public static final int PAUSING__NO_STATE
- See Also:
- Constant Field Values
-
PAUSING__HAS_STATE
public static final int PAUSING__HAS_STATE
- See Also:
- Constant Field Values
-
-
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 Pausableyield 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 followsat 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:
toStringin classjava.lang.Object
-
wrongPC
public void wrongPC()
-
stateToString
private static void stateToString(java.lang.StringBuilder sb, State s)
-
clearPausing
void clearPausing()
-
-