Longjmping back, and back again; Coroutines in C

Richard Tobin richard at aiai.ed.ac.uk
Wed Nov 22 04:42:00 AEST 1989


In article <457 at enea.se> jerker at helios.se (Jerker W}gberg) writes:
>I am trying to implement coroutines using plain C. 

Tricky!

>instead of actually switching stacks, I use the "real" stack but
>swap it in and out of malloced memory.

Aaargh!  What a hack!

>1. Can somebody explain why this fails on a SUN4. 

Sun 4s have "register windows" which means that the variables that
would be near the top of the stack are instead in registers.

As a quick hack with a remote chance of working, you could try burying
your stack copying code at the bottom of about 20 trivial procedures,
so that the relevant register windows will have been flushed.  This is
just a guess, and may well not work.

>3. Does anyone know of a better way to do coroutines in C ?

Sun OS 4 has a library for coroutines (which they refer to as threads).

One of my more inventive friends produced some code for coroutines
that worked under Berkeley-like Unixes; it worked by using sigstack(),
signal() and kill() to start executing on a malloc()ed stack.

-- Richard
-- 
Richard Tobin,                       JANET: R.Tobin at uk.ac.ed             
AI Applications Institute,           ARPA:  R.Tobin%uk.ac.ed at nsfnet-relay.ac.uk
Edinburgh University.                UUCP:  ...!ukc!ed.ac.uk!R.Tobin



More information about the Comp.lang.c mailing list