2. 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
3. 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
8. 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
9. 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
10. 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
16. 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