Can you help (was Jumping then jumping back again)

Bakul Shah bvs at light.uucp
Sat Feb 10 20:08:11 AEST 1990


In article <1797 at neon.siesoft.co.uk> huw at xstuart.siesoft.co.uk (Huw Roberts) writes:
>I remember there was a discussion a little while ago on coroutines/
>lightweight processes and portable ways of implementing them in C.
>I think the original subject was "Jumping then jumping back again".
>I wonder if someone would be good enough to send me a summary of the
>conclusions of the mail thread since I do not believe we have any way
>of getting access to an archive of the discussion (we are not on
>Internet and have no access to ftp or niftp).

There were many such threads....  Here is one point of view based
on implementing coroutines on two very different architectures.

To implement coroutines what is needed is an ability to save and
restore a coroutine's context (which consists of, usually, all of
the registers minus the temps that get destroyed across a call).
On many machines, but not all, this is effectively what the
setjmp/logjmp routines do so on these machines you can implement
coroutines entirely in C.  On machines with setjmp/longjmp that
are smarter and/or more picky you will need some assembly glue.
Then there are machines with two stacks (register stack & memory
stack) which add their own complication.

Also note that N coroutines will *need* N stacks but C does not
need more than one (assuming a new frame is not malloced on every
call).  A C machine with only one stack (and no way to switch it)
can be strictly standard conforming and yet won't allow you any
coroutines (you can emulate them but that is different).

I think if you *want* portable coroutines in C, you have to add
some standard extension to it.  Something like

	old_stack = stack_switch(new_stack)

will do.  The point is, in order to manage processor registers
properly the coroutine context switcher and the compiler have to
collaborate.  Also, the extended language would have to guarantee
sane behavior when stacks are switched.

-- Bakul Shah
   ..!{ames,sun,ucbvax,uunet}!amdcad!light!bvs



More information about the Comp.lang.c mailing list