Package kilim
Class Task<TT>
java.lang.Object
kilim.Task<TT>
- All Implemented Interfaces:
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
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 ClassesModifier and TypeClassDescription(package private) static classstatic classstatic classstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static booleanprotected booleanprivate LinkedList<Mailbox<ExitMsg<TT>>> protected FiberThe stack manager in charge of rewinding and unwinding the stack when Task.pause() is called.final intTask id, automatically generated(package private) static final AtomicInteger(package private) intprotected PauseReasonThe reason for pausing (duh) and performs the role of a await condition in CCS.(package private) intThe thread in which to resume this task.private static Fiber.MethodRefprotected AtomicBooleanrunning = true when it is put on the schdulers run Q (by Task.resume()).protected SchedulerThe object responsible for handing this task to a thread when the task is runnable.private int(package private) static PauseReason -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid(package private) voidcheckPin()(package private) booleandump()booleanstatic voidstatic voiderrNotWoven(Task t) static voidExit the task with a throwable indicating an error condition.static voidvoidexecute()voidstatic voidAnalogous to System.exit, except an Object can be used as the exit valuestatic voidstatic Taskfork(Pausable.Fork body) Wraps the given object or lambda expression in a Task and starts that task.static TaskThis is a placeholder that doesn't do anything useful.final PauseReason(package private) Fiber.MethodRefstatic 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 MethodinthashCode()intid()static voididledown()idledown the default schedulervoidinformOnExit(Mailbox<ExitMsg<TT>> exit) static ObjectInvoke a pausable method via reflection.booleanisDone()join()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) voidvoidprePin()booleanresume()Add itself to scheduler if it is neither already running nor done.voidvoidrun()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 final voidprotected voidsetTid(int tid) static voidshutdown()private static booleanskipInvoke(String klass, String name) static voidsleep(long millis) static Task.Invokestatic <TT> Task.Spawn<TT> spawn(Pausable.Spawn<TT> body) Wraps the given object or lambda expression in a Task and starts that task.start()Used to start the task; the task doesn't resume on its own.toString()voidstatic voidyield()Yield cooperatively to the next task waiting to use the thread.static void
-
Field Details
-
yieldReason
-
id
public final int idTask id, automatically generated -
idSource
-
fiber
The stack manager in charge of rewinding and unwinding the stack when Task.pause() is called. -
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:
-
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 preferredResumeThreadThe 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:
-
tid
private int tid -
numActivePins
int numActivePins- See Also:
-
exitMBs
- See Also:
-
scheduler
The object responsible for handing this task to a thread when the task is runnable. -
exitResult
-
timer
-
debugRunning
private static boolean debugRunning -
runnerInfo
-
-
Constructor Details
-
Task
public Task() -
Task
Task(boolean dummy)
-
-
Method Details
-
id
public int id() -
setScheduler
-
getScheduler
-
resumeOnScheduler
- Throws:
Pausable
-
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
-
getStackDepth
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
- Specified by:
onEventin interfaceEventSubscriber
-
getExecutionThread
-
resume
public boolean resume()Add itself to scheduler if it is neither already running nor done.- Returns:
- True if it scheduled itself.
-
informOnExit
-
getCurrentTask
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
Analogous to System.exit, except an Object can be used as the exit value- Throws:
Pausable
-
exit
-
errorExit
Exit 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
-
errNotWoven
public static void errNotWoven() -
errNotWoven
-
invoke
public static Object invoke(Method method, Object obj, Object... args) throws Pausable, IllegalAccessException, IllegalArgumentException, InvocationTargetException Invoke 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:
PausableIllegalAccessExceptionIllegalArgumentExceptionInvocationTargetException
-
getWovenMethod
-
sleep
- 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
Yield cooperatively to the next task waiting to use the thread.- Throws:
Pausable
-
yield
-
pause
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
-
execute
- Specified by:
executein interfaceFiber.Worker- Throws:
PausableException
-
execute
- Specified by:
executein interfaceFiber.Worker- Throws:
Exception
-
toString
-
dump
-
prePin
- Throws:
Pausable
-
checkPin
void checkPin() -
pinToThread
public void pinToThread() -
unpinFromThread
public void unpinFromThread() -
setPauseReason
-
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
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.- Specified by:
runin interfaceRunnable- Throws:
NotPausable
-
joinb
-
join
- Throws:
Pausable
-
equals
-
hashCode
public int hashCode() -
checkKill
public void checkKill() -
getState
public boolean getState() -
fork
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
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
-
idledown
public static void idledown()idledown the default scheduler- See Also:
-