throwaway7894 5 days ago

  #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.

4
gthompson512 5 days ago

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.

fuhsnn 5 days ago

We might get multi-line macros in C2y standard: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3524.txt

quietbritishjim 4 days ago

> 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...

makeset 5 days ago

I knew the name sounded familiar:

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

dwattttt 4 days ago

Also author of PuTTy

HeliumHydride 5 days ago

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:; })

eqvinox 3 days ago

Whether this is "simpler"… debatable ;D

tanelpoder 5 days ago

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.