Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Goswin von Brederlow <goswin-v-b@w...>
Subject: How to do thread save callbacks from C to ocaml?
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.