Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: How to do thread save callbacks from C to ocaml?
Date: Sat, 30 May 2009 22:51:13 +0200	[thread overview]
Message-ID: <87hbz2co1q.fsf@frosties.localdomain> (raw)

Hi,

for my fuse bindings I have a number (~20) of callbacks from C code to
ocaml that I want to make sure behave correctly when
multithreaded. The libfuse runs with enter_blocking_section() so other
ocaml threads can run while it blocks. That means I need to call
leave_blocking_section() before I do anything ocaml in the callback,
including registering a new root in CAMLparam0(), right?

So to me it looks like I do have to write the callbacks like this:

static void getattr_stub2(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) {
    CAMLparam0();
    CAMLlocal2(ml_req, ml_inode);

    Filesystem *fs = (Filesystem*)fuse_req_userdata(req);
    ml_req = caml_alloc(1, Abstract_tag);
    Store_field(ml_req, 0, (intptr_t)req);
    ml_inode = caml_copy_int64(ino);
    caml_callback2(Field(fs->ops, GETATTR), ml_req, ml_inode);

    CAMLreturn0;
}

static void getattr_stub(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) {
    leave_blocking_section();
    getattr_stub2(req, ino, fi);
    enter_blocking_section();
}


Is this code correct? Is there another way that avoids having 2 stubs
per callback?

MfG
        Goswin

PS: fs is malloc()ed (not on ocamls heap) and fs->ops is registered as
global value.


                 reply	other threads:[~2009-05-30 20:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=87hbz2co1q.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --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