125 points · by eatonphil · 8y ago · 38 comments
libuv v1.30 was release at on Jan 28, 2015. It is at v1.22.0.
A lot of the book was copied into the official docs later https://github.com/libuv/libuv/pull/1246.
The book definitely does not capture new things. I do not intend to update it, but I still look at pull requests.
How does it handle the well known incompatibility between fork and threads?
Forking for something else, though, god help you.
It looks like libuv creates a pipe for every new process, and uses that to send errno back on any failing system call. This has the disadvantage that you only get the error code, and none of the context. You get ENOENT, but you don't know which path was invalid.
(Or, if you want to be weird, do a PTRACE_TRACEME after fork and have the parent trace the child process and only detach when it sees a successful return from execve. If ptrace is unavailable, fall back to less-useful errors)
It's compatible with the native asyncio event loop and can be used as a drop-in replacement.
https://blogs.msdn.microsoft.com/webdev/2018/04/12/asp-net-c...
Much of the value in making a struct (and hence its size) public and supplying a pointer to the initializer is the potential to embed or avoid heap allocation of the thing altogether, and here in an example which would benefit from both the simplified code in addition to demonstrating the advantage, it's completely missed.
Fixed form:
int main() {
uv_loop_t loop;
uv_loop_init(&loop);
printf("Now quitting.\n");
uv_run(&loop, UV_RUN_DEFAULT);
uv_loop_close(&loop);
return 0;
}
[0] https://nikhilm.github.io/uvbook/basics.html#hello-worldmallocing is the entirely sensible thing to do here.
struct embbed { ... uv_loop_t loop; ... } e1, e2; ... e1 = e2; ?
As-is the API and the way its use is being demonstrated in that example appear amateur to say the least.
It's just a different programming approach in C.
Encapsulation is good only for making it harder for anyone to poke in the internals of your library, which has some benefits with binary compatibility, etc.
But it's strictly worse in all other metrics when coding in C. It limits you, it forces malloc calls where most of the time none would be necessary, it makes memory management more complicated, it forces explict initialization, etc.