Hi @tessi, thanks for taking time to write this approach. I also played around same idea in the past (in C though).
Just want to highlight the risks involved if someone is considering
- since mutex does not compose well, its easy to end-up with deadlocks. Especially if the callback is not internal function but something given by the user of the library (consider a callback calling another NIF).
- its tricky to handle errors, “let-it-crash” does not work well with this, since we have to unblock the thread no matter what happens to the callback.
- forces process at erlang/elixir side and thread at NIF side. And most likely we need thread pooling at NIF side
- needless to say its not very efficient if you are calling callback often - due to all the context-switching & synchronizations involved.
I think there were few more but not able to recall at the moment.
Another reference: [erlang-questions] Calling Erlang functions from NIFs






















