Coroutines in C
I remember blinking a few times the first time someone showed me Duff’s device. It made me ask “is that really legal C”. Simon Tatham’s article Coroutines in C comes very close to provoking the same response. Wacky, wacky code.
I remember blinking a few times the first time someone showed me Duff’s device. It made me ask “is that really legal C”. Simon Tatham’s article Coroutines in C comes very close to provoking the same response. Wacky, wacky code.
This entry was posted on Saturday, March 5th, 2005 at 5:55 pm and is filed under Computer Science. You can follow any responses to this entry through the RSS 2.0 feed. Responses are currently closed, but you can trackback from your own site.

marks_mass and comment and/or encourage. Posts on this day from years past:
brainwagon is powered by WordPress with White as Milk
designed by Azeem Azeez. Entries (RSS) and Comments (RSS).
April 14th, 2005 at 7:44 pm
About a month ago , MarkV pointed out Simon Tatham’s Coroutines in C. Worth a look if your C chops need a little updating. Yesterday, Rafe had a link to Sam Ruby’s Continuations for Curmudgeons, which showed me Python[IMG [Wiki]]
February 15th, 2006 at 1:36 am
at the bottom, a hint that Duff might also have independently invented this coroutine trick or something very like it. Update, 2005-03-07: Tom Duff confirms this in a blog comment. The “revolting way to use switches to implement interrupt driven state machines” of which he speaks in his
April 16th, 2006 at 4:01 am
is Tom Duff’s own discussion of Duff’s device. Note, right at the bottom, a hint that Duff might also have independently invented this coroutine trick or something very like it. Update, 2005-03-07:Tom Duff confirms this
February 15th, 2006 at 1:36 am
is Tom Duff’s own discussion of Duff’s device. Note, right at the bottom, a hint that Duff might also have independently invented this coroutine trick or something very like it. Update, 2005-03-07:Tom Duff confirms this in a blog comment. The “revolting way to use switches to implement interrupt driven state machines” of which he speaks in his original email is indeed the same trick as I describe here. PuTTY is a Win32 Telnet and SSH client. The SSH protocol code
February 15th, 2006 at 1:36 am
is Tom Duff’s own discussion of Duff’s device. Note, right at the bottom, a hint that Duff might also have independently invented this coroutine trick or something very like it. Update, 2005-03-07:Tom Duff confirms this in a blog comment. The “revolting way to use switches to implement interrupt driven state machines” of which he speaks in his original email is indeed the same trick as I describe here. PuTTY is a Win32 Telnet and SSH client. The SSH protocol code
March 6th, 2005 at 10:04 am
Yeah. I knew this. In my piece about The Device, I mention something about a similar trick for interrupt-driven state machines
that is too horrible to go into. This is what I was referring to. I never thought it was an adequate general-purpose coroutine implementation because it’s not easy to have multiple simultaneous activations of a coroutine and it’s not possible using this method to have coroutines give up control anywhere but in their top-level routine. A simple assembly-language stack-switching library lets you do both of those.
March 7th, 2005 at 4:38 pm
Aha, I suspected as much. I’ve updated the page to upgrade my suspicion to a certainty.
On the occasions when I need a sub-coroutine (or is it a co-subroutine?) to give up control, I generally do it by fiddling with the semantics of the subroutine’s return value and having the subroutine call look something like “while (subroutine(ctx, params) == NOT_FINISHED_YET) return NOT_FINISHED_YET;”, so that the internal stack structure of the entire co-thread is torn down and rebuilt on each invocation.
I agree it’s not nearly as efficient as doing a proper stack switch in assembly language, but on the other hand it beats having to write the stack switcher separately for every different platform and compiler on which I plan to run my code. Porting is more than enough hassle as it is…
March 24th, 2005 at 3:45 am
Revolting co-routines in C
I came across a link to this post (see in particular the response by Tom Duff) over at Brainwagon about using Duff’s device (as if Duff’s device isn’t revolting enough to start with) to implement Coroutines in C. Eughh…. Though…