[Erlang Systems]

3 Using Erlang from C

This section describes how to call Erlang functions from C and includes the following topics:

3.1 Calling Erlang Functions

IG generated stubs can call Erlang functions from C, in a similar way as C functions can be called from Erlang. Analogous with the IG_fun and IG_void keywords, there are two keywords for C to Erlang function calls:

For example, if the declaration IG_call int get_time(); is in the header file, then IG will generate a stub function get_time on the C side and will prepare for the actual Erlang call on the Erlang stub side.

The Erlang call is made into what is called a callback module. The callback module is the module where the interface functions are defined. The name of this module is given to IG as input at generate time so that it can generate correct stubs. The callback module name is given as an option to IG, as shown in the following example:

ig:gen(my_stuff, [{cback_mod, my_callbacks}])

3.2 Pointers to Callback Functions

The address of a callback function can be acquired by using the ig:adr_of/2 function. ig:adr_of/2 returns the integer value of the pointer to the function, and that value can in turn be passed on to C in other calls. This is useful when function pointers are used extensively on the C side and the functionality is implemented in Erlang. An example is window systems.

A third keyword can be used if only the address is of interest. With the keyword IG_extern, IG stores the address of the function but does not generate any stub code for it.

3.3 Precautions

Be careful when mixing callbacks and C function calls from Erlang. IG assumes that one party (C or Erlang) is in charge of the communication because both stubs needs strict serialization of messages being passed. If two-way communication is really needed, then two IG interfaces may be used instead, or at least two Erlang stubs, one for each direction.


Copyright © 1991-98 Ericsson Telecom AB