[ocaml-ctypes] Runtime Lock

Trevor Smith trevorsummerssmith at gmail.com
Tue Dec 2 13:25:24 GMT 2014


Hello,

I thought I'd try to clarify my question.

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? If that is correct, I was thinking one
could make an OCaml function to call `caml_release_runtime_system` and
`caml_acquire_runtime_system`, then wrap the user callback cb:

let cb' arg =
  let () = caml_acquire_runtime_system () in
  let () = cb arg in
  caml_release_runtime_system ()

This cb' would then be passed to the thread B, instead of the user's cb.

I think this would work iff the callback returns unit, and we don't want to
do anything with the return value. Also, my understanding is that the lock
calls are _not_ reentrant, so the acquire/release set are needed.

Does that make sense? Any thoughts? Thanks!

Trevor


On Fri, Nov 28, 2014 at 12:09 PM, Trevor Smith <trevorsummerssmith at gmail.com
> wrote:

> Hello,
>
> Following up from a thread "Lifecycle of a callback?" discussed on this
> list: I want to double check my thinking.
>
> The C functions I am calling in libuv mostly do one thing: put a struct
> representing the system call to call, and the callback to run once the
> system call is called, on a queue that another thread will work on. If my
> understanding is correct, the runtime lock would need to be re-acquired
> when the OCaml callback is ran. However, it's not clear to me how to do
> this.
>
> Thoughts? Thanks.
>
> Trevor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ocaml.org/pipermail/ctypes/attachments/20141202/2d70a772/attachment.html>


More information about the Ctypes mailing list