Skip to content
Alexander Belopolsky edited this page Sep 30, 2017 · 14 revisions

Async Notes

Libuv sample code

The source distribution of libuv comes with a test test-embed.c demonstrating how to embed a libuv event loop in another loop.

Overview

The sample app has three main components:

  1. An external loop;
  2. The embed thread; and
  3. 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);

Clone this wiki locally