[erlang-questions] weak resource reference possible in nif ?
Wed Nov 8 11:59:23 CET 2017
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.
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
More information about the erlang-questions