4 ms·
#define hc_task_yield(task) do { task->state = __LINE__; return; case __LINE__:; } while (0) That's just diabolical
by throwaway7894 1y 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.
- veltas 1y agoCredit to Simon Tatham https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
- makeset 1y agoI knew the name sounded familiar: Simon Tatham's Portable Puzzle Collection https://www.chiark.greenend.org.uk/~sgtatham/puzzles/ https://www.chiark.greenend.org.uk/~sgtatham/puzzles/
- dwattttt 1y agoAlso author of PuTTy
- quietbritishjim 1y 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/coroutines-c++20/ https://www.chiark.greenend.org.uk/~sgtatham/quasiblog/corou...
- HeliumHydride 1y agoWith 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:; })
- deleted 1y ago[deleted]
- eqvinox 1y agoWhether this is "simpler"… debatable ;D
- deleted 1y ago[deleted]
- gthompson512 1y agoMinor 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 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 1y agoWe might get multi-line macros in C2y standard: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3524.txt https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3524.txt
- tanelpoder 1y agoI'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.