- 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
Threadthere are two ways to implement a Coroutine: either by implementing a subclass of Coroutine (and overridingCoroutineBase.run()) or by providing aRunnableto 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 classCoroutine.StealResultthe result for steal
-
Constructor Summary
Constructors Constructor Description Coroutine()Allocates a newCoroutineobject.Coroutine(long stacksize)Allocates a newCoroutineobject.Coroutine(Runnable target)Allocates a newCoroutineobject.Coroutine(Runnable target, long stacksize)Allocates a newCoroutineobject.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StackTraceElement[]getCoroutineStack()get StackTrace element for coroutinevoidsetWispTask(int id, Object task, Object engine)Relate Coroutine to wisp data structuresCoroutine.StealResultsteal(boolean failOnContention)Steal a coroutine from it's carrier thread to current thread.voidstop()Coroutine exit.static voidunsafeYieldTo(Coroutine target)static voidyieldTo(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 newCoroutineobject.
-
Coroutine
public Coroutine(Runnable target)
Allocates a newCoroutineobject.- Parameters:
target- the runnable
-
Coroutine
public Coroutine(long stacksize)
Allocates a newCoroutineobject.- Parameters:
stacksize- the size of stack
-
Coroutine
public Coroutine(Runnable target, long stacksize)
Allocates a newCoroutineobject.- 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 aCoroutinefrom other threads 2. we won't switch to aCoroutinethat'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
-
-