From 8335159b919cc330e1c529464b6bbf89edbbe0a0 Mon Sep 17 00:00:00 2001 From: Sverker Eriksson Date: Thu, 3 Jun 2010 12:41:28 +0000 Subject: OTP-8555 Send message from NIF New NIF features: Send messages from a NIF, or from thread created by NIF, to any local process (enif_send) Store terms between NIF calls (enif_alloc_env, enif_make_copy) Create binary terms with user defined memory management (enif_make_resource_binary) --- erts/doc/src/erl_nif.xml | 410 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 282 insertions(+), 128 deletions(-) (limited to 'erts/doc/src') diff --git a/erts/doc/src/erl_nif.xml b/erts/doc/src/erl_nif.xml index 03bd42d3b1..f7b7b2f346 100644 --- a/erts/doc/src/erl_nif.xml +++ b/erts/doc/src/erl_nif.xml @@ -38,6 +38,18 @@ EXPERIMENTAL feature. The interfaces may be changed in any way in coming releases. The plan is however to lift the experimental label and maintain interface backward compatibility from R14B.

+

Incompatible changes in R14A:

+ + Environment argument removed for enif_alloc, + enif_realloc, enif_free, enif_alloc_binary, + enif_realloc_binary, enif_release_binary, + enif_alloc_resource, enif_release_resource, + enif_is_identical and enif_compare. + Character encoding argument added to enif_get_atom + and enif_make_existing_atom. + Module argument added to enif_open_resource_type + while changing name spaces of resource types from global to module local. +

Incompatible changes in R13B04:

The function prototypes of the NIFs have changed to expect argc and argv @@ -109,9 +121,9 @@ 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 NIF does not have to be exported, it can be local to the module. + Note however that unused local stub functions 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 @@ -122,7 +134,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 @@ -137,14 +149,20 @@ ok Read and write Erlang terms

Any Erlang terms can be passed to a NIF as function arguments and - be returned as function return values. 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). The functions to write terms are all prefixed enif_make_ and usually return the created ERL_NIF_TERM. There are also some functions to query terms, like enif_is_atom, enif_is_identical and - enif_compare.

+ enif_compare.

+

All terms of type ERL_NIF_TERM belong to an environment of type + ErlNifEnv. The lifetime of a term is + controlled by the lifetime of its environment object. All API functions that read + or write terms has the environment, that the term belongs to, as the first + function argument.

Binaries

Terms of type binary are accessed with the help of the struct type ErlNifBinary @@ -172,28 +190,29 @@ ok

The use of resource objects is 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(). + 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 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 user supplied destructor function that is automatically called when resources of that type are released (by either the garbage collector or enif_release_resource). Resource types - are uniquely identified by a supplied name string.

+ are uniquely identified by a supplied name string and the name of the + implementing module.

Resource types support upgrade in runtime by allowing a loaded NIF library to takeover an already existing resource type and thereby "inherit" all existing objects of that type. The destructor of the new @@ -208,7 +227,7 @@ ok

ERL_NIF_TERM term; - MyStruct* ptr = enif_alloc_resource(env, my_resource_type, sizeof(MyStruct)); + MyStruct* ptr = enif_alloc_resource(my_resource_type, sizeof(MyStruct)); /* initialize struct ... */ @@ -218,21 +237,31 @@ ok /* store 'ptr' in static variable, private data or other resource object */ } else { - enif_release_resource(env, obj); + enif_release_resource(obj); /* resource now only owned by "Erlang" */ } return term; } - +

Another usage of resource objects is to create binary terms with + user defined memory management. + enif_make_resource_binary + will create a binary term that is connected to a resource object. The + destructor of the resource will be called when the binary is garbage + collected, at which time the binary data can be released. An example of + this can be a binary term consisting of data from a mmap'ed file. + The destructor can then do munmap to release the memory + region.

Threads and concurrency

A NIF is thread-safe without any explicit synchronization as 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.

+ you need to supply your own explicit synchronization. This includes terms + in process independent environments that are shared between threads. + 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.

Avoid doing lengthy work in NIF calls as that may degrade the @@ -265,7 +294,7 @@ ok and there is no previously loaded library for this module.

*priv_data can be set to point to some private data that the library needs in order to keep a state between NIF - calls. enif_priv_data() will return this pointer. + calls. enif_priv_data will return this pointer. *priv_data will be initialized to NULL when load is called.

load_info is the second argument to

Variables of type ERL_NIF_TERM can refer to any Erlang term. This is an opaque type and values of it can only by used either as - arguments to API functions or as return values from NIFs. A variable of - type ERL_NIF_TERM is only valid until the NIF call, where it was - obtained, returns.

+ arguments to API functions or as return values from NIFs. All + ERL_NIF_TERM's belong to an environment + (ErlNifEnv). A term can not be + destructed individually, it is valid until its environment is destructed.

ErlNifEnv -

ErlNifEnv contains information about the context in - which a NIF call is made. This pointer should not be - dereferenced in any way, but only passed on to API - functions. An ErlNifEnv pointer is only valid until - the function, where it was supplied as argument, - returns. It is thus useless and dangerous to store ErlNifEnv - pointers in between NIF calls.

+

ErlNifEnv represents an environment that can host Erlang terms. + All terms in an environment are valid as long as the environment is valid. + ErlNifEnv is an opaque type and pointers to it can only be passed + on to API functions. There are two types of environments; process + bound and process independent.

+

A process bound environment is passed as the first argument to all NIFs. + All function arguments passed to a NIF will belong to that environment. + The return value from a NIF must also be a term belonging to the same + environment. + In addition a process bound environment contains transient information + about the calling Erlang process. The environment is only valid in the + thread where it was supplied as argument until the NIF returns. It is + thus useless and dangerous to store pointers to process bound + environments between NIF calls.

+

A process independent environment is created by calling + enif_alloc_env. It can be + used to store terms beteen NIF calls and to send terms with + enif_send. A process + independent environment with all its terms is valid until you explicitly + invalidates it with enif_free_env + or enif_send.

+

All elements of a list/tuple must belong to the same environment as the + list/tuple itself. Terms can be copied between environments with + enif_make_copy.

ErlNifFunc @@ -363,7 +410,18 @@ typedef struct {

ErlNifBinary contains transient information about an inspected binary term. data is a pointer to a buffer of size bytes with the raw content of the binary.

+

Note that ErlNifBinary is a semi-opaque type and you are + only allowed to read fields size and data.

+ ErlNifPid + +

ErlNifPid is a process identifier (pid). In contrast to + pid terms (instances of ERL_NIF_TERM), ErlNifPid's are self + contained and not bound to any + environment. ErlNifPid + is an opaque type.

+
+ ErlNifResourceType

Each instance of ErlNifResourceType represent a class of @@ -388,9 +446,9 @@ typedef enum { ERL_NIF_LATIN1 }ErlNifCharEncoding; -

The character encoding used in strings. The only supported - encoding is currently ERL_NIF_LATIN1 for iso-latin-1 - (8-bit ascii).

+

The character encoding used in strings and atoms. The only + supported encoding is currently ERL_NIF_LATIN1 for + iso-latin-1 (8-bit ascii).

ErlNifSysInfo @@ -407,24 +465,40 @@ typedef enum { Allocate dynamic memory.

Allocate memory of size bytes. Return NULL if allocation failed.

- intenif_alloc_binary(ErlNifEnv* env, unsigned size, ErlNifBinary* bin) + intenif_alloc_binary(size_t size, ErlNifBinary* bin) Create a new binary.

Allocate a new binary of size 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() + enif_release_binary or ownership transferred to an Erlang term with - enif_make_binary(). + enif_make_binary. An allocated (and owned) ErlNifBinary can be kept between NIF calls.

-

Return false if allocation failed.

+

Return true on success or false if allocation failed.

- void*enif_alloc_resource(ErlNifEnv* env, ErlNifResourceType* type, unsigned size) + ErlNifEnv*enif_alloc_env() + Create a new environment +

Allocate a new process independent environment. The environment can + be used to hold terms that is not bound to any process. Such terms can + later be copied to a process environment with + enif_make_copy + or be sent to a process as a message with enif_send.

+

Return pointer to the new environment.

+
+
+ void*enif_alloc_resource(ErlNifResourceType* type, unsigned size) Allocate a memory managed resource object

Allocate a memory managed resource object of type type and size size bytes.

- intenif_compare(ErlNifEnv* env, ERL_NIF_TERM lhs, ERL_NIF_TERM rhs) + voidenif_clear_env(ErlNifEnv* env) + Clear an environment for reuse. +

Free all terms in an environment and clear it for reuse. The environment must + have been allocated with enif_alloc_env. +

+
+ intenif_compare(ERL_NIF_TERM lhs, ERL_NIF_TERM rhs) Compare two terms

Return an integer less than, equal to, or greater than zero if lhs is found, respectively, to be less than, @@ -434,88 +508,98 @@ typedef enum { voidenif_cond_broadcast(ErlNifCond *cnd) -

Same as erl_drv_cond_broadcast(). +

Same as erl_drv_cond_broadcast.

ErlNifCond*enif_cond_create(char *name) -

Same as erl_drv_cond_create(). +

Same as erl_drv_cond_create.

voidenif_cond_destroy(ErlNifCond *cnd) -

Same as erl_drv_cond_destroy(). +

Same as erl_drv_cond_destroy.

voidenif_cond_signal(ErlNifCond *cnd) -

Same as erl_drv_cond_signal(). +

Same as erl_drv_cond_signal.

voidenif_cond_wait(ErlNifCond *cnd, ErlNifMutex *mtx) -

Same as erl_drv_cond_wait(). +

Same as erl_drv_cond_wait.

intenif_equal_tids(ErlNifTid tid1, ErlNifTid tid2) -

Same as erl_drv_equal_tids(). +

Same as erl_drv_equal_tids.

voidenif_free(ErlNifEnv* env, void* ptr) Free dynamic memory

Free memory allocated by enif_alloc.

- intenif_get_atom(ErlNifEnv* env, - ERL_NIF_TERM term, char* buf, unsigned size) - + voidenif_free_env(ErlNifEnv* env) + Free an environment allocated with enif_alloc_env +

Free an environment allocated with enif_alloc_env. + All terms created in the environment will be freed as well.

+
+ intenif_get_atom(ErlNifEnv* env, ERL_NIF_TERM term, char* buf, unsigned size, ErlNifCharEncoding encode) Get the text representation of an atom term

Write a null-terminated string, in the buffer pointed to by buf of size size, consisting of the string - representation of the atom term. Return the number of bytes - written (including terminating null character) or 0 if + representation of the atom term with encoding + encode. Return + the number of bytes written (including terminating null character) or 0 if term is not an atom with maximum length of size-1.

- intenif_get_atom_length(ErlNifEnv* env, ERL_NIF_TERM term, unsigned* len) + intenif_get_atom_length(ErlNifEnv* env, ERL_NIF_TERM term, unsigned* len, ErlNifCharEncoding encode) Get the length of atom term.

Set *len to the length (number of bytes excluding - terminating null character) of term or return false if - term is not an atom.

+ terminating null character) of the atom term with encoding + encode. Return true on success or false if term is not an + atom.

intenif_get_double(ErlNifEnv* env, ERL_NIF_TERM term, double* dp) Read a floating-point number term.

Set *dp to the floating point value of - term or return false if term is not a float.

+ term. Return true on success or false if term is not a float.

intenif_get_int(ErlNifEnv* env, ERL_NIF_TERM term, int* ip) - Read an integer term. + Read an integer term

Set *ip to the integer value of - term or return false if term is not an integer or is - outside the bounds of type int

+ term. Return true on success or false if term is not an + integer or is outside the bounds of type int

+
+ intenif_get_local_pid(ErlNifEnv* env, ERL_NIF_TERM term, ErlNifPid* pid) + Read an local pid term +

If term is the pid of a node local process, initialize the + pid variable *pid from it and return true. Otherwise return false. + No check if the process is alive is done.

intenif_get_list_cell(ErlNifEnv* env, ERL_NIF_TERM list, ERL_NIF_TERM* head, ERL_NIF_TERM* tail) Get head and tail from a list

Set *head and *tail from - list or return false if list is not a non-empty - list.

+ list and return true, or return false if list is not a + non-empty list.

intenif_get_list_length(ErlNifEnv* env, ERL_NIF_TERM term, unsigned* len) Get the length of list term. -

Set *len to the length of term or return - false if term is not a list.

+

Set *len to the length of list term and return true, + or return false if term is not a list.

intenif_get_long(ErlNifEnv* env, ERL_NIF_TERM term, long int* ip) Read an long integer term. -

Set *ip to the long integer value of - term or return false if term is not an integer or is +

Set *ip to the long integer value of term and + return true, or return false if term is not an integer or is outside the bounds of type long int.

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. - 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 +

Set *objp to point to the resource object referred to by term.

+

Return true on success or false if term is not a handle to a resource object of type type.

intenif_get_string(ErlNifEnv* env, @@ -540,27 +624,27 @@ typedef enum { *arity to the number of elements. Note that the array is read-only and (*array)[N-1] will be the Nth element of the tuple. *array is undefined if the arity of the tuple - is zero.

Return false if term is not a + is zero.

Return true on success or false if term is not a tuple.

intenif_get_uint(ErlNifEnv* env, ERL_NIF_TERM term, unsigned int* ip) Read an unsigned integer term. -

Set *ip to the unsigned integer value of - term or return false if term is not an unsigned integer or is - outside the bounds of type unsigned int

+

Set *ip to the unsigned integer value of term and + return true, or return false if term is not an unsigned integer or + is outside the bounds of type unsigned int.

intenif_get_ulong(ErlNifEnv* env, ERL_NIF_TERM term, unsigned long* ip) Read an unsigned integer term. -

Set *ip to the unsigned long integer value of - term or return false if term is not an unsigned integer or is - outside the bounds of type unsigned long

+

Set *ip to the unsigned long integer value of term + and return true, or return false if term is not an unsigned integer or is + outside the bounds of type unsigned long.

intenif_inspect_binary(ErlNifEnv* env, ERL_NIF_TERM bin_term, ErlNifBinary* bin) Inspect the content of a binary

Initialize the structure pointed to by bin with information about the binary term - bin_term. Return false if bin_term is not a binary.

+ bin_term. Return true on success or false if bin_term is not a binary.

intenif_inspect_iolist_as_binary(ErlNifEnv* env, ERL_NIF_TERM term, ErlNifBinary* bin) @@ -569,7 +653,7 @@ typedef enum {

Initialize the structure pointed to by bin with one continuous buffer with the same byte content as iolist. As with inspect_binary, the data pointed to by bin is transient and does - not need to be released. Return false if iolist is not an + not need to be released. Return true on success or false if iolist is not an iolist.

@@ -589,7 +673,7 @@ typedef enum { Determine if a term is a fun

Return true if term is a fun.

- intenif_is_identical(ErlNifEnv* env, ERL_NIF_TERM lhs, ERL_NIF_TERM rhs) + intenif_is_identical(ERL_NIF_TERM lhs, ERL_NIF_TERM rhs) Erlang operator =:=

Return true if the two terms are identical. Corresponds to the Erlang operators =:= and @@ -603,7 +687,6 @@ typedef enum { Determine if a term is a port

Return true if term is a port.

- intenif_is_ref(ErlNifEnv* env, ERL_NIF_TERM term) Determine if a term is a reference

Return true if term is a reference.

@@ -616,16 +699,23 @@ typedef enum { Determine if a term is a list

Return true if term is a list.

+ intenif_keep_resource(void* obj) + Add a reference to a resource object +

Add a reference to resource object obj obtained from + enif_alloc_resource. + Each call to enif_keep_resource for an object must be balanced by + a call to enif_release_resource + before the object will be destructed.

+
ERL_NIF_TERMenif_make_atom(ErlNifEnv* env, const char* name) Create an atom term -

Create an atom term from the null-terminated C-string name. - Unlike other terms, atom terms may be saved and used between NIF calls.

+

Create an atom term from the null-terminated C-string name + with iso-latin-1 encoding.

ERL_NIF_TERMenif_make_atom_len(ErlNifEnv* env, const char* name, size_t len) Create an atom term

Create an atom term from the string name with length len. - Null-characters are treated as any other characters. - Unlike other terms, atom terms may be saved and used between NIF calls.

+ Null-characters are treated as any other characters.

ERL_NIF_TERMenif_make_badarg(ErlNifEnv* env) Make a badarg exception. @@ -638,22 +728,30 @@ typedef enum { bin should be considered read-only for the rest of the NIF call and then as released.

+ ERL_NIF_TERMenif_make_copy(ErlNifEnv* dst_env, ERL_NIF_TERM src_term) + Make a copy of a term. +

Make a copy of term src_term. The copy will be created in + environment dst_env. The source term may be located in any + environment.

+
ERL_NIF_TERMenif_make_double(ErlNifEnv* env, double d) Create a floating-point term

Create a floating-point term from a double.

- intenif_make_existing_atom(ErlNifEnv* env, const char* name, ERL_NIF_TERM* atom) + intenif_make_existing_atom(ErlNifEnv* env, const char* name, ERL_NIF_TERM* atom, ErlNifCharEncoding encode) Create an existing atom term

Try to create the term of an already existing atom from - the null-terminated C-string name. If the atom already exists store the - term in *atom and return true, otherwise return - false.

+ the null-terminated C-string name with encoding + encode. If the atom + already exists store the term in *atom and return true, otherwise + return false.

- intenif_make_existing_atom_len(ErlNifEnv* env, const char* name, size_t len, ERL_NIF_TERM* atom) + intenif_make_existing_atom_len(ErlNifEnv* env, const char* name, size_t len, ERL_NIF_TERM* atom, ErlNifCharEncoding encoding) Create an existing atom term

Try to create the term of an already existing atom from the - string name with length len. Null-characters are treated - as any other characters. If the atom already exists store the term + string name with length len and encoding + encode. Null-characters + are treated as any other characters. If the atom already exists store the term in *atom and return true, otherwise return false.

ERL_NIF_TERMenif_make_int(ErlNifEnv* env, int i) @@ -694,7 +792,7 @@ typedef enum { Create an integer term from a long int

Create an integer term from a long int.

- unsigned char*enif_make_new_binary(ErlNifEnv* env, unsigned size, ERL_NIF_TERM* termp) + unsigned char*enif_make_new_binary(ErlNifEnv* env, size_t size, ERL_NIF_TERM* termp) Allocate and create a new binary term

Allocate a binary of size size bytes and create an owning term. The binary data is mutable until the calling NIF returns. This is a @@ -704,6 +802,10 @@ typedef enum { reallocated.

Return a pointer to the raw binary data and set *termp to the binary term.

+ ERL_NIF_TERMenif_make_pid(ErlNifEnv* env, const ErlNifPid* pid) + Make a pid term +

Make a pid term from *pid.

+
ERL_NIF_TERMenif_make_ref(ErlNifEnv* env) Create a reference.

Create a reference like erlang:make_ref/0.

@@ -719,6 +821,29 @@ typedef enum { same node. Other operations such as matching or term_to_binary will have unpredictable (but harmless) results.

+ ERL_NIF_TERMenif_make_resource_binary(ErlNifEnv* env, void* obj, const void* data, size_t size) + Create a custom binary term +

Create a binary term that is memory managed by a resource object + obj obtained by enif_alloc_resource. + The returned binary term will consist of size bytes pointed to + by data. This raw binary data must be kept readable and unchanged + until the destructor of the resource is called. The binary data may be + stored external to the resource object in which case it is the responsibility + of the destructor to release the data.

+

Several binary terms may be managed by the same resource object. The + destructor will not be called until the last binary is garbage collected. + This can be useful as a way to return different parts of a larger binary + buffer.

+

As with enif_make_resource, + no ownership transfer is done. The resource still needs to be released with + enif_release_resource.

+
+
+ ErlNifPid*enif_self(ErlNifEnv* caller_env, ErlNifPid* pid) + Get the pid of the calling process. +

Initialize the pid variable *pid to represent the + calling process. Return pid.

+
ERL_NIF_TERMenif_make_string(ErlNifEnv* env, const char* string, ErlNifCharEncoding encoding) Create a string.

Create a list containing the characters of the @@ -731,7 +856,7 @@ typedef enum { Null-characters are treated as any other characters.

ERL_NIF_TERMenif_make_sub_binary(ErlNifEnv* - env, ERL_NIF_TERM bin_term, unsigned pos, unsigned size) + env, ERL_NIF_TERM bin_term, size_t pos, size_t size) Make a subbinary term.

Make a subbinary of binary bin_term, starting at zero-based position pos with a length of size bytes. @@ -775,30 +900,31 @@ typedef enum { ErlNifMutex*enif_mutex_create(char *name) -

Same as erl_drv_mutex_create(). +

Same as erl_drv_mutex_create.

voidenif_mutex_destroy(ErlNifMutex *mtx) -

Same as erl_drv_mutex_destroy(). +

Same as erl_drv_mutex_destroy.

voidenif_mutex_lock(ErlNifMutex *mtx) -

Same as erl_drv_mutex_lock(). +

Same as erl_drv_mutex_lock.

intenif_mutex_trylock(ErlNifMutex *mtx) -

Same as erl_drv_mutex_trylock(). +

Same as erl_drv_mutex_trylock.

voidenif_mutex_unlock(ErlNifMutex *mtx) -

Same as erl_drv_mutex_unlock(). +

Same as erl_drv_mutex_unlock.

- ErlNifResourceType*enif_open_resource_type(ErlNifEnv* env, const char* name, + ErlNifResourceType*enif_open_resource_type(ErlNifEnv* env, + const char* module_str, const char* name, ErlNifResourceDtor* dtor, ErlNifResourceFlags flags, ErlNifResourceFlags* tried) Create or takeover a resource type

Create or takeover a resource type identified by the string @@ -812,10 +938,10 @@ typedef enum { The supplied destructor dtor will be called both for existing instances as well as new instances not yet created by the calling NIF library. -

The two flag values can be combined with bitwise-or. To avoid unintentional - name clashes a good practice is to include the module name as part of the - type name. The dtor may be NULL in case no destructor - is needed.

+

The two flag values can be combined with bitwise-or. The name of the + resource type is local to the calling module. Argument module_str + is not (yet) used and must be NULL. The dtor may be NULL + in case no destructor is needed.

On success, return a pointer to the resource type and *tried will be set to either ERL_NIF_RT_CREATE or ERL_NIF_RT_TAKEOVER to indicate what was actually done. @@ -832,123 +958,151 @@ typedef enum { reload or upgrade.

Was previously named enif_get_data.

- voidenif_realloc_binary(ErlNifEnv* env, ErlNifBinary* bin, unsigned size) + voidenif_realloc_binary(ErlNifBinary* bin, size_t size) Change the size of a binary.

Change the size of a binary bin. The source binary may be read-only, in which case it will be left untouched and a mutable copy is allocated and assigned to *bin.

- voidenif_release_binary(ErlNifEnv* env, ErlNifBinary* bin) + voidenif_release_binary(ErlNifBinary* bin) Release a binary. -

Release a binary obtained - from enif_alloc_binary.

+

Release a binary obtained from enif_alloc_binary.

- voidenif_release_resource(ErlNifEnv* env, void* obj) + voidenif_release_resource(void* obj) Release a resource object. -

Release a resource object obtained from enif_alloc_resource. - The object may still be alive if it is referred to by Erlang terms. Each call to - enif_release_resource must correspond to a previous call to enif_alloc_resource. - References made by enif_make_resource can only be released by the garbage collector.

+

Remove a reference to resource object objobtained from + enif_alloc_resource. + The resource object will be destructed when the last reference is removed. + Each call to enif_release_resource must correspond to a previous + call to enif_alloc_resource or + enif_keep_resource. + References made by enif_make_resource + can only be removed by the garbage collector.

ErlNifRWLock*enif_rwlock_create(char *name) -

Same as erl_drv_rwlock_create(). +

Same as erl_drv_rwlock_create.

voidenif_rwlock_destroy(ErlNifRWLock *rwlck) -

Same as erl_drv_rwlock_destroy(). +

Same as erl_drv_rwlock_destroy.

voidenif_rwlock_rlock(ErlNifRWLock *rwlck) -

Same as erl_drv_rwlock_rlock(). +

Same as erl_drv_rwlock_rlock.

voidenif_rwlock_runlock(ErlNifRWLock *rwlck) -

Same as erl_drv_rwlock_runlock(). +

Same as erl_drv_rwlock_runlock.

voidenif_rwlock_rwlock(ErlNifRWLock *rwlck) -

Same as erl_drv_rwlock_rwlock(). +

Same as erl_drv_rwlock_rwlock.

voidenif_rwlock_rwunlock(ErlNifRWLock *rwlck) -

Same as erl_drv_rwlock_rwunlock(). +

Same as erl_drv_rwlock_rwunlock.

intenif_rwlock_tryrlock(ErlNifRWLock *rwlck) -

Same as erl_drv_rwlock_tryrlock(). +

Same as erl_drv_rwlock_tryrlock.

intenif_rwlock_tryrwlock(ErlNifRWLock *rwlck) -

Same as erl_drv_rwlock_tryrwlock(). +

Same as erl_drv_rwlock_tryrwlock.

- unsignedenif_sizeof_resource(ErlNifEnv* env, void* obj) + unsignedenif_send(ErlNifEnv* env, ErlNifPid* to_pid, ErlNifEnv* msg_env, ERL_NIF_TERM msg) + Send a message to a process. +

Send a message to a process.

+ + env + The environment of the calling process. Must be NULL if and + only if calling from a created thread. + *to_pid + The pid of the receiving process. The pid should refer to a process on the local node. + msg_env + The environment of the message term. Must be a process + independent environment allocated with + enif_alloc_env. + msg + The message term to send. + +

Return true on success, or false if *to_pid does not refer to an alive local process.

+

The message environment msg_env with all its terms (including + msg) will be invalidated by a successful call to enif_send. The environment + should either be freed with enif_free_env + of cleared for reuse with enif_clear_env.

+

This function is only thread-safe when the emulator with SMP support is used. + It can only be used in a non-SMP emulator from a NIF-calling thread.

+
+
+ unsignedenif_sizeof_resource(void* obj) Get the byte size of a resource object

Get the byte size of a resource object obj obtained by - enif_alloc_resource.

+ enif_alloc_resource.

voidenif_system_info(ErlNifSysInfo *sys_info_ptr, size_t size) Get information about the Erlang runtime system -

Same as driver_system_info(). +

Same as driver_system_info.

intenif_thread_create(char *name,ErlNifTid *tid,void * (*func)(void *),void *args,ErlNifThreadOpts *opts) -

Same as erl_drv_thread_create(). +

Same as erl_drv_thread_create.

voidenif_thread_exit(void *resp) -

Same as erl_drv_thread_exit(). +

Same as erl_drv_thread_exit.

intenif_thread_join(ErlNifTid, void **respp) -

Same as erl_drv_thread_join (). +

Same as erl_drv_thread_join .

ErlNifThreadOpts*enif_thread_opts_create(char *name) -

Same as erl_drv_thread_opts_create(). +

Same as erl_drv_thread_opts_create.

voidenif_thread_opts_destroy(ErlNifThreadOpts *opts) -

Same as erl_drv_thread_opts_destroy(). +

Same as erl_drv_thread_opts_destroy.

ErlNifTidenif_thread_self(void) -

Same as erl_drv_thread_self(). +

Same as erl_drv_thread_self.

intenif_tsd_key_create(char *name, ErlNifTSDKey *key) -

Same as erl_drv_tsd_key_create(). +

Same as erl_drv_tsd_key_create.

voidenif_tsd_key_destroy(ErlNifTSDKey key) -

Same as erl_drv_tsd_key_destroy(). +

Same as erl_drv_tsd_key_destroy.

void*enif_tsd_get(ErlNifTSDKey key) -

Same as erl_drv_tsd_get(). +

Same as erl_drv_tsd_get.

voidenif_tsd_set(ErlNifTSDKey key, void *data) -

Same as erl_drv_tsd_set(). +

Same as erl_drv_tsd_set.

-- cgit v1.2.3