You are on page 1of 1

brainwagon

"There is much
pleasure in useless The Arduino n’ Gameduino Satellite Tracker Making an ATtiny13 Powered Pumpkin! Computer Checkers The Enigma 2600
knowledge." —
Bertrand Russell

COMPUTER SCIENCE

Coroutines in C
By Mark VandeWettering 3/5/2005 9 Comments

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.

PREVIOUS NEXT

Chinese Calligraphy Constantine

Similar Posts

More on Caxton Foster’s Lego Logic Gates FPGA CPUs


Blue Architecture… By Mark VandeWettering By Mark VandeWettering
12/14/2004 0 Comments 9/6/2005 0 Comments
By Mark VandeWettering
3/26/2016 2 Comments

9 Comments

Pingback: Flutterby!

Pingback: 逸岚居

Pingback: Coroutines in C

Pingback: 逸岚居

Pingback: ç”¨å¿ƒæ„Ÿå —è¿™ä¸ªä¸–ç•Œ

Tom Duff says:


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.

Simon Tatham says:


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…

Pingback: Vidar Hokstad's random musings

Pingback: Coroutines in C | ??

Comments are closed.

© 2021 brainwagon - WordPress Theme by Kadence WP

You might also like