Coroutines in C

March 5, 2005 | Computer Science | By: Mark VandeWettering

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.

Comments

Pingback from Flutterby!
Time 4/14/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]]

Pingback from 逸岚居
Time 2/15/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

Pingback from Coroutines in C
Time 4/16/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

Pingback from 逸岚居
Time 2/15/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

Pingback from 用心感受这个世界
Time 2/15/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

Comment from Tom Duff
Time 3/6/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.

Comment from Simon Tatham
Time 3/7/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…

Trackback from Vidar Hokstad’s random musings
Time 3/24/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…

Pingback from Coroutines in C | ??
Time 3/31/2015 at 9:17 pm

[…] 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 […]