[erlang-questions] weak resource reference possible in nif ?
Thu Nov 9 04:06:25 CET 2017
We don't want to get a new resource term every time when we got a new
we want to simply use erlang pattern matching without another function
actual data in the resource and matching against the data, if we do
this, a programmer
can keep the data around for later matching, and may forget to keep the
what's the reason to use erlang resource? we would give up.
On 2017年11月08日 18:59, John Högberg wrote:
> Why not let the resource hold nothing besides a shared_ptr to the
> actual data, and use weak_ptrs in the internal list?
> You'd need to allocate a new resource when sending notifications from
> that internal thread (Since the original resource could have been
> destroyed), but other than that it should be pretty straightforward
> unless I've misunderstood something.
> John Högberg
> On ons, 2017-11-08 at 17:32 +0800, Skype Xu wrote:
>> Suppose a nif module is multi-threaded, we keep a pointer to a
>> in the
>> module's internal list, when the resource is no longer used by
>> erlang vm calls the resource's destructor, and we remove the pointer
>> internal list in the destructor, that's fine.
>> However a problem arises when the internal list is also used by
>> created in the nif module, when some external events were triggered,
>> tries to use the resource pointer and sends it to a process in the
>> is there's a race condition between destructor and the internal
>> in the destructor we try to remove it from the internal list, at the
>> time, the
>> resource's reference count is already zero, and before the destructor
>> the internal thread sent the resource to a proecess, by calling
>> enif_make_resource() to increase the count to 1, but anyway after the
>> destructor returned, the vm will recycle the memory of the resource
>> the reference count is 1 again, this leads to corrupted memory.
>> We can not keep a resource by adding extra reference count in nif
>> if we do, the resource's destructor will never be called even if not
>> process is using the resource, because the reference count will never
>> dropped to 0.
>> It would be perfect if we can keep a weak reference in nif like the
>> one in
>> C++ does, one can use weak_ptr to get back a shared_ptr object if
>> is still using, otherwise a null pointer is returned. if this is
>> implemented, we
>> can queue a notification to the internal thread and the thread will
>> any data related to the resource. enif_monitor_process() is not a
>> because we want vm to recycle the resource when it is not used, not
>> a process crashed or exited.
>> erlang-questions mailing list
> erlang-questions mailing list
More information about the erlang-questions