[erlang-questions] erlang nif resource & thread safety

Sverker Eriksson sverker.eriksson@REDACTED
Wed May 18 14:49:58 CEST 2016

On 05/18/2016 02:13 PM, Benoit Chesneau wrote:
> On Mon, May 2, 2016 at 4:24 PM Sverker Eriksson 
> <sverker.eriksson@REDACTED <mailto:sverker.eriksson@REDACTED>> 
> wrote:
>     A resource will be deallocated when
>     * all terms created with enif_make_resource() have been garbage
>     collected
>     * AND the call to enif_alloc_resource() has been balanced by a
>     call to enif_release_resource()
>     * AND all calls to enif_keep_resource() have been balanced by an
>     equal number of calls to enif_release_resource()
>     On 05/02/2016 11:47 AM, Benoit Chesneau wrote:
>>     I had a look in the doc but I can't figure exactly how much a nif
>>     resource could live outside the process that created it, ie. how
>>     others processes can write/read to it and how to make sure the
>>     resource will be accessible to them.
>>     So I created a nif resource that have the following properties:
>>     - it  is ref counted, each time a call is made to it, the counter
>>     is incremented and decremented when it return.
>     A resource already has an internal ref counter, which is
>     decremented by the GC and by enif_release_resource().
>     Adding your own ref counter is probably either redundant or just
>     plain wrong.
>     Every call to enif_release_resource() must be balanced by a prior call
>     to either enif_alloc_resource() or enif_keep_resource(). You cannot
>     release a resource that is still referred to by one of more terms.
>>     - the process that create it will live until the vm is closed
>     A resource it not tied to the process that created it,
>     it is tied to the terms that refer it.
> Related to that I recently came with the following question:
> How to tie a nif resource to a owner process to make sure it's cleaned 
> if the process crash?
> Looking at the source of ETS it seems to use unexposed functions from
> https://github.com/erlang/otp/blob/maint/erts/emulator/beam/erl_process.h
> I think I miss a C function like "nif_tie_resource(pid, 
> somecalleback)" . Or is there any code that already does that?
> - benoît

If the crashing process contains the last reference to your resource
then it will be destructed as part of the process termination cleanup
and you don't have to do anything.

If you want your resource to be "cleaned" at process crash
even though it is still referred by other processes or ETS tables,
then there is no such callback today.

You can accomplish that however by creating a second "owner" resource
with a pointer to the "payload" resource. Keep the owner resource term
in your owning process and let it clean the payload resource in its 
The destructor of the payload resource (if any) and all code doing
enif_get_resource must recognize and handle an already cleaned resource.

/Sverker, Erlang/OTP

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160518/389c848f/attachment.htm>

More information about the erlang-questions mailing list