Embed Thread & Event Polling

11 views
Skip to first unread message

Nick Thompson

unread,
Apr 3, 2021, 4:22:20 PMApr 3
to libuv

Hi,

I'm working on a small project that follows a pattern quite like the example set in the test-embed.c file on github: https://github.com/libuv/libuv/blob/v1.x/test/test-embed.c

I'm following this pattern in an attempt to get two different event loops to play nicely together, and it works fairly well, but I have changed the `uv_run` behavior to `UV_RUN_NOWAIT` in order to avoid locking up the second event loop when processing the libuv loop.

Initially this seems fine, but I've noticed that if I install an active handle in the libuv event loop, my poll thread will immediately notify my main thread that there's work to do. Then, my `uv_run(loop, UV_RUN_NOWAIT)` may or may not service the handle right away. In my particular case I'm using node.js' `setTimeout` as an example, and the poll thread will say "hey, there's work to do," then the main thread wakes up and runs the loop but maybe the timer isn't ready to fire yet, so the main thread doesn't actually do any work, then signals the semaphore for the poll thread. These two threads go back and forth like this immediately (spinning up a lot of CPU %) until finally the timer is ready to fire.

So, it seems that this poll strategy wakes up the main thread whenever there's an active handle. Is there a way to change the poll strategy to only wake up the main thread when there's actaully work to do? (I.e., wake up the main thread only when UV_RUN_NOWAIT will actually find work to do?)

Thanks,
Nick
Reply all
Reply to author
Forward
0 new messages