Most active commenters

    ←back to thread

    218 points signa11 | 12 comments | | HN request time: 0.611s | source | bottom
    1. throwaway7894 ◴[] No.43681154[source]

      #define hc_task_yield(task)   
      do {     
        task->state = __LINE__;   
        return;     
        case __LINE__:;           
      } while (0) 
    
    
    That's just diabolical. I would not have thought to write "case __LINE__". In the case of a macro, using __LINE__ twice expands to the same value where the macro is used, even if the macro has newlines. It makes sense, but TIL.
    replies(5): >>43681327 #>>43681410 #>>43681515 #>>43681600 #>>43684691 #
    2. veltas ◴[] No.43681327[source]
    Credit to Simon Tatham

    https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

    replies(2): >>43686337 #>>43703289 #
    3. HeliumHydride ◴[] No.43681410[source]
    With GNU extensions, you can make a simpler coroutine macro without switch/case abuse:

        #define CO_BEGIN static void* cr_state_ = &&cr_st_0; goto *cr_state_; cr_st_0:
        #define CO_RETURN(x) ({ __label__ resume; cr_state_ = &&resume; return (x); resume:; })
    replies(2): >>43682077 #>>43714392 #
    4. ◴[] No.43681515[source]
    5. gthompson512 ◴[] No.43681600[source]
    Minor correction, macros CANT have newlines, you need to splice them during preprocessing using \ followed by a new line, the actual code has these:

    from https://github.com/codr7/hacktical-c/blob/main/macro/macro.h

    #define hc_align(base, size) ({ \ __auto_type _base = base; \ __auto_type _size = hc_min((size), _Alignof(max_align_t)); \ (_base) + _size - ((ptrdiff_t)(_base)) % _size; \ }) \

    After preprocessing it is a single line.

    replies(1): >>43682532 #
    6. ◴[] No.43682077[source]
    7. fuhsnn ◴[] No.43682532[source]
    We might get multi-line macros in C2y standard: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3524.txt
    8. tanelpoder ◴[] No.43684691[source]
    I've written C on-and-off for over 30 years (just various throw-away prototypes and OS/app interaction microbenchmarks) and it took a while + a web search to get it. Diabolical indeed. Edit: And makes sense in hindsight.
    9. makeset ◴[] No.43686337[source]
    I knew the name sounded familiar:

    Simon Tatham's Portable Puzzle Collection https://www.chiark.greenend.org.uk/~sgtatham/puzzles/

    replies(1): >>43702650 #
    10. dwattttt ◴[] No.43702650{3}[source]
    Also author of PuTTy
    11. quietbritishjim ◴[] No.43703289[source]
    > no commonly used high level language supports the coroutine call primitive

    Shows how old this post is. In fact I remember reading it well over 10 years ago, maybe more like 20. archive.org says that it's at least as old as 2001. A great article.

    I'm very excited to see he's published a new article on C++20 coroutines. I've read (or maybe skimmed...) a few introductions and not really got them, despite having used C# and Python coroutines a lot with no problems (even making changes to an async runtime for Python). Given how clear his C coroutine article is, I'm optimistic about the C++ article.

    > So, after the course, I went away and studied on my own, and wrote the introduction to C++ coroutines that I’d have liked to see.

    https://www.chiark.greenend.org.uk/~sgtatham/quasiblog/corou...

    12. eqvinox ◴[] No.43714392[source]
    Whether this is "simpler"… debatable ;D