Module java.base
Package java.dyn

Class Coroutine


  • public class Coroutine
    extends CoroutineBase
    Implementation of symmetric coroutines. A Coroutine will take part in thread-wide scheduling of coroutines. It transfers control to the next coroutine whenever yield is called.

    Similar to Thread there are two ways to implement a Coroutine: either by implementing a subclass of Coroutine (and overriding CoroutineBase.run()) or by providing a Runnable to the Coroutine constructor.

    An implementation of a simple Coroutine might look like this:


     class Numbers extends Coroutine {
            public void run() {
                    for (int i = 0; i < 10; i++) {
                            System.out.println(i);
                            yield();
            }
        }
     }
     

    A Coroutine is active as soon as it is created, and will run as soon as control is transferred to it:

     new Numbers();
     for (int i = 0; i < 10; i++)
            yield();
     

    • Constructor Detail

      • Coroutine

        public Coroutine()
        Allocates a new Coroutine object.
      • Coroutine

        public Coroutine​(Runnable target)
        Allocates a new Coroutine object.
        Parameters:
        target - the runnable
      • Coroutine

        public Coroutine​(long stacksize)
        Allocates a new Coroutine object.
        Parameters:
        stacksize - the size of stack
      • Coroutine

        public Coroutine​(Runnable target,
                         long stacksize)
        Allocates a new Coroutine object.
        Parameters:
        target - runnable
        stacksize - the size of stack
    • Method Detail

      • yieldTo

        public static void yieldTo​(Coroutine target)
        Yields execution to the target coroutine.
        Parameters:
        target - Coroutine
      • unsafeYieldTo

        public static void unsafeYieldTo​(Coroutine target)
        optimized version of yieldTo function for wisp based on the following assumptions: 1. we won't simultaneously steal a Coroutine from other threads 2. we won't switch to a Coroutine that's being stolen 3. we won't steal a running Coroutine
        Parameters:
        target - target coroutine
      • steal

        public Coroutine.StealResult steal​(boolean failOnContention)
        Steal a coroutine from it's carrier thread to current thread.
        Parameters:
        failOnContention - steal fail if there's too much lock contention
        Returns:
        result described by Coroutine.STEAL_*. Also return SUCCESS directly if coroutine's carrier thread is current.
      • stop

        public void stop()
        Coroutine exit.
      • setWispTask

        public void setWispTask​(int id,
                                Object task,
                                Object engine)
        Relate Coroutine to wisp data structures
        Parameters:
        id - wispTask id
        task - task oop
        engine - wispEngine oop
      • getCoroutineStack

        public StackTraceElement[] getCoroutineStack()
        get StackTrace element for coroutine
        Returns:
        StackTraceElement