tidwall / neco

Concurrency library for C (coroutines)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a way to cowork with other event loop like libuv ?

calvin2021y opened this issue · comments

I guess If I can use this with libuv ( or io uring, other event) will be great.

I'm not sure what you want to achieve by mixing event-based with coroutines. But I suppose it's possible to create an event loop in a separate thread and then feed those events to a coroutine using a pipe.

For example:

void *libuv_thread(void *arg) {
    int fd = *(int*)arg;
    
    // ... do all libuv stuff in this thread and notify the coroutine 
    // when something happens ...

    write(fd, "!important data!", 16);

    return 0;
}

int neco_main(int argc, void *argv[]) {
    int fds[2];
    pipe(fds);
    neco_setnonblock(fds[0], true, 0);
    pthread_create(&th, 0, libuv_thread, &fds[1]);
    while (1) {
        char data[64];
        int n = neco_read(fds[0],  data, sizeof(data));
        if (n <= 0) {
            break;
        }
        // handle important data
        printf("%.*s\n", n, data);
    }
    return 0;
}

BTW, Neco does use epoll or kqueue under-the-hood to wait on events.

Thanks for explain. I will try your test case.

There is some other case you want to mix event loop into one thread, to avoid cross thread cost. like to embed the neco into libuv event loop, (with uv_poll_t to get the fired event).

With this support you get to run some library that is design to work libuv.

And you get cross platform support for windows/ios/android and all other platform.

And the libuv io uring could speed up some app. (with batch syscall, some time much fast than epoll)