Longjmping back, and back again; Coroutines in C

Dave Jones djones at megatest.UUCP
Wed Nov 22 11:03:04 AEST 1989


>From article <457 at enea.se>, by jerker at enea.se (Jerker W}gberg):

> I am trying to implement coroutines using plain C. My
> application is not time critical, so there is no need for speed,
> being portable is far more important. It would be great if there
> was a way to switch stacks of the processes using just C.

True. But there ain't. Any way you go will require some machine-dependent
code, even if it's all written in C. (I've got an all-C coroutine
package that works on Sun3's, but I know it would fail on a Sun4.)

> I have figured out a way to implement this that works fine on a PC
> with MSC 5.1, but wreaks havoc when run on a SUN4. The idea is that
> instead of actually switching stacks, I use the "real" stack but
> swap it in and out of malloced memory.

(That's spelled "mallocked".) Anyway, on a SUN4, not all of the stack
is located in contiguous main memory.  The stack is implemented partially
in "register-windows". Read the SPARC Architecture Manual if that
sounds interesting.

It's probably not trivial to implement coroutines on a SPARC. Not
having tried it, I don't know. (There is a move afoot to put one
on my desk, but I have to clean the desk first, so that's a big
hurdle.)  I think there is some kind of coroutine package that comes
with the machine. Check it out.



More information about the Comp.lang.c mailing list