English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
How to do thread save 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-05-30 (20:51)
From: Goswin von Brederlow <goswin-v-b@w...>
Subject: How to do thread save callbacks from C to ocaml?

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) {
    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);


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

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


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