Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "Daniel Bünzli" <daniel.buenzli@epfl.ch>
To: caml-list@inria.fr
Subject: [Caml-list] C interface, threads and ocaml callbacks
Date: Wed, 3 Mar 2004 23:51:42 +0100	[thread overview]
Message-ID: <572FE07E-6D65-11D8-B22B-000393DBC266@epfl.ch> (raw)

Hello,

I'm trying to do a binding from ocaml to a C function f which may spawn 
threads when invoked. These threads may concurrently try to invoke 
ocaml registrated callbacks. However when f returns I'm guaranteed that 
all spawn threads have terminated.

I'm aware of these messages [1] but I'm not sure I really got the 
point. My understanding is that two threads should not callback into 
caml at the same time and that in order to do so, the following should 
be done. If anyone could comment/correct.

1. Link and compile everything used by the program invoking the stub to 
f
with the option -thread.

2. In the stub to function f, bracket the call to f with 
enter/leave_blocking_section().

CAMLprim value f_stub (value v)
{ /* convert arguments */
   enter_blocking_section()
     res = f(arg)  /* In this function threads will try to concurently 
access ocaml */
   leave_blocking_section()
   /* convert result of f() */
}

3. Around any call from f back into caml, bracket code with 
leave/enter_blocking_section.

leave_blocking_section()
/* convert parameters */
res = callback(closure,arg)
/* convert result */
enter_blocking_section()

4. The user of the stub should not use caml threads in the ocaml code 
(at all or only in the callbacks ?) and not link against caml threading 
libraries (even if I tell f() to stay single threaded but using the 
same stub code ?).

Thanks for your answers,

Daniel

P.S. Is that somewhere documented ?

[1]
<http://caml.inria.fr/archives/200308/msg00258.html>
<http://caml.inria.fr/archives/200106/msg00199.html>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


             reply	other threads:[~2004-03-03 23:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-03 22:51 Daniel Bünzli [this message]
     [not found] <200403050213.i252DtGV029925@mailserver2.hushmail.com>
2004-03-10  0:24 ` Daniel Bünzli

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=572FE07E-6D65-11D8-B22B-000393DBC266@epfl.ch \
    --to=daniel.buenzli@epfl.ch \
    --cc=caml-list@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