[erlang-questions] Nif and driver callbacks

Mikael Karlsson karlsson.rm@REDACTED
Sat May 25 22:17:11 CEST 2019


Thu May 23 20:42:37 CEST 2019 Sverker Ericsson wrote:
> On tor, 2019-05-23 at 15:54 +0200, Mikael Karlsson wrote:
>> Hi,
>> I just started to write some nif code to interface towards a Linux
driver API.
>> I can install a callback function in my nif module that gets called from
the
>> driver  whenever it is ready to receive new data. Currently it gets
called
>> every 5 ms. The callback function sends a message to an erlang process
that in
>> turn delivers new data to the driver. (Using another nif function).
>>
>> This works, although I am a bit puzzled how, since the callback function
that
>> is called from the linux driver appearantly is running outside the
Erlang VM
>> and still is able to send a message to the erlang process. Could anyone
>> explain how this works?
>>

> If your callback function calls enif_send to send the message and the
message
> arrives, then apparently it's running within the Erlang VM.
>
> If in doubt:
> 1. Enable core dump generation (ulimit -c, /proc/sys/kernel/core_pattern,
etc).
> 2. Put a call to abort() in your callback.
> 3. Run and watch your beam.smp crash.
> 4. Do post mortem with "gdb beam.smp <core-dump>"
> 5. Use 'backtrace' command to see who is calling you.

> /Sverker

Thanks,
I am using enif_send and I realise that it is running within the VM
(beam.smp) process now.
And it works fine even though I am still wondering how the asynchronous
notification plays together with the Erlang scheduling, or maybe it doesn't
need to, but just works anyway.

Mikael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190525/66ef426f/attachment.htm>


More information about the erlang-questions mailing list