caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "David Allsopp" <dra-news@metastack.com>
To: <caml-list@yquem.inria.fr>
Subject: RE: [Caml-list] Callbacks from C to OCaml
Date: Sat, 11 Jul 2009 12:39:23 +0100	[thread overview]
Message-ID: <005b01ca021c$3e7f9950$bb7ecbf0$@metastack.com> (raw)
In-Reply-To: <20090711105403.8bb1e112.ygrekheretix@gmail.com>

ygrek wrote:
> On Fri, 10 Jul 2009 12:16:17 +0100
> "David Allsopp" <dra-news@metastack.com> 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
decision!

> See also http://caml.inria.fr/mantis/view.php?id=4702

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.


David



      reply	other threads:[~2009-07-11 11:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-10 11:16 David Allsopp
2009-07-11  7:54 ` [Caml-list] " ygrek
2009-07-11 11:39   ` David Allsopp [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='005b01ca021c$3e7f9950$bb7ecbf0$@metastack.com' \
    --to=dra-news@metastack.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).