6 ms·
Libaco: A fast and lightweight C asymmetric coroutine library
- rjeli 8y agoComparison to libmill/libdill?
- FrozenVoid 8y agoLibmill is stack-unsafe https://github.com/sustrik/libmill/issues/167 https://github.com/sustrik/libmill/issues/167
- dmytroi 8y agoWould be nice to compare performance with others libraries! For example Stacklet [0] and Boost Context [1]. - [0] https://github.com/mozillazg/pypy/tree/master/rpython/translator/c/src/stacklet https://github.com/mozillazg/pypy/tree/master/rpython/transl... - [1] https://github.com/boostorg/context/tree/develop/src/asm https://github.com/boostorg/context/tree/develop/src/asm
- lkurusa 8y agoThis looks nice. It's always good to see formal proofs!
- ur-whale 8y agoA maybe naive question here, and also maybe slightly off-topic but: is there ever a reason to use co-routines when you have full-fledged multi-threading available? As much as multi-threading can quickly become a headache wrt robustness and provable behaviour, I've always felt that a complex co-routine based codebase - even if you can theoretically reason about it using FSMs - is harder to "fit in head".
- dmytroi 8y agoYou can easily have 1 million coroutines in-flight (my humble testing was showing that PyPy Stacklet on i4980hq has speed around 10-20 million switches/per second/per core). While with native threads I think it's in ballpark of hundred running threads, maybe single digit thousands. After that it looks like performance degrades too much. But it all depends on an usecase, spawning a thread per user in a webserver would be slower than thread per core.
- charliesome 8y agoHundreds of thousands of OS threads is viable on Linux. There's a couple of gotchas - you need to tweak the max pid and max memory map limits, but otherwise this is an approach that works just fine.
- akvadrako 8y agoThis is wrong. 1 million threads is doable, though 4 million is the max Linux supports. Coroutines are basically just user-space threads, so they scale the same way. You need to keep around a stack for each and swap it in and out of cache. See for example: https://stackoverflow.com/questions/1900165/how-long-does-it-take-to-create-1-million-threads-in-haskell https://stackoverflow.com/questions/1900165/how-long-does-it...
- stevefan1999 8y agoI suggest using setjmp/longjmp instead of copying registers manually.
- ufo 8y agoHow would you do that? longjmp let's you jump back to the parent coroutine but after that you cannot jump back to the child to continue where it left off.
- cryptonector 8y agoBy using different stacks for each coroutine. But it's still not trivial.
- ausjke 8y agothe most starred co-routine C is: https://github.com/Tencent/libco https://github.com/Tencent/libco It's said golang learned from libtask: https://github.com/jamwt/libtask https://github.com/jamwt/libtask And something else: https://github.com/baruch/libwire https://github.com/baruch/libwire https://github.com/halayli/lthread https://github.com/halayli/lthread (multi-core friendly)
- byuu 8y agoDisappointing that Tencent took my library's name. I'd been using the name since 2006 or so. I guess you can't expect a two-letter lib* name to remain unique. My luck that theirs would become the most well known in this space, though.
- skolemtotem 8y agoHi Byuu! You're a cool guy and I hope I can be like you one day!
- ausjke 8y agohi byuu, are you going to add arm64 support? or considering C99? if I have multiple cores, what's the best way to leverage that along with libco? will cpu affinity help?
- byuu 8y agoAarch64 support will show up as soon as I can get a Linux distro running on it to test the implementation with. My understanding is the ARM ABI isn't as well defined, and you tend to need slightly different implementations per device, so it may not be as useful as simply using the setjmp version. You can set -DLIBCO_MP to get the co_active handle to be multi-threaded. Each preemptive thread would then have its own set of cooperative threads.
- petee 8y agoto be fair you should point out that libco is written in c++, where as OP is actually written in c
- cryptonector 8y agoThe fastest C co-routine library is going to be inside PuTTY [0]. [0] https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
- deleted 8y ago[deleted]
- ComputerGuru 8y agoDoes anyone know if this plays well with multiple threads with a separate coroutine machine on each simultaneously?
- rijoja 8y agoDoes anybody feel like adding some define black magic to sugar coat the syntax?