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

public abstract class Task<TT> extends Object implements 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.
  • Field Details

    • yieldReason

      static PauseReason yieldReason
    • id

      public final int id
      Task id, automatically generated
    • idSource

      static final 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:
    • running

      protected 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:
    • tid

      private int tid
    • numActivePins

      int numActivePins
      See Also:
    • exitMBs

      private LinkedList<Mailbox<ExitMsg<TT>>> exitMBs
      See Also:
    • scheduler

      protected Scheduler scheduler
      The object responsible for handing this task to a thread when the task is runnable.
    • exitResult

      public volatile Object exitResult
    • timer

      public Timer timer
    • debugRunning

      private static boolean debugRunning
    • runnerInfo

      private static Fiber.MethodRef runnerInfo
  • Constructor Details

    • Task

      public Task()
    • Task

      Task(boolean dummy)
  • Method Details

    • id

      public int id()
    • setScheduler

      public Task<TT> setScheduler(Scheduler s)
    • getScheduler

      public Scheduler getScheduler()
    • resumeOnScheduler

      public void resumeOnScheduler(Scheduler s) throws Pausable
      Throws:
      Pausable
    • 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(String klass, 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:
      onEvent in interface EventSubscriber
    • getExecutionThread

      public Thread getExecutionThread()
    • resume

      public boolean resume()
      Add itself to scheduler if it is neither already running nor done.
      Returns:
      True if it scheduled itself.
    • informOnExit

      public void informOnExit(Mailbox<ExitMsg<TT>> exit)
    • 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(Object aExitValue) throws Pausable
      Analogous to System.exit, except an Object can be used as the exit value
      Throws:
      Pausable
    • exit

      public static void exit(Object aExitValue, Fiber f)
    • errorExit

      public static void errorExit(Throwable ex) throws Pausable
      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

      public static void errorExit(Throwable ex, Fiber f)
    • errNotWoven

      public static void errNotWoven()
    • errNotWoven

      public static void errNotWoven(Task t)
    • invoke

      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:
      Pausable
      IllegalAccessException
      IllegalArgumentException
      InvocationTargetException
    • getWovenMethod

      private static Method getWovenMethod(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 Pausable
      Yield 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, Exception
      Specified by:
      execute in interface Fiber.Worker
      Throws:
      Pausable
      Exception
    • execute

      public void execute(Fiber f) throws Exception
      Specified by:
      execute in interface Fiber.Worker
      Throws:
      Exception
    • toString

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

      public String dump()
    • prePin

      public void prePin() throws Pausable
      Throws:
      Pausable
    • 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 NotPausable
      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:
      run in interface Runnable
      Throws:
      NotPausable
    • joinb

      public ExitMsg<TT> joinb()
    • join

      public ExitMsg<TT> join() throws Pausable
      Throws:
      Pausable
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class 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(Method method, Object obj, Object... args)
    • idledown

      public static void idledown()
      idledown the default scheduler
      See Also: