That is very hard to do right. It is likely to screw up Boehm_gc,
and C++ exception handling, and also old versions of pthreads (written
by Xavier Leroy in fact) which use the stack to identify which
pthread is running (and thus don't permit end user stack swapping).
[This issue arose on a Debian list recently .. Xavier might know
more .. I think it only affects old Linux kernels]
There are actually some functions for stack swapping,
setcontext and getcontext, in <ucontext.h> on Linux and
SysV (conforming to SUSv2 according to my man page).
Note also I am HIGHLY suspicious of setjmp/longjump,
I have many doubts about its workability in the presence
of C++ and other stuff. It's unlikely to work with
continuations -- if you 'jump' up the stack when you
yield the stack is gone .. it can't be restored.
Same goes for ANY code that fiddles the C stack.
too many functions -- eg alloca, sbrk, etc etc
may rely on the stack not changing. Very tricky.
I found something interesting about this subject :
http://luajit.luaforge.net/coco.html
The only problem I see is that this will allocate a C stack per
corountine, hence it works nicely for iterators but might be too costly
for microthreads.
Nicolas
--
Neko : One VM to run them all (http://nekovm.org)