lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wednesday 20 October 2004 02:33, Edgar Toernig wrote:
[...]
> What about coroutines at the C-level?  Sure, it can't be done in
> ANSI-C and is in fact heavily system dependent but the problem of
> yielding anywhere within C-code becomes trivial.
>
> To get an idea how the system dependent part could look like I've
> attached some code I'm already using for a while (and afaik, Steve
> Dekorte is using it in his programming language Io).
>
> Would system dependent code like that below be acceptable?

It's not quite as hard as you might think. There are some very handy but 
little-known functions called 
setcontext()/getcontext()/makecontext()/swapcontext() that make implementing 
coroutines a doddle.

I use them in Spey, my greylisting SMTP proxy (plug? what plug?). It's written 
in C++, but you should be able to follow the logic easily enough:

http://cvs.sourceforge.net/viewcvs.py/spey/spey/src/Threadlet.cc?rev=1.1&view=auto
http://cvs.sourceforge.net/viewcvs.py/spey/spey/src/Threadlet.h?rev=1.1&view=auto

-- 
+- David Given --McQ-+ "The cup of Ireland's misfortunes has been
|  dg@cowlark.com    | overflowing for centuries, and it is not full yet."
| (dg@tao-group.com) | --- Sir Boyle Roche
+- www.cowlark.com --+