Coroutines in C (2000)
chiark.greenend.org.uk
Coroutines in C (2000)
1–10 of 61 posts
Re: Coroutines in C (2000)
#2Several previous discussions: https://hn.algolia.com/?query=Coroutines%20in%20C%20points%3...
Re: Coroutines in C (2000)
#3A 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
Re: Coroutines in C (2000)
#4I 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
Re: Coroutines in C (2000)
#5I thought this was going to be an article about setjmp() and longjmp(), which can be used to implement this kind of mechanism.
Re: Coroutines in C (2000)
#6python has this:
def function():
for i in range(10):
yield iRe: Coroutines in C (2000)
#7I really like that trick. It's very powerful. But I wonder what the difference is between this and protothreads?
Re: Coroutines in C (2000)
#8A 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).
- [0] https://byuu.org/library/libco/
- [1] https://fluentbit.io
Re: Coroutines in C (2000)
#9A 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
For those wondering how it works, libdill keeps track of context between function calls and does some clever stack modification in assembly.
Re: Coroutines in C (2000)
#10What's the purpose of this code in the original decompressor? Assuming c is an uchar, aren't EOF and 0xFF equal?
if (c == 0xFF) {
len = getchar();
c = getchar();
while (len--)
emit(c);
}