libuv

Cross platform asynchronous i/o
Saúl Ibarra Corretgé — @saghul
CROSS PLATFORM ASYNCHRONOUS I/O
LIBUV
▸ Cross-platform async I/O and more
▸ Small (relatively) C library: ~30K LOC (without tests)
▸ Extensive test suite and CI coverage
▸ Designed for C programs that miss the joy of JavaScript
callback hell
▸ Used by many projects:

https://github.com/libuv/libuv/wiki/Projects-that-use-libuv
CROSS PLATFORM ASYNCHRONOUS I/O
LIBUV: FEATURES
▸ Event loop
▸ Timers
▸ TCP / UDP sockets
▸ Named pipes
▸ Filesystem operations
▸ Signal handling
▸ Child processes
▸ ANSI escaped TTY
▸ Threading utilities
▸ Coolest logo ever
CROSS PLATFORM ASYNCHRONOUS I/O
LIBUV: ARCHITECTURE
▸ The event loop: uv_loop_t
▸ Handles: uv_handle_t
▸ Requests: uv_req_t
▸ Other utilities
CROSS PLATFORM ASYNCHRONOUS I/O
LIBUV: ARCHITECTURE
OPERATING SYSTEM
NETWORK I/O FILE I/O OTHER STUFF
OS INDEPENDENT
STUFF
CROSS PLATFORM ASYNCHRONOUS I/O
LIBUV ARCHITECTURE: NETWORK I/O
OPERATING SYSTEM
UV_TCP_T UV_PIPE_T UV_TTY_T
UV_UDP_T UV_POLL_TUV_STREAM_T
UV__IO_T (UNIX ONLY)
CROSS PLATFORM ASYNCHRONOUS I/O
LIBUV ARCHITECTURE: FILE I/O
UV_FS_T UV_GETADDRINFO_T UV_GETNAMEINFO_T
UV__WORK_T
THREAD POOL
UV_WORK_T
CROSS PLATFORM ASYNCHRONOUS I/O
LIBUV: A WORD ON THREADS
▸ We only use threads for file i/o and getaddrinfo
▸ http://blog.libtorrent.org/2012/10/asynchronous-disk-io/
▸ Default thread pool size is 4

(runtime env var: UV_THREADPOOL_SIZE)
▸ NOT FOR NETWORK I/O
▸ NOT FOR NETWORK I/O
▸ NOT FOR NETWORK I/O
THE LIBUV EVENT LOOP IS
SINGLE THREADED. THE THREAD
POOL IS ONLY USED FOR FILE I/O.
The libuv police
CROSS PLATFORM ASYNCHRONOUS I/O
CROSS PLATFORM ASYNCHRONOUS I/O
LIBUV ARCHITECTURE: OTHER
▸ OS independent
▸ uv_timer_t
▸ uv_idle_t, uv_prepare_t, uv_check_t

aka “loop watchers”
▸ The thread pool
▸ OS dependent
▸ uv_signal_t
▸ uv_process_t
▸ Threading and miscellaneous utilities
CROSS PLATFORM ASYNCHRONOUS I/O
LIBUV: THE EVENT LOOP
libuv in Node
CROSS PLATFORM ASYNCHRONOUS I/O
NODE EVENT LOOP
Coalesced, 1 Node
timer != 1 libuv timer
Run on a
check + idle handleNext tick callbacks run from
node::MakeCallback
CROSS PLATFORM ASYNCHRONOUS I/O
ONION ARCHITECTURE (TM)
net.Socket
TCPWrap
uv_tcp_t
Socket._handle
TCPWrap.handle_
fd / HANDLE
A CHAT APP,

OF COURSE
CROSS PLATFORM ASYNCHRONOUS I/O
SAMPLE: A LIBUV CHAT APPLICATION
▸ Simple TCP server application
▸ A twist on libuv-chat by Ben Noordhuis in 2011
▸ Multiple users, single “chat room”
▸ Pokemon, because why not?
▸ https://github.com/saghul/libuv-chat
WHEN THERE IS
SOMETHING STRANGE

IN YOUR
EVENT LOOP
CROSS PLATFORM ASYNCHRONOUS I/O
WHO YOU GONNA CALL? @LIBUV/COLLABORATORS
▸ @piscisaureus
▸ @bnoordhuis
▸ @indutny
▸ @saghul
▸ @cjihrig
▸ @iWuzHere
▸ @santigimeno
RESOURCES
libuv.org
docs.libuv.org
#libuv on IRC
libuv Google Group
#libuv on StackOverflow

