frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Hacking Coroutines into C

https://wiomoc.de/misc/posts/hacking_coroutines_into_c.html
75•jmillikin•6h ago

Comments

mikepurvis•6h ago
FreeRTOS can also be used with a cooperative scheduler: https://www.freertos.org/Why-FreeRTOS/Features-and-demos/RAM...

That said, if I was stuck rolling this myself, I think I’d prefer to try to do it with “real” codegen than macros. If nothing else it would give the ability to do things like blocks and correctness checks, and you’d get much more readable resulting source when it came to stepping through it with a debugger.

Neywiny•6h ago
The intent here is nice. I historically hate state machines for sequential executioners. To me they make sense in FPGA/ASIC/circuits. In software, they just get so complicated. I've even seen state managers managing an abstracted state machine implementing a custom device to do what's ultimately very sequential work.

It's my same argument that there should be no maximum number of lines to a function. Sometimes, you just need to do a lot of work. I comment the code blocks, maybe with steps/parts, but there's no point in making a function that's only called in one place.

But anything is better than one person I met who somehow was programming without knowing how to define their own functions. Gross

TechDebtDevin•5h ago
I actually write a lot of Go in state machine like patterns. My state types files would make you think im schizophrenic. I just finished up a project this week that was 10k lines of comments in 18k loc. Noone else has to read it tho, they actually probably appreciate it if they did.
throwaway81523•3h ago
> there's no point in making a function that's only called in one place.

There's nothing wrong with doing that if it helps make your code clearer. The compiler's optimizer will inline it when appropriate so there's no runtime overhead either.

duped•3h ago
> I comment the code blocks, maybe with steps/parts, but there's no point in making a function that's only called in one place.

I encourage junior developers that get into this habit (getting worse now, with LLMs) to convert the comment into a function name and add the block as a function, thinking pretty carefully about its function signature. If you have a `typedef struct state` that gets passed around, great.

The reason for splitting up this code is so that the person writing it doesn't fuck up, the input/output is expressed as types and validated before they push it. It's easy for me to review, because I can understand small chunks of code better than big chunks, and logically divides up the high level architecture from the actual implementation so I can avoid reviewing the latter if I find trouble with the former. It's also good as a workflow, where you can pair to write out the high level flow and then split off to work on implementation internally. And most importantly, it makes it possible to test the code.

I have had this discussion with many grumbly developers that think of the above as a "skill issue." I don't really want to work with those people, because their code sucks.

throwaway81523•6h ago
As the article acknowledges at the end, this is sort of like protothreads which has been around for ages. The article's CSS was so awful that I didn't read anything except the last paragraph, which seemed to tell me what I wanted to know.
mananaysiempre•5h ago
Right, this is more or less this blog author’s riff on (PuTTY author) Simon Tatham’s old page on coroutines using switch[1], which itself indicates that Tom Duff thought of this (which makes sense, it’s only a half-step away from Duff’s device) and described it as “revolting”. So in this sense the idea predates the standardization of C.

[1] https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

hecanjog•4h ago
> The article's CSS was so awful

Small text sizes? What is the problem for you?

shakna•4h ago
Whilst I wouldn't call it "awful", the spacing between the lines isn't helping any.
userbinator•3h ago
Of course, the project didn’t allow us to use an RTOS.

That tends to just make the project eventually implement an approximation of one... as what appears to have happened here.

How I'd solve the given problem is by using the PWM peripheral (or timer interrupts if no PWM peripheral exists) and pin change interrupts, with the CPU halted nearly 100% of the time. I suspect that approach is even simpler than what's shown here.

adinisom•2h ago
My favorite trick in C is a light-weight Protothreads implemented in-place without dependencies. Looks something like this for a hypothetical blinky coroutine:

  typedef struct blinky_state {
    size_t pc;
    uint64_t timer;
    ... variables that need to live across YIELDs ...
  } blinky_state_t;
  
  blinky_state_t blinky_state;
  
  #define YIELD() s->pc = __LINE__; return; case __LINE__:;
  void blinky(void) {
    blinky_state_t *s = &blinky_state;
    uint64_t now = get_ticks();
    
    switch(s->pc) {
      while(true) {
        turn_on_LED();
        s->timer = now;
        while( now - s->timer < 1000 ) { YIELD(); }
        
        turn_off_LED();
        s->timer = now;
        while( now - s->timer < 1000 ) { YIELD(); }
      }
    }
  }
  #undef YIELD
