Article View: fa.caml
Article #71Re: [Caml-list] Thread-private data in C stubs
From: jonathan
Date: Tue, 28 Aug 2012 11:48
Date: Tue, 28 Aug 2012 11:48
106 lines
3889 bytes
3889 bytes
On Monday, 27 August 2012 22:08:18 UTC+1, Andre Nathan wrote:
> On Mon, 2012-08-27 at 16:41 -0300, Andre Nathan wrote:
>
> > Hello
>
> >
>
> > I'm writing stubs to a C library uses pthreads and exposes two functions
>
> > to get and set thread-private data, getpriv(ctx) and setpriv(ctx, p)
>
> > where ctx is some opaque context object, and p is a void* pointer.
>
> >
>
> > What's the best way to deal with this from the stub code, ie., how do I
>
> > avoid the thread-private data to be garbage collected when the OCaml
>
> > variable goes out of scope?
>
> >
>
> > Since the library in question uses threads internally, this needs to be
>
> > a thread-safe solution.
>
>
>
> For the record, with help from #ocaml, I got it to work like this:
>
>
>
> struct priv {
>
> value v;
>
> };
>
>
>
> CAMLprim value
>
> caml_setpriv(value ctx_val, value priv_val)
>
> {
>
> CAMLparam2(ctx_val, priv_val);
>
> context *ctx = (context *)ctx_val;
>
> struct priv *p;
>
>
>
> p = getpriv(ctx);
>
> if (p != NULL) {
>
> caml_remove_global_root(&(p->v));
>
> caml_stat_free(p);
>
> }
>
>
>
> p = caml_stat_alloc(sizeof(*p));
>
> p->v = priv_val;
>
> ret = setpriv(ctx, p);
>
> caml_register_global_root(&(p->v));
>
>
>
> CAMLreturn(Val_unit);
>
> }
>
>
>
>
>
> --
>
> Caml-list mailing list. Subscription management and archives:
>
> https://sympa-roc.inria.fr/wws/info/caml-list
>
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>
> Bug reports: http://caml.inria.fr/bin/caml-bugs
Dear Andre, in my view it is needlessly inefficient to create a new global root for every item which crosses the C-interface. I used a system which uses dlmalloc to carve up an allocated caml custom block into convenient sized chunks. Each of these large blocks could be a local root or they could be chained together from one global root. Another advantage is that subroutines written in C that allocate memory do not need to know they are running under caml if dlmalloc is compiled to replace the system malloc. The one downside is that it is difficult to track when the global blocks are ready to be released. My impression is ocaml runtime would get very slow if there were huge numbers of global roots.
Technical Details
Message-ID:
<506691e7-68e5-4092-a9fc-4cc8711235d2@googlegroups.com>Path:
arc1-ca.pugleaf.net!da15ni36163658qab.0!nntp.google.com!r1no2299011qas.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mailReferences:
<fa.P6AfawLTOdz6VcNoVpOatntnvpI@ifi.uio.no> <fa.rchhrYogU61pR6TqMyzDz8fSIoA@ifi.uio.no>Show Full Headers
Complete Article Headers
X-Google-Thread: c0fe4b917e,853eb44f711a7e4d
X-Google-Attributes: gidc0fe4b917e,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII-7-bit
Received: by 10.224.180.205 with SMTP id bv13mr8674252qab.8.1346154519274;
Tue, 28 Aug 2012 04:48:39 -0700 (PDT)
Received: by 10.52.67.12 with SMTP id j12mr2273128vdt.7.1346154519252; Tue, 28
Aug 2012 04:48:39 -0700 (PDT)
Path: arc1-ca.pugleaf.net!da15ni36163658qab.0!nntp.google.com!r1no2299011qas.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: fa.caml
Date: Tue, 28 Aug 2012 04:48:39 -0700 (PDT)
In-Reply-To: <fa.rchhrYogU61pR6TqMyzDz8fSIoA@ifi.uio.no>
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=86.9.119.233; posting-account=JMPJagoAAACXMK_kS4Z5sK6kRCaAl6hy
NNTP-Posting-Host: 86.9.119.233
References: <fa.P6AfawLTOdz6VcNoVpOatntnvpI@ifi.uio.no> <fa.rchhrYogU61pR6TqMyzDz8fSIoA@ifi.uio.no>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <506691e7-68e5-4092-a9fc-4cc8711235d2@googlegroups.com>
Subject: Re: [Caml-list] Thread-private data in C stubs
From: jonathan@kimmitt.co.uk
Cc: caml-list@inria.fr
Injection-Date: Tue, 28 Aug 2012 11:48:39 +0000
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable