From 7fb88a9a605f090ee7a69eb91c95ecf883ef8cad Mon Sep 17 00:00:00 2001 From: Sverker Eriksson Date: Mon, 15 Feb 2010 17:50:48 +0000 Subject: OTP-8335 NIF improvements: Driver API for multi-threading made available for NIFs. Support for mempory managed (garbage collected) resource objects. A way to pass "pointers" to native data structures between C and Erlang in a safe way. Various new functions, like enif_inspect_iolist_as_binary, enif_make_sub_binary, enif_get_string, enif_get_atom, enif_make_tuple_from_array, enif_make_list_from_array, enif_make_existing_atom. --- erts/doc/src/erl_nif.xml | 113 ++++++++++++++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 41 deletions(-) (limited to 'erts') diff --git a/erts/doc/src/erl_nif.xml b/erts/doc/src/erl_nif.xml index c013d96fc4..d25c63be3d 100644 --- a/erts/doc/src/erl_nif.xml +++ b/erts/doc/src/erl_nif.xml @@ -36,24 +36,26 @@

The NIF concept was introduced in R13B03 as an EXPERIMENTAL feature. The interfaces may be changed in any way - in coming releases. The API is still sparse and contains only - the most basic functions to read and write Erlang terms. -

R13B04: The function prototypes of the NIFs - have changed to expect argc and argv - arguments. The arity of a NIF is by that no longer limited to - 3.

-

enif_get_data renamed as enif_priv_data.

+ in coming releases. The plan is however to lift the experimental label and + maintain interface backward compatibility from R14B.

+

Incompatible changes in R13B04:

+ + The function prototypes of the NIFs have changed to expect argc and argv + arguments. The arity of a NIF is by that no longer limited to 3. + enif_get_data renamed as enif_priv_data. + enif_make_string got a third argument for character encoding. +

A NIF library contains native implementation of some functions - of an erlang module. The native implemented functions (NIFs) are + of an Erlang module. The native implemented functions (NIFs) are called like any other functions without any difference to the caller. Each NIF must also have an implementation in Erlang that will be invoked if the function is called before the NIF library has been successfully loaded. A typical such stub implementation is to throw an exception. But it can also be used as a fallback implementation if the NIF library is not implemented for some - architecture.

+ architecture.

A minimal example of a NIF library can look like this:

@@ -62,7 +64,7 @@ static ERL_NIF_TERM hello(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { - return enif_make_string(env, "Hello world!"); + return enif_make_string(env, "Hello world!", ERL_NIF_LATIN1); } static ErlNifFunc nif_funcs[] = @@ -73,7 +75,7 @@ static ErlNifFunc nif_funcs[] = ERL_NIF_INIT(niftest,nif_funcs,NULL,NULL,NULL,NULL) -

and the erlang module would have to look something like +

and the Erlang module would have to look something like this:

@@ -107,6 +109,10 @@ ok the new directive on_load to automatically load the NIF library when the module is loaded.

+

A NIF must be exported or used locally by the module (or both). + An unused local stub function will be optimized away by the compiler + causing loading of the NIF library to fail.

+

A loaded NIF library is tied to the Erlang module code version that loaded it. If the module is upgraded with a new version, the new code will have to load its own NIF library (or maybe choose not @@ -116,7 +122,7 @@ ok will be shared as well. To avoid unintentionally shared static data, each Erlang module code can keep its own private data. This private data can be set when the NIF library is loaded and - then retrieved by calling enif_priv_data().

+ then retrieved by calling enif_priv_data().

There is no way to explicitly unload a NIF library. A library will be automatically unloaded when the module code that it belongs to is purged by the code server. A NIF library will also be unloaded if it is replaced @@ -131,7 +137,7 @@ ok Read and write Erlang terms

Any Erlang terms can be passed to a NIF as function arguments and - be returned as function return value. The terms are of C-type ERL_NIF_TERM + be returned as function return values. The terms are of C-type ERL_NIF_TERM and can only be read or written using API functions. Most functions to read the content of a term are prefixed enif_get_ and usually return true (or false) if the term was of the expected type (or not). @@ -139,25 +145,42 @@ ok return the created ERL_NIF_TERM. There are also some functions to query terms, like enif_is_atom, enif_is_identical and enif_compare.

+ Binaries +

Terms of type binary are accessed through the struct type ErlNifBinary + that contains a pointer (data) to the raw binary data and the length + (size) of the data in bytes. Both data and size are + read-only and should only be written using calls to API functions. + Instances of ErlNifBinary are however always allocated by the user + (usually as local variables).

+

The raw data pointed to by data is only mutable after a call to + enif_alloc_binary. + All other functions that operates on a binary will leave the data as read-only. + An allocated binary must in the end either be freed with + enif_release_binary + or transferred to an Erlang term with enif_make_binary. + But it does not have do happen in the same NIF call.

+

Binaries must be a number of whole bytes. Bitstrings with an arbitrary + bit length have no support yet.

+
Resource objects

Resource objects are a way to return pointers to native data structures from a NIF in a safe way. A resource object is just a block - of memory allocated with enif_alloc_resource(). + of memory allocated with enif_alloc_resource(). A handle ("safe pointer") to this memory block can then be returned to Erlang by the use of - enif_make_resource(). + enif_make_resource(). The term returned by enif_make_resource is totally opaque in nature. It can be stored and passed between processses on the same node, but the only real end usage is to pass it back as argument to a NIF. - The NIF can then do enif_get_resource() + The NIF can then do enif_get_resource() and get back a pointer to the memory block that is guaranteed to still be valid. A resource object will not be deallocated until the last handle term has been garbage collected by the VM and the resource has been - released with enif_release_resource() + released with enif_release_resource() (not necessarily in that order).

All resource objects are created as instances of some resource type. This makes resources from different modules or applications to be distinguishable. A resource type is created by calling - enif_open_resource_type() + enif_open_resource_type() when a library is loaded. Objects of that resource type can then later be allocated and enif_get_resource verifies that the resource is of the expected type. A resource type can have a destructor function that is automatically @@ -172,12 +195,18 @@ ok Threads and concurrency

A NIF is thread-safe without any explicit synchronization as - long as it acts as a pure function and only operates on the supplied - arguments. As soon as you access a shared state either through static - variables or enif_priv_data - you need to supply your own explicit synchronization.

+ long as it acts as a pure function and only reads the supplied + arguments. As soon as you write towards a shared state either through + static variables or enif_priv_data + you need to supply your own explicit synchronization. Resource objects + will also require synchronization if you treat them as mutable.

The library initialization callbacks load, reload and - upgrade are all thread-safe even for shared state data.

+ upgrade are all thread-safe even for shared state data.

+

Avoid doing lengthy work in NIF calls as that may degrade the + responsiveness of the VM. NIFs are called directly by the same scheduler + thread that executed the calling Erlang code. The calling scheduler will thus + be blocked from doing any other work until the NIF returns.

+
@@ -328,7 +357,7 @@ typedef enum { ErlNifSysInfo -

Used by enif_system_info +

Used by enif_system_info to return information about the runtime system. Contains currently the exact same content as ErlDrvSysInfo.

@@ -346,9 +375,9 @@ typedef enum {

Allocate a new binary of size of size bytes. Initialize the structure pointed to by bin to refer to the allocated binary. The binary must either be released by - enif_release_binary() - or ownership transferred to an erlang term with - enif_make_binary(). + enif_release_binary() + or ownership transferred to an Erlang term with + enif_make_binary(). An allocated (and owned) ErlNifBinary can be kept between NIF calls.

Return false if allocation failed.

@@ -437,7 +466,9 @@ typedef enum { intenif_get_resource(ErlNifEnv* env, ERL_NIF_TERM term, ErlNifResourceType* type, void** objp) Get the pointer to a resource object

Set *objp to point to the resource object referred to by term. - Return false if term is not a handle to a resource object of type type.

+ The pointer is valid until the calling NIF returns and should not be released.

+

Return false if term is not a handle to a resource object + of type type.

intenif_get_string(ErlNifEnv* env, ERL_NIF_TERM list, char* buf, unsigned size, @@ -446,11 +477,11 @@ typedef enum {

Write a null-terminated string, in the buffer pointed to by buf with size size, consisting of the characters in the string list. The characters are written using encoding - encode. Return the number of bytes written - (including terminating null character), or -size if the - string was truncated due to buffer space, or 0 if list - is not a string that can be encoded with encode or - if size was less than 1. + encode. + Return the number of bytes written (including terminating null + character), or -size if the string was truncated due to + buffer space, or 0 if list is not a string that can be + encoded with encode or if size was less than 1. The written string is always null-terminated unless buffer size is less than 1.

@@ -577,7 +608,7 @@ typedef enum { ERL_NIF_TERMenif_make_list9(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e9) Create a list term.

Create an ordinary list term with length indicated by the - function name. Prefere these functions (macros) over the variadic + function name. Prefer these functions (macros) over the variadic enif_make_list to get compile time error if the number of arguments does not match.

@@ -601,9 +632,9 @@ typedef enum { ERL_NIF_TERMenif_make_resource(ErlNifEnv* env, void* obj) Create an opaque handle to a resource object

Create an opaque handle to a memory managed resource object - obtained by enif_alloc_resource. + obtained by enif_alloc_resource. No ownership transfer is done, the resource object still needs to be released by - enif_release_resource.

+ enif_release_resource.

Note that the only defined behaviour when using of a resource term in an Erlang program is to store it and send it between processes on the same node. Other operations such as matching or term_to_binary @@ -612,7 +643,7 @@ typedef enum { ERL_NIF_TERMenif_make_string(ErlNifEnv* env, const char* string, ErlNifCharEncoding encoding) Create a string.

Create a list containing the characters of the - null-terminated string string with encoding encoding.

+ null-terminated string string with encoding encoding.

ERL_NIF_TERMenif_make_sub_binary(ErlNifEnv* env, ERL_NIF_TERM bin_term, unsigned pos, unsigned size) @@ -640,7 +671,7 @@ typedef enum { ERL_NIF_TERMenif_make_tuple9(ErlNifEnv* env, ERL_NIF_TERM e1, ..., ERL_NIF_TERM e9) Create a tuple term.

Create a tuple term with length indicated by the - function name. Prefere these functions (macros) over the variadic + function name. Prefer these functions (macros) over the variadic enif_make_tuple to get compile time error if the number of arguments does not match.

@@ -686,7 +717,7 @@ typedef enum { ErlNifResourceDtor* dtor, ErlNifResourceFlags flags, ErlNifResourceFlags* tried) Create or takeover a resource type

Create or takeover a resource type identified by the string - name and give it the destructor function pointed to by dtor. + name and give it the destructor function pointed to by dtor. Argument flags can have the following values:

ERL_NIF_RT_CREATE @@ -706,8 +737,8 @@ typedef enum { On failure, return NULL and set *tried to flags. It is allowed to set tried to NULL.

Note that enif_open_resource_type is only allowed to be called in the three callbacks - load, reload - and upgrade.

+ load, reload + and upgrade.

void*enif_priv_data(ErlNifEnv* env) -- cgit v1.2.3