[Cython] C stack based coroutines (was: local variable handling in generators)

Stefan Behnel stefan_ml at behnel.de
Mon May 23 08:40:16 CEST 2011


Dag Sverre Seljebotn, 22.05.2011 21:48:
> Keep in mind that if speed is the objective, another idea is to use real C
> coroutines. This would likely be faster than anything we can make up
> ourselves; a single stack jump is bound to be faster than copying things in
> and out of the stack.
>
> Of course, probably more work. And then portability as the API is seperate
> for Windows (fibers) and POSIX (makecontext). But I don't think there's a
> lack of compatability layer libraries which unite the platform-specific APIs.

What if we have to call back into CPython? Would that work if the call ends 
up on a different stack than previous ones? What about CPython thread 
switches and signal handlers?

Wikipedia has a couple of things to say about coroutines in C:

http://en.wikipedia.org/wiki/Coroutine#Implementations_for_C

including links to some libraries that are supposed to be portable:

http://xmailserver.org/libpcl.html

http://software.schmorp.de/pkg/libcoro.html

http://code.google.com/p/libconcurrency/

http://www.dekorte.com/projects/opensource/libCoroutine/

http://www.goron.de/~froese/coro/

Overall, I think this has some potential but also major uncertainties. And 
it's a substantially larger change than making local variables C variables. 
May have made a good GSoC topic...

Stefan


More information about the cython-devel mailing list