Package kilim
Class Task<TT>
- java.lang.Object
-
- kilim.Task<TT>
-
- All Implemented Interfaces:
java.lang.Runnable,EventSubscriber,Fiber.Worker
- Direct Known Subclasses:
Chain,Continuation.FakeTask,Ex,Group.GroupTask,NioSelectorScheduler.RegistrationTask,PerfTest,PerfTest.Producer1,Pure.TaskDemo,Reflect,SessionTask,SimpleTask,SimpleTask2,Task.Fork,Task.Invoke,Task.Spawn,TaskGroup,TimedTask,TimerBlast,TimerBlast.Tick,TimerBlast2.Tick,TimerBlast2.Tock,Userdata
public abstract class Task<TT> extends java.lang.Object implements java.lang.Runnable, EventSubscriber, Fiber.Worker
A base class for tasks. A task is a lightweight thread (it contains its own stack in the form of a fiber). A concrete subclass of Task must provide a pausable execute method.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classTask.ArgStatestatic classTask.Forkstatic classTask.Invoke<TT>static classTask.Spawn<TT>
-
Field Summary
Fields Modifier and Type Field Description private static booleandebugRunningprotected booleandoneprivate java.util.LinkedList<Mailbox<ExitMsg<TT>>>exitMBsjava.lang.ObjectexitResultprotected FiberfiberThe stack manager in charge of rewinding and unwinding the stack when Task.pause() is called.intidTask id, automatically generated(package private) static java.util.concurrent.atomic.AtomicIntegeridSource(package private) intnumActivePinsprotected PauseReasonpauseReasonThe reason for pausing (duh) and performs the role of a await condition in CCS.(package private) intpreferredResumeThreadThe thread in which to resume this task.private static Fiber.MethodRefrunnerInfoprotected java.util.concurrent.atomic.AtomicBooleanrunningrunning = true when it is put on the schdulers run Q (by Task.resume()).protected SchedulerschedulerThe object responsible for handing this task to a thread when the task is runnable.private inttidTimertimer(package private) static PauseReasonyieldReason
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcheckKill()(package private) voidcheckPin()(package private) booleancheckTimeout()java.lang.Stringdump()booleanequals(java.lang.Object obj)static voiderrNotWoven()static voiderrNotWoven(Task t)static voiderrorExit(java.lang.Throwable ex)Exit the task with a throwable indicating an error condition.static voiderrorExit(java.lang.Throwable ex, Fiber f)voidexecute()voidexecute(Fiber f)static voidexit(java.lang.Object aExitValue)Analogous to System.exit, except an Object can be used as the exit valuestatic voidexit(java.lang.Object aExitValue, Fiber f)static Taskfork(Pausable.Fork body)Wraps the given object or lambda expression in a Task and starts that task.static TaskgetCurrentTask()This is a placeholder that doesn't do anything useful.java.lang.ThreadgetExecutionThread()PauseReasongetPauseReason()(package private) Fiber.MethodRefgetRunnerInfo()SchedulergetScheduler()static intgetStackDepth(Task task)The generated code calls Fiber.upEx, which in turn calls this to find out out where the current method is w.r.t the closest _runExecute method.booleangetState()intgetTid()return the thread ID that the task is currently running on, valid only during executeprivate static java.lang.reflect.MethodgetWovenMethod(java.lang.reflect.Method m)inthashCode()intid()static voididledown()idledown the default schedulervoidinformOnExit(Mailbox<ExitMsg<TT>> exit)static java.lang.Objectinvoke(java.lang.reflect.Method method, java.lang.Object obj, java.lang.Object... args)Invoke a pausable method via reflection.booleanisDone()ExitMsg<TT>join()ExitMsg<TT>joinb()voidonEvent(EventPublisher ep, Event e)static voidpause(PauseReason pauseReason)Ask the current task to pause with a reason object, that is responsible for resuming the task when the reason (for pausing) is not valid any more.static voidpause(PauseReason pauseReason, Fiber f)voidpinToThread()voidprePin()booleanresume()Add itself to scheduler if it is neither already running nor done.voidresumeOnScheduler(Scheduler s)voidrun()Called by WorkerThread, it is the wrapper that performs pre and post execute processing (in addition to calling the execute(fiber) method of the task.protected voidsetPauseReason(PauseReason pr)Task<TT>setScheduler(Scheduler s)protected voidsetTid(int tid)static voidshutdown()private static booleanskipInvoke(java.lang.String klass, java.lang.String name)static voidsleep(long millis)static Task.Invokespawn(java.lang.reflect.Method method, java.lang.Object obj, java.lang.Object... args)static <TT> Task.Spawn<TT>spawn(Pausable.Spawn<TT> body)Wraps the given object or lambda expression in a Task and starts that task.Task<TT>start()Used to start the task; the task doesn't resume on its own.java.lang.StringtoString()voidunpinFromThread()static voidyield()Yield cooperatively to the next task waiting to use the thread.static voidyield(Fiber f)
-
-
-
Field Detail
-
yieldReason
static PauseReason yieldReason
-
id
public final int id
Task id, automatically generated
-
idSource
static final java.util.concurrent.atomic.AtomicInteger idSource
-
fiber
protected Fiber fiber
The stack manager in charge of rewinding and unwinding the stack when Task.pause() is called.
-
pauseReason
protected PauseReason pauseReason
The reason for pausing (duh) and performs the role of a await condition in CCS. This object is responsible for resuming the task.- See Also:
PauseReason
-
running
protected java.util.concurrent.atomic.AtomicBoolean running
running = true when it is put on the schdulers run Q (by Task.resume()). The Task.runExecute() method is called at some point; 'running' remains true until the end of runExecute (where it is reset), at which point a fresh decision is made whether the task needs to continue running.
-
done
protected volatile boolean done
-
preferredResumeThread
volatile int preferredResumeThread
The thread in which to resume this task. Ideally, we shouldn't have any preferences, but using locks in pausable methods will require the task to be pinned to a thread.- See Also:
ReentrantLock
-
tid
private int tid
-
numActivePins
int numActivePins
- See Also:
preferredResumeThread
-
exitMBs
private java.util.LinkedList<Mailbox<ExitMsg<TT>>> exitMBs
- See Also:
informOnExit(Mailbox)
-
scheduler
protected Scheduler scheduler
The object responsible for handing this task to a thread when the task is runnable.
-
exitResult
public volatile java.lang.Object exitResult
-
timer
public Timer timer
-
debugRunning
private static boolean debugRunning
-
runnerInfo
private static Fiber.MethodRef runnerInfo
-
-
Method Detail
-
id
public int id()
-
getScheduler
public Scheduler getScheduler()
-
start
public Task<TT> start()
Used to start the task; the task doesn't resume on its own. Custom schedulers must be set (@see #setScheduler(Scheduler)) before start() is called.- Returns:
-
getRunnerInfo
Fiber.MethodRef getRunnerInfo()
-
skipInvoke
private static boolean skipInvoke(java.lang.String klass, java.lang.String name)
-
getStackDepth
public static int getStackDepth(Task task)
The generated code calls Fiber.upEx, which in turn calls this to find out out where the current method is w.r.t the closest _runExecute method.- Returns:
- the number of stack frames above _runExecute(), not including this method
-
checkTimeout
boolean checkTimeout()
-
onEvent
public void onEvent(EventPublisher ep, Event e)
- Specified by:
onEventin interfaceEventSubscriber
-
getExecutionThread
public java.lang.Thread getExecutionThread()
-
resume
public boolean resume()
Add itself to scheduler if it is neither already running nor done.- Returns:
- True if it scheduled itself.
-
getCurrentTask
public static Task getCurrentTask() throws Pausable
This is a placeholder that doesn't do anything useful. Weave replaces the call in the bytecode from invokestateic Task.getCurrentTask to load fiber getfield task- Throws:
Pausable
-
exit
public static void exit(java.lang.Object aExitValue) throws PausableAnalogous to System.exit, except an Object can be used as the exit value- Throws:
Pausable
-
exit
public static void exit(java.lang.Object aExitValue, Fiber f)
-
errorExit
public static void errorExit(java.lang.Throwable ex) throws PausableExit the task with a throwable indicating an error condition. The value is conveyed through the exit mailslot (see informOnExit). All exceptions trapped by the task scheduler also set the error result.- Throws:
Pausable
-
errorExit
public static void errorExit(java.lang.Throwable ex, Fiber f)
-
errNotWoven
public static void errNotWoven()
-
errNotWoven
public static void errNotWoven(Task t)
-
invoke
public static java.lang.Object invoke(java.lang.reflect.Method method, java.lang.Object obj, java.lang.Object... args) throws Pausable, java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetExceptionInvoke a pausable method via reflection. Equivalent to Method.invoke().- Parameters:
method- : The method to be invoked. (Implementation note: the corresponding woven method is invoked instead).obj- : The object on which the method is invoked. Can be null if the method is static.args- : Arguments to the method- Returns:
- Throws:
Pausablejava.lang.IllegalAccessExceptionjava.lang.IllegalArgumentExceptionjava.lang.reflect.InvocationTargetException
-
getWovenMethod
private static java.lang.reflect.Method getWovenMethod(java.lang.reflect.Method m)
-
sleep
public static void sleep(long millis) throws Pausable- Parameters:
millis- to sleep. Like thread.sleep, except it doesn't throw an interrupt, and it doesn't hog the java thread.- Throws:
Pausable
-
shutdown
public static void shutdown()
-
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)
-
pause
public static void pause(PauseReason pauseReason) throws Pausable
Ask the current task to pause with a reason object, that is responsible for resuming the task when the reason (for pausing) is not valid any more.- Parameters:
pauseReason- the reason- Throws:
Pausable
-
pause
public static void pause(PauseReason pauseReason, Fiber f)
-
execute
public void execute() throws Pausable, java.lang.Exception- Specified by:
executein interfaceFiber.Worker- Throws:
Pausablejava.lang.Exception
-
execute
public void execute(Fiber f) throws java.lang.Exception
- Specified by:
executein interfaceFiber.Worker- Throws:
java.lang.Exception
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
dump
public java.lang.String dump()
-
checkPin
void checkPin()
-
pinToThread
public void pinToThread()
-
unpinFromThread
public void unpinFromThread()
-
setPauseReason
protected final void setPauseReason(PauseReason pr)
-
getPauseReason
public final PauseReason getPauseReason()
-
isDone
public boolean isDone()
-
setTid
protected void setTid(int tid)
-
getTid
public int getTid()
return the thread ID that the task is currently running on, valid only during execute
-
run
public void run() throws NotPausableCalled by WorkerThread, it is the wrapper that performs pre and post execute processing (in addition to calling the execute(fiber) method of the task.- Specified by:
runin interfacejava.lang.Runnable- Throws:
NotPausable
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
checkKill
public void checkKill()
-
getState
public boolean getState()
-
fork
public static Task fork(Pausable.Fork body)
Wraps the given object or lambda expression in a Task and starts that task. Beware of inadvertent sharing when multiple lambdas are created in the same context- Parameters:
body- the lambda to delegate to- Returns:
- the spawned task.
-
spawn
public static <TT> Task.Spawn<TT> spawn(Pausable.Spawn<TT> body)
Wraps the given object or lambda expression in a Task and starts that task. Beware of inadvertent sharing when multiple lambdas are created in the same context- Parameters:
body- the lambda to delegate to- Returns:
- the spawned task.
-
spawn
public static Task.Invoke spawn(java.lang.reflect.Method method, java.lang.Object obj, java.lang.Object... args)
-
idledown
public static void idledown()
idledown the default scheduler- See Also:
Scheduler.idledown()
-
-