Coroutines in C
Peter da Silva
peter at ficc.uu.net
Mon Aug 28 23:09:08 AEST 1989
In article <214 at castle.ed.ac.uk>, sam at lfcs.ed.ac.uk (S. Manoharan) writes:
> In article <5773 at ficc.uu.net> peter at ficc.uu.net (Peter da Silva) writes:
> >In many C compilers I wouldn't even need to write any assembly code, since
> >setjmp/longjmp can be used to implement the context switch.
[ problem deleted to save space ]
> Intutively, it appears the co-routines could be applied in this
> context. Am I correct?
I believe so.
> And then, is there an easy way to tackle this problem?
Have a look at my article a couple of weeks back where I introduced this
subject, then implement coroutines :->.
In existing, portable, C... no.
> Since there could be a number of event?()s, I think setjmp()
> and longjmp() would be of no help.
The naive implementation of setjmp() is to save all the registers in jmp_buf.
The corresponding naive implementation of longjmp is to restore those registers
and return. If this implementation is used, it should be possible to create
a fake stack frame and register set in a jmp_buf and tranfer control to it
using longjmp. This is, of course, hideously non-portable. But if the calling
sequence for coroutines can be standardized then this non-portable code could
be hidden inside co_call() and friends.
--
Peter da Silva, *NIX support guy @ Ferranti International Controls Corporation.
Biz: peter at ficc.uu.net, +1 713 274 5180. Fun: peter at sugar.hackercorp.com. `-_-'
"Just once I'd like to meet an alien menace that isn't immune to bullets" 'U`
-- The Brigadier, Dr Who.
More information about the Comp.lang.c
mailing list