Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Callbacks from C to OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-07-11 (11:39)
From: David Allsopp <dra-news@m...>
Subject: RE: [Caml-list] Callbacks from C to OCaml
ygrek wrote:
> On Fri, 10 Jul 2009 12:16:17 +0100
> "David Allsopp" <> wrote:
> > Now, at any time, the SCM may invoke the ServiceCtrlHandler function
> > registered with it. This also needs to callback to an OCaml function
> > to work out what to do (the closure was registered in the initial call).
> >
> > What happens at this point with regard to OCaml's global lock and
> > being in the correct threading context? I must confess that I don't
> > fully understand how the callback works at all in the single-threaded
> > context - presumably when SCM calls the function, it simply executes
> > code in its own thread context (so it can presumably introduce a form
> > of multi-threading to a program which isn't expecting it?).
> Yes, SCM creates new thread for the callback.

That's handy to know - I was going to do some further experimentation in C
to check that so that hassle is saved, thanks!

> And this thread is not registered with OCaml runtime, and so you can't
> do any allocation on it. I used windows services in the same setup and
> just set a boolean flag when SCM signalled service to stop, while
> periodically checking this flag in ocaml thread..

Following this, and an off-list exchange with Philippe Wang, I'd arrived at
the same conclusion. I'd tried to model the whole procedure essentially on
how'd you'd write it in C with callbacks and that was clearly the wrong

> See also

Presumably if this patch is eventually incorporated, that would allow the C
version of ServiceCtrlHandler to register its thread with the OCaml runtime
and therefore safely callback into OCaml code? I shall watch the issue -
although having to create a couple of message-passing helper threads to
accomplish this task is hardly going to slow anything down.

> Hope it helps.

Thank you! It's good to know for definite that I've been barking up the
wrong tree - especially as it tantalising almost worked.