[erlang-questions] Is there anyway to share NIF resource type among NIF modules?

Sverker Eriksson sverker@REDACTED
Tue Jan 8 15:06:46 CET 2019


On tor, 2019-01-03 at 21:52 +0800, Wu Ray wrote:
> Hi, guys
> 
> I have a C struct, as a NIF resource type, and want it to be shared between
> several NIF modules, is it possible?
> 
> Best Regards
> 
There is no offical documented support to use the same resource type from more
than one NIF module.
But if you want to experiment, there are two problems I can think of.
1. How to get the other NIF modules access to the resource type pointer.
One way to solve this is to put the resource type in a separate dynamic shared
library, dynamically linked by all NIF modules using the resource type.
2. Prevent usage of an unloaded resource type. 
You must make sure yourself that other modules do not try to use the pointer to
an unloaded resource type  (as argument to enif_get_resource or
enif_make_resource for example). That will probably lead to VM crash or other
unpleasantries.
A resource type will exists as long as the NIF module that called
enif_open_resource_type is loaded. If the resource type has a destructor
function then the NIF module will not be unloaded until the last resource object
is garbage collected.
/Sverker

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20190108/83075bf1/attachment.htm>


More information about the erlang-questions mailing list