Allow definition of a load and unload NIF callback#16
Allow definition of a load and unload NIF callback#16jonatanklosko merged 8 commits intoelixir-nx:mainfrom
load and unload NIF callback#16Conversation
It is sometimes necessary for a NIF to have some global initialization logic in its load callback, like we already have for atom and resource registration. This commit inlines the `fine::__private__::load` function in the `FINE_INIT` macro to delay template instantiation of a new `fine::__private__::OnLoad` struct that can be overriden by a user using the `FINE_LOAD(env)` macro to provide customized initialization logic, while still registering atoms and resources.
jonatanklosko
left a comment
There was a problem hiding this comment.
Hey @brodeuralexis! I dropped a comment regarding the API :)
That said, I wonder if there is a logic that the user would put on load, which couldn't be a typical initialization. In the docs example, couldn't they could as well do this:
static ThreadPool s_pool = ThreadPool(std::thread::hardware_concurrency());And if that's the case, I would probably hold off with extra APIs, until there is a use case.
load NIF callbackload and unload NIF callback
|
I've also added code to support the unload callback, as I believe both should be provided at the same time.
I've reworked the example to accept the number of threads from
In libbpf, ring buffers must be polled in a separate thread. I do something akin to the following in a separate thread: while (!g_exiting) {
err = ring_buffer__poll(rb, 100 /* timeout */);
if (err < 0 && err != -EINTR) {
perror("ring_buffer__poll");
abort();
}
}
g_exited.set();For the ERTS to exit successfully, I need to modify the For now, I hard-code 100ms as a timeout for polling, but in the future, this value could be injected from the config :bpf,
ring_buffer_polling_timeout: 100 |
jonatanklosko
left a comment
There was a problem hiding this comment.
The API looks great, I dropped one comment with alternative implementation :)
While using `fine::Registration` incurs some runtime-cost, there is no longer any constraints on the order of callback registration and `FINE_INIT`.
jonatanklosko
left a comment
There was a problem hiding this comment.
Small comments and looks good to me!
jonatanklosko
left a comment
There was a problem hiding this comment.
Fantastic, thank you! 🍵
It is sometimes necessary for a NIF to have some global initialization logic in its load callback, like we already have for atom and resource registration.
This commit inlines the
fine::__private__::loadfunction in theFINE_INITmacro to delay template instantiation of a newfine::__private__::OnLoadstruct that can be overriden by a user using theFINE_LOAD(env)macro to provide customized initialization logic, while still registering atoms and resources.