5 ms·
Thats not the sane way of implementing coroutines in C. Use makecontext/swapcontext if they exist, or implement in assembly.
by justincormack 12y ago
Thats not the sane way of implementing coroutines in C. Use makecontext/swapcontext if they exist, or implement in assembly.
- psykotic 12y agoIt's sane enough that Adam Dunkels wrote a functional if minimal TCP/IP stack using this style of coroutines: http://en.wikipedia.org/wiki/UIP_(micro_IP) http://en.wikipedia.org/wiki/UIP_(micro_IP) Edit: He more recently wrote the Contiki operating system which is entirely based on protothreads: http://www.contiki-os.org/ http://www.contiki-os.org/
- lmb 12y agoIf you look at uIP you'll realize its pretty much a gigantic function [1] with a lot of gotos and ifdefs. Not exactly what I would cite for sane code. 1: https://github.com/contiki-os/contiki/blob/master/core/net/ipv4/uip.c#L673 https://github.com/contiki-os/contiki/blob/master/core/net/i... P.S. UIP doesn't use the coroutine stuff at all it seems.
- psykotic 12y ago> Not exactly what I would cite for sane code. The uses of goto I saw compensate for C's lack of nested breaks and continues. Reasonable people may differ on this topic, but I'm perfectly fine with it. The real sin is obfuscating the natural control flow of a program by introducing dummy variables like try_again and keep_running, or being forced to split things into multiple functions just so you can use a return in lieu of a nested break. (There are single-exit zealots for whom an early-out return like that would be an equally grievous sin as the goto!) In any case, my point was to demonstrate by example that serious software can be written this way. The point wasn't to hold up uIP or Contiki or anything else as impeccable exemplars of coding style. > P.S. UIP doesn't use the coroutine stuff at all it seems. It's not used in the core but in the protocol servers: https://github.com/adamdunkels/uip/blob/master/apps/webserver/httpd.c https://github.com/adamdunkels/uip/blob/master/apps/webserve.... All the PT and PSOCK code is protothreads.
- pm215 12y agoThose aren't portable (in particular they don't exist on Windows, and occasionally they exist but are buggy), and "write a pile of assembly" is even less portable. Personally I think the correct conclusion to draw is "C is not a language which supports coroutines" and not try to implement them at all.
- justincormack 12y agoI hear they are buggy on OSX too. It is not a very big pile of assembly though; that is what eg Go does (gccgo uses and requires swapcontext). EDIT: I agree really, C is not very suitable and that is the correct conclusion but if you are eg implementing a language that has them, assembly is the best solution.
- jeffreyrogers 12y agoThe author is aware of that. In one of the references he notes that PuTTY uses this trick and calls it "the worst piece of C hackery ever seen in serious production code."
- tedunangst 12y agoNote the context functions were marked obsolescent in POSIX 6 and removed entirely in issue 7 because the makecontext function prototype itself cannot be expressed in standard C.
- justincormack 12y agoYeah I know. C really needs to fix some expressiveness issues.
- justincormack 12y agoActually, makecontext should have just taken a single void pointer not a varargs...