Calling Erlang functions from NIFs

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Calling Erlang functions from NIFs

Ilya Khaprov
Hello,

How to call an Erlang function from NIFs? Been trying to find for a
while now.

My use case - data structure implemented in C++ with user validation. I
want to implement this validation via callbacks e.g. user calls NIF with
a fun as an argument and NIF calls this function say in a filter loop. I
can imagine some scheduling problems behind this, but can it be done in
principle?

Regards,

Ilya

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Calling Erlang functions from NIFs

Loïc Hoguin-3
Hello,

I did something similar to what you need a few days ago:

 
https://git.ninenines.eu/esdl2.git/commit/?id=d6de0fce0b46be3b382ba3c30fb7c9559f3fcb8e

I have also put some thoughts on various callback mechanisms and how I
would implement them at the bottom here:
https://git.ninenines.eu/esdl2.git/about/

The only way to call an Erlang function from a NIF is to send a message
to a callback process that will then run the function. You can send the
fun in this message but to keep it in the NIF you will probably need to
copy it in an environment you allocated yourself (and then copy it again
in the environment that will send the message).

Calling a function from a NIF is easy. Having the NIF receive the result
of the callback is much harder.

My solution involves having a separate NIF function for receiving the
result, called by the callback process at the end of the callback execution.

This NIF function will then require a mutex lock/cond to store the
result where I will be able to read it afterwards, and to signal the
waiting thread that it can read it.

My own code runs in a separate thread from the schedulers, if you need
to wait from inside a scheduler thread things might get funny if there's
only one scheduler or if the callback takes too long.

Suffice to say that it's probably not very efficient to do all this.

On 01/18/2018 02:52 PM, Ilya Khaprov wrote:

> Hello,
>
> How to call an Erlang function from NIFs? Been trying to find for a
> while now.
>
> My use case - data structure implemented in C++ with user validation. I
> want to implement this validation via callbacks e.g. user calls NIF with
> a fun as an argument and NIF calls this function say in a filter loop. I
> can imagine some scheduling problems behind this, but can it be done in
> principle?
>
> Regards,
>
> Ilya
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>

--
Loïc Hoguin
https://ninenines.eu
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions