coru icon indicating copy to clipboard operation
coru copied to clipboard

Request: function to get active coroutine

Open andrewwade opened this issue 6 years ago • 2 comments

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

andrewwade avatar Jun 20 '19 17:06 andrewwade

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.

geky avatar Jul 21 '19 19:07 geky

I wanna bite you for implement this.

lygstate avatar Jun 06 '20 15:06 lygstate