[erlang-questions] Thoughts on enif_whereis?

Ted Burghart <>
Wed May 18 23:08:51 CEST 2016


I have a NIF library from which I want to send messages to a registered local service. The service should be quite long-lived, so it's reasonable for me to hold on to its pid in the NIF's private data, but if it crashes and is re-started/re-registered I need to be able to pick up its new pid.

I can initialize the NIF library with the pid at the time it's loaded, and my current approach is to have an Erlang process monitoring the service so it can call into the NIF library with the new pid if it changes, but it would be much less convoluted if I could just respond to a (presumably very rare) failure result from enif_send() by looking up the new pid. Before anyone warns of the perils of long-running NIFs, I do intend to sprinkle enif_consume_timeslice() and enif_schedule_nif() calls liberally throughout this code.

erts_whereis_name_to_id() is pretty straightforward, and looks like it would be no more onerous to call from a NIF than, say, enif_send().

Has an enif_whereis() function been considered and, if so, have any conclusions been reached? I can see where a stern warning about not holding any lock while calling it would be in order due to the global mutex in register.c, but beyond that it seems relatively simple and really handy.

Am I missing something?

– Ted

--
Ted Burghart
Senior Engineer
Basho Technologies  http://www.basho.com





More information about the erlang-questions mailing list