[erlang-questions] enif_send rules questions

Sverker Eriksson sverker.eriksson@REDACTED
Fri Jan 13 17:09:54 CET 2017


On 01/12/2017 08:21 PM, Jason Orendorff wrote:
> Hi all,
>
> The documentation for enif_send says this:
>
>      env
>          The environment of the calling process. Must be NULL only if
>          calling from a created thread.
>
> I have a few questions about this.
>
> 1.  What is "the environment of the calling process"? Does that mean the
>      environment that was passed to the NIF?
Yes.

> 2.  What is a "created thread"?
Any non-scheduler thread where you don't have "the environment of the 
calling process".
Typically a thread created with enif_thread_create().

> 3.  Is it really invalid to do this from a NIF call -- unless I spawn a
>      separate thread to do it, and then it's OK?
>
>          NIF_ENV msg_env = enif_alloc_env();
>          NIF_TERM msg = enif_make_int(env, 1);
>          enif_send(NULL, &pid, msg_env, msg);
>          enif_free_env(msg_env);
Yes.

Documentation contributions to make thing more clear are always welcome.

> 4.  We suspect it's actually totally safe to pass a process-independent
>      environment as the first parameter, regardless of whether there's a
>      "calling process" or we're "calling from a created thread". Is it?
>      If so, would you accept a patch to document that that's OK?
I'm not super excited about such a guarantee, at least not right now.

Even if you could conclude that it will work with current implementation,
the more guarantees we give, the harder it gets to do future
changes and optimizations.

Also master branch is a moving target in this area right now as we're
in the process of making the dirty scheduler feature more robust for OTP 20.


/Sverker, Erlang/OTP




More information about the erlang-questions mailing list