[HN Gopher] Bunki, a C Coroutine Library
       ___________________________________________________________________
        
       Bunki, a C Coroutine Library
        
       Author : anfilt
       Score  : 117 points
       Date   : 2023-03-13 09:16 UTC (13 hours ago)
        
 (HTM) web link (github.com)
 (TXT) w3m dump (github.com)
        
       | delfinom wrote:
       | Should be saving rdi + 0x0020 on windows to save the current
       | fiber storage pointer on jump.
       | 
       | Certain win32 api calls actually use fibers deep down even if you
       | don't use them. You will get a crash after bouncing around. I
       | vaguely remember(or think) schannel is one such set of win32
       | functions that'll cause it.
        
         | anfilt wrote:
         | I will take a look at this if that's, if I need to save the
         | fiber_data field from the TEB on windows easy enough to add.
        
       | stefanos82 wrote:
       | How does it compare with https://github.com/edubart/minicoro ?
        
         | anfilt wrote:
         | Anything in particular your curious about?
         | 
         | For a few similar things:
         | 
         | * It can Yield from anywhere
         | 
         | * You can push data on the stack when creating a co-routine
         | 
         | * It has a slot for local storage in the coroutine (the library
         | you linked calls it user_data).
         | 
         | * It should be pretty light weight since it uses assembly and
         | saves as little state as possible based off the calling the
         | conventions.
         | 
         | There are differences though. One big difference is I do have
         | functions that let you call functions that generate deep call
         | stacks on the thread stack.
        
       | iainmerrick wrote:
       | _The name is Japanese word bunki (Fen Qi ) which means to branch
       | off. I consider the name quite fitting for a coroutine library
       | just google image (Fen Qi ) and you will see what I mean._
       | 
       | Good name! That's very nice.
        
         | froh wrote:
         | to save you a click: the image search gives you railway
         | switches. sophisticated ones.
        
         | dagurp wrote:
         | Bunki in Icelandic means batch or stack.
        
           | anfilt wrote:
           | Lol even better it since it is a stackful co-routine library.
           | =)
        
           | actionfromafar wrote:
           | I bet it's related to "bunch".
        
         | [deleted]
        
         | anfilt wrote:
         | Thanks, I am definitely happy with the name.
        
       | bonzini wrote:
       | Don't use stackful coroutines. Compilers don't understand it and
       | you will sooner or later get miscompilations or bugs,
       | particularly if your code uses thread-local storage (which
       | doesn't take a lot of effort, for example "errno" is a thread-
       | local variable).
        
         | OskarS wrote:
         | Do you have any specific reason to say why it will cause
         | miscompilation? Anything in particular that makes this UB? My
         | understanding is that as long as your functions compile to the
         | correct ABI, it shouldn't be a problem. All you're doing is
         | setting different stack/frame pointers before calling the
         | function.
         | 
         | As for the errno thing: the whole point about coroutines is
         | that they are cooperatively scheduled, they don't yield unless
         | you specifically tell them to. As long as you don't insert a
         | yield-point in between the function that sets errno and then
         | reading the value (and you should also always read errno
         | immediately anyway), I don't see what the problem is (except
         | errno being a terrible API in general, but that ship has pretty
         | much sailed).
        
         | anfilt wrote:
         | There are lots of things that happen when a program runs that a
         | compiler is unaware of. A compilers don't run during runtime...
         | So I am confused what you mean by mis-compilations.
         | 
         | As for runtime this is why people follow an ABI. Otherwise lots
         | of things like dynamic linking ect... would not work.
        
       | samsquire wrote:
       | Thanks for this.
       | 
       | There is a really good blog post to understand coroutines from an
       | assembly perspective here: https://blog.dziban.net/coroutines/
       | 
       | I ported the intel assembly syntax in that blog post to at&t
       | syntax and assembled it with GNU Assembler
       | https://github.com/samsquire/assembly as coroutines.S
       | 
       | there is also protothreads and Tina http://dunkels.com/adam/pt/
       | https://github.com/slembcke/Tina
        
         | anfilt wrote:
         | No problem. If you have any questions feel free to ask.
        
           | qprofyeh wrote:
           | My C is a bit rusty. What do (void*)0xcafe and (void*)0xbeef
           | in the example mean?
        
             | anfilt wrote:
             | The example just shows some of the functions for the
             | library being used. Mainly the storing and returning of
             | values. As for the numbers like "(void*)0xcafe" it's
             | casting the number 0xcafe (in hex) to a void*. Why those
             | constants they just easy to read in hex since they happen
             | to spell a word. They could be anything, depending on your
             | application like a pointer to data or some important
             | constant ect...
        
               | qprofyeh wrote:
               | Thanks, so void* is the any type in C. And the argument
               | is not necessarily a pointer nor should it be
               | dereferenced. I think I understand.
        
               | Warwolt wrote:
               | More particularly, I would describe it as a "pointer to
               | anything". The size of the value will be exactly the size
               | used for pointers in the given system (e.g. 8 bytes).
               | 
               | If you happen to know what a void* is pointing to, you
               | can cast it to another type like "my_struct_t*" and
               | deference the value. I would call it a work around for C
               | lacking true polymorphism.
        
       ___________________________________________________________________
       (page generated 2023-03-13 23:01 UTC)