Can, of course, abstract the delay code into it's own coroutine.

Your company is probably using hardware containing code I've written like this.

What's especially nice that I miss in other languages with async/await is ability to mix declarative and procedural code. Code you write before the switch(s->pc) statement gets run on every call to the function. Can put code you want to be declarative, like updating "now" in the code above, or if I have streaming code it's a great place to copy data.

csmantle•2h ago
Yeah. Protothreads (with PT_TIMER extensions) is one of the classics libraries, and also was used in my own early embedded days. I was totally fascinated by its turning ergonomic function-like macros into state machines back then.
fjfaase•1h ago
I have used this approach, with an almost similar looking define for YIELD myself.

If there is just one instance of a co-routine, which is often the case for embedded software, one could also make use of static variables inside the function. This also makes the code slightly faster.

You need some logic, if for example two co-routines need to access a shared peripheral, such as I2C. Than you might also need to implement a queue. Last year, I worked a bit on a tiny cooperative polling OS, including a transpiler. I did not finish the project, because it was considered too advanced for the project I wanted to use it for. Instead old fashion state machines documented with flow-charts were required. Because everyone can read those, is the argument. I feel that the implementation of state machines is error prone, because it is basically implementing goto statements where the state is like the label. Nasty bugs are easily introduced if you forget a break statement at the right place is my experience.

adinisom•1h ago
Yes, 100%. State transitions are "goto" by another name. State machines have their place but tend to be write-only (hard to read and modify) so are ideally small and few. Worked at a place that drank Miro Samek's "Practical Statecharts in C/C++" kool-aid... caused lots of problems. So instead I use this pattern everywhere that I can linearize control flow. And if I need a state machine with this pattern I can just use goto.

Agreed re: making the state a static variable inside the function. Great for simple coroutines. I made it a pointer in the example for two reasons:

- Demonstrates access to the state variables with very little visual noise... "s->"

- For sub-coroutines that can be called from multiple places such as "delay" you make the state variable the first argument. The caller's state contains the sub-coroutine's state and the caller passes it to the sub-coroutine. The top level coroutine's state ends up becoming "the stack" allocated at compile-time.

dkjaudyeqooe•43m ago
A cleaner, faster way to implement this sort of thing is to use the "labels as values" extension if using GCC or Clang []. It avoids the switch statement and associated comparisons. Particularly useful if you're yielding inside nested loops (which IMHO is one of the most useful applications of coroutines) or switch statements.

[] https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html

Nursie•2h ago
Cooperative multithreading via setjmp and longjmp has been around in C since the 80s at least.

I’m not sure this is so much hacking as an accepted technique from the old-old days which has somewhat fallen out of favour, especially as C is falling a little outside of the mainstream these days.

Perhaps it’s almost becoming lost knowledge :)

ajb•1h ago
This isn't using setjmp/longjmp

It's using Simon Tatham's method based on Duff's device (https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html)

syncurrent•17m ago
A similar approach, but rooted in the idea of synchronous languages like Esterel or Blech:

https://github.com/frameworklabs/proto_activities

Leading your engineers towards an AI-assisted future

https://blog.thepete.net/blog/2025/06/26/leading-your-engineers-towards-an-ai-assisted-future/
1•r4um•1m ago•0 comments

Thoughts on Motivation and My 40-Year Career

https://charity.wtf/2025/07/09/thoughts-on-motivation-and-my-40-year-career/
1•r4um•2m ago•0 comments

Assumptions

http://theprogrammersparadox.blogspot.com/2025/07/assumptions.html
1•r4um•3m ago•0 comments

Sega Enterprises Ltd. vs. Accolade, Inc., 977 F.2d 1510 (9th Cir. 1992)

https://en.wikipedia.org/wiki/Sega_v._Accolade
1•Bluestein•9m ago•0 comments

UK graduates share their job-hunting woes amid the AI fallout

https://www.theguardian.com/money/2025/jul/13/student-debt-graduates-share-job-hunting-woes-ai-fallout
1•mykowebhn•12m ago•0 comments

Nearly two-thirds of UK workers turning to drink because of job pressures

