[ocaml-ctypes] Calling from C into OCaml

Daniel Bünzli daniel.buenzli at erratique.ch
Tue Dec 17 16:53:46 GMT 2013


Hello, 

How does ctypes handle callback closures from C when they are stored in C structures ? 

  1) Do they respect the OCaml runtime lock ? 
  2) Are they ensured of not being gc'd ?

In tsdl I did that once for the audio callback which gets called on another thread, it *worked* but I wonder if what I do is safe or if it works by chance (tried to Gc.compact () in the callback but the world didn't explode...).

For reference here are the various bits:

The OCaml record where you specify your callback:
http://erratique.ch/software/tsdl/doc/Tsdl.Sdl.html#TYPEaudio_spec

Callback and C structure holding it:
http://hg.libsdl.org/SDL/file/default/include/SDL_audio.h#l159

ocaml-ctypes translation:
https://github.com/dbuenzli/tsdl/blob/master/src/tsdl.ml#L4305

Translation of the OCaml record to a C struct that has a pointer on the callback:
https://github.com/dbuenzli/tsdl/blob/master/src/tsdl.ml#L4333

Example of use (440Hz sin wave):
https://github.com/dbuenzli/tsdl/blob/master/test/test.ml#L1250

Best,

Daniel


More information about the Ctypes mailing list