coru
coru copied to clipboard
Request: function to get active coroutine
Would be nice to get access to the active coroutine with a function call. Then using container_of will allow you to get the container struct.
New Function:
coru_t *coru_active(void) {
return active_coru;
}
Helper macro:
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
Example:
struct task {
int wait_time_ms;
coru_t coru;
} my_task;
void task_wait(int ms) {
coru_t *co = coru_active();
if(co) {
struct task * t = container_of(co, struct task, coru);
t->wait_time_ms = ms;
coru_yield();
}
}
Hi @andrewwade, thanks for creating an issue, sorry it's taken me so long to get to this. This is a good idea 👍 It can also be used as a general test if you're in a coroutine.
I'll see if I can add this next chance I get, but feel free to try to beat me to it.
I wanna bite you for implement this.