←back to thread

218 points signa11 | 1 comments | | HN request time: 0s | source
Show context
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 #
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 #
1. eqvinox ◴[] No.43714392[source]
Whether this is "simpler"… debatable ;D