- java.lang.Object
-
- java.dyn.CoroutineBase
-
- java.dyn.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 overridingCoroutineBase.run()
) or by providing aRunnable
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();
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Coroutine.StealResult
the result for steal
-
Constructor Summary
Constructors Constructor Description Coroutine()
Allocates a newCoroutine
object.Coroutine(long stacksize)
Allocates a newCoroutine
object.Coroutine(Runnable target)
Allocates a newCoroutine
object.Coroutine(Runnable target, long stacksize)
Allocates a newCoroutine
object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StackTraceElement[]
getCoroutineStack()
get StackTrace element for coroutinevoid
setWispTask(int id, Object task, Object engine)
Relate Coroutine to wisp data structuresCoroutine.StealResult
steal(boolean failOnContention)
Steal a coroutine from it's carrier thread to current thread.void
stop()
Coroutine exit.static void
unsafeYieldTo(Coroutine target)
static void
yieldTo(Coroutine target)
Yields execution to the target coroutine.-
Methods declared in class java.dyn.CoroutineBase
current, getThread, isFinished, run
-
-
-
-
Constructor Detail
-
Coroutine
public Coroutine()
Allocates a newCoroutine
object.
-
Coroutine
public Coroutine(Runnable target)
Allocates a newCoroutine
object.- Parameters:
target
- the runnable
-
Coroutine
public Coroutine(long stacksize)
Allocates a newCoroutine
object.- Parameters:
stacksize
- the size of stack
-
Coroutine
public Coroutine(Runnable target, long stacksize)
Allocates a newCoroutine
object.- Parameters:
target
- runnablestacksize
- 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 aCoroutine
from other threads 2. we won't switch to aCoroutine
that's being stolen 3. we won't steal a runningCoroutine
- 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 idtask
- task oopengine
- wispEngine oop
-
getCoroutineStack
public StackTraceElement[] getCoroutineStack()
get StackTrace element for coroutine- Returns:
- StackTraceElement
-
-