[erlang-questions] calling Erlang functions from linked-in driver

Motiejus Jakštys <>
Wed Aug 22 10:06:59 CEST 2012

On Wed, Aug 22, 2012 at 9:17 AM, Michael Truog <> wrote:
> You could use
> http://www.erlang.org/doc/man/erl_driver.html#driver_send_term
> or
> http://www.erlang.org/doc/man/erl_nif.html#enif_send

Thanks. Somehow I missed these. This is getting really interesting.
Comments from more experienced users than me highly appreciated :)

> to call Erlang code by sending an Erlang process a message (which is then handled so that it calls the Erlang code you want to call).

OK, this is a chance. So creating an Erlang processor process is
trivial, executing functions - too. Now I need some way to pass the
return value back to the caller. Is there a way to *receive* a message
in NIF or port driver? I can't find one in the manual.

If not, I could do this:

When I need to call another Erlang function from C:
* create an Erlang resource* protected by a mutex and condition
variable (should be per-port)
* lock a mutex
* send {rpc, Resource, M, F, Args} to another Erlang process
* hold on condition variable (perhaps in another thread?)**

Then another Erlang process (implemented partially in C, partially in Erlang):
* do the RPC call, fetch the results
* lock the mutex
* put the results to the Resource
* signal condition variable

For performance,

The first process continues execution with Resource set to the returned value.

Is there a simpler way to do this?

[*] in NIF case, it would be Resource. In port driver case, it would
be a void*...
[**] how could driver_async help me here not to block the scheduler
while this is happening? Is this a valid use case for driver_async?

Motiejus Jakštys

More information about the erlang-questions mailing list