[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: C coroutines in ANSI C
- From: David Given <dg@...>
- Date: Thu, 4 Aug 2005 16:51:31 +0100
On Thursday 04 August 2005 10:46, Dave Dodge wrote:
[...]
> >From a glance at the examples, the main problem that sticks out is
> that all of the variables have to be static. Note that even local
> loop counters within the functions are static. I think this is
> because the library has no way of actually saving/restoring stack
> data.
I've seen this. Neat, but probably not particularly useful from C.
It would, however, be quite handy for encapsulating state machines inside
functors in C++ --- you wouldn't have to use all those nasty static variables
because you'd use class variables instead.
[...]
> A generic approach would be a C-to-C translator. Basically you add
> something like a "resumable" function-specifier to the C grammar,
> and perhaps a "yield" keyword, so that you can do things like this:
You may be interested in OpenC++:
http://opencxx.sourceforge.net/
It's a programmable C++ transformer. It will read in a C++ source file into a
syntax tree, let you do all kinds of wacky stuff to it, and write it out
again. One of the examples is implementing a forall(){} construction on a
collection class, which takes as a parameter a code block. Very neat. It
could do the kind of code rewriting you're talking about in its sleep (if you
programmed it to).
> I'm not sure this is pure ANSI C. The stack/context switching is the
> tricky part since C doesn't really have a portable way of doing that.
Last time we talked about coroutines, I suggested ucontext, but it was pointed
out to me that they're not terribly widespread --- Cygwin doesn't have them,
for example. setjmp() based systems are more portable and Edgar Toernig
posted a link to this sample code that implements a bunch of platforms:
http://goron.de/~froese/httpp/threads.c
--
+- David Given --McQ-+
| dg@cowlark.com | "The only thing to prevent what's past is to put a
| (dg@tao-group.com) | stop to it before it happens." --- Sir Boyle Roche
+- www.cowlark.com --+
Attachment:
pgpfBfkrpyTWq.pgp
Description: PGP signature