* How to do thread save callbacks from C to ocaml?
@ 2009-05-30 20:51 Goswin von Brederlow
0 siblings, 0 replies; only message in thread
From: Goswin von Brederlow @ 2009-05-30 20:51 UTC (permalink / raw)
To: caml-list
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.
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2009-05-30 20:51 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-30 20:51 How to do thread save callbacks from C to ocaml? Goswin von Brederlow
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).