[erlang-questions] NIF Callback Pids

Daniel Goertzen daniel.goertzen@REDACTED
Tue Sep 29 16:55:02 CEST 2015


I love the Erlang NIF API, but one thing I’ve missed is a nice way to send
messages into NIF code. Native Processes could provide that, but for a
variety of reasons it doesn’t look like that will be implemented any time
soon. As a stopgap I would like to pitch the idea of “NIF Callback Pids”.
The idea is that there would be a new NIF API that would create a new PID
and bind it to a user NIF function. When anything is sent to that pid, the
user NIF function is executed with the message term as a parameter. It
would look something like... ErlNifPid enif_make_callback_pid(ErlNifEnv *,
ErlNifSendCallback *cb, void *data); ... and then the callback would look
like ... void my_callback(ErlNifEnv *env, ErlNifCBEvent event, ERL_NIF_TERM
msg, void *data) { if(event == Message) { ... handle message, parse and
post to thread, etc } else if(event == Destroy) { ... free private data } }
This would give NIF modules the power to emulate processes in a very
flexible way. A typical application would parse the message term and post a
C struct to a thread. A single thread could possibly represent many
callback pids. Or for certain applications, a thread may not be needed at
all. So, I’ve been reading VM code to see how this might be implemented,
but before I get too far I should ask… 1. Has anyone explored this already?
2. Is the idea fundamentally broken is some way that would prevent it from
ever working? 3. Does anyone other than me think this would be useful?


Thanks,

Dan.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150929/69cc81c4/attachment.htm>


More information about the erlang-questions mailing list