<div dir="ltr">Jeremy,<div><br></div><div>Thanks. What you said makes sense. I think I misunderstood the funptr argument but it is very clear now.</div><div><br></div><div>I saw that "Advanced topic: multithreading". Given that the library I'm linking in -- libuv -- spawns the threads itself we will need to be creative about getting them to call the caml register function. That's next on my list.</div><div><br></div><div>Thanks!</div><div><br></div><div>Trevor</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 2, 2014 at 12:07 PM, Jeremy Yallop <span dir="ltr"><<a href="mailto:yallop@gmail.com" target="_blank">yallop@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 2 December 2014 at 13:25, Trevor Smith <<a href="mailto:trevorsummerssmith@gmail.com">trevorsummerssmith@gmail.com</a>> wrote:<br>
> 1) C-function in thread A will be called from OCaml, and places an OCaml<br>
> callback cb onto a queue.<br>
> 2) C-function in thread B will asynchronously receive the callback on the<br>
> queue, do some computation, then run cb.<br>
><br>
> My understanding is that I need to re-acquire the runtime lock when cb is<br>
> ran in thread B. Is that correct?<br>
<br>
</span>Yes.  You should be able to use the "runtime_lock" argument to funptr<br>
to indicate that the lock should be (re)acquired when calling back<br>
into OCaml from C:<br>
<br>
  <a href="https://github.com/ocamllabs/ocaml-ctypes/blob/e190603a4/src/ctypes-foreign-threaded/foreign.mli#L53" target="_blank">https://github.com/ocamllabs/ocaml-ctypes/blob/e190603a4/src/ctypes-foreign-threaded/foreign.mli#L53</a><br>
  <a href="https://github.com/ocamllabs/ocaml-ctypes/blob/e190603a4/src/ctypes-foreign-threaded/foreign.mli#L72-L74" target="_blank">https://github.com/ocamllabs/ocaml-ctypes/blob/e190603a4/src/ctypes-foreign-threaded/foreign.mli#L72-L74</a><br>
<br>
You probably also need to call caml_c_thread_register() from your C<br>
code to register the thread that's going to be calling back into<br>
OCaml:<br>
<br>
   <a href="http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual033.html#toc151" target="_blank">http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual033.html#toc151</a><br>
<br>
(It's worth reading that whole "Advanced topic: multithreading"<br>
section of the OCaml manual, if you haven't already.)<br>
<span class=""><br>
> Also, my understanding is that the lock<br>
> calls are _not_ reentrant, so the acquire/release set are needed.<br>
<br>
</span>Right.<br>
</blockquote></div><br></div>