A really good library that we use is libco[0], it supports different architectures and mechanisms. We use it extensively on Fluent Bit[1] to manage async IO network operations (epoll + coroutines).
A cool library that implements C coroutines, available for both idiomatic C (http://libdill.org/) and in the same style as Go (http://libmill.org/) with line by line translations
I ran with this idea last year and wrote a proof of concept library that wraps libev using using this concept to allow you to write async C code inline. It's about 250 lines of macro abuse and the resulting code looks pretty funky but it ultimately works. I have a simple echo server example here: https://github.com/jeremycw/zasync/blob/master/echo_server.c
As an OOP dev this kind of horrifies me, even though it works okay (though it's surely not thread-safe?)
Nevertheless, even in C, I'd find it conceptually simpler to understand if the functions were written as pure functions, returning a tuple with (c, state) and passing the state back into the caller. Using non-idiomatic tricks and macros in this way is harder to read and understand.
Here's a POSIX compliant portable getopt() routine I wrote which uses this style. Is this hard to read? Note that it's several times shorter than every other implementation I've ever seen, and if I do say so myself I think it's eminently readable.
Has anyone used these to write interrupt service routines? Seems like there's an opportunity here. You can take your existing synchronous code and not have to rewrite it into something like continuation-passing style. This is a source of a lot of bugs.
> Protothreads is a library implementation of this trick. In fact, the protothreads creator even references the linked site in their explanation of protothreads (very last paragraph)
c should not be a char or unsigned char, because the return type of getchar() is "int". If you put it into a char-width variable then you lose the distinction between EOF (which is -1) and the byte 0xff. Getting the type of 'c' wrong is quite a common bug, because the API makes it an easy mistake to make. In this case, if you look down at the eventual transformed code in the "Evaluation" section you'll see that 'c' is indeed correctly declared with 'int' type.
I guess one of the applications of Coroutines could be WebAssembly. When you run WebAssembly on a website which needs 100% of the CPU for more than a few ms, you have to use the "setTimeout(fn, 0)" trick to prevent the "this website stopped responding" message. That means you have to store the current state at some point and continue in the function fn (after waiting 0 ms). Languages which support coroutines out of the box would help a lot here.
Protothreads is a library implementation of this trick. In fact, the protothreads creator even references the linked site in their explanation of protothreads (very last paragraph) [1].
That page uses very simple HTML. Just paragraphs of text divided into sections with headings. Some code examples laid out side-by-side with a table. Tiny stylesheet. No JS. Loads quickly, easy to read. Warms the cockles of my heart.
This page is written by the creator of PuTTY. They could probably release this as a straight TXT file and still get the readership they deserve. Simon Tatham know's what he's doing!
[+] [-] edsiper2|7 years ago|reply
- [0] https://byuu.org/library/libco/
- [1] https://fluentbit.io
[+] [-] _fbpt|7 years ago|reply
[+] [-] brobdingnagians|7 years ago|reply
[+] [-] saagarjha|7 years ago|reply
[+] [-] jeremycw|7 years ago|reply
[+] [-] plaguuuuuu|7 years ago|reply
Nevertheless, even in C, I'd find it conceptually simpler to understand if the functions were written as pure functions, returning a tuple with (c, state) and passing the state back into the caller. Using non-idiomatic tricks and macros in this way is harder to read and understand.
[+] [-] wahern|7 years ago|reply
[+] [-] c-smile|7 years ago|reply
https://www.codeproject.com/Tips/29524/Generators-in-C
[+] [-] entelechy|7 years ago|reply
CoroutineTS uses coroutines that are implemented on the LLVM-IR level, as a result they get optimized away in many cases.
Unfortunately the CoroutineTS has also some design flaws around allocations and ownership
[+] [-] ThJ|7 years ago|reply
[+] [-] evancox100|7 years ago|reply
[+] [-] nraynaud|7 years ago|reply
[+] [-] mcwoods|7 years ago|reply
There are implementations that use the GCC goto and label pointer, which then avoid the need for a switch statement.
[1] http://dunkels.com/adam/pt/
[+] [-] srean|7 years ago|reply
In lining the answer.
> Protothreads is a library implementation of this trick. In fact, the protothreads creator even references the linked site in their explanation of protothreads (very last paragraph)
[+] [-] boomlinde|7 years ago|reply
[+] [-] hectorhector|7 years ago|reply
[+] [-] pm215|7 years ago|reply
[+] [-] varjag|7 years ago|reply
The code snippet itself is run length encoding with 0xff as an escape.
[+] [-] tonyedgecombe|7 years ago|reply
[+] [-] s-macke|7 years ago|reply
[+] [-] tangent128|7 years ago|reply
[+] [-] dang|7 years ago|reply
[+] [-] jfries|7 years ago|reply
[+] [-] VygmraMGVl|7 years ago|reply
[1] http://dunkels.com/adam/pt/expansion.html
[+] [-] 0db532a0|7 years ago|reply
[+] [-] guenthert|7 years ago|reply
[+] [-] bluesnowmonkey|7 years ago|reply
[+] [-] sandov|7 years ago|reply
If you add {max-width: 60em; margin: auto;} to the body tag it looks much better.
[+] [-] Intermernet|7 years ago|reply
[+] [-] airstrike|7 years ago|reply
[+] [-] slezyr|7 years ago|reply
Not on 27" with browser in full screen.
[+] [-] yydcool|7 years ago|reply