https://news.sky.com/story/nearly-two-thirds-of-uk-workers-turning-to-drink-because-of-job-pressures-survey-says-13395997
3•austinallegro•15m ago•0 comments

Gmail AI hallucinates, distorts email contents

https://www.t-online.de/digital/aktuelles/id_100811852/gmail-fantasiert-googles-mail-programm-verfaelscht-fremde-inhalte.html
2•disentanglement•16m ago•0 comments

Show HN: TXT OS – Open-Source AI Reasoning, One Plain-Text File at a Time

https://github.com/onestardao/WFGY/tree/main/OS
4•TXTOS•21m ago•2 comments

Empirical evidence of LLM's influence on human spoken communication

https://arxiv.org/abs/2409.01754
2•ransom_rs•22m ago•0 comments

I built Findly.tools – a successor to 1000.tools

https://findly.tools/
1•drdruide•23m ago•1 comments

A (Limited) Defence of Footnotes

https://www.kryogenix.org/days/2025/07/03/a-limited-defence-of-footnotes/
2•jruohonen•27m ago•0 comments

Retrieval Embedding Benchmark

https://huggingface.co/spaces/embedding-benchmark/RTEB
1•fzliu•29m ago•0 comments

Stop memoizing Hash lookups in Ruby

https://nithinbekal.com/posts/ruby-hash-memoization/
2•thunderbong•30m ago•0 comments

Summary of Bluesky Whitepaper

https://twitter.com/RealmOfSoftware/status/1904941497910505833
1•krsoninikhil•31m ago•0 comments

Vibe-Engineering: When AI Does All the Coding, What Do We Do?

https://img.ly/blog/vibe-engineering-when-ai-does-all-the-coding-what-do-we-actually-do/
2•tosh•31m ago•0 comments

Show HN: We developed an AI tool to diagnose car problems

https://autoai.help
2•mariyan250•33m ago•0 comments

Ask HN: I own wtf.store -what to build that makes people say WTF (in gud way)?

1•deep_thinker26•34m ago•0 comments

Windows Update forced new version of Chengjie which is unusable to users

https://old.reddit.com/r/Windows11/comments/1lvelyb/warning_to_windows_11_24h2_chengji_users_for_july/
2•charlieyu1•35m ago•1 comments

Moiré materials based on M-point twisting

https://www.nature.com/articles/s41586-025-09187-5
2•Bluestein•36m ago•0 comments

Iota Legacy Migration Tool Issue

https://old.reddit.com/r/Iota/comments/1kx02eb/iota_legacy_migration_tool_issue/
1•Bluestein•44m ago•0 comments

Turns out you can just hack any train in the USA

https://twitter.com/midwestneil/status/1943708133421101446
5•lyu07282•44m ago•1 comments

Kawase Hasui Catalogue

https://mokuhanga1.com/kawase-hasui/
1•vinhnx•44m ago•0 comments

Trump announces 30% tariffs on EU and Mexico, starting Aug. 1

https://www.cnbc.com/2025/07/12/trump-tariffs-european-union-eu-mexico-trade.html
3•kamaraju•55m ago•5 comments

Show HN: I built a entire blogging platform just for myself lol

https://lykhari.com
1•yaKashif•59m ago•0 comments

Ask HN: How important is to learn Software Defined Networking?

1•shivajikobardan•1h ago•1 comments

Govern Amazon Bedrock Gen AI Models in SageMaker Unified Studio

https://aws.amazon.com/blogs/machine-learning/configure-fine-grained-access-to-amazon-bedrock-models-using-amazon-sagemaker-unified-studio/
1•funnyguy678•1h ago•0 comments

Hill Space: Neural nets that do perfect arithmetic (to 10⁻¹⁶ precision)

https://hillspace.justindujardin.com/
2•peili7•1h ago•1 comments

Dynamic Chunking for End-to-End Hierarchical Sequence Modeling

https://arxiv.org/abs/2507.07955
1•fzliu•1h ago•0 comments

Show HN: Invoicesplitter – Split and Rename invoices in seconds

https://www.invoicesplitter.com/
1•Protonu3102•1h ago•0 comments

The Jackpot Age

https://twitter.com/thiccyth0t/status/1943773395209924769
2•Michelangelo11•1h ago•0 comments