coroutine support

Jochen Theodorou blackdrag at gmx.org
Fri Nov 13 09:09:56 PST 2009


Lukas Stadler schrieb:
> Hi everybody!
> 
> I've just checked in the first prototype of a coroutine implementation. 
> It's implemented using the continuations framework, so it's not very 
> speedy (~1µs per context switch in a simple example) but it allows me to 
> experiment on how the API for coroutines could look like.
> This is how it currently works:
> 
> public class CoroutineTest extends Coroutine {
>     public CoroutineTest(CoroutineContext context) {
>         super(context);
>     }
> 
>     public static void main(String[] args) {
>         CoroutineContext context = new CoroutineContext();
>         new CoroutineTest(context);
>         new CoroutineTest(context);
>         context.start(null);
>     }
> 
>     @Continuable
>     protected Object run(Object value) {
>         for (int i = 0; i < 10; i++) {
>             System.out.println(i);
>             yield(null);           
>         }
>         return null;
>     }
> }

the alternative would be to generate for run something like:

private Object run$co(Context context) {
   l0:
   switch (context.jumpToLabel) {
      case 1: jmp l1
   }
   context.l0=0;
   for (;context.l0<10; context.l0++) {
     System.out.println(context.l0);
     l1:
     context.jumpToLabel=1;
     return null;
   }
   context.jumpToLabel=-1;
   return null;
}

jumpToLabel is used to identify the entry/exit point, context will hold 
all local variables (l*)

Where would such an approach be not as good as yours?


bye blackdrag

-- 
Jochen "blackdrag" Theodorou
The Groovy Project Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/



More information about the mlvm-dev mailing list