<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<br>
<br>
<div class="moz-cite-prefix">On 05/18/2016 02:13 PM, Benoit Chesneau
wrote:<br>
</div>
<blockquote
cite="mid:CAJNb-9ppOD1eXJOFpDcJ39byvFqmm_4+Lk+YKBDtTnvN1O-uMg@mail.gmail.com"
type="cite">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<div dir="ltr"><br>
<br>
<div class="gmail_quote">
<div dir="ltr">On Mon, May 2, 2016 at 4:24 PM Sverker Eriksson
<<a moz-do-not-send="true"
href="mailto:sverker.eriksson@ericsson.com"
target="_blank">sverker.eriksson@ericsson.com</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"> A resource will be
deallocated when<br>
<br>
* all terms created with enif_make_resource() have been
garbage collected<br>
<br>
* AND the call to enif_alloc_resource() has been balanced
by a call to enif_release_resource()<br>
<br>
* AND all calls to enif_keep_resource() have been balanced
by an equal number of calls to enif_release_resource()</div>
<div bgcolor="#FFFFFF" text="#000000"><br>
<br>
<br>
<div>On 05/02/2016 11:47 AM, Benoit Chesneau wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">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.
<div><br>
</div>
<div>So I created a nif resource that have the
following properties:</div>
<div><span style="line-height:1.5">- it is ref
counted, each time a call is made to it, the
counter is incremented and decremented when it
return.</span><br>
</div>
</div>
</blockquote>
</div>
<div bgcolor="#FFFFFF" text="#000000"> A resource already
has an internal ref counter, which is decremented by the
GC and by enif_release_resource().<br>
<br>
Adding your own ref counter is probably either redundant
or just plain wrong.<br>
Every call to enif_release_resource() must be balanced by
a prior call<br>
to either enif_alloc_resource() or enif_keep_resource().
You cannot<br>
release a resource that is still referred to by one of
more terms.</div>
<div bgcolor="#FFFFFF" text="#000000"><br>
<br>
<blockquote type="cite">
<div dir="ltr">
<div>- the process that create it will live until the
vm is closed</div>
</div>
</blockquote>
</div>
<div bgcolor="#FFFFFF" text="#000000"> A resource it not
tied to the process that created it,<br>
it is tied to the terms that refer it.</div>
</blockquote>
<div><br>
</div>
<div>Related to that I recently came with the following
question:</div>
How to tie a nif resource to a owner process to make sure it's
cleaned if the process crash?</div>
<div class="gmail_quote"><br>
</div>
<div class="gmail_quote">Looking at the source of ETS it seems
to use unexposed functions from</div>
<div class="gmail_quote"><a moz-do-not-send="true"
href="https://github.com/erlang/otp/blob/maint/erts/emulator/beam/erl_process.h"
rel="noreferrer"
style="color:rgb(42,128,185);text-decoration:none;font-family:slack-lato,applelogo,sans-serif;font-size:15px">https://github.com/erlang/otp/blob/maint/erts/emulator/beam/erl_process.h</a><br>
</div>
<div class="gmail_quote"><br>
</div>
<div class="gmail_quote">I think I miss a C function like
"nif_tie_resource(pid, somecalleback)" . Or is there any code
that already does that?</div>
<div class="gmail_quote"><br>
</div>
<div class="gmail_quote">- benoît</div>
</div>
</blockquote>
<br>
<br>
If the crashing process contains the last reference to your resource<br>
then it will be destructed as part of the process termination
cleanup<br>
and you don't have to do anything.<br>
<br>
If you want your resource to be "cleaned" at process crash<br>
even though it is still referred by other processes or ETS tables,<br>
then there is no such callback today.<br>
<br>
You can accomplish that however by creating a second "owner"
resource<br>
with a pointer to the "payload" resource. Keep the owner resource
term<br>
in your owning process and let it clean the payload resource in its
destructor.<br>
The destructor of the payload resource (if any) and all code doing<br>
enif_get_resource must recognize and handle an already cleaned
resource.<br>
<br>
<br>
/Sverker, Erlang/OTP<br>
<br>
</body>
</html>