[ocaml-ctypes] Runtime Lock
Jeremy Yallop
yallop at gmail.com
Tue Dec 2 17:07:14 GMT 2014
On 2 December 2014 at 13:25, Trevor Smith <trevorsummerssmith at gmail.com> wrote:
> 1) C-function in thread A will be called from OCaml, and places an OCaml
> callback cb onto a queue.
> 2) C-function in thread B will asynchronously receive the callback on the
> queue, do some computation, then run cb.
>
> My understanding is that I need to re-acquire the runtime lock when cb is
> ran in thread B. Is that correct?
Yes. You should be able to use the "runtime_lock" argument to funptr
to indicate that the lock should be (re)acquired when calling back
into OCaml from C:
https://github.com/ocamllabs/ocaml-ctypes/blob/e190603a4/src/ctypes-foreign-threaded/foreign.mli#L53
https://github.com/ocamllabs/ocaml-ctypes/blob/e190603a4/src/ctypes-foreign-threaded/foreign.mli#L72-L74
You probably also need to call caml_c_thread_register() from your C
code to register the thread that's going to be calling back into
OCaml:
http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual033.html#toc151
(It's worth reading that whole "Advanced topic: multithreading"
section of the OCaml manual, if you haven't already.)
> Also, my understanding is that the lock
> calls are _not_ reentrant, so the acquire/release set are needed.
Right.
More information about the Ctypes
mailing list