libuv: cross platform asynchronous i/o

  • 1.
    libuv
 Cross platform asynchronousi/o Saúl Ibarra Corretgé — @saghul
  • 2.
    CROSS PLATFORM ASYNCHRONOUSI/O LIBUV ▸ Cross-platform async I/O and more ▸ Small (relatively) C library: ~30K LOC (without tests) ▸ Extensive test suite and CI coverage ▸ Designed for C programs that miss the joy of JavaScript callback hell ▸ Used by many projects:
 https://github.com/libuv/libuv/wiki/Projects-that-use-libuv
  • 3.
    CROSS PLATFORM ASYNCHRONOUSI/O LIBUV: FEATURES ▸ Event loop ▸ Timers ▸ TCP / UDP sockets ▸ Named pipes ▸ Filesystem operations ▸ Signal handling ▸ Child processes ▸ ANSI escaped TTY ▸ Threading utilities ▸ Coolest logo ever
  • 4.
    CROSS PLATFORM ASYNCHRONOUSI/O LIBUV: ARCHITECTURE ▸ The event loop: uv_loop_t ▸ Handles: uv_handle_t ▸ Requests: uv_req_t ▸ Other utilities
  • 5.
    CROSS PLATFORM ASYNCHRONOUSI/O LIBUV: ARCHITECTURE OPERATING SYSTEM NETWORK I/O FILE I/O OTHER STUFF OS INDEPENDENT STUFF
  • 6.
    CROSS PLATFORM ASYNCHRONOUSI/O LIBUV ARCHITECTURE: NETWORK I/O OPERATING SYSTEM UV_TCP_T UV_PIPE_T UV_TTY_T UV_UDP_T UV_POLL_TUV_STREAM_T UV__IO_T (UNIX ONLY)
  • 7.
    CROSS PLATFORM ASYNCHRONOUSI/O LIBUV ARCHITECTURE: FILE I/O UV_FS_T UV_GETADDRINFO_T UV_GETNAMEINFO_T UV__WORK_T THREAD POOL UV_WORK_T
  • 8.
    CROSS PLATFORM ASYNCHRONOUSI/O LIBUV: A WORD ON THREADS ▸ We only use threads for file i/o and getaddrinfo ▸ http://blog.libtorrent.org/2012/10/asynchronous-disk-io/ ▸ Default thread pool size is 4
 (runtime env var: UV_THREADPOOL_SIZE) ▸ NOT FOR NETWORK I/O ▸ NOT FOR NETWORK I/O ▸ NOT FOR NETWORK I/O
  • 9.
    THE LIBUV EVENTLOOP IS SINGLE THREADED. THE THREAD POOL IS ONLY USED FOR FILE I/O. The libuv police CROSS PLATFORM ASYNCHRONOUS I/O
  • 10.
    CROSS PLATFORM ASYNCHRONOUSI/O LIBUV ARCHITECTURE: OTHER ▸ OS independent ▸ uv_timer_t ▸ uv_idle_t, uv_prepare_t, uv_check_t
 aka “loop watchers” ▸ The thread pool ▸ OS dependent ▸ uv_signal_t ▸ uv_process_t ▸ Threading and miscellaneous utilities
  • 11.
    CROSS PLATFORM ASYNCHRONOUSI/O LIBUV: THE EVENT LOOP
  • 12.
  • 13.
    CROSS PLATFORM ASYNCHRONOUSI/O NODE EVENT LOOP Coalesced, 1 Node timer != 1 libuv timer Run on a check + idle handleNext tick callbacks run from node::MakeCallback
  • 14.
    CROSS PLATFORM ASYNCHRONOUSI/O ONION ARCHITECTURE (TM) net.Socket TCPWrap uv_tcp_t Socket._handle TCPWrap.handle_ fd / HANDLE
  • 15.
  • 16.
    CROSS PLATFORM ASYNCHRONOUSI/O SAMPLE: A LIBUV CHAT APPLICATION ▸ Simple TCP server application ▸ A twist on libuv-chat by Ben Noordhuis in 2011 ▸ Multiple users, single “chat room” ▸ Pokemon, because why not? ▸ https://github.com/saghul/libuv-chat
  • 17.
    WHEN THERE IS SOMETHINGSTRANGE
 IN YOUR EVENT LOOP
  • 18.
    CROSS PLATFORM ASYNCHRONOUSI/O WHO YOU GONNA CALL? @LIBUV/COLLABORATORS ▸ @piscisaureus ▸ @bnoordhuis ▸ @indutny ▸ @saghul ▸ @cjihrig ▸ @iWuzHere ▸ @santigimeno
  • 19.
    RESOURCES libuv.org docs.libuv.org #libuv on IRC libuvGoogle Group #libuv on StackOverflow