Is there anyway to share NIF resource type among NIF modules?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there anyway to share NIF resource type among NIF modules?

Wu Ray
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



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Is there anyway to share NIF resource type among NIF modules?

Lukas Larsson-8
Hello,

On Thu, Jan 3, 2019 at 2:53 PM Wu Ray <[hidden email]> wrote:
I have a C struct, as a NIF resource type, and want it to be shared between several NIF modules, is it possible?

As far as I know it is not possible.

Lukas

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Is there anyway to share NIF resource type among NIF modules?

Dmytro Lytovchenko
The reasonable way would be to use a ref-counted pointer to struct which will be shared by both NIF modules. I believe it must also be allocated somewhere in VM memory in case both NIF modules get unloaded. As C is not particularly famous for its smart refc pointers, you will have to find a library or do it yourself, and hook the refc check onto both NIF resource free functions.

On Tue, 8 Jan 2019 at 14:11, Lukas Larsson <[hidden email]> wrote:
Hello,

On Thu, Jan 3, 2019 at 2:53 PM Wu Ray <[hidden email]> wrote:
I have a C struct, as a NIF resource type, and want it to be shared between several NIF modules, is it possible?

As far as I know it is not possible.

Lukas
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Is there anyway to share NIF resource type among NIF modules?

Sverker Eriksson-5
In reply to this post by Wu Ray
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





_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions