yallop / ocaml-ctypes

Library for binding to C libraries using pure OCaml
MIT License
371 stars 95 forks source link

Customizing inverted stubs generation #263

Open braibant opened 9 years ago

braibant commented 9 years ago

I am looking at the stubs generated using Cstubs_inverted and I am wondering to what extent these stubs are usable when used in the context of a multi-threaded application.

Here is a typical stub generated:

unsigned long C_Initialize(void* x1)
{
   enum { nargs = 1 };
   CAMLparam0();
   CAMLlocalN(locals, nargs);
   locals[0] = CTYPES_FROM_PTR(x1);
   value x2 = functions[fn_C_Initialize];
   value x3 = caml_callbackN(x2, nargs, locals);
   unsigned long x4 = ctypes_ulong_val(x3);
   CAMLreturnT(unsigned long, x4);
}

What could be really nice is to have support in the inverted stub mechanisms to be able to insert arbitrary code at the very beginning of that function, and just before the return. We were able to simulate that behavior by patching memory.h (a dirty hack) but this is in no way satisfying. With these hooks in place, it should be possible to put some sane locking in place. Does it make sense? Is it compatible with the kind of customatization you had in mind for #197?

yallop commented 9 years ago

I think it should be possible to support customization of Cstubs_inverted output. We should certainly add this.

braibant commented 9 years ago

I am working on it.

braibant commented 9 years ago

Maybe we should continue the discussion in the #265 pull request