-
Notifications
You must be signed in to change notification settings - Fork 7
Async
Alexander Belopolsky edited this page Sep 30, 2017
·
14 revisions
The source distribution of libuv comes with a test test-embed.c demonstrating how to embed a libuv event loop in another loop.
The sample app has three main components:
- An external loop;
- The embed thread; and
- The embed_async task.
The program sets up its components and then enters the external loop:
uv_run(&external, UV_RUN_DEFAULT);At the same time, the embed thread obtains the backend fd of the default loop
fd = uv_backend_fd(uv_default_loop());and goes to sleep waiting for events to appear on fd:
r = epoll_wait(fd, &ev, 1, timeout);Once an event is received, the embed thread wakes up the external loop by making an async call to embed_async and waits for embed_cb to raise the embed_sem semaphore.
uv_async_send(&embed_async);
uv_sem_wait(&embed_sem);